示例#1
0
 def test_can_detect_os_or_distribution(self):
     class DummyGuest(Guest):
         name = 'dummy'
     lxd_container_1 = unittest.mock.Mock()
     lxd_container_1.files.get.return_value = 'ID=dummy'
     lxd_container_2 = unittest.mock.Mock()
     lxd_container_2.files.get.return_value = 'ID=unknown'
     lxd_container_3 = unittest.mock.Mock()
     lxd_container_3.files.get.side_effect = NotFound(response=unittest.mock.Mock())
     assert DummyGuest.detect(lxd_container_1)
     assert not DummyGuest.detect(lxd_container_2)
     assert not DummyGuest.detect(lxd_container_3)
示例#2
0
    def test_get_container_lxc_config(self):
        """Test that _get_container generates a valid lxc_config
        """

        # The options below has an lxc_config value, that overrides some values
        # that are driven within lxdoc, these values are marked as invalid and should not
        # be passed directly to the container at creation time.
        container_options = {
            'name': self.containername('lxc-config'),
            'image': 'alpine/3.10',
            'lxc_config': {
                'security.privileged': 'invalid',
                'user.lxdock.homedir': 'invalid',
                'user.lxdock.made': 'invalid',
                'valid_key': 'valid_value',
            },
        }

        cont_return = ()  # mock container object to return

        def mock_create(container_config, *args, **kwargs):
            """Mocks the container create call, returns the mock container object
            and also ensures that the container_config is correct
            """
            config = container_config['config']
            # Values below should not be driven by the values in container_options
            assert config['security.privileged'] != 'invalid'
            assert config['user.lxdock.homedir'] != 'invalid'
            assert config['user.lxdock.made'] != 'invalid'
            # Value below is a custom value that should be passed from container_options
            assert config['valid_key'] == 'valid_value'
            return cont_return

        client_mock = unittest.mock.Mock(
            **{
                'containers.create.side_effect': mock_create,
                'containers.get.side_effect': NotFound(''),
            })

        container = Container('myproject', THIS_DIR, client_mock,
                              **container_options)

        assert container._get_container() is cont_return
        assert client_mock.containers.get.called
        assert client_mock.containers.create.called
示例#3
0
 def test_check_if_lxc_image_exists_does_not_call_exists_if_get_by_alias_fails(self):
     self.images.get_by_alias.side_effect = NotFound(response="blaap")
     check_if_lxc_image_exists("test")
     self.assertFalse(self.images.exists.called)
示例#4
0
 def test_check_if_lxc_image_exists_returns_false_if_get_by_alias_raises_not_found(self):
     self.images.get_by_alias.side_effect = NotFound(response="blaap")
     self.assertFalse(check_if_lxc_image_exists("test"))
示例#5
0
 def test_write_files_to_lxc_container_does_not_call_files_put_when_container_does_not_exist(
         self):
     self.get_lxd_client.return_value.containers.get.side_effect = NotFound(
         response="banaan")
     write_file_to_lxc_container("blaap", "blaap", "blaap")
     self.assertFalse(self.machine.files.put.called)
示例#6
0
 def test_place_file_on_lxc_machine_does_nothing_if_container_not_found(
         self, open_mock):
     self.get_lxd_client.side_effect = NotFound(response=b"response")
     place_file_on_lxc_machine("router100", self.host_file_p,
                               self.guest_file_p)
     self.assertFalse(self.machine.files.put.called)
示例#7
0
 def test_get_lxc_machine_status_returns_na_on_not_found_exception(self):
     self.machine.containers.get.side_effect = NotFound(response="blaap")
     self.assertEqual(get_lxc_machine_status("test"), ["test", "NA", "LXC"])
示例#8
0
 def test_destroy_lxc_machine_does_nothing_if_machine_does_not_exist(self):
     self.client.containers.get.side_effect = NotFound(response="blaap")
     destroy_lxc_machine("banaan")
     self.assertFalse(self.machine.delete.called)
     self.assertFalse(self.machine.stop.called)
示例#9
0
 def test_change_lxc_machine_status_deals_with_not_found_error(self):
     self.client.containers.get.side_effect = NotFound(response="blaap")
     change_lxc_machine_status("banaan")
     self.assertFalse(self.machine.stop.called)
     self.assertFalse(self.machine.start.called)
     self.assertFalse(self.wait_for_lxc_machine_status.called)