~smoser/cloud-init/trunk.new-ds

« back to all changes in this revision

Viewing changes to tests/unittests/test_datasource/test_maas.py

  • Committer: Scott Moser
  • Date: 2015-02-11 01:53:20 UTC
  • mfrom: (1052.1.45 py2-3.smoser)
  • Revision ID: smoser@ubuntu.com-20150211015320-049dv6n1mk2in7l1
python3 support.

This gives us functional python3 support.  There are likely
still bugs, but instance boot on openstack is functional now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from copy import copy
2
2
import os
 
3
import shutil
 
4
import tempfile
3
5
 
4
6
from cloudinit.sources import DataSourceMAAS
5
7
from cloudinit import url_helper
6
 
from ..helpers import populate_dir
7
 
 
8
 
import mocker
9
 
 
10
 
 
11
 
class TestMAASDataSource(mocker.MockerTestCase):
 
8
from ..helpers import TestCase, populate_dir
 
9
 
 
10
try:
 
11
    from unittest import mock
 
12
except ImportError:
 
13
    import mock
 
14
 
 
15
 
 
16
class TestMAASDataSource(TestCase):
12
17
 
13
18
    def setUp(self):
14
19
        super(TestMAASDataSource, self).setUp()
15
20
        # Make a temp directoy for tests to use.
16
 
        self.tmp = self.makeDir()
 
21
        self.tmp = tempfile.mkdtemp()
 
22
        self.addCleanup(shutil.rmtree, self.tmp)
17
23
 
18
24
    def test_seed_dir_valid(self):
19
25
        """Verify a valid seeddir is read as such."""
93
99
 
94
100
    def test_seed_url_valid(self):
95
101
        """Verify that valid seed_url is read as such."""
96
 
        valid = {'meta-data/instance-id': 'i-instanceid',
 
102
        valid = {
 
103
            'meta-data/instance-id': 'i-instanceid',
97
104
            'meta-data/local-hostname': 'test-hostname',
98
105
            'meta-data/public-keys': 'test-hostname',
99
 
            'user-data': 'foodata'}
 
106
            'user-data': 'foodata',
 
107
            }
100
108
        valid_order = [
101
109
            'meta-data/local-hostname',
102
110
            'meta-data/instance-id',
103
111
            'meta-data/public-keys',
104
112
            'user-data',
105
 
        ]
 
113
            ]
106
114
        my_seed = "http://example.com/xmeta"
107
115
        my_ver = "1999-99-99"
108
116
        my_headers = {'header1': 'value1', 'header2': 'value2'}
110
118
        def my_headers_cb(url):
111
119
            return my_headers
112
120
 
113
 
        mock_request = self.mocker.replace(url_helper.readurl,
114
 
            passthrough=False)
115
 
 
116
 
        for key in valid_order:
117
 
            url = "%s/%s/%s" % (my_seed, my_ver, key)
118
 
            mock_request(url, headers=None, timeout=mocker.ANY,
119
 
                         data=mocker.ANY, sec_between=mocker.ANY,
120
 
                         ssl_details=mocker.ANY, retries=mocker.ANY,
121
 
                         headers_cb=my_headers_cb,
122
 
                         exception_cb=mocker.ANY)
123
 
            resp = valid.get(key)
124
 
            self.mocker.result(url_helper.StringResponse(resp))
125
 
        self.mocker.replay()
126
 
 
127
 
        (userdata, metadata) = DataSourceMAAS.read_maas_seed_url(my_seed,
128
 
            header_cb=my_headers_cb, version=my_ver)
129
 
 
130
 
        self.assertEqual("foodata", userdata)
131
 
        self.assertEqual(metadata['instance-id'],
132
 
            valid['meta-data/instance-id'])
133
 
        self.assertEqual(metadata['local-hostname'],
134
 
            valid['meta-data/local-hostname'])
 
121
        # Each time url_helper.readurl() is called, something different is
 
122
        # returned based on the canned data above.  We need to build up a list
 
123
        # of side effect return values, which the mock will return.  At the
 
124
        # same time, we'll build up a list of expected call arguments for
 
125
        # asserting after the code under test is run.
 
126
        calls = []
 
127
 
 
128
        def side_effect():
 
129
            for key in valid_order:
 
130
                resp = valid.get(key)
 
131
                url = "%s/%s/%s" % (my_seed, my_ver, key)
 
132
                calls.append(
 
133
                    mock.call(url, headers=None, timeout=mock.ANY,
 
134
                              data=mock.ANY, sec_between=mock.ANY,
 
135
                              ssl_details=mock.ANY, retries=mock.ANY,
 
136
                              headers_cb=my_headers_cb,
 
137
                              exception_cb=mock.ANY))
 
138
                yield url_helper.StringResponse(resp)
 
139
 
 
140
        # Now do the actual call of the code under test.
 
141
        with mock.patch.object(url_helper, 'readurl',
 
142
                               side_effect=side_effect()) as mockobj:
 
143
            userdata, metadata = DataSourceMAAS.read_maas_seed_url(
 
144
                my_seed, header_cb=my_headers_cb, version=my_ver)
 
145
 
 
146
            self.assertEqual("foodata", userdata)
 
147
            self.assertEqual(metadata['instance-id'],
 
148
                             valid['meta-data/instance-id'])
 
149
            self.assertEqual(metadata['local-hostname'],
 
150
                             valid['meta-data/local-hostname'])
 
151
 
 
152
            mockobj.has_calls(calls)
135
153
 
136
154
    def test_seed_url_invalid(self):
137
155
        """Verify that invalid seed_url raises MAASSeedDirMalformed."""