1
1
from argparse import Namespace
7
6
from assess_jes_deploy import (
11
make_hosted_env_client,
13
13
from jujupy import (
15
16
JUJU_DEV_FEATURE_FLAGS,
19
from tests.test_jujupy import fake_juju_client
22
class TestJES(tests.FakeHomeTestCase):
19
from tests.test_jujupy import FakeJujuClient
22
class TestJES(unittest.TestCase):
24
24
client_class = EnvJujuClient25
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
37
37
@patch('assess_jes_deploy.get_random_string', autospec=True)
65
65
check_token_func.assert_called_once_with(client, 'tokenfakeran')
66
66
print_now_func.assert_called_once_with('checking services in token')
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(
76
78
configure_logging_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)
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')
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)
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)
125
class TestHostedEnvironment(tests.FakeHomeTestCase):
128
@patch('assess_jes_deploy.EnvJujuClient.by_version')
129
def test_make_hosted_env_client(
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')
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)
144
@patch('assess_jes_deploy.EnvJujuClient.by_version')
145
def test_make_hosted_env_client_jes_by_default(
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'))
155
class TestHostedEnvironment(unittest.TestCase):
127
157
def test_hosted_environment(self):
128
hosting_client = fake_juju_client()
129
log_dir = os.path.join(self.home_dir, 'logs')
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')))
140
def test_gathers_machine_logs(self):
141
hosting_client = fake_juju_client()
142
log_dir = os.path.join(self.home_dir, 'logs')
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)