def test_no_leader_only_has_controller(self, ti_mock, wsss_mock):
     client = fake_juju_client()
     bs_manager = make_bs_manager(client)
     client.bootstrap()
     bs_manager.has_controller = True
     delete_controller_members(bs_manager,
                               client.get_controller_client(),
                               leader_only=False)
     self.assertIs(False, bs_manager.has_controller)
 def test_delete_controller_members_azure(self, ti_mock, wsss_mock):
     client = Mock(spec=['env', 'get_controller_leader'])
     client.env = sentinel.env
     client.env.provider = 'azure'
     client.get_controller_leader.return_value = Machine(
         '3', {
             'dns-name': '10.0.0.3',
             'instance-id': 'juju-dddd-machine-3',
             'controller-member-status': 'has-vote'
         })
     known_hosts = {'1': 'a', '2': 'b', '3': '10.0.0.3'}
     bs_manager = Mock(client=client,
                       known_hosts=known_hosts,
                       has_controller=False)
     with patch('assess_recovery.convert_to_azure_ids',
                autospec=True,
                return_value=['juju-azure-id']):
         deleted = delete_controller_members(bs_manager,
                                             client,
                                             leader_only=True)
     self.assertEqual(['3'], deleted)
     client.get_controller_leader.assert_called_once_with()
     ti_mock.assert_called_once_with(client.env, ['juju-azure-id'])
     wsss_mock.assert_called_once_with('10.0.0.3',
                                       client,
                                       'juju-azure-id',
                                       timeout=120)
     self.assertEqual(
         self.log_stream.getvalue(),
         'INFO Instrumenting node failure for member 3:'
         ' juju-azure-id at 10.0.0.3\n'
         "INFO Deleted ['3']\n")
     self.assertEqual({'1': 'a', '2': 'b'}, bs_manager.known_hosts)
 def test_delete_controller_members(self, ti_mock, wsss_mock):
     client = Mock(spec=['env', 'get_controller_members'])
     client.env = sentinel.env
     client.env.provider = 'lxd'
     client.get_controller_members.return_value = [
         Machine(
             '3', {
                 'dns-name': '10.0.0.3',
                 'instance-id': 'juju-dddd-machine-3',
                 'controller-member-status': 'has-vote'
             }),
         Machine(
             '0', {
                 'dns-name': '10.0.0.0',
                 'instance-id': 'juju-aaaa-machine-0',
                 'controller-member-status': 'has-vote'
             }),
         Machine(
             '2', {
                 'dns-name': '10.0.0.2',
                 'instance-id': 'juju-cccc-machine-2',
                 'controller-member-status': 'has-vote'
             }),
     ]
     known_hosts = {'0': '10.0.0.0', '2': '10.0.0.2', '3': '10.0.0.3'}
     bs_manager = Mock(client=client,
                       known_hosts=known_hosts,
                       has_controller=False)
     deleted = delete_controller_members(bs_manager, client)
     self.assertEqual(['2', '0', '3'], deleted)
     client.get_controller_members.assert_called_once_with()
     # terminate_instance was call in the reverse order of members.
     self.assertEqual([
         call(client.env, ['juju-cccc-machine-2']),
         call(client.env, ['juju-aaaa-machine-0']),
         call(client.env, ['juju-dddd-machine-3'])
     ], ti_mock.mock_calls)
     self.assertEqual([
         call('10.0.0.2', client, 'juju-cccc-machine-2', timeout=120),
         call('10.0.0.0', client, 'juju-aaaa-machine-0', timeout=120),
         call('10.0.0.3', client, 'juju-dddd-machine-3', timeout=120)
     ], wsss_mock.mock_calls)
     self.assertEqual(
         self.log_stream.getvalue(),
         'INFO Instrumenting node failure for member 2:'
         ' juju-cccc-machine-2 at 10.0.0.2\n'
         'INFO Instrumenting node failure for member 0:'
         ' juju-aaaa-machine-0 at 10.0.0.0\n'
         'INFO Instrumenting node failure for member 3:'
         ' juju-dddd-machine-3 at 10.0.0.3\n'
         "INFO Deleted ['2', '0', '3']\n")
     self.assertEqual({}, bs_manager.known_hosts)