class JobTest(HelperTest):

    @patch("lava.config.Config.save")
    def setUp(self, mocked_config):
        super(JobTest, self).setUp()
        self.config = Config()
        self.config.config_file = self.temp_file.name

    def test_from_template(self):
        template = {}
        job = Job(template, self.temp_file.name)
        self.assertEqual(job.data, template)
        self.assertIsNot(job.data, template)

    def test_update_data(self):
        image = "/path/to/panda.img"
        param1 = Parameter("device_type")
        param2 = Parameter("image", depends=param1)
        self.config.put_parameter(param1, "panda")
        self.config.put_parameter(param2, image)

        job = Job(BOOT_TEST, self.temp_file.name)
        job.update(self.config)

        self.assertEqual(job.data['device_type'], "panda")
        self.assertEqual(job.data['actions'][0]["parameters"]["image"], image)

    def test_write(self):
        try:
            orig_data = {"foo": "bar"}
            job_file = os.path.join(tempfile.gettempdir(), "a_json_file.json")
            job = Job(orig_data, job_file)
            job.write()

            output = ""
            with open(job_file) as read_file:
                output = read_file.read()

            data = json.loads(output)
            self.assertEqual(data, orig_data)
        finally:
            os.unlink(job_file)

    def test_writes_nicely_formatted_json(self):
        try:
            orig_data = {"foo": "bar"}
            job_file = os.path.join(tempfile.gettempdir(), "b_json_file.json")
            job = Job(orig_data, job_file)
            job.write()

            output = ""
            with open(job_file) as read_file:
                output = read_file.read()

            self.assertTrue(output.startswith("{\n"))
        finally:
            os.unlink(job_file)
Ejemplo n.º 2
0
    def test_device_write(self, mocked_save):
        # User tries to create a new panda device. The conf file is written
        # and contains the expected results.
        hostname = "panda_device"

        config = Config()
        config._config_file = self.temp_file.name
        config.put_parameter(HOSTNAME_PARAMETER, hostname)
        config.put_parameter(PANDA_DEVICE_TYPE, "panda")
        config.put_parameter(PANDA_CONNECTION_COMMAND, "test")

        expected = {
            "hostname": hostname,
            "device_type": "panda",
            "connection_command": "test"
        }

        instance = get_known_device(hostname)
        instance.update(config)
        instance.write(self.temp_file.name)

        expected = ("hostname = panda_device\nconnection_command = test\n"
                    "device_type = panda\n")

        obtained = ""
        with open(self.temp_file.name) as f:
            obtained = f.read()
        self.assertEqual(expected, obtained)
    def setUp(self):
        super(CommandTest, self).setUp()
        self.args.FILE = self.temp_file.name
        self.args.type = "boot-test"

        self.device_type = Parameter('device_type')
        self.prebuilt_image = Parameter('prebuilt_image',
                                        depends=self.device_type)
        self.config = Config()
        self.config.put_parameter(self.device_type, 'foo')
        self.config.put_parameter(self.prebuilt_image, 'bar')
    def setUp(self):
        super(ConfigTest, self).setUp()

        self.config_dir = os.path.join(tempfile.gettempdir(), "config")
        self.xdg_resource = os.path.join(self.config_dir, "linaro")
        self.lavatool_resource = os.path.join(self.xdg_resource, "lava-tool")

        os.makedirs(self.lavatool_resource)

        self.config = Config()
        self.config._ensure_xdg_dirs = MagicMock(
            return_value=self.lavatool_resource)
        self.config.save = MagicMock()
Ejemplo n.º 5
0
 def setUp(self):
     super(CompareDeviceConfCommandTests, self).setUp()
     self.config_file = self.tmp("compare_device_conf_command_tests")
     self.config = Config()
     self.config.config_file = self.config_file
     self.args.use_stored = None
     self.args.dispatcher_config_dir = "/etc/lava-server/dispatcher-config/"
     self.temp_yaml = tempfile.NamedTemporaryFile(suffix=".yaml",
                                                  delete=False)
Ejemplo n.º 6
0
    def test_device_update_1(self, patched_save):
        # Tests that when calling update() on a Device, the template gets
        # updated with the correct values from a Config instance.
        hostname = "panda_device"

        config = Config()
        config._config_file = self.temp_file.name
        config.put_parameter(HOSTNAME_PARAMETER, hostname)
        config.put_parameter(PANDA_DEVICE_TYPE, "panda")
        config.put_parameter(PANDA_CONNECTION_COMMAND, "test")

        expected = {
            "hostname": hostname,
            "device_type": "panda",
            "connection_command": "test"
        }

        instance = get_known_device(hostname)
        instance.update(config)

        self.assertEqual(expected, instance.data)
