Ejemplo n.º 1
0
    def test_publish(self):
        """Containers can be published."""
        self.add_rule({
            'text':
            json.dumps({
                'type': 'sync',
                'metadata': {
                    'id': 'operation-abc',
                    'metadata': {
                        'fingerprint': ('e3b0c44298fc1c149afbf4c8996fb92427'
                                        'ae41e4649b934ca495991b7852b855')
                    }
                }
            }),
            'method':
            'GET',
            'url':
            r'^http://pylxd.test/1.0/operations/operation-abc$',
        })

        an_container = models.Container(self.client, name='an-container')

        image = an_container.publish(wait=True)

        self.assertEqual(
            'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
            image.fingerprint)
Ejemplo n.º 2
0
    def test_rename(self):
        an_container = models.Container(
            self.client, name='an-container')

        an_container.rename('an-renamed-container', wait=True)

        self.assertEqual('an-renamed-container', an_container.name)
Ejemplo n.º 3
0
    def test_fetch(self):
        """A sync updates the properties of a container."""
        an_container = models.Container(self.client, name='an-container')

        an_container.sync()

        self.assertTrue(an_container.ephemeral)
Ejemplo n.º 4
0
    def test_delete(self):
        """A container is deleted."""
        # XXX: rockstar (21 May 2016) - This just executes
        # a code path. There should be an assertion here, but
        # it's not clear how to assert that, just yet.
        an_container = models.Container(self.client, name='an-container')

        an_container.delete(wait=True)
Ejemplo n.º 5
0
    def test_raw_interactive_execute_string(self):
        """A command passed as string raises a TypeError."""
        an_container = models.Container(
            self.client, name='an-container')

        self.assertRaises(TypeError,
                          an_container.raw_interactive_execute,
                          'apt-get update')
Ejemplo n.º 6
0
    def test_raw_interactive_execute(self):
        an_container = models.Container(self.client, name='an-container')

        result = an_container.raw_interactive_execute(['/bin/bash'])

        self.assertEqual(result['ws'],
                         '/1.0/operations/operation-abc/websocket?secret=abc')
        self.assertEqual(result['control'],
                         '/1.0/operations/operation-abc/websocket?secret=jkl')
Ejemplo n.º 7
0
    def test_migrate(self):
        """A container is migrated."""
        from pylxd.client import Client

        client2 = Client(endpoint='http://pylxd2.test')
        an_container = models.Container(self.client, name='an-container')

        an_migrated_container = an_container.migrate(client2)

        self.assertEqual('an-container', an_migrated_container.name)
        self.assertEqual(client2, an_migrated_container.client)
Ejemplo n.º 8
0
    def test_execute(self, _CommandWebsocketClient, _StdinWebsocket):
        """A command is executed on a container."""
        fake_websocket = mock.Mock()
        fake_websocket.data = 'test\n'
        _StdinWebsocket.return_value = fake_websocket
        _CommandWebsocketClient.return_value = fake_websocket

        an_container = models.Container(self.client, name='an-container')

        result = an_container.execute(['echo', 'test'])

        self.assertEqual(0, result.exit_code)
        self.assertEqual('test\n', result.stdout)
Ejemplo n.º 9
0
    def test_migrate_local_client(self, get):
        """Migration from local clients is not supported."""
        # Mock out the _APINode for the local instance.
        response = mock.Mock()
        response.json.return_value = {'metadata': {'fake': 'response'}}
        response.status_code = 200
        get.return_value = response

        from pylxd.client import Client

        client2 = Client(endpoint='http+unix://pylxd2.test')
        an_container = models.Container(client2, name='an-container')

        self.assertRaises(ValueError, an_container.migrate, self.client)
Ejemplo n.º 10
0
    def test_execute_no_ws4py(self):
        """If ws4py is not installed, ValueError is raised."""
        from pylxd.models import container
        old_installed = container._ws4py_installed
        container._ws4py_installed = False

        def cleanup():
            container._ws4py_installed = old_installed

        self.addCleanup(cleanup)

        an_container = models.Container(self.client, name='an-container')

        self.assertRaises(ValueError, an_container.execute, ['echo', 'test'])
Ejemplo n.º 11
0
    def test_execute_with_env(self, _CommandWebsocketClient, _StdinWebsocket):
        """A command is executed on a container with custom env variables."""
        fake_websocket = mock.Mock()
        fake_websocket.data = 'test\n'
        _StdinWebsocket.return_value = fake_websocket
        _CommandWebsocketClient.return_value = fake_websocket

        an_container = models.Container(self.client, name='an-container')

        result = an_container.execute(['echo', 'test'],
                                      environment={"DISPLAY": ":1"})

        self.assertEqual(0, result.exit_code)
        self.assertEqual('test\n', result.stdout)
Ejemplo n.º 12
0
    def test_update(self):
        """A container is updated."""
        an_container = models.Container(self.client, name='an-container')
        an_container.architecture = 1
        an_container.config = {}
        an_container.created_at = 1
        an_container.devices = {}
        an_container.ephemeral = 1
        an_container.expanded_config = {}
        an_container.expanded_devices = {}
        an_container.profiles = 1
        an_container.status = 1

        an_container.save(wait=True)

        self.assertTrue(an_container.ephemeral)
Ejemplo n.º 13
0
    def test_migrate_exception_error(self, generate_migration_data):
        """LXDAPIException is raised in case of migration failure"""
        from pylxd.client import Client
        from pylxd.exceptions import LXDAPIException

        def generate_exception(*args, **kwargs):
            response = mock.Mock()
            response.status_code = 400
            raise LXDAPIException(response)

        generate_migration_data.side_effect = generate_exception

        an_container = models.Container(self.client, name='an-container')

        client2 = Client(endpoint='http://pylxd2.test')
        self.assertRaises(LXDAPIException, an_container.migrate, client2)
Ejemplo n.º 14
0
    def test_fetch_error(self):
        """LXDAPIException is raised on error."""
        def not_found(request, context):
            context.status_code = 500
            return json.dumps({
                'type': 'error',
                'error': 'An bad error',
                'error_code': 500})
        self.add_rule({
            'text': not_found,
            'method': 'GET',
            'url': r'^http://pylxd.test/1.0/containers/an-missing-container$',
        })

        an_container = models.Container(
            self.client, name='an-missing-container')

        self.assertRaises(exceptions.LXDAPIException, an_container.sync)
Ejemplo n.º 15
0
    def test_migrate_exception_running(self, generate_migration_data):
        """Migrated container already running on destination"""
        from pylxd.client import Client
        from pylxd.exceptions import LXDAPIException

        client2 = Client(endpoint='http://pylxd2.test')
        an_container = models.Container(self.client, name='an-container')
        an_container.status_code = 103

        def generate_exception():
            response = mock.Mock()
            response.status_code = 103
            raise LXDAPIException(response)

        generate_migration_data.side_effect = generate_exception

        an_migrated_container = an_container.migrate(client2)

        self.assertEqual('an-container', an_migrated_container.name)
        self.assertEqual(client2, an_migrated_container.client)
Ejemplo n.º 16
0
 def test_restore_snapshot(self):
     """Snapshots can be restored"""
     an_container = models.Container(self.client, name='an-container')
     an_container.restore_snapshot('thing')