23
23
self.patches.enter_context(mock.patch('{0}.uhelp'.format(mod_name)))
25
25
def _set_password_server_response(self, response_string):
26
http_client = mock.MagicMock()
27
http_client.HTTPConnection.return_value.sock.recv.return_value = \
28
response_string.encode('utf-8')
26
subp = mock.MagicMock(return_value=(response_string, ''))
29
27
self.patches.enter_context(
30
mock.patch('cloudinit.sources.DataSourceCloudStack.http_client',
28
mock.patch('cloudinit.sources.DataSourceCloudStack.util.subp',
34
32
def test_empty_password_doesnt_create_config(self):
35
33
self._set_password_server_response('')
55
53
ds = DataSourceCloudStack({}, None, helpers.Paths({}))
56
54
self.assertTrue(ds.get_data())
58
def assertRequestTypesSent(self, http_client, expected_request_types):
60
kwargs['headers']['DomU_Request']
62
in http_client.HTTPConnection.return_value.request.call_args_list]
56
def assertRequestTypesSent(self, subp, expected_request_types):
58
for call in subp.call_args_list:
61
if arg.startswith('DomU_Request'):
62
request_types.append(arg.split()[1])
63
63
self.assertEqual(expected_request_types, request_types)
65
65
def test_valid_response_means_password_marked_as_saved(self):
66
66
password = 'SekritSquirrel'
67
http_client = self._set_password_server_response(password)
67
subp = self._set_password_server_response(password)
68
68
ds = DataSourceCloudStack({}, None, helpers.Paths({}))
70
self.assertRequestTypesSent(http_client,
70
self.assertRequestTypesSent(subp,
71
71
['send_my_password', 'saved_password'])
73
73
def _check_password_not_saved_for(self, response_string):
74
http_client = self._set_password_server_response(response_string)
74
subp = self._set_password_server_response(response_string)
75
75
ds = DataSourceCloudStack({}, None, helpers.Paths({}))
77
self.assertRequestTypesSent(http_client, ['send_my_password'])
77
self.assertRequestTypesSent(subp, ['send_my_password'])
79
79
def test_password_not_saved_if_empty(self):
80
80
self._check_password_not_saved_for('')