~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

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

  • Committer: Daniel Watkins
  • Date: 2015-06-16 16:35:03 UTC
  • mto: This revision was merged to the branch mainline in revision 1118.
  • Revision ID: daniel.watkins@canonical.com-20150616163503-a6pfzanz9v4c1hyf
Use wget to fetch CloudStack passwords.

Different versions of the CloudStack password server respond
differently; wget handles these nicely for us, so it's easier to just
use wget.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
        self.patches.enter_context(mock.patch('{0}.uhelp'.format(mod_name)))
24
24
 
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',
31
 
                       http_client))
32
 
        return http_client
 
28
            mock.patch('cloudinit.sources.DataSourceCloudStack.util.subp',
 
29
                       subp))
 
30
        return subp
33
31
 
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())
57
55
 
58
 
    def assertRequestTypesSent(self, http_client, expected_request_types):
59
 
        request_types = [
60
 
            kwargs['headers']['DomU_Request']
61
 
            for _, kwargs
62
 
            in http_client.HTTPConnection.return_value.request.call_args_list]
 
56
    def assertRequestTypesSent(self, subp, expected_request_types):
 
57
        request_types = []
 
58
        for call in subp.call_args_list:
 
59
            args = call[0][0]
 
60
            for arg in args:
 
61
                if arg.startswith('DomU_Request'):
 
62
                    request_types.append(arg.split()[1])
63
63
        self.assertEqual(expected_request_types, request_types)
64
64
 
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({}))
69
69
        ds.get_data()
70
 
        self.assertRequestTypesSent(http_client,
 
70
        self.assertRequestTypesSent(subp,
71
71
                                    ['send_my_password', 'saved_password'])
72
72
 
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({}))
76
76
        ds.get_data()
77
 
        self.assertRequestTypesSent(http_client, ['send_my_password'])
 
77
        self.assertRequestTypesSent(subp, ['send_my_password'])
78
78
 
79
79
    def test_password_not_saved_if_empty(self):
80
80
        self._check_password_not_saved_for('')