def test_create_succeeds_when_host_map_already_exists(self): # To omshell, creating the same host map twice is an error. But # Omshell.create swallows the error and makes it look like # success. params = { "ip": factory.make_ip_address(ipv6=self.ipv6), "mac": factory.make_mac_address(), "hostname": factory.make_name("hostname"), } shell = Omshell( factory.make_name("server"), factory.make_name("key"), ipv6=self.ipv6, ) # This is the kind of error output we get if a host map has # already been created. error_output = (dedent("""\ obj: host ip-address = %(ip)s hardware-address = %(mac)s name = "%(hostname)s" > can't open object: I/O error obj: host ip-address = %(ip)s hardware-address = %(mac)s name = "%(hostname)s" """) % params) shell._run = Mock(return_value=(0, error_output.encode("ascii"))) shell.create(params["ip"], params["mac"]) # The test is that we get here without error. pass
def test_create_calls_omshell_correctly(self): server_address = factory.make_string() shared_key = factory.make_string() ip_address = factory.make_ip_address(ipv6=self.ipv6) 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"hardware-type")) shell._run = recorder shell.create(ip_address, mac_address) expected_script = dedent("""\ server {server} port {port} key omapi_key {key} connect new host set ip-address = {ip} set hardware-address = {mac} set hardware-type = 1 set name = "{name}" create """) expected_script = expected_script.format( server=server_address, port=self.port, key=shared_key, ip=ip_address, mac=mac_address, name=mac_address.replace(":", "-"), ) # Check that the 'stdin' arg contains the correct set of # commands. self.assertEqual( [1, (expected_script.encode("utf-8"), )], [recorder.call_count, recorder.extract_args()[0]], )