class TestConfigSave(ConfigTestCase):

    """Used to test the save() method of config class.

    Done here since in the other tests we want to mock the atexit save call
    in order not to write the file, or accidentaly overwrite the real
    user file.
    """

    def setUp(self):
        super(TestConfigSave, self).setUp()
        self.config = Config()
        self.config.config_file = self.temp_file.name

    def test_config_save(self):
        self.config.put_parameter(self.param1, "foo")
        self.config.save()

        expected = "[DEFAULT]\nfoo = foo\n\n"
        obtained = ""
        with open(self.temp_file.name) as tmp_file:
            obtained = tmp_file.read()
        self.assertEqual(expected, obtained)

    def test_save_list_param(self):
        # Tests that when saved to file, the ListParameter parameter is stored
        # correctly.
        param_values = ["foo", "more than one words", "bar"]
        list_param = ListParameter("list")
        list_param.set(param_values)

        self.config.put_parameter(list_param, param_values)
        self.config.save()

        expected = "[DEFAULT]\nlist = " + ",".join(param_values) + "\n\n"
        obtained = ""
        with open(self.temp_file.name, "r") as read_file:
            obtained = read_file.read()
        self.assertEqual(expected, obtained)
 def setUp(self):
     super(InitCommandTests, self).setUp()
     self.config_file = self.tmp("init_command_tests")
     self.config = Config()
     self.config.config_file = self.config_file
 def setUp(self):
     super(SubmitCommandTests, self).setUp()
     self.config_file = self.tmp("submit_command_tests")
     self.config = Config()
     self.config.config_file = self.config_file
     self.config.save = MagicMock()
 def setUp(self, mocked_config):
     super(JobTest, self).setUp()
     self.config = Config()
     self.config.config_file = self.temp_file.name
class ConfigTest(ConfigTestCase):

    def setUp(self):
        super(ConfigTest, self).setUp()

        self.config_dir = os.path.join(tempfile.gettempdir(), "config")
        self.xdg_resource = os.path.join(self.config_dir, "linaro")
        self.lavatool_resource = os.path.join(self.xdg_resource, "lava-tool")

        os.makedirs(self.lavatool_resource)

        self.config = Config()
        self.config._ensure_xdg_dirs = MagicMock(
            return_value=self.lavatool_resource)
        self.config.save = MagicMock()

    def tearDown(self):
        super(ConfigTest, self).tearDown()
        if os.path.isdir(self.config_dir):
            shutil.rmtree(self.config_dir)

    def test_ensure_xdg_dirs(self):
        # Test that xdg can create the correct cache path, we remove it
        # at the end since we patch the default value.
        obtained = self.config._ensure_xdg_dirs()
        self.assertEquals(self.lavatool_resource, obtained)

    def test_config_file(self):
        expected = os.path.join(self.lavatool_resource, "lava-tool.ini")
        obtained = self.config.config_file
        self.assertEquals(expected, obtained)

    def test_config_put_in_cache_0(self):
        self.config._put_in_cache("key", "value", "section")
        self.assertEqual(self.config._cache["section"]["key"], "value")

    def test_config_get_from_cache_0(self):
        self.config._put_in_cache("key", "value", "section")
        obtained = self.config._get_from_cache(Parameter("key"), "section")
        self.assertEqual("value", obtained)

    def test_config_get_from_cache_1(self):
        self.config._put_in_cache("key", "value", "DEFAULT")
        obtained = self.config._get_from_cache(Parameter("key"), "DEFAULT")
        self.assertEqual("value", obtained)

    def test_config_put_0(self):
        # Puts a value in the DEFAULT section.
        self.config._put_in_cache = MagicMock()
        self.config.put("foo", "foo")
        expected = "foo"
        obtained = self.config._config_backend.get("DEFAULT", "foo")
        self.assertEqual(expected, obtained)

    def test_config_put_1(self):
        # Puts a value in a new section.
        self.config._put_in_cache = MagicMock()
        self.config.put("foo", "foo", "bar")
        expected = "foo"
        obtained = self.config._config_backend.get("bar", "foo")
        self.assertEqual(expected, obtained)

    def test_config_put_parameter_0(self):
        self.config._calculate_config_section = MagicMock(return_value="")
        self.assertRaises(CommandError, self.config.put_parameter, self.param1)

    @patch("lava.config.Config.put")
    def test_config_put_parameter_1(self, mocked_config_put):
        self.config._calculate_config_section = MagicMock(
            return_value="DEFAULT")

        self.param1.value = "bar"
        self.config.put_parameter(self.param1)

        self.assertEqual(mocked_config_put.mock_calls,
                         [call("foo", "bar", "DEFAULT")])

    def test_config_get_0(self):
        # Tests that with a non existing parameter, it returns None.
        param = Parameter("baz")
        self.config._get_from_cache = MagicMock(return_value=None)
        self.config._calculate_config_section = MagicMock(
            return_value="DEFAULT")

        expected = None
        obtained = self.config.get(param)
        self.assertEqual(expected, obtained)

    def test_config_get_1(self):
        self.config.put_parameter(self.param1, "foo")
        self.config._get_from_cache = MagicMock(return_value=None)
        self.config._calculate_config_section = MagicMock(
            return_value="DEFAULT")

        expected = "foo"
        obtained = self.config.get(self.param1)
        self.assertEqual(expected, obtained)

    def test_calculate_config_section_0(self):
        expected = "DEFAULT"
        obtained = self.config._calculate_config_section(self.param1)
        self.assertEqual(expected, obtained)

    def test_calculate_config_section_1(self):
        self.config.put_parameter(self.param1, "foo")
        expected = "foo=foo"
        obtained = self.config._calculate_config_section(self.param2)
        self.assertEqual(expected, obtained)

    def test_config_get_from_backend_public(self):
        # Need to to this, since we want a clean Config instance, with
        # a config_file with some content.
        with open(self.config.config_file, "w") as write_config:
            write_config.write("[DEFAULT]\nfoo=bar\n")
        param = Parameter("foo")
        obtained = self.config.get_from_backend(param)
        self.assertEquals("bar", obtained)
 def setUp(self):
     super(TestConfigSave, self).setUp()
     self.config = Config()
     self.config.config_file = self.temp_file.name