~viswesn/juju-ci-tools/aws_boto3

« back to all changes in this revision

Viewing changes to tests/test_assess_add_cloud.py

  • Committer: Curtis Hovey
  • Date: 2017-01-25 02:32:29 UTC
  • mfrom: (1855 trunk)
  • mto: This revision was merged to the branch mainline in revision 1865.
  • Revision ID: curtis@canonical.com-20170125023229-g7c6bzt0cqe1j8g3
Merged trunk and resolved conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from contextlib import contextmanager
2
2
from copy import deepcopy
 
3
from StringIO import StringIO
3
4
from textwrap import dedent
4
 
from StringIO import StringIO
 
5
from unittest import TestCase
5
6
 
6
7
from mock import (
7
8
    call,
8
9
    patch,
9
10
    )
10
11
 
11
 
from fakejuju import fake_juju_client
12
12
from jujupy import (
13
13
    AuthNotAccepted,
14
 
    JujuData,
 
14
    fake_juju_client,
 
15
    InvalidEndpoint,
15
16
    NameNotAccepted,
16
17
    TypeNotAccepted,
17
18
    )
18
19
from assess_add_cloud import (
19
20
    assess_all_clouds,
20
21
    assess_cloud,
 
22
    CloudMismatch,
21
23
    CloudSpec,
 
24
    cloud_spec,
22
25
    iter_clouds,
 
26
    NameMismatch,
23
27
    write_status,
 
28
    xfail,
24
29
    )
25
30
from tests import FakeHomeTestCase
26
31
from utility import JujuAssertionError
27
32
 
28
33
 
29
 
class TestCase(FakeHomeTestCase):
30
 
 
31
 
    def make_fake_juju_client(self):
32
 
        env = JujuData('foo', juju_home=self.juju_home)
33
 
        return fake_juju_client(env=env)
34
 
 
35
 
 
36
 
class TestAssessCloud(TestCase):
 
34
class TestCloudSpec(TestCase):
 
35
 
 
36
    def test_cloud_spec(self):
 
37
        self.assertEqual(
 
38
                CloudSpec('label1', 'name1', {'config': '1'}, None, None),
 
39
                cloud_spec('label1', 'name1', {'config': '1'}))
 
40
 
 
41
 
 
42
class TestXFail(TestCase):
 
43
 
 
44
    def test_xfail(self):
 
45
        spec = CloudSpec('label', 'name', {'config': 'value'}, 'foo', 'bar')
 
46
        fail_spec = xfail(spec, 'baz', 'qux')
 
47
        self.assertEqual(fail_spec, CloudSpec(
 
48
            'label', 'name', {'config': 'value'}, 'qux', 'baz'))
 
49
 
 
50
 
 
51
class TestAssessCloud(FakeHomeTestCase):
37
52
 
38
53
    @contextmanager
39
54
    def cloud_client(self, clouds):
40
 
        client = self.make_fake_juju_client()
 
55
        client = fake_juju_client(juju_home=self.juju_home)
41
56
        client.env.load_yaml()
42
57
 
43
58
        def dump(cloud_name, cloud):
