Beispiel #1
0
    def remove(self, ip_address):
        # The "name" is not a host name; it's an identifier used within
        # the DHCP server.  We just happen to use the IP address.
        stdin = dedent("""\
            server {self.server_address}
            key omapi_key {self.shared_key}
            connect
            new host
            set name = "{ip_address}"
            open
            remove
            """)
        stdin = stdin.format(self=self, ip_address=ip_address)

        returncode, output = self._run(stdin)

        # If the omshell worked, the last line should reference a null
        # object.
        lines = output.splitlines()
        try:
            last_line = lines[-1]
        except IndexError:
            last_line = ""
        if last_line != "obj: <null>":
            raise ExternalProcessError(returncode, "omshell", output)
Beispiel #2
0
 def _run(self, stdin):
     command = ["omshell"]
     proc = Popen(command, stdin=PIPE, stdout=PIPE)
     stdout, stderr = proc.communicate(stdin)
     if proc.poll() != 0:
         raise ExternalProcessError(proc.returncode, command, stdout)
     return proc.returncode, stdout
Beispiel #3
0
    def create(self, ip_address, mac_address):
        # The "name" is not a host name; it's an identifier used within
        # the DHCP server.  We just happen to use the IP address.
        stdin = dedent("""\
            server {self.server_address}
            key omapi_key {self.shared_key}
            connect
            new host
            set ip-address = {ip_address}
            set hardware-address = {mac_address}
            set hardware-type = 1
            set name = "{ip_address}"
            create
            """)
        stdin = stdin.format(self=self,
                             ip_address=ip_address,
                             mac_address=mac_address)

        returncode, output = self._run(stdin)
        # If the call to omshell doesn't result in output containing the
        # magic string 'hardware-type' then we can be reasonably sure
        # that the 'create' command failed.  Unfortunately there's no
        # other output like "successful" to check so this is the best we
        # can do.
        if "hardware-type" in output:
            # Success.
            pass
        elif "can't open object: I/O error" in output:
            # Host map already existed.  Treat as success.
            pass
        else:
            raise ExternalProcessError(returncode, "omshell", output)
Beispiel #4
0
 def test_to_unicode_defers_to_unicode_constructor(self):
     # Unicode strings and non-byte strings are handed to unicode()
     # to undergo Python's normal coercion strategy. (For unicode
     # strings this is actually a no-op, but it's cheaper to do this
     # than special-case unicode strings.)
     self.assertEqual(
         unicode(self), ExternalProcessError._to_unicode(self))
Beispiel #5
0
 def test_to_ascii_removes_non_printable_chars(self):
     # After conversion to a byte string, all non-printable and
     # non-ASCII characters are replaced with question marks.
     byte_string = b"*How* many roads\x01\x02\xb2\xfe"
     expected_byte_string = b"*How* many roads????"
     converted_string = ExternalProcessError._to_ascii(byte_string)
     self.assertIsInstance(converted_string, bytes)
     self.assertEqual(expected_byte_string, converted_string)
Beispiel #6
0
 def test_to_unicode_decodes_to_unicode(self):
     # Byte strings are decoded as ASCII by _to_unicode(), replacing
     # all non-ASCII characters with U+FFFD REPLACEMENT CHARACTERs.
     byte_string = b"This string will be converted. \xe5\xb2\x81\xe5."
     expected_unicode_string = (
         u"This string will be converted. \ufffd\ufffd\ufffd\ufffd.")
     converted_string = ExternalProcessError._to_unicode(byte_string)
     self.assertIsInstance(converted_string, unicode)
     self.assertEqual(expected_unicode_string, converted_string)
Beispiel #7
0
    def test_cleans_up_after_failure(self):
        self.patch(subprocess,
                   'check_call').side_effect = (ExternalProcessError(
                       -1, "some_command"))
        fake_image = factory.make_name('image')
        self.patch(ephemerals_script,
                   'move_file_by_glob').return_value = (fake_image)
        tarball = factory.make_name('tarball') + '.tar.gz'
        target_dir = self.make_dir()
        temp_location = self.make_dir()

        self.assertRaises(ExternalProcessError, extract_image_tarball, tarball,
                          target_dir, temp_location)

        self.assertItemsEqual([], listdir(temp_location))
Beispiel #8
0
 def test__unicode__contains_output(self):
     output = "Hëré's søme øu†pût"
     error = ExternalProcessError(
         returncode=-1, cmd="foo-bar", output=output)
     self.assertIn(output, error.__unicode__())
Beispiel #9
0
 def test__str__contains_output(self):
     output = u"Hëré's søme øu†pût"
     ascii_output = "H?r?'s s?me ?u?p?t"
     error = ExternalProcessError(
         returncode=-1, cmd="foo-bar", output=output)
     self.assertIn(ascii_output, error.__str__())
Beispiel #10
0
 def test__unicode__returns_unicode(self):
     error = ExternalProcessError(returncode=-1, cmd="foo-bar")
     self.assertIsInstance(error.__unicode__(), unicode)
Beispiel #11
0
 def test__str__returns_bytes(self):
     error = ExternalProcessError(returncode=-1, cmd="foo-bar")
     self.assertIsInstance(error.__str__(), bytes)
Beispiel #12
0
 def test_to_ascii_defers_to_bytes(self):
     # Byte strings and non-unicode strings are handed to bytes() to
     # undergo Python's normal coercion strategy. (For byte strings
     # this is actually a no-op, but it's cheaper to do this than
     # special-case byte strings.)
     self.assertEqual(bytes(self), ExternalProcessError._to_ascii(self))
Beispiel #13
0
 def test_to_ascii_encodes_to_bytes(self):
     unicode_string = u"Thîs nøn-åßçií s†ring will be cönvërted"
     expected_byte_string = b"Th?s n?n-???i? s?ring will be c?nv?rted"
     converted_string = ExternalProcessError._to_ascii(unicode_string)
     self.assertIsInstance(converted_string, bytes)
     self.assertEqual(expected_byte_string, converted_string)