Example #1
0
    def test_custom_error_attributes(self):
        """Test the properties of ValueUnsupportedError and its children."""
        with self.assertRaises(ValueUnsupportedError) as catcher:
            validate_int("a")
        self.assertEqual(catcher.exception.value_type, "input")
        self.assertEqual(catcher.exception.actual_value, "a")
        self.assertEqual(catcher.exception.expected_value, "integer")
        self.assertEqual(str(catcher.exception),
                         "Unsupported value 'a' for input - expected integer")

        with self.assertRaises(ValueTooLowError) as catcher:
            non_negative_int(-1)
        self.assertEqual(catcher.exception.value_type, "input")
        self.assertEqual(catcher.exception.actual_value, -1)
        self.assertEqual(catcher.exception.expected_value, 0)
        self.assertEqual(
            str(catcher.exception),
            "Value '-1' for input is too low - must be at least 0")

        with self.assertRaises(ValueTooHighError) as catcher:
            validate_int("100", maximum=10, label="score")
        self.assertEqual(catcher.exception.value_type, "score")
        self.assertEqual(catcher.exception.actual_value, 100)
        self.assertEqual(catcher.exception.expected_value, 10)
        self.assertEqual(
            str(catcher.exception),
            "Value '100' for score is too high - must be at most 10")
Example #2
0
    def test_custom_error_attributes(self):
        """Test the properties of ValueUnsupportedError and its children."""
        with self.assertRaises(ValueUnsupportedError) as catcher:
            validate_int("a")
        self.assertEqual(catcher.exception.value_type, "input")
        self.assertEqual(catcher.exception.actual_value, "a")
        self.assertEqual(catcher.exception.expected_value, "integer")
        self.assertEqual(str(catcher.exception),
                         "Unsupported value 'a' for input - expected integer")

        with self.assertRaises(ValueTooLowError) as catcher:
            non_negative_int(-1)
        self.assertEqual(catcher.exception.value_type, "input")
        self.assertEqual(catcher.exception.actual_value, -1)
        self.assertEqual(catcher.exception.expected_value, 0)
        self.assertEqual(
            str(catcher.exception),
            "Value '-1' for input is too low - must be at least 0")

        with self.assertRaises(ValueTooHighError) as catcher:
            validate_int("100", maximum=10, label="score")
        self.assertEqual(catcher.exception.value_type, "score")
        self.assertEqual(catcher.exception.actual_value, 100)
        self.assertEqual(catcher.exception.expected_value, 10)
        self.assertEqual(
            str(catcher.exception),
            "Value '100' for score is too high - must be at most 10")
Example #3
0
    def test_validate_int(self):
        """Test the validate_int() validator."""
        for valid_string in ["1", "08", "123", " 256 ", "-10"]:
            self.assertEqual(validate_int(valid_string), int(valid_string))

        self.assertEqual(256, validate_int("256", minimum=128, maximum=512))
        self.assertEqual(256, validate_int("256", minimum=256))
        self.assertEqual(256, validate_int("256", maximum=256))
        self.assertRaises(ValueUnsupportedError, validate_int, "a")
        self.assertRaises(ValueTooLowError, validate_int, "10", minimum=11)
        self.assertRaises(ValueTooLowError, validate_int, "-1", minimum=0)
        self.assertRaises(ValueTooHighError, validate_int, "10", maximum=9)
        self.assertRaises(ValueTooHighError, validate_int, "1", maximum=0)
Example #4
0
    def test_validate_int(self):
        """Test the validate_int() validator."""
        for valid_string in ["1", "08", "123", " 256 ", "-10"]:
            self.assertEqual(validate_int(valid_string), int(valid_string))

        self.assertEqual(256, validate_int("256", minimum=128, maximum=512))
        self.assertEqual(256, validate_int("256", minimum=256))
        self.assertEqual(256, validate_int("256", maximum=256))
        self.assertRaises(ValueUnsupportedError, validate_int, "a")
        self.assertRaises(ValueTooLowError, validate_int, "10", minimum=11)
        self.assertRaises(ValueTooLowError, validate_int, "-1", minimum=0)
        self.assertRaises(ValueTooHighError, validate_int, "10", maximum=9)
        self.assertRaises(ValueTooHighError, validate_int, "1", maximum=0)
