~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to tests/test_assess_jes_deploy.py

  • Committer: Aaron Bentley
  • Date: 2016-03-18 14:47:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1324.
  • Revision ID: aaron.bentley@canonical.com-20160318144706-z7wy9c21m3psi6g5
Introduce set_model_name, update tests, check controller on bootstrap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
    env_token,
10
10
    hosted_environment,
11
11
    jes_setup,
 
12
    make_hosted_env_client,
12
13
)
13
14
from jujupy import (
 
15
    EnvJujuClient,
14
16
    EnvJujuClient25,
 
17
    JujuData,
15
18
    JUJU_DEV_FEATURE_FLAGS,
16
19
    SimpleEnvironment,
17
20
)
18
21
import tests
19
 
from tests.test_jujupy import fake_juju_client
 
22
from tests.test_jujupy import FakeJujuClient
20
23
 
21
24
 
22
25
class TestJES(tests.FakeHomeTestCase):
24
27
    client_class = EnvJujuClient25
25
28
 
26
29
    @patch('assess_jes_deploy.get_random_string', autospec=True)
27
 
    @patch('jujupy.SimpleEnvironment.from_config')
 
30
    @patch('assess_jes_deploy.SimpleEnvironment.from_config')
28
31
    def mock_client(self, from_config_func, get_random_string_func):
29
32
        from_config_func.return_value = SimpleEnvironment('baz', {})
30
33
        get_random_string_func.return_value = 'fakeran'
46
49
 
47
50
    @patch('assess_jes_deploy.print_now', autospec=True)
48
51
    @patch('assess_jes_deploy.get_random_string', autospec=True)
49
 
    @patch('jujupy.EnvJujuClient.juju', autospec=True)
 
52
    @patch('assess_jes_deploy.EnvJujuClient.juju', autospec=True)
50
53
    @patch('assess_jes_deploy.check_token', autospec=True)
51
54
    def test_check_services(
52
55
            self,
65
68
        check_token_func.assert_called_once_with(client, 'tokenfakeran')
66
69
        print_now_func.assert_called_once_with('checking services in token')
67
70
 
68
 
    @patch('jujupy.EnvJujuClient.get_full_path')
69
 
    @patch('jujupy.EnvJujuClient.add_ssh_machines', autospec=True)
 
71
    @patch('assess_jes_deploy.EnvJujuClient.get_full_path')
 
72
    @patch('assess_jes_deploy.EnvJujuClient.add_ssh_machines', autospec=True)
70
73
    @patch('assess_jes_deploy.boot_context', autospec=True)
71
74
    @patch('assess_jes_deploy.configure_logging', autospec=True)
72
 
    @patch('assess_jes_deploy.client_from_config')
 
75
    @patch('assess_jes_deploy.SimpleEnvironment.from_config')
 
76
    @patch('assess_jes_deploy.EnvJujuClient.by_version')
73
77
    def test_jes_setup(
74
78
            self,
75
79
            by_version_func,
 
80
            from_config_func,
76
81
            configure_logging_func,
77
82
            boot_context_func,
78
83
            add_ssh_machines_func,
79
84
            get_full_path_func):
80
85
        # patch helper funcs
81
86
        expected_client = self.mock_client()
 
87
        expected_env = SimpleEnvironment('baz', {})
 
88
        from_config_func.return_value = expected_env
82
89
        by_version_func.return_value = expected_client
83
90
        configure_logging_func.return_value = None
84
91
        get_full_path_func.return_value = '/path/to/juju'
94
101
        with patch.object(expected_client, 'enable_jes'):
95
102
            with patch.object(expected_client, 'is_jes_enabled',
96
103
                              return_value=True):
97
 
                with jes_setup(setup_args) as (client, charm_series, base_env):
 
104
                with jes_setup(setup_args) as (client, charm_previx, base_env):
98
105
                    self.assertEqual(1, client.is_jes_enabled.call_count)
99
106
                    self.assertEqual(0, client.enable_jes.call_count)
100
107
 
101
108
        # assert that jes_setup provides expected values
102
109
        self.assertIs(client, expected_client)
103
 
        self.assertEqual(charm_series, 'trusty')
 
110
        self.assertEqual(charm_previx, 'local:trusty/')
104
111
        self.assertEqual(base_env, 'baz')
105
112
 
106
113
        # assert that helper funcs were called with expected args.
107
114
        by_version_func.assert_called_once_with(
108
 
            'baz', '/path/to/bin/juju', True)
 
115
            expected_env, '/path/to/bin/juju', True)
