~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-02-09 17:37:54 UTC
  • mto: This revision was merged to the branch mainline in revision 1260.
  • Revision ID: aaron.bentley@canonical.com-20160209173754-a1qbialilhwp4lkm
Implement get_controller_member_status.

Show diffs side-by-side

added added

removed removed

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