Example #5
0
    def validate_nic_count(self, count):
        """Throw an error if the number of NICs is not supported.

        Args:
          count (int): Number of NICs.

        Raises:
          ValueTooLowError: if ``count`` is less than the minimum
              required by this platform
          ValueTooHighError: if ``count`` is more than the maximum
              supported by this platform
        """
        if count not in self._already_validated[Hardware.nic_count]:
            self._already_validated[Hardware.nic_count][count] = True
            validate_int(count, *self.HARDWARE_LIMITS[Hardware.nic_count],
                         label="NIC count for platform {0}".format(self))
Example #6
0
    def validate_memory_amount(self, mebibytes):
        """Throw an error if the amount of RAM is not supported.

        Args:
          mebibytes (int): RAM, in MiB.

        Raises:
          ValueTooLowError: if ``mebibytes`` is less than the minimum
              required by this platform
            ValueTooHighError: if ``mebibytes`` is more than the maximum
                supported by this platform
        """
        if mebibytes not in self._already_validated[Hardware.memory]:
            self._already_validated[Hardware.memory][mebibytes] = True
            validate_int(mebibytes, *self.HARDWARE_LIMITS[Hardware.memory],
                         label="MiB of RAM for platform {0}".format(self))
Example #7
0
    def validate_cpu_count(self, cpus):
        """Throw an error if the number of CPUs is not a supported value.

        Args:
          cpus (int): Number of CPUs

        Raises:
          ValueTooLowError: if ``cpus`` is less than the minimum required
              by this platform
          ValueTooHighError: if ``cpus`` exceeds the maximum supported
              by this platform
        """
        if cpus not in self._already_validated[Hardware.cpus]:
            self._already_validated[Hardware.cpus][cpus] = True
            validate_int(cpus, *self.HARDWARE_LIMITS[Hardware.cpus],
                         label="CPUs for platform {0}".format(self))
Example #8
0
    def validate_nic_count(self, count):
        """Throw an error if the number of NICs is not supported.

        Args:
          count (int): Number of NICs.

        Raises:
          ValueTooLowError: if ``count`` is less than the minimum
              required by this platform
          ValueTooHighError: if ``count`` is more than the maximum
              supported by this platform
        """
        if count not in self._already_validated[Hardware.nic_count]:
            self._already_validated[Hardware.nic_count][count] = True
            validate_int(count,
                         *self.HARDWARE_LIMITS[Hardware.nic_count],
                         label="NIC count for platform {0}".format(self))
Example #9
0
    def validate_memory_amount(self, mebibytes):
        """Throw an error if the amount of RAM is not supported.

        Args:
          mebibytes (int): RAM, in MiB.

        Raises:
          ValueTooLowError: if ``mebibytes`` is less than the minimum
              required by this platform
            ValueTooHighError: if ``mebibytes`` is more than the maximum
                supported by this platform
        """
        if mebibytes not in self._already_validated[Hardware.memory]:
            self._already_validated[Hardware.memory][mebibytes] = True
            validate_int(mebibytes,
                         *self.HARDWARE_LIMITS[Hardware.memory],
                         label="MiB of RAM for platform {0}".format(self))
Example #10
0
    def validate_cpu_count(self, cpus):
        """Throw an error if the number of CPUs is not a supported value.

        Args:
          cpus (int): Number of CPUs

        Raises:
          ValueTooLowError: if ``cpus`` is less than the minimum required
              by this platform
          ValueTooHighError: if ``cpus`` exceeds the maximum supported
              by this platform
        """
        if cpus not in self._already_validated[Hardware.cpus]:
            self._already_validated[Hardware.cpus][cpus] = True
            validate_int(cpus,
                         *self.HARDWARE_LIMITS[Hardware.cpus],
                         label="CPUs for platform {0}".format(self))