def test__get_hosts_name_for_node_no_match(self): info = ssh._parse_driver_info(self.node) info["macs"] = ["11:11:11:11:11:11", "22:22:22:22:22:22"] self.mox.StubOutWithMock(ssh, "_exec_ssh_command") ssh._exec_ssh_command(self.sshclient, info["cmd_set"]["list_all"]).AndReturn(["NodeName"]) cmd_to_exec = info["cmd_set"]["get_node_macs"] cmd_to_exec = cmd_to_exec.replace("{_NodeName_}", "NodeName") ssh._exec_ssh_command(self.sshclient, cmd_to_exec).AndReturn(["52:54:00:cf:2d:31"]) self.mox.ReplayAll() found_name = ssh._get_hosts_name_for_node(self.sshclient, info) self.assertEqual(found_name, None) self.mox.VerifyAll()
def test__get_power_status_error(self): info = ssh._parse_driver_info(self.node) self.mox.StubOutWithMock(ssh, "_exec_ssh_command") self.mox.StubOutWithMock(ssh, "_get_hosts_name_for_node") ssh._exec_ssh_command(self.sshclient, info["cmd_set"]["list_running"]).AndReturn( ['"NodeName" {b43c4982-110c-4c29-9325-d5f41b053513}'] ) ssh._get_hosts_name_for_node(self.sshclient, info).AndReturn(None) self.mox.ReplayAll() pstate = ssh._get_power_status(self.sshclient, info) self.assertEqual(pstate, states.ERROR) self.mox.VerifyAll()
def test__get_hosts_name_for_node_no_match(self): info = ssh._parse_driver_info(self.node) info['macs'] = ["11:11:11:11:11:11", "22:22:22:22:22:22"] self.mox.StubOutWithMock(ssh, '_exec_ssh_command') ssh._exec_ssh_command(self.sshclient, info['cmd_set']['list_all']).\ AndReturn(['NodeName']) cmd_to_exec = info['cmd_set']['get_node_macs'] cmd_to_exec = cmd_to_exec.replace('{_NodeName_}', 'NodeName') ssh._exec_ssh_command(self.sshclient, cmd_to_exec).\ AndReturn(['52:54:00:cf:2d:31']) self.mox.ReplayAll() found_name = ssh._get_hosts_name_for_node(self.sshclient, info) self.assertEqual(found_name, None) self.mox.VerifyAll()
def test__get_power_status_error(self): info = ssh._parse_driver_info(self.node) self.mox.StubOutWithMock(ssh, '_exec_ssh_command') self.mox.StubOutWithMock(ssh, '_get_hosts_name_for_node') ssh._exec_ssh_command( self.sshclient, info['cmd_set']['list_running']).AndReturn( ['"NodeName" {b43c4982-110c-4c29-9325-d5f41b053513}']) ssh._get_hosts_name_for_node(self.sshclient, info).\ AndReturn(None) self.mox.ReplayAll() pstate = ssh._get_power_status(self.sshclient, info) self.assertEqual(pstate, states.ERROR) self.mox.VerifyAll()
def test__get_power_status_off(self): info = ssh._parse_driver_info(self.node) self.mox.StubOutWithMock(ssh, '_exec_ssh_command') self.mox.StubOutWithMock(ssh, '_get_hosts_name_for_node') ssh._exec_ssh_command( self.sshclient, info['cmd_set']['list_running']).AndReturn( ['"NodeName" {b43c4982-110c-4c29-9325-d5f41b053513}']) ssh._get_hosts_name_for_node(self.sshclient, info).\ AndReturn("NotNodeName") self.mox.ReplayAll() pstate = ssh._get_power_status(self.sshclient, info) self.assertEqual(pstate, states.POWER_OFF) self.mox.VerifyAll()
def test_exec_ssh_command_good(self): #stop mocking the _exec_ssh_command because we are testing it here self.ssh_patcher.stop() self.ssh_patcher = None class Channel(object): def recv_exit_status(self): return 0 class Stream(object): def __init__(self, buffer=''): self.buffer = buffer self.channel = Channel() def read(self): return self.buffer def close(self): pass with mock.patch.object(self.sshclient, 'exec_command') \ as exec_command_mock: exec_command_mock.return_value = (Stream(), Stream('hello'), Stream()) stdout, stderr = ssh._exec_ssh_command(self.sshclient, "command") self.assertEqual(stdout, 'hello') exec_command_mock.assert_called_once_with("command")
def test_exec_ssh_command_good(self): self.mox.StubOutWithMock(self.sshclient, 'exec_command') class Channel(object): def recv_exit_status(self): return 0 class Stream(object): def __init__(self, buffer=''): self.buffer = buffer self.channel = Channel() def read(self): return self.buffer def close(self): pass self.sshclient.exec_command("command").AndReturn( (Stream(), Stream('hello'), Stream())) self.mox.ReplayAll() stdout, stderr = ssh._exec_ssh_command(self.sshclient, "command") self.assertEqual(stdout, 'hello') self.mox.VerifyAll()
def test__power_off_fail(self): info = ssh._parse_driver_info(self.node) info["macs"] = ["11:11:11:11:11:11", "52:54:00:cf:2d:31"] self.mox.StubOutWithMock(ssh, "_get_power_status") self.mox.StubOutWithMock(ssh, "_get_hosts_name_for_node") self.mox.StubOutWithMock(ssh, "_exec_ssh_command") ssh._get_power_status(self.sshclient, info).AndReturn(states.POWER_ON) ssh._get_hosts_name_for_node(self.sshclient, info).AndReturn("NodeName") cmd_to_exec = info["cmd_set"]["stop_cmd"] cmd_to_exec = cmd_to_exec.replace("{_NodeName_}", "NodeName") ssh._exec_ssh_command(self.sshclient, cmd_to_exec).AndReturn(None) ssh._get_power_status(self.sshclient, info).AndReturn(states.POWER_ON) self.mox.ReplayAll() current_state = ssh._power_off(self.sshclient, info) self.assertEqual(current_state, states.ERROR) self.mox.VerifyAll()
def test__power_off_fail(self): info = ssh._parse_driver_info(self.node) info['macs'] = ["11:11:11:11:11:11", "52:54:00:cf:2d:31"] self.mox.StubOutWithMock(ssh, '_get_power_status') self.mox.StubOutWithMock(ssh, '_get_hosts_name_for_node') self.mox.StubOutWithMock(ssh, '_exec_ssh_command') ssh._get_power_status(self.sshclient, info).\ AndReturn(states.POWER_ON) ssh._get_hosts_name_for_node(self.sshclient, info).\ AndReturn("NodeName") cmd_to_exec = info['cmd_set']['stop_cmd'] cmd_to_exec = cmd_to_exec.replace('{_NodeName_}', 'NodeName') ssh._exec_ssh_command(self.sshclient, cmd_to_exec).\ AndReturn(None) ssh._get_power_status(self.sshclient, info).\ AndReturn(states.POWER_ON) self.mox.ReplayAll() current_state = ssh._power_off(self.sshclient, info) self.assertEqual(current_state, states.ERROR) self.mox.VerifyAll()
def test__power_off_good(self): info = ssh._parse_driver_info(self.node) info['macs'] = ["11:11:11:11:11:11", "52:54:00:cf:2d:31"] self.mox.StubOutWithMock(ssh, '_get_power_status') self.mox.StubOutWithMock(ssh, '_get_hosts_name_for_node') self.mox.StubOutWithMock(ssh, '_exec_ssh_command') ssh._get_power_status(self.sshclient, info).\ AndReturn(states.POWER_ON) ssh._get_hosts_name_for_node(self.sshclient, info).\ AndReturn("NodeName") cmd_to_exec = info['cmd_set']['stop_cmd'] cmd_to_exec = cmd_to_exec.replace('{_NodeName_}', 'NodeName') ssh._exec_ssh_command(self.sshclient, cmd_to_exec).\ AndReturn(None) ssh._get_power_status(self.sshclient, info).\ AndReturn(states.POWER_OFF) self.mox.ReplayAll() current_state = ssh._power_off(self.sshclient, info) self.assertEqual(current_state, states.POWER_OFF) self.mox.VerifyAll()