def test_migrate_local_client(self): """Raise ValueError, cannot migrate from local connection""" from pylxd.client import Client second_host = "https://10.0.3.222:8443/" client2 = Client(endpoint=second_host, verify=False) client2.authenticate("password") self.assertRaises(ValueError, self.container.migrate, client2)
def test_migrate_local_client(self): """Raise ValueError, cannot migrate from local connection""" from pylxd.client import Client second_host = 'https://10.0.3.222:8443/' client2 =\ Client(endpoint=second_host, verify=False) client2.authenticate('password') self.assertRaises(ValueError, self.container.migrate, client2)
def test_copy_no_wait(self): """Try to copy and don't wait.""" from pylxd.client import Client a_image = self.client.images.all()[0] client2 = Client(endpoint="http://pylxd2.test") a_image.copy(client2, public=False, auto_update=False)
def test_copy(self): """Try to copy an image to another LXD instance.""" from pylxd.client import Client a_image = self.client.images.all()[0] client2 = Client(endpoint="http://pylxd2.test") copied_image = a_image.copy(client2, wait=True) self.assertEqual(a_image.fingerprint, copied_image.fingerprint)
def test_copy_public(self): """Try to copy a public image.""" from pylxd.client import Client def image_get(request, context): context.status_code = 200 return json.dumps({ "type": "sync", "metadata": { "aliases": [{ "name": "an-alias", "fingerprint": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", }], "architecture": "x86_64", "cached": False, "filename": "a_image.tar.bz2", "fingerprint": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "public": True, "properties": {}, "size": 1, "auto_update": False, "created_at": "1983-06-16T02:42:00Z", "expires_at": "1983-06-16T02:42:00Z", "last_used_at": "1983-06-16T02:42:00Z", "uploaded_at": "1983-06-16T02:42:00Z", }, }) self.add_rule({ "text": image_get, "method": "GET", "url": r"^http://pylxd.test/1.0/images/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855$", }) a_image = self.client.images.all()[0] self.assertTrue(a_image.public) client2 = Client(endpoint="http://pylxd2.test") copied_image = a_image.copy(client2, wait=True) self.assertEqual(a_image.fingerprint, copied_image.fingerprint)
def test_copy_public(self): """Try to copy a public image.""" from pylxd.client import Client def image_get(request, context): context.status_code = 200 return json.dumps({ 'type': 'sync', 'metadata': { 'aliases': [{ 'name': 'an-alias', # NOQA 'fingerprint': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', # NOQA }], 'architecture': 'x86_64', 'cached': False, 'filename': 'a_image.tar.bz2', 'fingerprint': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', # NOQA 'public': True, 'properties': {}, 'size': 1, 'auto_update': False, 'created_at': '1983-06-16T02:42:00Z', 'expires_at': '1983-06-16T02:42:00Z', 'last_used_at': '1983-06-16T02:42:00Z', 'uploaded_at': '1983-06-16T02:42:00Z', }, }) self.add_rule({ 'text': image_get, 'method': 'GET', 'url': r'^http://pylxd.test/1.0/images/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855$', # NOQA }) a_image = self.client.images.all()[0] self.assertTrue(a_image.public) client2 = Client(endpoint='http://pylxd2.test') copied_image = a_image.copy(client2, wait=True) self.assertEqual(a_image.fingerprint, copied_image.fingerprint)
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_migrate(self): """A instance is migrated.""" from pylxd.client import Client client2 = Client(endpoint="http://pylxd2.test") an_instance = models.Instance(self.client, name="an-instance") an_migrated_instance = an_instance.migrate(client2) self.assertEqual("an-instance", an_migrated_instance.name) self.assertEqual(client2, an_migrated_instance.client)
def test_migrate_stopped(self): """A instance is migrated.""" from pylxd.client import Client client2 = Client(endpoint='http://pylxd2.test') an_instance = models.Instance.get(self.client, name='an-instance') an_instance.status_code = 102 an_migrated_instance = an_instance.migrate(client2) self.assertEqual('an-instance', an_migrated_instance.name) self.assertEqual(client2, an_migrated_instance.client)
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_instance = models.Instance(client2, name="an-instance") self.assertRaises(ValueError, an_instance.migrate, self.client)
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_migrate_stopped(self): """A stopped container is migrated.""" from pylxd.client import Client first_host = "https://10.0.3.111:8443/" second_host = "https://10.0.3.222:8443/" client1 = Client(endpoint=first_host, verify=False) client1.authenticate("password") client2 = Client(endpoint=second_host, verify=False) client2.authenticate("password") an_container = client1.containers.get(self.container.name) an_migrated_container = an_container.migrate(client2, wait=True) self.assertEqual(an_container.name, an_migrated_container.name) self.assertEqual(client2, an_migrated_container.client)
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 init(opts=None): ''' Required. Can be used to initialize the server connection. ''' log.debug('LXD-Proxy Init') if opts is None: opts = __opts__ # pylint: disable=locally-disabled, undefined-variable try: log.debug("LXD-Proxy Init: " \ "Client(endpoint='" + opts['proxy']['url'] + "', " \ "cert='" + opts['proxy']['cert'] + "', " \ "key='" + opts['proxy']['key'] + "')") DETAILS['server'] = Client(endpoint=opts['proxy']['url'], cert=(opts['proxy']['cert'], opts['proxy']['key']), verify=opts['proxy']['verify']) except ClientConnectionFailed as err: log.debug('LXD-Proxy Init: Client() failed') log.error(err) return False if not DETAILS['server'].trusted: # Don't log the password try: DETAILS['server'].authenticate(opts['proxy']['password']) except LXDAPIException as err: log.debug('LXD-Proxy Init: authenticate() failed') log.error(err) return False try: log.debug('LXD-Proxy Init: container.get(name=' + opts['proxy']['name'] + ')') DETAILS['container'] = DETAILS['server'].containers.get( opts['proxy']['name']) except LXDAPIException as err: log.debug('LXD-Proxy Init: container.get() failed') log.error(err) return False log.debug('LXD-Proxy Init: Success') DETAILS['initialized'] = True return True
def test_migrate_running(self): """A running container is migrated.""" from pylxd.client import Client first_host = 'https://10.0.3.111:8443/' second_host = 'https://10.0.3.222:8443/' client1 = Client(endpoint=first_host, verify=False) client1.authenticate('password') client2 = Client(endpoint=second_host, verify=False) client2.authenticate('password') an_container = \ client1.containers.get(self.container.name) an_container.start(wait=True) an_container.sync() an_migrated_container = \ an_container.migrate(client2, wait=True) self.assertEqual(an_container.name, an_migrated_container.name) self.assertEqual(client2, an_migrated_container.client)
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_migrate_exception_running(self, generate_migration_data): """Migrated instance already running on destination""" from pylxd.client import Client from pylxd.exceptions import LXDAPIException client2 = Client(endpoint="http://pylxd2.test") an_instance = models.Instance(self.client, name="an-instance") an_instance.status_code = 103 def generate_exception(*args, **kwargs): response = mock.Mock() response.status_code = 103 raise LXDAPIException(response) generate_migration_data.side_effect = generate_exception an_migrated_instance = an_instance.migrate(client2, live=True) self.assertEqual("an-instance", an_migrated_instance.name) self.assertEqual(client2, an_migrated_instance.client) generate_migration_data.assert_called_once_with(True)
def setUp(self): super(IntegrationTestCase, self).setUp() self.client = Client() self.lxd = self.client.api
def setUp(self): mock_services.update_http_rules(mock_lxd.RULES) mock_services.start_http_mock() self.client = Client(endpoint='http://pylxd.test')
import shlex import socket import subprocess import sys import time from collections import defaultdict from pylxd.client import Client from pylxd import exceptions def short_md5(thing): return hashlib.md5(thing).hexdigest()[:16] lxd = Client() with open('Lxffile', 'r') as f: lxffile = f.read() lines = lxffile.splitlines() def create_container(name, snapshot_name=None): try: cntr = lxd.containers.get(name) except (NameError, exceptions.LXDAPIException): print("Creating new Container") else: print("Container already exists...") return cntr