def test_run_with_unit_fallback(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status") as st: st.return_value = Status.from_text(self.precise_status_output) remote = remote_from_unit(client, unit) with patch.object(client, "get_juju_output") as mock_gjo: mock_gjo.side_effect = subprocess.CalledProcessError(255, "ssh", output="") with patch.object(remote, "_run_subprocess") as mock_run: mock_run.return_value = "contents of /a/file" output = remote.run("cat /a/file") self.assertEqual(output, "contents of /a/file") mock_gjo.assert_called_once_with("ssh", unit, "cat /a/file", timeout=120) mock_run.assert_called_once_with([ "ssh", "-o", "User ubuntu", "-o", "UserKnownHostsFile /dev/null", "-o", "StrictHostKeyChecking no", "-o", "PasswordAuthentication no", "10.55.60.1", "cat /a/file", ]) self.assertRegexpMatches( self.log_stream.getvalue(), "(?m)^WARNING juju ssh to 'a-application/0' failed, .*")
def test_application_machines(self): status = Status(self.SAMPLE_SHOW_MODEL_OUTPUT, '') output_mock = Mock(return_value=status) fake_client = Mock(get_status=output_mock) data = application_machines(fake_client, 'wiki') output_mock.assert_called_once_with() self.assertEquals(['0'], data)
def test_run_with_unit_fallback(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status") as st: st.return_value = Status.from_text(self.precise_status_output) remote = remote_from_unit(client, unit) with patch.object(client, "get_juju_output") as mock_gjo: mock_gjo.side_effect = subprocess.CalledProcessError( 255, "ssh", output="") with patch.object(remote, "_run_subprocess") as mock_run: mock_run.return_value = "contents of /a/file" output = remote.run("cat /a/file") self.assertEqual(output, "contents of /a/file") mock_gjo.assert_called_once_with("ssh", unit, "cat /a/file", timeout=120) mock_run.assert_called_once_with([ "ssh", "-o", "User ubuntu", "-o", "UserKnownHostsFile /dev/null", "-o", "StrictHostKeyChecking no", "-o", "PasswordAuthentication no", "10.55.60.1", "cat /a/file", ]) self.assertRegexpMatches( self.log_stream.getvalue(), "(?m)^WARNING juju ssh to 'a-application/0' failed, .*")
def test_remote_from_unit(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.precise_status_output) remote = remote_from_unit(client, unit) self.assertEqual(repr(remote), "<SSHRemote env='an-env' unit='a-application/0'>") self.assertIs(False, remote.is_windows())
def test_remote_from_unit_with_status(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" status = Status.from_text(self.win2012hvr2_status_output) remote = remote_from_unit(client, unit, status=status) self.assertEqual( repr(remote), "<WinRmRemote env='an-env' unit='a-application/0'" " addr='10.55.60.2'>") self.assertIs(True, remote.is_windows())
def test_generate_parameters_no_windows(self): class MachineActionNoWindows(MachineAction): skip_windows = True status = Status({'machines': { '1': { 'series': 'winfoo' }, }}, '') with self.assertRaises(InvalidActionError): parameters = MachineActionNoWindows.generate_parameters( None, status) status_2 = Status({'machines': { '1': { 'series': 'wifoo' }, }}, '') parameters = MachineActionNoWindows.generate_parameters(None, status_2) self.assertEqual(parameters, {'machine_id': '1'})
def test_deploy_machine_and_verify_unknown_hostname(self): controller_url_sha = ['url', "1234"] client = fake_juju_client() status = Status({'machines': {"0": {'dns-name': None}}}, '') client.bootstrap() with patch('assess_agent_metadata.get_controller_url_and_sha256', return_value=controller_url_sha, autospec=True): with patch('jujupy.ModelClient.wait_for_started', autospec=True, return_value=status): with self.assertRaises(JujuAssertionError): deploy_machine_and_verify(client)
def test_remote_from_unit(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.precise_status_output) remote = remote_from_unit(client, unit) self.assertEqual( repr(remote), "<SSHRemote env='an-env' unit='a-application/0'>") self.assertIs(False, remote.is_windows())
def test_vivid_controller_to_raise_value_error(self): fake_client = Mock(wraps=fake_juju_client()) get_status_output = Status({ 'machines': { '0': { 'series': 'vivid' } } }, '') fake_client.get_status.return_value = get_status_output with self.assertRaises(ValueError): get_controller_series_and_alternative_series(fake_client)
def test_skip_windows(self): status = Status( { 'machines': { '0': { 'series': 'winfoo' }, '1': { 'series': 'angsty' }, } }, '') for x in range(50): if choose_machine(status, skip_windows=True) == '0': raise AssertionError('Chose windows machine.') status_2 = Status({'machines': { '0': { 'series': 'winfoo' }, }}, '') with self.assertRaises(InvalidActionError): choose_machine(status_2, skip_windows=True)
def test_zesty_controller_to_get_alt_controller_xenial_or_trusty(self): fake_client = Mock(wraps=fake_juju_client()) get_status_output = Status({ 'machines': { '0': { 'series': 'zesty' } } }, '') fake_client.get_status.return_value = get_status_output controller_series, alt_controller_series = \ get_controller_series_and_alternative_series(fake_client) self.assertEquals(controller_series, "zesty") self.assertIn(alt_controller_series, ["xenial", "trusty"])
def test_cat_on_windows(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.win2012hvr2_status_output) response = winrm.Response(("contents of /a/file", "", 0)) remote = remote_from_unit(client, unit) with patch.object(remote.session, "run_cmd", autospec=True, return_value=response) as mock_run: output = remote.cat("/a/file") self.assertEqual(output, "contents of /a/file") st.assert_called_once_with() mock_run.assert_called_once_with("type", ["/a/file"])
def test_perform(self): client = fake_juju_client() client.bootstrap() client.juju('add-machine', ()) up_status = Status( { 'machines': { '0': { 'juju-status': { 'current': 'started' }, 'series': 'angsty', } } }, '') down_status = Status( { 'machines': { '0': { 'juju-status': { 'current': 'down' }, 'series': 'angsty', } } }, '') with patch.object(client._backend, 'juju', wraps=client._backend.juju) as juju_mock: with patch.object(client, 'get_status', side_effect=[up_status, up_status, down_status]): perform(InterruptNetworkAction, client) self.assertEqual([ backend_call( client, 'run', ('--machine', '0', InterruptNetworkAction.get_command())), ], juju_mock.mock_calls)
def test_run_cmd(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.win2012hvr2_status_output) response = winrm.Response(("some out", "some err", 0)) remote = remote_from_unit(client, unit) with patch.object(remote.session, "run_cmd", autospec=True, return_value=response) as mock_run: output = remote.run_cmd( ["C:\\Program Files\\bin.exe", "/IN", "Bob's Stuff"]) self.assertEqual(output, response) st.assert_called_once_with() mock_run.assert_called_once_with( '"C:\\Program Files\\bin.exe"', ['/IN "Bob\'s Stuff"'])
def test_run_cmd(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.win2012hvr2_status_output) response = winrm.Response(("some out", "some err", 0)) remote = remote_from_unit(client, unit) with patch.object(remote.session, "run_cmd", autospec=True, return_value=response) as mock_run: output = remote.run_cmd( ["C:\\Program Files\\bin.exe", "/IN", "Bob's Stuff"]) self.assertEqual(output, response) st.assert_called_once_with() mock_run.assert_called_once_with('"C:\\Program Files\\bin.exe"', ['/IN "Bob\'s Stuff"'])
def test_run_default_command_error_fallback(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" error = subprocess.CalledProcessError(1, "ssh", output="bad command") with patch.object(client, "get_status") as st: st.return_value = Status.from_text(self.precise_status_output) remote = remote_from_unit(client, unit) with patch.object(client, "get_juju_output") as mock_gjo: mock_gjo.side_effect = error with self.assertRaises(subprocess.CalledProcessError) as c: remote.run("cat /a/file") self.assertIs(c.exception, error) mock_gjo.assert_called_once_with("ssh", unit, "cat /a/file", timeout=120) self.assertRegexpMatches( self.log_stream.getvalue(), "(?m)^WARNING juju ssh to 'a-application/0' failed, .*")
def test_copy_on_windows(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" dest = "/local/path" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.win2012hvr2_status_output) response = winrm.Response(("fake output", "", 0)) remote = remote_from_unit(client, unit) with patch.object(remote.session, "run_ps", autospec=True, return_value=response) as mock_run: with patch.object(remote, "_encoded_copy_to_dir", autospec=True) as mock_cpdir: remote.copy(dest, ["C:\\logs\\*", "%APPDATA%\\*.log"]) mock_cpdir.assert_called_once_with(dest, "fake output") st.assert_called_once_with() self.assertEquals(mock_run.call_count, 1) self.assertRegexpMatches( mock_run.call_args[0][0], r'.*"C:\\logs\\[*]","%APPDATA%\\[*].log".*')
def test_copy_on_windows(self): env = JujuData("an-env", {"type": "nonlocal"}) client = ModelClient(env, None, None) unit = "a-application/0" dest = "/local/path" with patch.object(client, "get_status", autospec=True) as st: st.return_value = Status.from_text(self.win2012hvr2_status_output) response = winrm.Response(("fake output", "", 0)) remote = remote_from_unit(client, unit) with patch.object(remote.session, "run_ps", autospec=True, return_value=response) as mock_run: with patch.object(remote, "_encoded_copy_to_dir", autospec=True) as mock_cpdir: remote.copy(dest, ["C:\\logs\\*", "%APPDATA%\\*.log"]) mock_cpdir.assert_called_once_with(dest, "fake output") st.assert_called_once_with() self.assertEquals(mock_run.call_count, 1) self.assertRegexpMatches(mock_run.call_args[0][0], r'.*"C:\\logs\\[*]","%APPDATA%\\[*].log".*')
current: unknown since: 01 Jan 2017 00:00:00-00:00 exposed: true series: trusty network-health: application-status: current: unknown since: 01 Jan 2017 00:00:00-00:00 subordinate-to: - ubuntu relations: juju-info: - network-health series: trusty """) status = Status(yaml.safe_load(status_value), status_value) maas_spaces = dedent(""" [{ "subnets": [{}], "resource_uri": "", "id": 0, "name": "bar" }, { "subnets": [{}], "resource_uri": "", "id": 1, "name": "baz" }] """)
def get_status(self, machine_id=None): return Status(deepcopy(self._status), "")
def test_get_controller_address(self): status = Status({'machines': {"0": {'dns-name': '255.1.1.0'}}}, '') client = fake_juju_client() with patch('jujupy.ModelClient.status_until', return_value=[status], autospec=True): self.assertEqual('255.1.1.0', get_controller_address(client))