109
116
 
110
117
        configure_logging_func.assert_called_once_with(True)
111
118
        boot_context_func.assert_called_once_with(
121
128
                    self.assertEqual(1, client.is_jes_enabled.call_count)
122
129
                    self.assertEqual(1, client.enable_jes.call_count)
123
130
 
 
131
    @patch('assess_jes_deploy.EnvJujuClient.by_version')
 
132
    def test_make_hosted_env_client(
 
133
            self,
 
134
            by_version_func):
 
135
        env = SimpleEnvironment('env', {'type': 'any'})
 
136
        old_client = EnvJujuClient25(env, None, '/a/path')
 
137
        old_client.enable_feature('jes')
 
138
        new_client = make_hosted_env_client(old_client, 'test')
 
139
 
 
140
        self.assertEqual(by_version_func.call_count, 0)
 
141
        self.assertEqual(new_client.env.environment, 'env-test')
 
142
        self.assertEqual(new_client.env.config, {'type': 'any'})
 
143
        self.assertEqual(new_client.full_path, '/a/path')
 
144
        self.assertIs(new_client.debug, False)
 
145
        self.assertIn('jes', new_client.feature_flags)
 
146
 
 
147
    @patch('assess_jes_deploy.EnvJujuClient.by_version')
 
148
    def test_make_hosted_env_client_jes_by_default(
 
149
            self,
 
150
            by_version_func):
 
151
 
 
152
        env = JujuData('env', {'type': 'any'})
 
153
        old_client = EnvJujuClient(env, None, '/a/path')
 
154
        with patch.object(JujuData, 'load_yaml'):
 
155
            new_client = make_hosted_env_client(old_client, 'test')
 
156
        self.assertNotIn('jes', new_client.feature_flags)
 
157
 
124
158
 
125
159
class TestHostedEnvironment(tests.FakeHomeTestCase):
126
160
 
127
161
    def test_hosted_environment(self):
128
 
        hosting_client = fake_juju_client()
 
162
        hosting_client = FakeJujuClient(jes_enabled=True)
129
163
        log_dir = os.path.join(self.home_dir, 'logs')
130
164
        os.mkdir(log_dir)
131
165
        with hosted_environment(hosting_client, log_dir, 'bar') as client:
132
 
            models = client._backend.controller_state.models
133
 
            model_state = models[client.model_name]
 
166
            model_state = client._backing_state
134
167
            self.assertEqual({'name-bar': model_state},
135
 
                             hosting_client._backend.controller_state.models)
 
168
                             hosting_client._backing_state.controller.models)
136
169
            self.assertEqual('created', model_state.state)
137
170
        self.assertEqual('model-destroyed', model_state.state)
138
171
        self.assertTrue(os.path.isdir(os.path.join(log_dir, 'bar')))
139
172
 
140
173
    def test_gathers_machine_logs(self):
141
 
        hosting_client = fake_juju_client()
 
174
        hosting_client = FakeJujuClient(jes_enabled=True)
142
175
        log_dir = os.path.join(self.home_dir, 'logs')
143
176
        os.mkdir(log_dir)
144
177
        with patch("deploy_stack.copy_remote_logs", autospec=True) as mock_crl:
145
178
            with hosted_environment(hosting_client, log_dir, 'bar') as client:
146
 
                client.juju("deploy", ["cs:a-application"])
 
179
                client.juju("deploy", ["cs:a-service"])
147
180
                status = client.get_status()
148
 
                unit_machine = status.get_unit("a-application/0")["machine"]
 
181
                unit_machine = status.get_unit("a-service/0")["machine"]
149
182
                addr = status.status["machines"][unit_machine]["dns-name"]
150
183
        hosted_dir = os.path.join(log_dir, "bar")
151
184
        machine_dir = os.path.join(hosted_dir, "machine-0")