Example #1
0
    def test_bsc_v1(self):
        """Test :class:`nailgun.config.BaseServerConfig`.

        Assert that ``__repr__`` works correctly when ``url`` is specified.

        """
        target = "nailgun.config.BaseServerConfig(url='bogus')"
        self.assertEqual(target, repr(BaseServerConfig('bogus')))
        import nailgun  # noqa
        self.assertEqual(target, repr(eval(repr(BaseServerConfig('bogus')))))
Example #2
0
    def test_bsc_v2(self):
        """Test :class:`nailgun.config.BaseServerConfig`.

        Assert that ``__repr__`` works correctly when ``url`` and ``auth`` are
        specified.

        """
        targets = (
            "nailgun.config.BaseServerConfig(url='flim', auth='flam')",
            "nailgun.config.BaseServerConfig(auth='flam', url='flim')",
        )
        self.assertIn(repr(BaseServerConfig('flim', auth='flam')), targets)
        import nailgun  # noqa
        self.assertIn(repr(eval(repr(BaseServerConfig('flim', auth='flam')))),
                      targets)
Example #3
0
    def test_save(self):
        """Test :meth:`nailgun.config.BaseServerConfig.save`.

        Assert that the method reads the config file before writing, and that
        it writes out a correct config file.

        """
        label = 'Ask Aak'
        config = {label: {'url': 'https://example.org'}}
        open_ = mock_open(read_data=json.dumps(CONFIGS))
        with patch.object(builtins, 'open', open_):
            BaseServerConfig(config[label]['url']).save(label, FILE_PATH)

        # We care about two things: that this method reads the config file
        # before writing, and that the written string is correct. The first is
        # easy to verify...
        self.assertEqual(
            open_.call_args_list,
            [call(FILE_PATH), call(FILE_PATH, 'w')],
        )
        # ...and the second is a PITA to verify.
        actual_config = _get_written_json(open_)
        target_config = CONFIGS.copy()
        target_config[label] = config[label]
        self.assertEqual(target_config, actual_config)
Example #4
0
    def test_delete(self):
        """Test :meth:`nailgun.config.BaseServerConfig.delete`.

        Assert that the method reads the config file before writing, and that
        it writes out a correct config file.

        """
        open_ = mock_open(read_data=json.dumps(CONFIGS))
        with patch.object(builtins, "open", open_):
            BaseServerConfig.delete("Ask Aak", FILE_PATH)

        # See `test_save` for further commentary on what's being done here.
        self.assertEqual(open_.call_args_list, [call(FILE_PATH), call(FILE_PATH, "w")])
        actual_config = _get_written_json(open_)
        target_config = CONFIGS.copy()
        del target_config["Ask Aak"]
        self.assertEqual(target_config, actual_config)
Example #5
0
    def test_get_labels(self):
        """Test :meth:`nailgun.config.BaseServerConfig.get_labels`.

        Assert that the method returns the correct labels.

        """
        open_ = mock_open(read_data=json.dumps(CONFIGS))
        with patch.object(builtins, "open", open_):
            self.assertEqual(set(CONFIGS.keys()), set(BaseServerConfig.get_labels(FILE_PATH)))
        open_.assert_called_once_with(FILE_PATH)
Example #6
0
    def test_init(self):
        """Test instantiating :class:`nailgun.config.BaseServerConfig`.

        Assert that only provided values become object attributes.

        """
        for config in CONFIGS.values():
            self.assertEqual(
                _convert_bsc_attrs(config),
                vars(BaseServerConfig(**config)),
            )
Example #7
0
    def test_delete(self):
        """Test :meth:`nailgun.config.BaseServerConfig.delete`.

        Assert that the method reads the config file before writing, and that
        it writes out a correct config file.

        """
        open_ = mock_open(read_data=json.dumps(CONFIGS))
        with patch.object(builtins, 'open', open_):
            BaseServerConfig.delete('Ask Aak', FILE_PATH)

        # See `test_save` for further commentary on what's being done here.
        self.assertEqual(
            open_.call_args_list,
            [call(FILE_PATH), call(FILE_PATH, 'w')],
        )
        actual_config = _get_written_json(open_)
        target_config = CONFIGS.copy()
        del target_config['Ask Aak']
        self.assertEqual(target_config, actual_config)
Example #8
0
    def test_bsc_v3(self):
        """Test :class:`nailgun.config.BaseServerConfig`.

        Assert that ``__repr__`` works correctly when ``url`` and ``version``
        are specified.

        """
        targets = (
            "nailgun.config.BaseServerConfig(url='flim', version='1')",
            "nailgun.config.BaseServerConfig(version='1', url='flim')",
        )
        self.assertIn(repr(BaseServerConfig('flim', version='1')), targets)
Example #9
0
    def test_get_labels(self):
        """Test :meth:`nailgun.config.BaseServerConfig.get_labels`.

        Assert that the method returns the correct labels.

        """
        open_ = mock_open(read_data=json.dumps(CONFIGS))
        with patch.object(builtins, 'open', open_):
            self.assertEqual(
                set(CONFIGS.keys()),
                set(BaseServerConfig.get_labels(FILE_PATH)),
            )
        open_.assert_called_once_with(FILE_PATH)
Example #10
0
    def test_get(self):
        """Test :meth:`nailgun.config.BaseServerConfig.get`.

        Assert that the method extracts the asked-for section from a
        configuration file and correctly populates a new ``BaseServerConfig``
        object. Also assert that the ``auth`` attribute is a list. (See the
        docstring for :meth:`nailgun.config.ServerConfig.get`.)

        """
        for label, config in CONFIGS.items():
            open_ = mock_open(read_data=json.dumps(CONFIGS))
            with patch.object(builtins, 'open', open_):
                server_config = BaseServerConfig.get(label, FILE_PATH)
            open_.assert_called_once_with(FILE_PATH)
            _compare_configs(self, config, server_config)
            if hasattr(server_config, 'auth'):
                self.assertIsInstance(server_config.auth, list)
Example #11
0
    def test_get(self):
        """Test :meth:`nailgun.config.BaseServerConfig.get`.

        Assert that the method extracts the asked-for section from a
        configuration file and correctly populates a new ``BaseServerConfig``
        object. Also assert that the ``auth`` attribute is a list. (See the
        docstring for :meth:`nailgun.config.ServerConfig.get`.)

        """
        for label, config in CONFIGS.items():
            open_ = mock_open(read_data=json.dumps(CONFIGS))
            with patch.object(builtins, 'open', open_):
                server_config = BaseServerConfig.get(label, FILE_PATH)
            self.assertEqual(type(server_config), BaseServerConfig)
            open_.assert_called_once_with(FILE_PATH)
            self.assertEqual(_convert_bsc_attrs(config), vars(server_config))
            if hasattr(server_config, 'auth'):
                self.assertIsInstance(server_config.auth, list)