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)
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)
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)
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)
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')
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')
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)
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)
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)
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'])
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)
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)
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)
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)
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)
def test_restore_snapshot(self): """Snapshots can be restored""" an_container = models.Container(self.client, name='an-container') an_container.restore_snapshot('thing')