def get_config_holder(log_to_file=True): configHolder = ConfigHolder(context={"empty": None}, configFile=MTA.config_file) configHolder.set("log_to_file", log_to_file) configHolder.set("log", MTA.get_logger(configHolder)) return configHolder
def setUp(self): self.ch = ConfigHolder() self.ch.set('serviceurl', endpoint) self.ch.set('verboseLevel', 3) self.client = Client(self.ch) self.client.login(username, password) self.ch.set('endpoint', endpoint) self.ch.set('session', self.client.get_session())
class MainProgram(CommandBase): """A command-line program to download deployment reports.""" def __init__(self, argv=None): self.module = '' self.endpoint = None self.ss_client = None self.configHolder = None super(MainProgram, self).__init__(argv) def parse(self): usage = '''usage: %prog [options] [<run-uuid>] <run-uuid> UUID of the run to get reports from.''' self.parser.usage = usage self.addEndpointOption() self.parser.add_option('-c', '--components', dest='components', help='Comma separated list of components to download the reports for. ' 'Example: nginx,worker.1,worker.3 - will download reports for all component ' 'instances of nginx and only for instances 1 and 3 of worker. ' 'Default: all instances of all components.', default='') self.parser.add_option('-o', '--output-dir', dest='output_dir', help='Path to the folder to store the reports. Default: <working directory>/<run-uuid>.', default=os.getcwd()) self.parser.add_option('--no-orch', dest='no_orch', help='Do not download Orchestrator report.', default=False, action='store_true') self.options, self.args = self.parser.parse_args() if self.options.components: self.options.components = self.options.components.split(',') else: self.options.components = [] self._checkArgs() def _checkArgs(self): if len(self.args) == 1: self.run_uuid = self.args[0] if len(self.args) > 1: self.usageExitTooManyArguments() def _init_client(self): self.configHolder = ConfigHolder(self.options) self.configHolder.set('serviceurl', self.options.endpoint) self.ss_client = SlipStreamHttpClient(self.configHolder) def doWork(self): self._init_client() rg = ReportsGetter(self.ss_client.get_api(), self.configHolder) rg.get_reports(self.run_uuid, components=self.options.components, no_orch=self.options.no_orch)
class TestOkeanosClientCloud(unittest.TestCase): def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'Test' self.ch = ConfigHolder(config={'foo': 'bar'}, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, RUN_CATEGORY_DEPLOYMENT) def test_OkeanosClientCloudInit(self): okeanos = OkeanosClientCloud(self.ch) assert okeanos assert okeanos.run_category == RUN_CATEGORY_DEPLOYMENT
def get_rtp_all(side_effect, no_block=False): ch = ConfigHolder() ch.set('noBlock', no_block) ch.set('timeout', 1) ch.set('verboseLevel', 3) ch.set('endpoint', 'https://foo.bar') Client._getRuntimeParameter = Mock(side_effect=side_effect) client = Client(ch) client.httpClient.getRuntimeParameter = Mock(side_effect=side_effect) return client.get_rtp_all('foo', 'bar')
class TestFlexiantClientCloud(unittest.TestCase): def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'Test' self.ch = ConfigHolder(config={'foo': 'bar'}, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, RUN_CATEGORY_DEPLOYMENT) def test_FcoClientCloudInit(self): fco = FlexiantClientCloud(self.ch) assert fco assert fco.run_category == RUN_CATEGORY_DEPLOYMENT
def get_ss_client_for_user_info(self): ch = ConfigHolder(config={'foo': 'bar'}, context={}) ch.context = {} client = SlipStreamHttpClient(ch) client._get_user = Mock(return_value=client._strip_unwanted_attrs(self.resource_user)) client._get_user_params = Mock(return_value=client._strip_unwanted_attrs( self.resource_user_param.get('userParam')[0])) client._get_cloud_creds = Mock(return_value=client._strip_unwanted_attrs(self.resource_cloud_cred)) client._get_connector_conf = Mock(return_value=client._strip_unwanted_attrs(self.resource_connector)) return client
def test_getExternalCloudConnectorModuleName(self): ch = ConfigHolder(config={'foo': 'bar'}, context={'foo': 'bar'}) ch.cloud = 'baz' self.failUnlessRaises(NotFoundError, ccf_module.get_connector_module_name, (ch)) module_name = 'foo.bar.baz' ch = ConfigHolder(config={'cloudconnector': module_name}, context={'foo': 'bar'}) ch.cloud = 'baz' assert module_name == ccf_module.get_connector_module_name(ch)
def test_init_session_login_apikey(self): ch = ConfigHolder() ch.context = {} ch.set('verboseLevel', 0) ch.set('cookie_filename', '/dev/null') ch.set('api_key', 'key') ch.set('api_secret', 'secret') client = HttpClient(ch) client.init_session('http://foo.bar') assert client.session is not None assert client.session.login_params assert 'key' in client.session.login_params assert 'secret' in client.session.login_params
def setUp(self): self.serviceurl = 'http://example.com' self.config_holder = ConfigHolder( { 'username': base64.b64encode('user'), 'password': base64.b64encode('pass'), 'cookie_filename': '/var/tmp/cookies', 'serviceurl': self.serviceurl, 'node_instance_name': 'instance-name' }, context={'foo': 'bar'}, config={'foo': 'bar'}) os.environ[ENV_CONNECTOR_INSTANCE] = 'Test' BaseWrapper.is_mutable = Mock(return_value=False)
def setUp(self): BaseCloudConnector.publishVmInfo = Mock() os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'cloudstack' os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-%s' % time.time() if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') self.client = CloudStackClientCloud(self.ch) self.user_info = UserInfo('cloudstack') self.user_info['cloudstack.endpoint'] = self.ch.config['cloudstack.endpoint'] self.user_info['cloudstack.zone'] = self.ch.config['cloudstack.zone'] self.user_info['cloudstack.username'] = self.ch.config['cloudstack.key'] self.user_info['cloudstack.password'] = self.ch.config['cloudstack.secret'] self.user_info['General.ssh.public.key'] = self.ch.config['General.ssh.public.key'] image_id = self.ch.config['cloudstack.template'] self.multiplicity = 4 self.node_info = { 'multiplicity': self.multiplicity, 'nodename': 'test_node', 'image': { 'cloud_parameters': { 'cloudstack': { 'cloudstack.instance.type': self.ch.config['cloudstack.instance.type'], 'cloudstack.security.groups': self.ch.config['cloudstack.security.groups'] }, 'Cloud': {'network': 'public'} }, 'attributes': { 'imageId': image_id, 'platform': 'Ubuntu' }, 'targets': { 'prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """, 'packages': ['lvm2', 'nano'] } }, }
def do_work(self): ids = self.get_option(self.INSTANCE_IDS_KEY) ch = ConfigHolder(options={ 'verboseLevel': self.options.verbose and 3 or 0, 'retry': False, KEY_RUN_CATEGORY: '' }, context={'foo': 'bar'}) cc = self.get_connector_class()(ch) # pylint: disable=protected-access cc._initialization(self.user_info, **self.get_initialization_extra_kwargs()) fname = self.get_option(self.INSTANCES_IDS_FILE_KEY) if fname: with open(fname) as f: ids += f.read().splitlines() if cc.has_capability(cc.CAPABILITY_VAPP): cc.stop_vapps_by_ids(ids, self.get_option(self.INSTANCES_NAMESPACE)) else: cc._stop_instances_in_namespace( ids, self.get_option(self.INSTANCES_NAMESPACE))
def instantiate_from_cimi(cimi_connector, cimi_cloud_credential): user_info = UserInfo(cimi_connector['instanceName']) cloud_params = { UserInfo.CLOUD_USERNAME_KEY: cimi_cloud_credential['key'], UserInfo.CLOUD_PASSWORD_KEY: cimi_cloud_credential['secret'], 'domain-name': cimi_cloud_credential['domain-name'], 'tenant-name': cimi_cloud_credential['tenant-name'], 'endpoint': cimi_connector.get('endpoint'), 'serviceRegion': cimi_connector.get('serviceRegion'), 'serviceName': cimi_connector.get('serviceName'), 'serviceType': cimi_connector.get('serviceType'), 'identityVersion': cimi_connector.get('identityVersion') } user_info.set_cloud_params(cloud_params) config_holder = ConfigHolder(options={'verboseLevel': 0, 'retry': False}) os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cimi_connector['instanceName'] connector_instance = OpenStackClientCloud(config_holder) connector_instance._initialization(user_info) return connector_instance
def test_getAvailableCloudConnectorsModuleNames(self): ch = ConfigHolder(config={'foo': 'bar'}, context={'foo': 'bar'}) for module_name in self.get_cloudconnector_modulenames(): setattr(ch, CONFIGPARAM_CONNECTOR_MODULE_NAME, module_name) try: assert len(ccf_module.get_connector_module_name(ch)) != 0 except NotFoundError: self.fail('Should not have raised NotFoundError')
def test_init_session_login_internal(self): ch = ConfigHolder() ch.context = {} ch.set('verboseLevel', 0) ch.set('cookie_filename', '/dev/null') ch.set('username', 'foo') ch.set('password', 'bar') client = HttpClient(ch) client.init_session('http://foo.bar') assert client.session is not None assert client.session.login_params assert 'username' in client.session.login_params assert 'password' in client.session.login_params
def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'physicalhost' os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-000000000000' if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) os.environ['PHYSICALHOST_ORCHESTRATOR_HOST'] = self.ch.config[ 'PHYSICALHOST_ORCHESTRATOR_HOST'] self.client = PhysicalHostClientCloud(self.ch) self.user_info = UserInfo('physicalhost') self.user_info['physicalhost.private.key'] = self.ch.config[ 'physicalhost.private.key'] self.user_info['physicalhost.password'] = self.ch.config[ 'physicalhost.password'] self.user_info['physicalhost.username'] = self.ch.config[ 'physicalhost.username'] hosta = self.ch.config['physicalhost.hosta'] hostb = self.ch.config['physicalhost.hostb'] node_instance_name_a = 'test_node_a' node_instance_name_b = 'test_node_b' self.node_instances = {} self.node_instances[node_instance_name_a] = NodeInstance({ 'name': node_instance_name_a, 'cloudservice': 'physicalhost', 'image.platform': 'Ubuntu', 'image.imageId': hosta, 'image.id': hosta, 'network': 'private', }) self.node_instances[node_instance_name_b] = NodeInstance({ 'name': node_instance_name_b, 'cloudservice': 'physicalhost', 'image.platform': 'Ubuntu', 'image.imageId': hostb, 'image.id': hostb, 'network': 'private', })
def __init__(self, wrapper, config_holder=ConfigHolder()): self.verboseLevel = 0 super(NodeDeploymentExecutor, self).__init__(wrapper, config_holder) self.SCALE_ACTION_TO_TARGET = \ {self.wrapper.SCALE_ACTION_CREATION: 'onvmadd', self.wrapper.SCALE_ACTION_REMOVAL: 'onvmremove'} self._skip_execute_due_to_vertical_scaling = False
class TestCloudWrapper(TestCloudConnectorsBase): def setUp(self): self.serviceurl = 'http://example.com' self.configHolder = ConfigHolder( { 'username': base64.b64encode('user'), 'password': base64.b64encode('pass'), 'cookie_filename': 'cookies', 'serviceurl': self.serviceurl }, context={'foo': 'bar'}, config={'foo': 'bar'}) os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'Test' def tearDown(self): os.environ.pop('SLIPSTREAM_CONNECTOR_INSTANCE') self.configHolder = None shutil.rmtree('%s/.cache/' % os.getcwd(), ignore_errors=True) def test_getCloudName(self): for module_name in self.get_cloudconnector_modulenames(): setattr(self.configHolder, CONFIGPARAM_CONNECTOR_MODULE_NAME, module_name) setattr(self.configHolder, KEY_RUN_CATEGORY, RUN_CATEGORY_DEPLOYMENT) cw = CloudWrapper(self.configHolder) cw.initCloudConnector() assert cw.getCloudInstanceName() == 'Test' def test_putImageId(self): self.configHolder.set(CONFIGPARAM_CONNECTOR_MODULE_NAME, self.get_cloudconnector_modulename_by_cloudname('local')) cw = CloudWrapper(self.configHolder) cw.initCloudConnector() cw.clientSlipStream.httpClient._call = Mock(return_value=('', '')) cw._updateSlipStreamImage('module/Name', 'ABC') cw.clientSlipStream.httpClient._call.assert_called_with( '%s/module/Name/Test' % self.serviceurl, 'PUT', 'ABC', 'application/xml', 'application/xml')
def doWork(self): ch = ConfigHolder(self.options) client = Client(ch) if self.options.cancel: client.cancel_abort() else: value = truncate_middle(Client.VALUE_LENGTH_LIMIT, self.reason, '\n(truncated)\n') client.setRuntimeParameter(NodeDecorator.ABORT_KEY, value)
def _download_reports(self, api, run_url): if not (self.options.reports_components or self.options.get_reports_all): return components = [] if self.options.reports_components: components = self.options.reports_components ch = ConfigHolder(options=self.options) rg = ReportsGetter(api, ch) rg.get_reports(run_url_to_uuid(run_url), components=components)
def doWork(self): rvalue = ''.join([random.choice(string.ascii_letters + string.digits) for _ in xrange(self.size)]) if self.key is not None: ch = ConfigHolder(self.options) client = Client(ch) client.setRuntimeParameter(self.key, rvalue) print(rvalue)
def ss_get(param, ignore_abort=False, timeout=30, no_block=False): """Returns None if parameter is not set. Raises Exceptions.NotFoundError if parameter doesn't exist. """ ch = ConfigHolder(config={'foo': None}) ch.set('ignoreAbort', ignore_abort) ch.set('noBlock', no_block) ch.set('timeout', timeout) client = Client(ch) return client.getRuntimeParameter(param)
def _describe_instances(self): ch = ConfigHolder(options={ 'verboseLevel': 0, 'retry': False, KEY_RUN_CATEGORY: '' }, context={'foo': 'bar'}) cc = self.get_connector_class()(ch) cc._initialization(self.user_info, **self.get_initialization_extra_kwargs()) vms = self._list_instances(cc) return cc, vms
def test_post_with_data(self): ch = ConfigHolder() ch.context = {} ch.set('verboseLevel', 0) ch.set('cookie_filename', '/dev/null') ch.set('api_key', 'key') ch.set('api_secret', 'secret') client = HttpClient(ch) resp = requests.Response() resp.status_code = 200 resp.get = Mock(return_value=None) resp.request = Mock() resp.request.headers = {} requests.sessions.Session.send = Mock(return_value=resp) client.post('http://example.com', 'a=b\nc=d') args, kwargs = requests.sessions.Session.send.call_args self.assertEqual(len(args), 1) req = args[0] self.assertEqual(req.body, 'a=b\nc=d')
def setUp(self): self.serviceurl = 'http://example.com' self.configHolder = ConfigHolder( { 'username': base64.b64encode('user'), 'password': base64.b64encode('pass'), 'cookie_filename': 'cookies', 'serviceurl': self.serviceurl }, context={'foo': 'bar'}, config={'foo': 'bar'}) os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'Test'
def test_do_not_qualify_parameter(self): orch_node_name = NodeDecorator.orchestratorName + '-cloudX' orch_param = orch_node_name + \ NodeDecorator.NODE_PROPERTY_SEPARATOR + \ 'foo' context = {NodeDecorator.NODE_INSTANCE_NAME_KEY: orch_node_name} ch = ConfigHolder(context=context, config={ 'bar': 'baz', 'endpoint': 'https://foo.bar' }) c = Client(ch) assert orch_param == c._qualifyKey(orch_param)
def setUp(self): BaseCloudConnector._publish_vm_info = Mock() # pylint: disable=protected-access os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = self.connector_instance_name os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-%s' % time.time() if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') self.ch.set('verboseLevel', self.ch.config['General.verbosity']) self.client = CloudStackClientCloud(self.ch) self.user_info = UserInfo(self.connector_instance_name) self.user_info[self.constructKey('endpoint')] = self.ch.config['cloudstack.endpoint'] self.user_info[self.constructKey('zone')] = self.ch.config['cloudstack.zone'] self.user_info[self.constructKey('username')] = self.ch.config['cloudstack.key'] self.user_info[self.constructKey('password')] = self.ch.config['cloudstack.secret'] security_groups = self.ch.config['cloudstack.security.groups'] instance_type = self.ch.config['cloudstack.instance.type'] self.user_info['General.' + UserInfo.SSH_PUBKEY_KEY] = self.ch.config['General.ssh.public.key'] image_id = self.ch.config[self.constructKey('template')] self.multiplicity = 2 self.max_iaas_workers = self.ch.config.get('cloudstack.max.iaas.workers', str(self.multiplicity)) self.node_name = 'test_node' self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = self.node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: self.node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': self.connector_instance_name, # 'index': i,s 'image.platform': 'linux', 'image.imageId': image_id, 'image.id': image_id, self.constructKey('instance.type'): instance_type, self.constructKey('security.groups'): security_groups, 'network': 'private' })
def __init__(self, wrapper, config_holder=ConfigHolder()): """ :param wrapper: SlipStream client and cloud client wrapper :type wrapper: slipstream.wrappers.CloudWrapper :param config_holder: configuration holder :type config_holder: slipstream.ConfigHolder """ self.wrapper = wrapper self.timeout = 55 * 60 # 55 minutes self.ssLogDir = util.get_platform_reports_dir() self.verboseLevel = 0 config_holder.assign(self) self.reportFilesAndDirsList = [self.ssLogDir] self.node_instance = self._retrieve_my_node_instance() self.recovery_mode = False self._send_reports = False
def _run_instance(self, node_instance): nodename = node_instance.get_name() cloud_connector_class = self.get_connector_class() cloud_connector_class._publish_vm_info = publish_vm_info verbose_level = self.get_option('verbose') and 3 or 0 ch = ConfigHolder(options={ 'verboseLevel': verbose_level, 'retry': False, KEY_RUN_CATEGORY: RUN_CATEGORY_DEPLOYMENT }, context={'foo': 'bar'}, config={'foo': 'bar'}) cc = cloud_connector_class(ch) cc.start_nodes_and_clients(self.user_info, {nodename: node_instance}, self.get_initialization_extra_kwargs())
class TestResultArchiver(unittest.TestCase): def setUp(self): self.ch = ConfigHolder() self.ch.set('serviceurl', endpoint) self.ch.set('verboseLevel', 3) self.client = Client(self.ch) self.client.login(username, password) self.ch.set('endpoint', endpoint) self.ch.set('session', self.client.get_session()) def tearDown(self): shutil.rmtree(run_uuid, ignore_errors=True) try: self.client.logout() except: pass @unittest.skipIf(not all([username, password, run_uuid]), "Live test. Creds not set.") def test_get_reports(self): rg = ReportsGetter(self.ch) rg.get_reports(run_uuid) self.assertTrue(os.path.isdir(run_uuid))
def test_init_session_fail_no_creds(self): ch = ConfigHolder() ch.context = {} ch.set('verboseLevel', 0) ch.set('cookie_filename', '/dev/null') client = HttpClient(ch) client.init_session('http://foo.bar') assert client.session is not None assert client.session.login_params == {} resp = Mock(spec=Response) resp.status_code = 403 resp.cookies = None resp.headers = {} client.session._request = Mock(return_value=resp) client.session.cimi_login = Mock(return_value=resp) try: client.get('http://foo.bar', retry=False) except Exception as ex: assert ex.code == 403 assert client.session.cimi_login.called is True
def do_work(self): ch = ConfigHolder(options={ 'verboseLevel': 0, 'retry': False, KEY_RUN_CATEGORY: '' }, context={'foo': 'bar'}) self.cc = self.get_connector_class()(ch) self.cc._initialization(self.user_info, **self.get_initialization_extra_kwargs()) self.base_currency = self.get_option(self.BASE_CURRENCY_KEY) verbose = self.get_option('verbose') dry_run = self.get_option(self.DRY_RUN_KEY) ss_endpoint = self.get_option(self.SS_ENDPOINT_KEY) ss_username = self.get_option(self.SS_USERNAME_KEY) ss_password = self.get_option(self.SS_PASSWORD_KEY) connector_instance_name = self.get_option(self.CONNECTOR_NAME_KEY) filter_connector_vm = ' and '.join([ 'connector/href="{0}"'.format(connector_instance_name), 'resource:type="VM"' ]) self.ssapi = Api(endpoint=ss_endpoint, cookie_file=None, insecure=True) if not dry_run: self.ssapi.login_internal(ss_username, ss_password) self._initialize() service_offers = self._generate_service_offers(connector_instance_name) if not service_offers: raise RuntimeError("No service offer found") if not dry_run and service_offers: self._add_service_attribute_namespace_if_not_exist('resource') self._add_service_attribute_namespace_if_not_exist('price') prefix = self._get_prefix() if prefix: self._add_service_attribute_namespace_if_not_exist(prefix) service_offers_ids = set() for service_offer in service_offers: if dry_run: print('\nService offer {0}:\n{1}'.format( service_offer['name'], service_offer)) else: cimi_filter = \ ' and '.join([filter_connector_vm, 'resource:class="{0}"'.format(service_offer['resource:class']), 'resource:vcpu={0}'.format(service_offer['resource:vcpu']), 'resource:ram={0}'.format(service_offer['resource:ram']), 'resource:disk={0}'.format(service_offer['resource:disk']), 'resource:operatingSystem="{0}"'.format(service_offer['resource:operatingSystem']), 'resource:country="{0}"'.format(service_offer['resource:country']), 'resource:instanceType="{0}"'.format(service_offer['resource:instanceType'])]) search_result = self.ssapi.cimi_search('serviceOffers', filter=cimi_filter) result_list = search_result.resources_list result_count = len(result_list) if result_count == 0: if verbose: print( '\nAddinging the following service offer {0} to {1}...\n{2}' .format(service_offer['name'], ss_endpoint, service_offer)) response = self.ssapi.cimi_add('serviceOffers', service_offer) service_offers_ids.add(response.json['resource-id']) elif result_count == 1: if verbose: print( '\nUpdating the following service offer {0} to {1}...\n{2}' .format(service_offer['name'], ss_endpoint, service_offer)) response = self.ssapi.cimi_edit(result_list[0].id, service_offer) service_offers_ids.add(response.id) else: print( '\n!!! Warning duplicates found of following service offer on {0} !!!\n{1}' .format(ss_endpoint, service_offer['name'])) for result in result_list: service_offers_ids.add(result.id) if not dry_run: response = self.ssapi.cimi_search('serviceOffers', filter=filter_connector_vm) old_service_offers_ids = set(r.id for r in response.resources()) service_offers_ids_to_delete = old_service_offers_ids - service_offers_ids for id in service_offers_ids_to_delete: if verbose: offer = self.ssapi.cimi_get(id) print( '\nDeleting the following service offer with id {0}...\n{1}' .format(id, offer.json)) self.ssapi.cimi_delete(id) print('\n\nCongratulation, executon completed.')
def _init_client(self): self.configHolder = ConfigHolder(self.options) self.configHolder.set('serviceurl', self.options.endpoint) self.ss_client = SlipStreamHttpClient(self.configHolder)
class TestCloudStackClientCloud(unittest.TestCase): connector_instance_name = 'cloudstack' def constructKey(self, name): return self.connector_instance_name + '.' + name def setUp(self): BaseCloudConnector._publish_vm_info = Mock() # pylint: disable=protected-access os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = self.connector_instance_name os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-%s' % time.time() if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') self.ch.set('verboseLevel', self.ch.config['General.verbosity']) self.client = CloudStackClientCloud(self.ch) self.user_info = UserInfo(self.connector_instance_name) self.user_info[self.constructKey('endpoint')] = self.ch.config['cloudstack.endpoint'] self.user_info[self.constructKey('zone')] = self.ch.config['cloudstack.zone'] self.user_info[self.constructKey('username')] = self.ch.config['cloudstack.key'] self.user_info[self.constructKey('password')] = self.ch.config['cloudstack.secret'] security_groups = self.ch.config['cloudstack.security.groups'] instance_type = self.ch.config['cloudstack.instance.type'] self.user_info['General.' + UserInfo.SSH_PUBKEY_KEY] = self.ch.config['General.ssh.public.key'] image_id = self.ch.config[self.constructKey('template')] self.multiplicity = 2 self.max_iaas_workers = self.ch.config.get('cloudstack.max.iaas.workers', str(self.multiplicity)) self.node_name = 'test_node' self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = self.node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: self.node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': self.connector_instance_name, # 'index': i,s 'image.platform': 'linux', 'image.imageId': image_id, 'image.id': image_id, self.constructKey('instance.type'): instance_type, self.constructKey('security.groups'): security_groups, 'network': 'private' }) def tearDown(self): os.environ.pop('SLIPSTREAM_CONNECTOR_INSTANCE') os.environ.pop('SLIPSTREAM_BOOTSTRAP_BIN') self.client = None self.ch = None def xtest_1_startStopImages(self): self.client.run_category = RUN_CATEGORY_DEPLOYMENT try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started') vms = self.client.get_vms() assert len(vms) == self.multiplicity finally: self.client.stop_deployment() def xtest_2_buildImage(self): raise NotImplementedError()
def _init_client(self): configHolder = ConfigHolder(self.options, context={'empty': None}, config={'empty': None}) configHolder.set('serviceurl', self.options.endpoint) self.client = Client(configHolder)
def setUp(self): cloudName = TestOkeanosClientCloud.CLOUD_NAME flavorKey = TestOkeanosClientCloud.FLAVOR_KEY resizeFlavorKey = TestOkeanosClientCloud.RESIZE_FLAVOR_KEY os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cloudName os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = \ '%s-1234-1234-1234-123456789012' % str(int(time.time()))[2:] if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.verboseLevel = int(self.ch.verboseLevel) flavor = self.ch.config[flavorKey] resizeFlavor = self.ch.config[resizeFlavorKey] self.log("Initial Flavor: '%s' = %s" % (flavorKey, flavor)) self.log("Resize Flavor: '%s' = %s" % (resizeFlavorKey, resizeFlavor)) self.user_info = UserInfo(cloudName) self.user_info['General.ssh.public.key'] = self.ch.config['General.ssh.public.key'] self.user_info[cloudName + '.endpoint'] = self.ch.config[cloudName + '.auth_url'] self.user_info[cloudName + '.username'] = self.ch.config[cloudName + '.user.uuid'] self.user_info[cloudName + '.password'] = self.ch.config[cloudName + '.token'] self.user_info[cloudName + '.project.id'] = self.ch.config[cloudName + '.project.id'] node_name = 'test_node' self.multiplicity = int(self.ch.config['multiplicity']) self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) ni = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': cloudName, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cloudName + '.image.platform'], 'image.id': self.ch.config[cloudName + '.imageid'], flavorKey: flavor, resizeFlavorKey: resizeFlavor, 'network': self.ch.config['network'] }) ni.set_parameter(NodeDecorator.SCALE_DISK_ATTACH_SIZE, 1) self.node_instances[node_instance_name] = ni self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': cloudName, 'disk.attach.size': self.ch.config[cloudName + '.disk.attach.size'], 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cloudName + '.image.platform'], 'image.loginUser': self.ch.config[cloudName + '.image.loginuser'], 'image.id': self.ch.config[cloudName + '.imageid'], flavorKey: flavor, resizeFlavorKey: resizeFlavor, 'network': self.ch.config['network'], 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ })
class MainProgram(CommandBase): """A command-line program to download deployment reports.""" def __init__(self, argv=None): self.module = '' self.endpoint = None self.ss_client = None self.configHolder = None super(MainProgram, self).__init__(argv) def parse(self): usage = '''usage: %prog [options] [<run-uuid>] <run-uuid> UUID of the run to get reports from.''' self.parser.usage = usage self.addEndpointOption() self.parser.add_option( '-c', '--components', dest='components', help= 'Comma separated list of components to download the reports for. ' 'Example: nginx,worker.1,worker.3 - will download reports for all component ' 'instances of nginx and only for instances 1 and 3 of worker. ' 'Default: all instances of all components.', default='') self.parser.add_option( '-o', '--output-dir', dest='output_dir', help= 'Path to the folder to store the reports. Default: <working directory>/<run-uuid>.', default=os.getcwd()) self.parser.add_option('--no-orch', dest='no_orch', help='Do not download Orchestrator report.', default=False, action='store_true') self.options, self.args = self.parser.parse_args() if self.options.components: self.options.components = self.options.components.split(',') else: self.options.components = [] self._checkArgs() def _checkArgs(self): if len(self.args) == 1: self.run_uuid = self.args[0] if len(self.args) > 1: self.usageExitTooManyArguments() def _init_client(self): self.configHolder = ConfigHolder(self.options) self.configHolder.set('serviceurl', self.options.endpoint) self.ss_client = SlipStreamHttpClient(self.configHolder) def doWork(self): self._init_client() rg = ReportsGetter(self.ss_client.get_api(), self.configHolder) rg.get_reports(self.run_uuid, components=self.options.components, no_orch=self.options.no_orch)
def setUp(self): cn = getConnectorClass().cloudName os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cn os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = \ '%s-1234-1234-1234-123456789012' % str(int(time.time()))[2:] if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.verboseLevel = int(self.ch.verboseLevel) self.user_info = UserInfo(cn) self.user_info['General.ssh.public.key'] = self.ch.config[ 'General.ssh.public.key'] self.user_info[cn + '.user.uuid'] = self.ch.config[cn + '.user.uuid'] self.user_info[cn + '.username'] = self.ch.config[cn + '.username'] self.user_info[cn + '.password'] = self.ch.config[cn + '.password'] self.user_info[cn + '.endpoint'] = self.ch.config[cn + '.endpoint'] node_name = 'test_node' self.multiplicity = int(self.ch.config['multiplicity']) self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'] }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.loginUser': self.ch.config[cn + '.image.loginuser'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'], 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ })
def doWork(self): ch = ConfigHolder(self.options) client = Client(ch) client.cancel_abort()
class TestFlexiantClientCloud(unittest.TestCase): def setUp(self): cn = getConnectorClass().cloudName os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cn os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = \ '%s-1234-1234-1234-123456789012' % str(int(time.time()))[2:] if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.verboseLevel = int(self.ch.verboseLevel) self.user_info = UserInfo(cn) self.user_info['General.ssh.public.key'] = self.ch.config[ 'General.ssh.public.key'] self.user_info[cn + '.user.uuid'] = self.ch.config[cn + '.user.uuid'] self.user_info[cn + '.username'] = self.ch.config[cn + '.username'] self.user_info[cn + '.password'] = self.ch.config[cn + '.password'] self.user_info[cn + '.endpoint'] = self.ch.config[cn + '.endpoint'] node_name = 'test_node' self.multiplicity = int(self.ch.config['multiplicity']) self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'] }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.loginUser': self.ch.config[cn + '.image.loginuser'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'], 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ }) def tearDown(self): os.environ.pop('SLIPSTREAM_CONNECTOR_INSTANCE') os.environ.pop('SLIPSTREAM_BOOTSTRAP_BIN') os.environ.pop('SLIPSTREAM_DIID') self.client = None self.ch = None def _init_connector(self, run_category=RUN_CATEGORY_DEPLOYMENT): self.ch.set(KEY_RUN_CATEGORY, run_category) self.client = getConnector(self.ch) self.client._publish_vm_info = Mock() def xtest_1_startWaitRunningStopImage(self): self._init_connector() self._start_wait_running_stop_images() def xtest_2_buildImage(self): self._init_connector(run_category=RUN_CATEGORY_IMAGE) try: instances_details = self.client.start_nodes_and_clients( self.user_info, {NodeDecorator.MACHINE_NAME: self.node_instance}) assert instances_details assert instances_details[0][NodeDecorator.MACHINE_NAME] new_id = self.client.build_image(self.user_info, self.node_instance) assert new_id finally: self.client.stop_deployment() print('Deregistering image %s ... ' % new_id) self.client.deregister_image(new_id) print('Done.') def xtest_3_addDisk(self): self._init_connector() try: print('Node instances: %s' % self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): node_instance.set_parameter( NodeDecorator.SCALE_DISK_ATTACH_SIZE, 20) vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print 'VM Created: %s' % vm disk_uuid = self.client._attach_disk(node_instance) print '=================================================================' print('Disk created with uuid %s ' % disk_uuid) # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the created VM is %s' % i['status'] assert i['status'] == "RUNNING" for disk in disks: if (disk['resourceUUID'] == disk_uuid): print 'The status of the attached disk %s' % disk[ 'status'] assert disk['status'] == "ATTACHED_TO_SERVER" assert disk['serverUUID'] == vm_uuid print 'Attached disk info: %s' % disk print '=================================================================' finally: self.client.stop_deployment() print('Done.') def xtest_4_removeDisk(self): self._init_connector() try: print('Node instances: %s' % self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print node_instance print 'VM Created: %s' % vm node_instance.set_parameter( NodeDecorator.SCALE_DISK_ATTACH_SIZE, 20) disk_uuid = self.client._attach_disk(node_instance) print '=================================================================' print('Disk created with uuid %s ' % disk_uuid) # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the VM is %s' % i['status'] for disk in disks: if (disk['resourceUUID'] == disk_uuid): print 'The status of the attached disk %s' % disk[ 'status'] node_instance.set_parameter( NodeDecorator.SCALE_DISK_DETACH_DEVICE, disk_uuid) self.client._detach_disk(node_instance) vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print "Disks on the VM are:" print disks for disk in disks: assert disk['resourceUUID'] != disk_uuid print 'The status of the VM is %s' % i['status'] assert i['status'] == "RUNNING" print '=================================================================' finally: self.client.stop_deployment() print('Done.') def xtest_5_resize(self): self._init_connector() MODIFIED_CPU_COUNT = 2 MODIFIED_RAM_AMT = 4096 try: print('Node instances: %s' % self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): node_instance.set_cloud_parameters({'cpu': MODIFIED_CPU_COUNT}) node_instance.set_cloud_parameters({'ram': MODIFIED_RAM_AMT}) vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print 'VM Created: %s' % vm self.client._resize(node_instance) print '=================================================================' # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the created VM is %s' % i['status'] assert i['status'] == "RUNNING" assert i['cpu'] == MODIFIED_CPU_COUNT assert i['ram'] == MODIFIED_RAM_AMT print '=================================================================' finally: self.client.stop_deployment() def _start_wait_running_stop_images(self): try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started\n') vms = self.client.get_vms() assert len(vms) == self.multiplicity for vm in vms.values(): self.client._wait_vm_in_state_running_or_timeout(vm['id']) time.sleep(2) finally: self.client.stop_deployment()
def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'Test' self.ch = ConfigHolder(config={'foo': 'bar'}, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, RUN_CATEGORY_DEPLOYMENT)
class TestFlexiantClientCloud(unittest.TestCase): def setUp(self): cn = getConnectorClass().cloudName os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cn os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = \ '%s-1234-1234-1234-123456789012' % str(int(time.time()))[2:] if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.verboseLevel = int(self.ch.verboseLevel) self.user_info = UserInfo(cn) self.user_info['General.ssh.public.key'] = self.ch.config['General.ssh.public.key'] self.user_info[cn + '.user.uuid'] = self.ch.config[cn + '.user.uuid'] self.user_info[cn + '.username'] = self.ch.config[cn + '.username'] self.user_info[cn + '.password'] = self.ch.config[cn + '.password'] self.user_info[cn + '.endpoint'] = self.ch.config[cn + '.endpoint'] node_name = 'test_node' self.multiplicity = int(self.ch.config['multiplicity']) self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'] }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.loginUser': self.ch.config[cn + '.image.loginuser'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'], 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ }) def tearDown(self): os.environ.pop('SLIPSTREAM_CONNECTOR_INSTANCE') os.environ.pop('SLIPSTREAM_BOOTSTRAP_BIN') os.environ.pop('SLIPSTREAM_DIID') self.client = None self.ch = None def _init_connector(self, run_category=RUN_CATEGORY_DEPLOYMENT): self.ch.set(KEY_RUN_CATEGORY, run_category) self.client = getConnector(self.ch) self.client._publish_vm_info = Mock() def xtest_1_startWaitRunningStopImage(self): self._init_connector() self._start_wait_running_stop_images() def xtest_2_buildImage(self): self._init_connector(run_category=RUN_CATEGORY_IMAGE) try: instances_details = self.client.start_nodes_and_clients( self.user_info, {NodeDecorator.MACHINE_NAME: self.node_instance}) assert instances_details assert instances_details[0][NodeDecorator.MACHINE_NAME] new_id = self.client.build_image(self.user_info, self.node_instance) assert new_id finally: self.client.stop_deployment() print('Deregistering image %s ... ' % new_id) self.client.deregister_image(new_id) print('Done.') def xtest_3_addDisk(self): self._init_connector() try: print('Node instances: %s' %self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): node_instance.set_parameter(NodeDecorator.SCALE_DISK_ATTACH_SIZE, 20) vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print 'VM Created: %s' %vm disk_uuid = self.client._attach_disk(node_instance); print '=================================================================' print ('Disk created with uuid %s ' %disk_uuid) # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the created VM is %s' %i['status'] assert i['status'] == "RUNNING" for disk in disks: if (disk['resourceUUID'] == disk_uuid): print 'The status of the attached disk %s' %disk['status'] assert disk['status'] == "ATTACHED_TO_SERVER" assert disk['serverUUID'] == vm_uuid print 'Attached disk info: %s' %disk print '=================================================================' finally: self.client.stop_deployment() print('Done.') def xtest_4_removeDisk(self): self._init_connector() try: print('Node instances: %s' %self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print node_instance print 'VM Created: %s' %vm node_instance.set_parameter(NodeDecorator.SCALE_DISK_ATTACH_SIZE, 20) disk_uuid = self.client._attach_disk(node_instance) print '=================================================================' print ('Disk created with uuid %s ' %disk_uuid) # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the VM is %s' %i['status'] for disk in disks: if (disk['resourceUUID'] == disk_uuid): print 'The status of the attached disk %s' %disk['status'] node_instance.set_parameter(NodeDecorator.SCALE_DISK_DETACH_DEVICE, disk_uuid) self.client._detach_disk(node_instance) vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print "Disks on the VM are:" print disks for disk in disks: assert disk['resourceUUID'] != disk_uuid print 'The status of the VM is %s' %i['status'] assert i['status'] == "RUNNING" print '=================================================================' finally: self.client.stop_deployment() print('Done.') def xtest_5_resize(self): self._init_connector() MODIFIED_CPU_COUNT = 2 MODIFIED_RAM_AMT = 4096 try: print('Node instances: %s' %self.node_instances.values()) self.client.start_nodes_and_clients(self.user_info, self.node_instances) for node_instance in self.node_instances.values(): node_instance.set_cloud_parameters({'cpu': MODIFIED_CPU_COUNT}) node_instance.set_cloud_parameters({'ram': MODIFIED_RAM_AMT}) vm = self.client._get_vm(node_instance.get_name()) vm_uuid = vm['resourceUUID'] print 'VM Created: %s' %vm self.client._resize(node_instance) print '=================================================================' # Get the list of VMs vm_list = self.client.list_instances() for i in vm_list: # Get the VM that was created in the test using the UUID obtained after creation if (i['resourceUUID'] == vm_uuid): #print 'Disks attached on the VM are: ' disks = i['disks'] print 'The status of the created VM is %s' %i['status'] assert i['status'] == "RUNNING" assert i['cpu'] == MODIFIED_CPU_COUNT assert i['ram'] == MODIFIED_RAM_AMT print '=================================================================' finally: self.client.stop_deployment() def _start_wait_running_stop_images(self): try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started\n') vms = self.client.get_vms() assert len(vms) == self.multiplicity for vm in vms.values(): self.client._wait_vm_in_state_running_or_timeout(vm['id']) time.sleep(2) finally: self.client.stop_deployment()
class TestOpenNebulaClientCloudLive(unittest.TestCase): connector_instance_name = 'opennebula' def constructKey(self, name): return self.connector_instance_name + '.' + name def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = self.connector_instance_name os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-000000000000' if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') OpenNebulaClientCloud._publish_vm_info = publish_vm_info # pylint: disable=protected-access self.client = OpenNebulaClientCloud(self.ch) self.user_info = UserInfo(self.connector_instance_name) self.user_info['General.'+ UserInfo.SSH_PUBKEY_KEY] = self.ch.config['General.ssh.public.key'] self.user_info[self.constructKey('endpoint')] = self.ch.config['opennebula.endpoint'] self.user_info[self.constructKey('username')] = self.ch.config['opennebula.username'] self.user_info[self.constructKey('password')] = self.ch.config['opennebula.password'] self.user_info[self.constructKey(UserInfo.NETWORK_PUBLIC_KEY)] = self.ch.config['opennebula.networkPublic'] self.user_info[self.constructKey(UserInfo.NETWORK_PRIVATE_KEY)] = self.ch.config['opennebula.networkPrivate'] self.user_info[self.constructKey('cpuRatio')] = '1.0' image_id = self.ch.config['opennebula.imageid'] instance_type = self.ch.config.get('opennebula.intance.type', 'm1.tiny') node_name = 'test_node' self.multiplicity = 1 self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': self.connector_instance_name, 'image.platform': 'Ubuntu', 'image.imageId': image_id, 'image.id': image_id, 'network': self.ch.config['opennebula.network'], self.constructKey('instance.type'): instance_type, self.constructKey('ram'): '2', self.constructKey('cpu'): '1' }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': self.connector_instance_name, 'image.platform': 'Ubuntu', 'image.imageId': image_id, 'image.id': image_id, self.constructKey('instance.type'): instance_type, 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ }) def tearDown(self): os.environ.pop('SLIPSTREAM_CONNECTOR_INSTANCE') os.environ.pop('SLIPSTREAM_BOOTSTRAP_BIN') self.client = None self.ch = None def xtest_1_startStopImages(self): self.client.run_category = RUN_CATEGORY_DEPLOYMENT self.client.start_nodes_and_clients(self.user_info, self.node_instances) util.printAndFlush('Instances started') vms = self.client.get_vms() assert len(vms) == self.multiplicity self.client.stop_deployment() def xtest_2_buildImage(self): self.client.run_category = RUN_CATEGORY_IMAGE self.client.start_nodes_and_clients(self.user_info, {NodeDecorator.MACHINE_NAME: self.node_instance}) instances_details = self.client.get_vms_details() assert instances_details assert instances_details[0][NodeDecorator.MACHINE_NAME] new_id = self.client.build_image(self.user_info, self.node_instance) assert new_id
def setUp(self): cn = getConnectorClass().cloudName os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = cn os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = \ '%s-1234-1234-1234-123456789012' % str(int(time.time()))[2:] if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.verboseLevel = int(self.ch.verboseLevel) self.user_info = UserInfo(cn) self.user_info['General.ssh.public.key'] = self.ch.config['General.ssh.public.key'] self.user_info[cn + '.user.uuid'] = self.ch.config[cn + '.user.uuid'] self.user_info[cn + '.username'] = self.ch.config[cn + '.username'] self.user_info[cn + '.password'] = self.ch.config[cn + '.password'] self.user_info[cn + '.endpoint'] = self.ch.config[cn + '.endpoint'] node_name = 'test_node' self.multiplicity = int(self.ch.config['multiplicity']) self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'] }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': cn, 'image.description': 'This is a test image.', 'image.platform': self.ch.config[cn + '.image.platform'], 'image.loginUser': self.ch.config[cn + '.image.loginuser'], 'image.id': self.ch.config[cn + '.imageid'], cn + '.ram': self.ch.config[cn + '.ram'], cn + '.cpu': self.ch.config[cn + '.cpu'], 'network': self.ch.config['network'], 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ })
def doWork(self): ch = ConfigHolder(self.options) client = Client(ch) client.terminateRun()
def _load_config(self, conf_file): if not os.path.exists(conf_file): raise Exception('Configuration file %s not found.' % conf_file) self.ch = ConfigHolder(configFile=conf_file, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '')
class TestCloudWrapper(TestCloudConnectorsBase): def setUp(self): self.serviceurl = 'http://example.com' self.config_holder = ConfigHolder( { 'username': base64.b64encode('user'), 'password': base64.b64encode('pass'), 'cookie_filename': '/var/tmp/cookies', 'serviceurl': self.serviceurl, 'node_instance_name': 'instance-name' }, context={'foo': 'bar'}, config={'foo': 'bar'}) os.environ[ENV_CONNECTOR_INSTANCE] = 'Test' BaseWrapper.is_mutable = Mock(return_value=False) def tearDown(self): os.environ.pop(ENV_CONNECTOR_INSTANCE) self.config_holder = None shutil.rmtree('%s/.cache/' % os.getcwd(), ignore_errors=True) def test_get_cloud_name(self): for module_name in self.get_cloudconnector_modulenames(): setattr(self.config_holder, CONFIGPARAM_CONNECTOR_MODULE_NAME, module_name) setattr(self.config_holder, KEY_RUN_CATEGORY, RUN_CATEGORY_DEPLOYMENT) cw = CloudWrapper(self.config_holder) cw.initCloudConnector() assert cw._get_cloud_service_name() == 'Test' def test_put_image_id(self): # pylint: disable=protected-access self.config_holder.set(CONFIGPARAM_CONNECTOR_MODULE_NAME, self.get_cloudconnector_modulename_by_cloudname('local')) cw = CloudWrapper(self.config_holder) cw.initCloudConnector() cw._ss_client.httpClient._call = Mock(return_value=Mock()) cw._update_slipstream_image(NodeInstance({'image.resourceUri': 'module/Name'}), 'ABC') cw._ss_client.httpClient._call.assert_called_with( '%s/module/Name/Test' % self.serviceurl, 'PUT', 'ABC', 'application/xml', 'application/xml', retry=True) def test_no_scaling(self): """ No scaling is happening. Node instances are in terminal states. """ node_instances = { 'n.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_OPERATIONAL}), 'm.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_GONE}) } cw = CloudWrapper(self.config_holder) cw._get_nodes_instances = Mock(return_value=node_instances) assert {} == cw._get_effective_scale_states() assert None == cw._get_global_scale_state() assert False == cw.is_vertical_scaling() node_and_instances = cw.get_scaling_node_and_instance_names() assert '' == node_and_instances[0] assert [] == node_and_instances[1] def test_consistent_scale_state_inconsistent_scaling_nodes(self): """ Consistent scale state: only one scaling action at a time on different node instances. In case node instance is not in a terminal state (as set on the NodeInstance object), we check the state directly on the Run (via CloudWrapper._get_runtime_parameter()). Inconsistent scaling nodes: only one node type at a time is allowed to be scaled. """ def _get_runtime_parameter(key): if key.endswith(NodeDecorator.NODE_PROPERTY_SEPARATOR + NodeDecorator.SCALE_STATE_KEY): return CloudWrapper.SCALE_STATE_RESIZING else: return 'unknown' node_instances = { 'n.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}), 'n.2': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_OPERATIONAL}), 'm.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1', NodeDecorator.NODE_NAME_KEY: 'm', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}), 'm.2': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.2', NodeDecorator.NODE_NAME_KEY: 'm', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_GONE}), } cw = CloudWrapper(self.config_holder) cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._get_nodes_instances = Mock(return_value=node_instances) scale_states = cw._get_effective_scale_states() assert 1 == len(scale_states) assert CloudWrapper.SCALE_STATE_RESIZING in scale_states assert 2 == len(scale_states[CloudWrapper.SCALE_STATE_RESIZING]) assert ['m.1', 'n.1'] == sorted(scale_states[CloudWrapper.SCALE_STATE_RESIZING]) assert CloudWrapper.SCALE_STATE_RESIZING == cw._get_global_scale_state() assert True == cw.is_vertical_scaling() self.failUnlessRaises(InconsistentScalingNodesError, cw.get_scaling_node_and_instance_names) def test_inconsistent_scale_state_inconsistent_scaling_nodes(self): """ Inconsistent scale state: different scaling actions at a time are not allowed. In case node instance is not in a terminal state (as set on the NodeInstance object), we check the state directly on the Run (via CloudWrapper._get_runtime_parameter()). Inconsistent scaling nodes: only one node type at a time is allowed to be scaled. """ def _get_runtime_parameter(key): if key.startswith('n.'): return CloudWrapper.SCALE_STATE_RESIZING elif key.startswith('m.'): return CloudWrapper.SCALE_STATE_DISK_ATTACHING else: return 'unknown' node_instances = { 'n.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}), 'n.2': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_OPERATIONAL}), 'm.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1', NodeDecorator.NODE_NAME_KEY: 'm', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}) } cw = CloudWrapper(self.config_holder) cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._get_nodes_instances = Mock(return_value=node_instances) scale_states = cw._get_effective_scale_states() assert 2 == len(scale_states) assert CloudWrapper.SCALE_STATE_RESIZING in scale_states assert 1 == len(scale_states[CloudWrapper.SCALE_STATE_RESIZING]) assert ['n.1'] == scale_states[CloudWrapper.SCALE_STATE_RESIZING] assert CloudWrapper.SCALE_STATE_DISK_ATTACHING in scale_states assert 1 == len(scale_states[CloudWrapper.SCALE_STATE_DISK_ATTACHING]) assert ['m.1'] == scale_states[CloudWrapper.SCALE_STATE_DISK_ATTACHING] self.failUnlessRaises(InconsistentScaleStateError, cw._get_global_scale_state) self.failUnlessRaises(InconsistentScaleStateError, cw.is_vertical_scaling) self.failUnlessRaises(InconsistentScaleStateError, cw.check_scale_state_consistency) self.failUnlessRaises(InconsistentScalingNodesError, cw.get_scaling_node_and_instance_names) def test_consistent_scale_state_consistent_scaling_nodes(self): """ Consistent scale state: different scaling actions at a time are not allowed. In case node instance is not in a terminal state (as set on the NodeInstance object), we check the state directly on the Run (via CloudWrapper._get_runtime_parameter()). Consistent scaling nodes: only one node type at a time is allowed to be scaled. """ def _get_runtime_parameter(key): if key.endswith(NodeDecorator.NODE_PROPERTY_SEPARATOR + NodeDecorator.SCALE_STATE_KEY): return CloudWrapper.SCALE_STATE_RESIZING else: return 'unknown' node_instances = { 'n.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}), 'n.2': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: 'not terminal'}), 'm.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1', NodeDecorator.NODE_NAME_KEY: 'm', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_OPERATIONAL}) } cw = CloudWrapper(self.config_holder) cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._get_nodes_instances = Mock(return_value=node_instances) scale_states = cw._get_effective_scale_states() assert 1 == len(scale_states) assert CloudWrapper.SCALE_STATE_RESIZING in scale_states assert 2 == len(scale_states[CloudWrapper.SCALE_STATE_RESIZING]) assert ['n.1', 'n.2'] == sorted(scale_states[CloudWrapper.SCALE_STATE_RESIZING]) assert CloudWrapper.SCALE_STATE_RESIZING == cw._get_global_scale_state() try: cw.check_scale_state_consistency() except InconsistentScaleStateError as ex: self.fail('Should not have failed with: %s' % str(ex)) node_and_instances = cw.get_scaling_node_and_instance_names() assert 'n' == node_and_instances[0] assert ['n.1', 'n.2'] == sorted(node_and_instances[1]) def test_vertically_scalle_instances_nowait(self): _scale_state = None def _get_runtime_parameter(key): if key.endswith(NodeDecorator.NODE_PROPERTY_SEPARATOR + NodeDecorator.SCALE_STATE_KEY): return _scale_state else: return 'unknown' node_instances = { 'n.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_RESIZING}), 'n.2': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_DISK_ATTACHING}), 'n.3': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.3', NodeDecorator.NODE_NAME_KEY: 'n', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_DISK_DETACHING}), 'm.1': NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1', NodeDecorator.NODE_NAME_KEY: 'm', NodeDecorator.SCALE_STATE_KEY: CloudWrapper.SCALE_STATE_OPERATIONAL}) } self.config_holder.set('verboseLevel', 3) setattr(self.config_holder, 'cloud', 'local') setattr(self.config_holder, CONFIGPARAM_CONNECTOR_MODULE_NAME, 'slipstream.cloudconnectors.dummy.DummyClientCloud') cw = CloudWrapper(self.config_holder) cw._get_nodes_instances = Mock(return_value=node_instances) cw.initCloudConnector(self.config_holder) cw._set_runtime_parameter = Mock() cw._get_user_timeout = Mock(return_value=2) # No waiting. cw._wait_pre_scale_done = Mock() cw._wait_scale_state = Mock() _scale_state = 'resizing' cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._cloud_client.resize = Mock(wraps=cw._cloud_client.resize) cw.vertically_scale_instances() assert True == cw._cloud_client.resize.called node_instance = cw._cloud_client.resize.call_args[0][0][0] assert 'n.1' in node_instance.get_name() assert cw._set_runtime_parameter.called_with('n.1:' + NodeDecorator.SCALE_IAAS_DONE, 'true') _scale_state = 'disk_attaching' cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._cloud_client.attach_disk = Mock(wraps=cw._cloud_client.attach_disk) cw.vertically_scale_instances() assert True == cw._cloud_client.attach_disk.called node_instance = cw._cloud_client.attach_disk.call_args[0][0][0] assert 'n.2' in node_instance.get_name() assert cw._set_runtime_parameter.called_with('n.2:' + NodeDecorator.SCALE_IAAS_DONE, 'true') _scale_state = 'disk_detaching' cw._get_runtime_parameter = Mock(side_effect=_get_runtime_parameter) cw._cloud_client.detach_disk = Mock(wraps=cw._cloud_client.detach_disk) cw.vertically_scale_instances() assert True == cw._cloud_client.detach_disk.called node_instance = cw._cloud_client.detach_disk.call_args[0][0][0] assert 'n.3' in node_instance.get_name() assert cw._set_runtime_parameter.called_with('n.3:' + NodeDecorator.SCALE_IAAS_DONE, 'true') def test_wait_pre_scale_done(self): node_instances = [ NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.3'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1'}) ] cw = CloudWrapper(self.config_holder) cw.get_pre_scale_done = Mock(return_value='true') cw._get_state_timeout_time = Mock(return_value=(time.time() + 10)) cw._wait_pre_scale_done(node_instances) cw.get_pre_scale_done = Mock(return_value='true') cw._get_state_timeout_time = Mock(return_value=(time.time() - 1)) self.failUnlessRaises(TimeoutException, cw._wait_pre_scale_done, node_instances) cw.get_pre_scale_done = Mock(return_value='') cw._get_state_timeout_time = Mock(return_value=(time.time() + 2)) self.failUnlessRaises(TimeoutException, cw._wait_pre_scale_done, node_instances) def test_wait_scale_state(self): node_instances = [ NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.1'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.2'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'n.3'}), NodeInstance({NodeDecorator.NODE_INSTANCE_NAME_KEY: 'm.1'}) ] cw = CloudWrapper(self.config_holder) # All set before timeout. cw._get_effective_scale_state = Mock(return_value=CloudWrapper.SCALE_STATE_RESIZED) cw._get_state_timeout_time = Mock(return_value=(time.time() + 5)) cw._wait_scale_state(CloudWrapper.SCALE_STATE_RESIZED, node_instances) # Timeout is in the past. cw._get_effective_scale_state = Mock(return_value=CloudWrapper.SCALE_STATE_RESIZED) cw._get_state_timeout_time = Mock(return_value=(time.time() - 1)) self.failUnlessRaises(TimeoutException, cw._wait_scale_state, *(CloudWrapper.SCALE_STATE_RESIZED, node_instances)) # VMs do not set proper value and we timeout. cw._get_effective_scale_state = Mock(return_value=CloudWrapper.SCALE_STATE_RESIZING) cw._get_state_timeout_time = Mock(return_value=(time.time() + 2)) self.failUnlessRaises(TimeoutException, cw._wait_scale_state, *(CloudWrapper.SCALE_STATE_RESIZED, node_instances))
def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = self.connector_instance_name os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-000000000000' if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') OpenNebulaClientCloud._publish_vm_info = publish_vm_info # pylint: disable=protected-access self.client = OpenNebulaClientCloud(self.ch) self.user_info = UserInfo(self.connector_instance_name) self.user_info['General.'+ UserInfo.SSH_PUBKEY_KEY] = self.ch.config['General.ssh.public.key'] self.user_info[self.constructKey('endpoint')] = self.ch.config['opennebula.endpoint'] self.user_info[self.constructKey('username')] = self.ch.config['opennebula.username'] self.user_info[self.constructKey('password')] = self.ch.config['opennebula.password'] self.user_info[self.constructKey(UserInfo.NETWORK_PUBLIC_KEY)] = self.ch.config['opennebula.networkPublic'] self.user_info[self.constructKey(UserInfo.NETWORK_PRIVATE_KEY)] = self.ch.config['opennebula.networkPrivate'] self.user_info[self.constructKey('cpuRatio')] = '1.0' image_id = self.ch.config['opennebula.imageid'] instance_type = self.ch.config.get('opennebula.intance.type', 'm1.tiny') node_name = 'test_node' self.multiplicity = 1 self.node_instances = {} for i in range(1, self.multiplicity + 1): node_instance_name = node_name + '.' + str(i) self.node_instances[node_instance_name] = NodeInstance({ NodeDecorator.NODE_NAME_KEY: node_name, NodeDecorator.NODE_INSTANCE_NAME_KEY: node_instance_name, 'cloudservice': self.connector_instance_name, 'image.platform': 'Ubuntu', 'image.imageId': image_id, 'image.id': image_id, 'network': self.ch.config['opennebula.network'], self.constructKey('instance.type'): instance_type, self.constructKey('ram'): '2', self.constructKey('cpu'): '1' }) self.node_instance = NodeInstance({ NodeDecorator.NODE_NAME_KEY: NodeDecorator.MACHINE_NAME, NodeDecorator.NODE_INSTANCE_NAME_KEY: NodeDecorator.MACHINE_NAME, 'cloudservice': self.connector_instance_name, 'image.platform': 'Ubuntu', 'image.imageId': image_id, 'image.id': image_id, self.constructKey('instance.type'): instance_type, 'image.prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'image.packages': ['lvm2', 'nano'], 'image.recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """ })
def doWork(self): ch = ConfigHolder(self.options, context={'empty': None}, config={'empty': None}) client = Client(ch) client.login(self.username, self.password)
def setUp(self): os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = 'stratuslab' os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-000000000000' if not os.path.exists(CONFIG_FILE): raise Exception('Configuration file %s not found.' % CONFIG_FILE) self.ch = ConfigHolder(configFile=CONFIG_FILE, context={'foo': 'bar'}) self.ch.set('verboseLevel', '2') os.environ['SLIPSTREAM_MESSAGING_ENDPOINT'] = self.ch.config['SLIPSTREAM_MESSAGING_ENDPOINT'] os.environ['SLIPSTREAM_MESSAGING_TYPE'] = self.ch.config['SLIPSTREAM_MESSAGING_TYPE'] os.environ['SLIPSTREAM_MESSAGING_QUEUE'] = self.ch.config['SLIPSTREAM_MESSAGING_QUEUE'] self.client = StratuslabClientCloud(self.ch) self.client.publishVmInfo = Mock() self.user_info = UserInfo('stratuslab') self.user_info['stratuslab.endpoint'] = self.ch.config['stratuslab.endpoint'] self.user_info['stratuslab.ip.type'] = self.ch.config['stratuslab.ip.type'] self.user_info['stratuslab.marketplace.endpoint'] = self.ch.config['stratuslab.marketplace.endpoint'] self.user_info['stratuslab.password'] = self.ch.config['stratuslab.password'] self.user_info['General.ssh.public.key'] = self.ch.config['General.ssh.public.key'] self.user_info['stratuslab.username'] = self.ch.config['stratuslab.username'] self.user_info['User.firstName'] = 'Foo' self.user_info['User.lastName'] = 'Bar' self.user_info['User.email'] = '*****@*****.**' extra_disk_volatile = self.ch.config['stratuslab.extra.disk.volatile'] image_id = self.ch.config['stratuslab.imageid'] self.multiplicity = 1 self.node_info = { 'multiplicity': self.multiplicity, 'nodename': 'test_node', 'image': { 'extra_disks': {}, 'cloud_parameters': { 'stratuslab': { 'stratuslab.instance.type': 'm1.small', 'stratuslab.disks.bus.type': 'virtio', 'stratuslab.cpu': '', 'stratuslab.ram': '' }, 'Cloud': { 'network': 'public', 'extra.disk.volatile': extra_disk_volatile } }, 'attributes': { 'resourceUri': '', 'imageId': image_id, 'platform': 'Ubuntu' }, 'targets': { 'prerecipe': """#!/bin/sh set -e set -x ls -l /tmp dpkg -l | egrep "nano|lvm" || true """, 'recipe': """#!/bin/sh set -e set -x dpkg -l | egrep "nano|lvm" || true lvs """, 'packages': ['lvm2', 'nano'] } }, }
def ss_set(key, value, ignore_abort=False): ch = ConfigHolder(config={'foo': None}) ch.set('ignoreAbort', ignore_abort) client = Client(ch) client.setRuntimeParameter(key, value)
class TestBaseLive(unittest.TestCase): cin = '' node_instances = {} # of NodeInstance() multiplicity = 0 max_iaas_workers = 1 def construct_key(self, name): return self.cin + '.' + name def _conf_val(self, key, default=None): conf_key = self.construct_key(key) if default: return self.ch.config.get(conf_key, default) return self.ch.config[conf_key] def _build_user_info(self, keys): self.user_info = UserInfo(self.cin) self.user_info['General.' + UserInfo.SSH_PUBKEY_KEY] = self.ch.config[ 'General.ssh.public.key'] for k in keys: self.user_info[self.construct_key(k)] = self._conf_val(k) def _load_config(self, conf_file): if not os.path.exists(conf_file): raise Exception('Configuration file %s not found.' % conf_file) self.ch = ConfigHolder(configFile=conf_file, context={'foo': 'bar'}) self.ch.set(KEY_RUN_CATEGORY, '') def _build_client(self, testedCls): testedCls._publish_vm_info = publish_vm_info # pylint: disable=protected-access self.client = testedCls(self.ch) def _get_ex_msg(self, ex): if hasattr(ex, 'message'): return ex.message if hasattr(ex, 'arg'): return ex.arg return '' def _setUp(self, testedCls, conf_file, conf_keys): """(Re-)sets the following fields self.ch - ConfigHolder self.client - instance of BaseCloudConnector self.user_info - UserInfo self.multiplicity - int self.max_iaas_workers - str """ os.environ['SLIPSTREAM_CONNECTOR_INSTANCE'] = self.cin os.environ['SLIPSTREAM_BOOTSTRAP_BIN'] = 'http://example.com/bootstrap' os.environ['SLIPSTREAM_DIID'] = '00000000-0000-0000-0000-000000000000' self._load_config(conf_file) self._build_client(testedCls) self._build_user_info(conf_keys) pp(self.user_info) self.multiplicity = int(self._conf_val('multiplicity', 2)) self.max_iaas_workers = self._conf_val('max.iaas.workers', str(self.multiplicity)) def _test_start_stop_images(self): "Live test that starts and stops VMs on a cloud." self.client.run_category = RUN_CATEGORY_DEPLOYMENT success = True error = '' try: self.client.start_nodes_and_clients(self.user_info, self.node_instances) vms = self.client.get_vms() assert len(vms) == self.multiplicity util.printAction('Instances started.') pp(vms) except Exception as ex: success = False error = self._get_ex_msg(ex) util.printError("Exception caught while starting instances!") exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) finally: util.printAction("Stopping deployment.") self.client.stop_deployment() self.assertEquals(success, True, error)