def import_bundle(request, deployer): """Start or schedule a bundle deployment. If the request is valid, the response will contain the DeploymentId assigned to the bundle deployment. Request: 'Import'. Parameters example: {'Name': 'bundle-name', 'YAML': 'bundles'}. """ # Validate the request parameters. try: name, bundle, bundle_id = _validate_import_params(request.params) except ValueError as err: raise response(error='invalid request: {}'.format(err)) # Validate and prepare the bundle. try: prepare_bundle(bundle) except ValueError as err: error = 'invalid request: invalid bundle {}: {}'.format(name, err) raise response(error=error) # Validate the bundle against the current state of the Juju environment. err = yield deployer.validate(request.user, name, bundle) if err is not None: raise response(error='invalid request: {}'.format(err)) # Add the bundle deployment to the Deployer queue. logging.info('import_bundle: scheduling {!r} deployment'.format(name)) deployment_id = deployer.import_bundle( request.user, name, bundle, bundle_id) raise response({'DeploymentId': deployment_id})
def test_invalid_bundle(self): # A ValueError is raised if the bundle is not well structured. with self.assertRaises(ValueError) as context_manager: utils.prepare_bundle('invalid') self.assertEqual( 'the bundle data is not well formed', str(context_manager.exception))
def test_no_services(self): # A ValueError is raised if the bundle does not include services. with self.assertRaises(ValueError) as context_manager: utils.prepare_bundle({}) self.assertEqual( 'the bundle does not contain any services', str(context_manager.exception))
def import_bundle(request, deployer): """Start or schedule a bundle deployment. If the request is valid, the response will contain the DeploymentId assigned to the bundle deployment. Request: 'Import'. Parameters example: { 'Name': 'bundle-name', 'YAML': 'bundles', 'Version': 4, 'BundleID': '~user/bundle-name', }. """ # Validate the request parameters. try: name, bundle, version, id_ = _validate_import_params(request.params) except ValueError as err: raise response(error='invalid request: {}'.format(err)) # Validate and prepare the bundle. try: prepare_bundle(bundle) except ValueError as err: error = 'invalid request: invalid bundle {}: {}'.format(name, err) raise response(error=error) # Validate the bundle against the current state of the Juju environment. err = yield deployer.validate(request.user, bundle) if err is not None: raise response(error='invalid request: {}'.format(err)) # Add the bundle deployment to the Deployer queue. logging.info('import_bundle: scheduling deployment of v{} bundle {!r}' ''.format(version, name)) deployment_id = deployer.import_bundle(request.user, name, bundle, version, id_) raise response({'DeploymentId': deployment_id})
def test_dict_constraints(self): # A bundle with valid constraints as dict is not modified. bundle = { 'services': { 'django': { 'constraints': { 'arch': 'i386', 'cpu-cores': '4', 'mem': '2000', }, }, }, } expected = bundle.copy() utils.prepare_bundle(bundle) self.assertEqual(expected, bundle)
def test_constraints_conversion(self): # Service constraints stored as strings are converted to a dict. bundle = { 'services': { 'django': {'constraints': 'arch=i386,cpu-cores=4,mem=2000'}, }, } expected = { 'services': { 'django': { 'constraints': { 'arch': 'i386', 'cpu-cores': '4', 'mem': '2000', }, }, }, } utils.prepare_bundle(bundle) self.assertEqual(expected, bundle)
def test_constraints_conversion_comma_separated(self): # Service constraints stored as strings are converted to a dict. bundle = { 'services': { 'django': { 'constraints': 'arch=i386,cpu-cores=4,mem=2000' }, }, } expected = { 'services': { 'django': { 'constraints': { 'arch': 'i386', 'cpu-cores': '4', 'mem': '2000', }, }, }, } utils.prepare_bundle(bundle) self.assertEqual(expected, bundle)
def test_no_constraints(self): # A bundle with no constraints is not modified. bundle = {'services': {'django': {}}} expected = bundle.copy() utils.prepare_bundle(bundle) self.assertEqual(expected, bundle)
def test_constraints_deletion(self): # Empty service constraints are deleted. bundle = {'services': {'django': {'constraints': ''}}} expected = {'services': {'django': {}}} utils.prepare_bundle(bundle) self.assertEqual(expected, bundle)
def test_no_services(self): # A ValueError is raised if the bundle does not include services. with self.assertRaises(ValueError) as context_manager: utils.prepare_bundle({}) self.assertEqual('the bundle does not contain any services', str(context_manager.exception))
def test_invalid_bundle(self): # A ValueError is raised if the bundle is not well structured. with self.assertRaises(ValueError) as context_manager: utils.prepare_bundle('invalid') self.assertEqual('the bundle data is not well formed', str(context_manager.exception))