83
98
            """), stderr.getvalue())
84
99
 
85
100
 
86
 
def make_long_endpoint(spec, regions=False):
 
101
def make_long_endpoint(spec, endpoint_validation, regions=False):
87
102
    config = deepcopy(spec.config)
88
103
    config['endpoint'] = 'A' * 4096
89
104
    if regions:
90
105
        for region in config['regions'].values():
91
106
            region['endpoint'] = 'A' * 4096
92
 
    return CloudSpec('long-endpoint-{}'.format(spec.name), spec.name, config,
93
 
                     exception=None)
94
 
 
95
 
 
96
 
class TestIterClouds(TestCase):
97
 
 
98
 
    bogus_type = CloudSpec('bogus-type', 'bogus-type', {'type': 'bogus'},
99
 
                           exception=TypeNotAccepted)
 
107
    spec = cloud_spec('long-endpoint-{}'.format(spec.name), spec.name, config,
 
108
                      InvalidEndpoint)
 
109
    if not endpoint_validation:
 
110
        spec = xfail(spec, 1641970, CloudMismatch)
 
111
    return spec
 
112
 
 
113
 
 
114
class TestIterClouds(FakeHomeTestCase):
 
115
 
 
116
    bogus_type = cloud_spec('bogus-type', 'bogus-type', {'type': 'bogus'},
 
117
                            exception=TypeNotAccepted)
100
118
 
101
119
    def test_manual(self):
102
 
        cloud = {'type': 'manual', 'endpoint': 'http://example.com'}
103
 
        spec = CloudSpec('foo', 'foo', cloud, exception=None)
 
120
        self.maxDiff = None
 
121
        cloud = {'type': 'manual', 'endpoint': 'http://example.com'}
 
122
        spec = cloud_spec('foo', 'foo', cloud)
 
123
        self.assertItemsEqual([
 
124
            self.bogus_type, xfail(spec, 1649721, InvalidEndpoint),
 
125
            xfail(xfail(cloud_spec('long-name-foo', 'A' * 4096, cloud),
 
126
                        1641970, NameMismatch), 1649721, InvalidEndpoint),
 
127
            xfail(xfail(cloud_spec('invalid-name-foo', 'invalid/name', cloud,
 
128
                        exception=NameNotAccepted), 1641981, None),
 
129
                  1649721, InvalidEndpoint),
 
130
            make_long_endpoint(spec, endpoint_validation=True)],
 
131
                iter_clouds({'foo': cloud}, endpoint_validation=True))
 
132
 
 
133
    def test_manual_no_validation(self):
 
134
        self.maxDiff = None
 
135
        cloud = {'type': 'manual', 'endpoint': 'http://example.com'}
 
136
        spec = cloud_spec('foo', 'foo', cloud)
104
137
        self.assertItemsEqual([
105
138
            self.bogus_type, spec,
106
 
            CloudSpec('long-name-foo', 'A' * 4096, cloud, exception=None),
107
 
            CloudSpec('invalid-name-foo', 'invalid/name', cloud,
108
 
                      exception=NameNotAccepted),
109
 
            make_long_endpoint(spec),
110
 
            ], iter_clouds({'foo': cloud}))
 
139
            xfail(cloud_spec('long-name-foo', 'A' * 4096, cloud),
 
140
                  1641970, NameMismatch),
 
141
            xfail(cloud_spec('invalid-name-foo', 'invalid/name', cloud,
 
142
                             exception=NameNotAccepted), 1641981, None),
 
143
            make_long_endpoint(spec, endpoint_validation=False)],
 
144
                iter_clouds({'foo': cloud}, endpoint_validation=False))
111
145
 
112
146
    def test_vsphere(self):
113
147
        cloud = {
115
149
            'endpoint': '1.2.3.4',
116
150
            'regions': {'q': {'endpoint': '1.2.3.4'}},
117
151
            }
118
 
        spec = CloudSpec('foo', 'foo', cloud, exception=None)
 
152
        spec = cloud_spec('foo', 'foo', cloud, exception=None)
119
153
        self.assertItemsEqual([
120
154
            self.bogus_type, spec,
121
 
            CloudSpec('invalid-name-foo', 'invalid/name', cloud,
122
 
                      exception=NameNotAccepted),
123
 
            CloudSpec('long-name-foo', 'A' * 4096, cloud, exception=None),
124
 
            make_long_endpoint(spec, regions=True),
125
 
            ], iter_clouds({'foo': cloud}))
 
155
            xfail(cloud_spec('invalid-name-foo', 'invalid/name', cloud,
 
156
                             exception=NameNotAccepted), 1641981, None),
 
157
            xfail(cloud_spec('long-name-foo', 'A' * 4096, cloud,
 
158
                             exception=None), 1641970, NameMismatch),
 
159
            xfail(make_long_endpoint(spec, regions=True,
 
160
                                     endpoint_validation=True),
 
161
                  1641970, CloudMismatch),
 
162
            ], iter_clouds({'foo': cloud}, endpoint_validation=True))
126
163
 
127
164
    def test_maas(self):
128
165
        cloud = {
129
166
            'type': 'maas',
130
167
            'endpoint': 'http://example.com',
131
168
            }
132
 
        spec = CloudSpec('foo', 'foo', cloud, exception=None)
133
 
        self.assertItemsEqual([
134
 
            self.bogus_type, spec,
135
 
            CloudSpec('invalid-name-foo', 'invalid/name', cloud,
136
 
                      exception=NameNotAccepted),
137
 
            CloudSpec('long-name-foo', 'A' * 4096, cloud, exception=None),
138
 
            make_long_endpoint(spec),
139
 
            ], iter_clouds({'foo': cloud}))
 
169
        spec = cloud_spec('foo', 'foo', cloud, exception=None)
 
170
        self.assertItemsEqual([
 
171
            self.bogus_type, spec,
 
172
            xfail(cloud_spec('invalid-name-foo', 'invalid/name', cloud,
 
173
                             exception=NameNotAccepted), 1641981, None),
 
174
            xfail(cloud_spec('long-name-foo', 'A' * 4096, cloud,
 
175
                             exception=None), 1641970, NameMismatch),
 
176
            make_long_endpoint(spec, endpoint_validation=True),
 
177
            ], iter_clouds({'foo': cloud}, endpoint_validation=True))
 
178
 
 
179
    def test_maas_no_validation(self):
 
180
        cloud = {
 
181
            'type': 'maas',
 
182
            'endpoint': 'http://example.com',
 
183
            }
 
184
        spec = cloud_spec('foo', 'foo', cloud, exception=None)
 
185
        self.assertItemsEqual([
 
186
            self.bogus_type, spec,
 
187
            xfail(cloud_spec('invalid-name-foo', 'invalid/name', cloud,
 
188
                             exception=NameNotAccepted), 1641981, None),
 
189
            xfail(cloud_spec('long-name-foo', 'A' * 4096, cloud,
 
190
                             exception=None), 1641970, NameMismatch),
 
191
            make_long_endpoint(spec, endpoint_validation=False),
 
192
            ], iter_clouds({'foo': cloud}, endpoint_validation=False))
140
193
 
141
194
    def test_openstack(self):
142
195
        config = {'type': 'openstack', 'endpoint': 'http://example.com',
143
196
                  'regions': {'bar': {'endpoint': 'http://baz.example.com'}}}
144
 
        spec = CloudSpec('foo', 'foo', config, exception=None)
145
 
        invalid_name = CloudSpec('invalid-name-foo', 'invalid/name', config,
146
 
                                 exception=NameNotAccepted)
147
 
        long_name = CloudSpec('long-name-foo', 'A' * 4096, config,
148
 
                              exception=None)
149
 
        long_region = CloudSpec('long-endpoint-foo-bar', 'foo',
150
 
                                deepcopy(config), exception=None)
151
 
        long_region.config['regions']['bar']['endpoint'] = 'A' * 4096
152
 
        bogus_auth = CloudSpec('bogus-auth-foo', 'foo',
153
 
                               deepcopy(config), exception=AuthNotAccepted)
154
 
        bogus_auth.config['auth-types'] = ['asdf']
155
 
        self.assertItemsEqual([
156
 
            self.bogus_type, spec, invalid_name, long_name, long_region,
157
 
            bogus_auth, make_long_endpoint(spec),
158
 
            ], iter_clouds({'foo': config}))
159
 
 
160
 
 
161
 
class TestAssessAllClouds(TestCase):
 
197
        spec = cloud_spec('foo', 'foo', config, exception=None)
 
198
        invalid_name = xfail(
 
199
            cloud_spec('invalid-name-foo', 'invalid/name', config,
 
200
                       exception=NameNotAccepted), 1641981, None)
 
201
        long_name = xfail(
 
202
            cloud_spec('long-name-foo', 'A' * 4096, config, exception=None),
 
203
            1641970, NameMismatch)
 
204
        long_region = cloud_spec(
 
205
            'long-endpoint-foo-bar', 'foo', deepcopy(config), InvalidEndpoint)
 
206
        long_region.config['regions']['bar']['endpoint'] = 'A' * 4096
 
207
        bogus_auth = cloud_spec('bogus-auth-foo', 'foo',
 
208
                                deepcopy(config), exception=AuthNotAccepted)
 
209
        bogus_auth.config['auth-types'] = ['asdf']
 
210
        self.assertItemsEqual([
 
211
            self.bogus_type, spec, invalid_name, long_name, long_region,
 
212
            bogus_auth,
 
213
            make_long_endpoint(spec, endpoint_validation=True),
 
214
            ], iter_clouds({'foo': config}, endpoint_validation=True))
 
215
 
 
216
    def test_openstack_no_validation(self):
 
217
        config = {'type': 'openstack', 'endpoint': 'http://example.com',
 
218
                  'regions': {'bar': {'endpoint': 'http://baz.example.com'}}}
 
219
        spec = cloud_spec('foo', 'foo', config, exception=None)
 
220
        invalid_name = xfail(
 
221
            cloud_spec('invalid-name-foo', 'invalid/name', config,
 
222
                       exception=NameNotAccepted), 1641981, None)
 
223
        long_name = xfail(
 
224
            cloud_spec('long-name-foo', 'A' * 4096, config, exception=None),
 
225
            1641970, NameMismatch)
 
226
        long_region = xfail(cloud_spec(
 
227
            'long-endpoint-foo-bar', 'foo', deepcopy(config),
 
228
            InvalidEndpoint), 1641970, CloudMismatch)
 
229
        long_region.config['regions']['bar']['endpoint'] = 'A' * 4096
 
230
        bogus_auth = cloud_spec('bogus-auth-foo', 'foo',
 
231
                                deepcopy(config), exception=AuthNotAccepted)
 
232
        bogus_auth.config['auth-types'] = ['asdf']
 
233
        self.assertItemsEqual([
 
234
            self.bogus_type, spec, invalid_name, long_name, long_region,
 
235
            bogus_auth,
 
236
            make_long_endpoint(spec, endpoint_validation=False),
 
237
            ], iter_clouds({'foo': config}, endpoint_validation=False))
 
238
 
 
239
 
 
240
class TestAssessAllClouds(FakeHomeTestCase):
162
241
 
163
242
    def test_assess_all_clouds(self):
164
 
        client = self.make_fake_juju_client()
 
243
        client = fake_juju_client(juju_home=self.juju_home)
165
244
        clouds = {'a': {'type': 'foo'}, 'b': {'type': 'bar'}}
 
245
        cloud_specs = iter_clouds(clouds, endpoint_validation=True)
166
246
        exception = Exception()
167
247
        with patch('assess_add_cloud.assess_cloud',
168
248
                   side_effect=[TypeNotAccepted(), None] + [exception] * 7):
169
249
            with patch('sys.stdout'):
170
250
                with patch('logging.exception') as exception_mock:
171
 
                    succeeded, failed = assess_all_clouds(client, clouds)
 
251
                    succeeded, xfail, failed = assess_all_clouds(client,
 
252
                                                                 cloud_specs)
172
253
        self.assertEqual({'bogus-type', 'a'}, succeeded)
173
254
        self.assertEqual({
174
255
            'b', 'bogus-auth-a', 'bogus-auth-b', 'invalid-name-a',
176
257
            failed)
177
258
        self.assertEqual(exception_mock.mock_calls, [call(exception)] * 7)
178
259
 
179
 
 
180
 
class TestWriteStatus(TestCase):
 
260
    def test_xfail(self):
 
261
        cloud_specs = [xfail(cloud_spec('label1', 'name1', {'config': '1'}),
 
262
                             27, TypeNotAccepted)]
 
263
        client = fake_juju_client(juju_home=self.juju_home)
 
264
        with patch('assess_add_cloud.assess_cloud',
 
265
                   side_effect=TypeNotAccepted):
 
266
            with patch('logging.exception') as exception_mock:
 
267
                with patch('sys.stdout'):
 
268
                    succeeded, xfailed, failed = assess_all_clouds(client,
 
269
                                                                   cloud_specs)
 
270
        self.assertEqual(set(), failed)
 
271
        self.assertEqual({27: {'label1'}}, xfailed)
 
272
        self.assertEqual(0, exception_mock.call_count)
 
273
 
 
274
 
 
275
class TestWriteStatus(FakeHomeTestCase):
181
276
 
182
277
    def do_write(self, status, items):
183
278
        stdout = StringIO()