Merge lp:~veebers/juju-ci-tools/ec2_client_handle_when_None into lp:juju-ci-tools

Proposed by Christopher Lee
Status: Merged
Merged at revision: 1978
Proposed branch: lp:~veebers/juju-ci-tools/ec2_client_handle_when_None
Merge into: lp:juju-ci-tools
Diff against target: 135 lines (+54/-35)
3 files modified
substrate.py (+8/-1)
tests/test_industrial_test.py (+13/-10)
tests/test_substrate.py (+33/-24)
To merge this branch: bzr merge lp:~veebers/juju-ci-tools/ec2_client_handle_when_None
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+321810@code.launchpad.net

Commit message

It's possible that the creation of the EC2Connection client fails and is None.

Description of the change

It's possible that the creation of the EC2Connection client fails and is None, this causes an issue further down the track when we attempt to use that client and it's only then discovered that it's None.

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'substrate.py'
2--- substrate.py 2017-03-29 17:44:24 +0000
3+++ substrate.py 2017-04-04 05:00:37 +0000
4@@ -121,7 +121,14 @@
5 client = ec2.connect_to_region(
6 region, aws_access_key_id=euca_environ['EC2_ACCESS_KEY'],
7 aws_secret_access_key=euca_environ['EC2_SECRET_KEY'])
8- yield cls(euca_environ, region, client)
9+ # There is no point constructing a AWSAccount if client is None.
10+ # It can't do anything.
11+ if client is None:
12+ log.info(
13+ 'Failed to create ec2 client for region: {}.'.format(region))
14+ yield None
15+ else:
16+ yield cls(euca_environ, region, client)
17
18 def __init__(self, euca_environ, region, client):
19 self.euca_environ = euca_environ
20
21=== modified file 'tests/test_industrial_test.py'
22--- tests/test_industrial_test.py 2017-02-22 18:02:19 +0000
23+++ tests/test_industrial_test.py 2017-04-04 05:00:37 +0000
24@@ -2149,8 +2149,9 @@
25 def test_make_substrate_no_requirements(self):
26 client = ModelClient(get_aws_juju_data(), '', '')
27 client.env.credentials = {'credentials': {'aws': {'creds': {}}}}
28- with make_substrate_manager(client, []) as substrate:
29- self.assertIs(type(substrate), AWSAccount)
30+ with patch('substrate.ec2.connect_to_region', autospec=True):
31+ with make_substrate_manager(client, []) as substrate:
32+ self.assertIs(type(substrate), AWSAccount)
33
34 def test_make_substrate_manager_unsatisifed_requirements(self):
35 client = ModelClient(get_aws_juju_data(), '', '')
36@@ -2164,14 +2165,16 @@
37 def test_make_substrate_satisfied_requirements(self):
38 client = ModelClient(get_aws_juju_data(), '', '')
39 client.env.credentials = {'credentials': {'aws': {'creds': {}}}}
40- with make_substrate_manager(
41- client, ['iter_security_groups']) as substrate:
42- self.assertIs(type(substrate), AWSAccount)
43- with make_substrate_manager(
44- client, ['iter_security_groups',
45- 'iter_instance_security_groups']
46- ) as substrate:
47- self.assertIs(type(substrate), AWSAccount)
48+ with patch('substrate.ec2.connect_to_region', autospec=True):
49+ with make_substrate_manager(
50+ client, ['iter_security_groups']) as substrate:
51+ self.assertIs(type(substrate), AWSAccount)
52+ with make_substrate_manager(
53+ client, [
54+ 'iter_security_groups',
55+ 'iter_instance_security_groups']
56+ ) as substrate:
57+ self.assertIs(type(substrate), AWSAccount)
58
59
60 class TestStageInfo(TestCase):
61
62=== modified file 'tests/test_substrate.py'
63--- tests/test_substrate.py 2017-03-29 17:53:54 +0000
64+++ tests/test_substrate.py 2017-04-04 05:00:37 +0000
65@@ -286,20 +286,28 @@
66 class TestAWSAccount(TestCase):
67
68 def test_from_boot_config(self):
69- with AWSAccount.from_boot_config(SimpleEnvironment('foo', {
70- 'type': 'aws',
71- 'access-key': 'skeleton',
72- 'region': 'france',
73- 'secret-key': 'hoover',
74- })) as aws:
75- self.assertEqual(aws.euca_environ, {
76- 'AWS_ACCESS_KEY': 'skeleton',
77- 'AWS_SECRET_KEY': 'hoover',
78- 'EC2_ACCESS_KEY': 'skeleton',
79- 'EC2_SECRET_KEY': 'hoover',
80- 'EC2_URL': 'https://france.ec2.amazonaws.com',
81- })
82- self.assertEqual(aws.region, 'france')
83+ with patch('substrate.ec2.connect_to_region', autospec=True):
84+ with AWSAccount.from_boot_config(SimpleEnvironment('foo', {
85+ 'type': 'aws',
86+ 'access-key': 'skeleton',
87+ 'region': 'france',
88+ 'secret-key': 'hoover',
89+ })) as aws:
90+ self.assertEqual(aws.euca_environ, {
91+ 'AWS_ACCESS_KEY': 'skeleton',
92+ 'AWS_SECRET_KEY': 'hoover',
93+ 'EC2_ACCESS_KEY': 'skeleton',
94+ 'EC2_SECRET_KEY': 'hoover',
95+ 'EC2_URL': 'https://france.ec2.amazonaws.com',
96+ })
97+ self.assertEqual(aws.region, 'france')
98+
99+ def test_client_construction_failure_returns_None(self):
100+ with patch(
101+ 'substrate.ec2.connect_to_region',
102+ autospec=True, return_value=None):
103+ with AWSAccount.from_boot_config(get_aws_env()) as aws:
104+ self.assertIsNone(aws)
105
106 def test_iter_security_groups(self):
107
108@@ -1403,16 +1411,17 @@
109
110 def test_make_substrate_manager_aws(self):
111 boot_config = get_aws_env()
112- with make_substrate_manager(boot_config) as aws:
113- self.assertIs(type(aws), AWSAccount)
114- self.assertEqual(aws.euca_environ, {
115- 'AWS_ACCESS_KEY': 'skeleton-key',
116- 'AWS_SECRET_KEY': 'secret-skeleton-key',
117- 'EC2_ACCESS_KEY': 'skeleton-key',
118- 'EC2_SECRET_KEY': 'secret-skeleton-key',
119- 'EC2_URL': 'https://ca-west.ec2.amazonaws.com',
120- })
121- self.assertEqual(aws.region, 'ca-west')
122+ with patch('substrate.ec2.connect_to_region', autospec=True):
123+ with make_substrate_manager(boot_config) as aws:
124+ self.assertIs(type(aws), AWSAccount)
125+ self.assertEqual(aws.euca_environ, {
126+ 'AWS_ACCESS_KEY': 'skeleton-key',
127+ 'AWS_SECRET_KEY': 'secret-skeleton-key',
128+ 'EC2_ACCESS_KEY': 'skeleton-key',
129+ 'EC2_SECRET_KEY': 'secret-skeleton-key',
130+ 'EC2_URL': 'https://ca-west.ec2.amazonaws.com',
131+ })
132+ self.assertEqual(aws.region, 'ca-west')
133
134 def test_make_substrate_manager_openstack(self):
135 boot_config = get_os_boot_config()

Subscribers

People subscribed via source and target branches