65
68
check_token_func.assert_called_once_with(client, 'tokenfakeran')
66
69
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)
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(
76
81
configure_logging_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)
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')
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)
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)
131
@patch('assess_jes_deploy.EnvJujuClient.by_version')
132
def test_make_hosted_env_client(
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')
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)
147
@patch('assess_jes_deploy.EnvJujuClient.by_version')
148
def test_make_hosted_env_client_jes_by_default(
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)
125
159
class TestHostedEnvironment(tests.FakeHomeTestCase):
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')))
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")