def test_remove_calls_omshell_correctly(self): server_address = factory.make_string() shared_key = factory.make_string() mac_address = factory.make_mac_address() shell = Omshell(server_address, shared_key, ipv6=self.ipv6) # Instead of calling a real omshell, we'll just record the # parameters passed to Popen. recorder = FakeMethod(result=(0, b"thing1\nthing2\nobj: <null>")) shell._run = recorder shell.remove(mac_address) expected_script = dedent("""\ server {server} port {port} key omapi_key {key} connect new host set name = "{mac}" open remove """).format( server=server_address, port=self.port, key=shared_key, mac=mac_address.replace(":", "-"), ) expected_results = (expected_script.encode("utf-8"), ) # Check that the 'stdin' arg contains the correct set of # commands. self.assertEqual([expected_results], recorder.extract_args())
def test_remove_works_when_object_already_removed(self): server_address = factory.make_string() shared_key = factory.make_string() ip_address = factory.make_ip_address(ipv6=self.ipv6) shell = Omshell(server_address, shared_key, ipv6=self.ipv6) output = b"obj: <null>\nobj: host\ncan't open object: not found\n" self.patch(shell, "_run").return_value = (0, output) self.assertIsNone(shell.remove(ip_address))
def test_remove_works_when_extraneous_gt_char_present(self): # Sometimes omshell puts a leading '>' character in responses. # We need to test that the code still works if that's the case. server_address = factory.make_string() shared_key = factory.make_string() ip_address = factory.make_ip_address(ipv6=self.ipv6) shell = Omshell(server_address, shared_key, ipv6=self.ipv6) # Fake a call that results in a something with our special output. output = b"\n>obj: <null>\n>\n" self.patch(shell, "_run").return_value = (0, output) self.assertIsNone(shell.remove(ip_address))
def test_remove_works_when_extraneous_blank_last_lines(self): # Sometimes omshell puts blank lines after the 'obj: <null>' so # we need to test that the code still works if that's the case. server_address = factory.make_string() shared_key = factory.make_string() ip_address = factory.make_ip_address(ipv6=self.ipv6) shell = Omshell(server_address, shared_key, ipv6=self.ipv6) # Fake a call that results in a something with our special output. output = b"\n> obj: <null>\n\n" self.patch(shell, "_run").return_value = (0, output) self.assertIsNone(shell.remove(ip_address))