def test_creation_validation_external_resource(self): """ must run without any errors and check with empty server description """ # unknow resource_id fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={'use_external_resource': True}, provider_context={}) with mock.patch('vcloud_plugin_common.VcloudAirClient', self.generate_vca()): with self.assertRaises(cfy_exc.NonRecoverableError): server.creation_validation(ctx=fake_ctx) # with resource_id fake_ctx = cfy_mocks.MockCloudifyContext(node_id='test', node_name='test', properties={ 'use_external_resource': True, 'resource_id': 'ServerName' }, provider_context={}) with mock.patch('vcloud_plugin_common.VcloudAirClient', self.generate_vca()): server.creation_validation(ctx=fake_ctx)
def setUp(self): super(VolumeTestCase, self).setUp() self.volume_test_dict = self.test_config['volume'] name = 'volume' self.properties = { 'volume': { 'name': self.volume_test_dict['name'], 'size': self.volume_test_dict['size'] }, 'use_external_resource': True, 'resource_id': self.volume_test_dict['name_exists'], 'vcloud_config': self.vcloud_config } self.target = MockCloudifyContext( node_id="target", properties={'vcloud_config': self.vcloud_config}, runtime_properties={VCLOUD_VAPP_NAME: self.test_config['test_vm']}) self.source = MockCloudifyContext(node_id="source", properties=self.properties) self.nodectx = cfy_mocks.MockCloudifyContext( node_id=name, node_name=name, properties=self.properties) self.relationctx = cfy_mocks.MockCloudifyContext(node_id=name, node_name=name, target=self.target, source=self.source) self.ctx = self.nodectx ctx_patch1 = mock.patch('server_plugin.volume.ctx', self.nodectx) ctx_patch2 = mock.patch('vcloud_plugin_common.ctx', self.nodectx) ctx_patch1.start() ctx_patch2.start() self.addCleanup(ctx_patch1.stop) self.addCleanup(ctx_patch2.stop)
def test_create_volume(self): fake_client = self.generate_client() # external resource fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={ 'use_external_resource': True, 'vcloud_config': { 'vdc': 'vdc_name' } } ) with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): volume.create_volume(ctx=fake_ctx) # fail on create volume fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={ 'use_external_resource': False, 'volume': { 'name': 'some-other', 'size': 11 }, 'vcloud_config': { 'vdc': 'vdc_name' } } ) with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): with self.assertRaises(cfy_exc.NonRecoverableError): volume.create_volume(ctx=fake_ctx) fake_client.add_disk.assert_called_with( 'vdc_name', 'some-other', 11534336 ) # positive case disk = mock.Mock() disk.get_Tasks = mock.MagicMock( return_value=[self.generate_task( vcloud_plugin_common.TASK_STATUS_SUCCESS )] ) fake_client.add_disk = mock.MagicMock( return_value=(True, disk) ) with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): volume.create_volume(ctx=fake_ctx)
def _setup_floating_ip(self): self.fip_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={}, target=cfy_mocks.MockCloudifyContext( node_id="target", properties={'floatingip': self.test_config['floatingip']}), source=cfy_mocks.MockCloudifyContext( node_id="source", properties={'vcloud_config': self.vcloud_config}, runtime_properties={server.VCLOUD_VAPP_NAME: self.server_name}))
def test_creation_validation_external_resource(self): fake_client = self.generate_client() fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={ 'use_external_resource': True, 'vcloud_config': { 'vdc': 'vdc_name' } } ) # use external without resorse_id with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): with self.assertRaises(cfy_exc.NonRecoverableError): volume.creation_validation(ctx=fake_ctx) fake_client.get_disks.assert_called_with('vdc_name') # with resource id, but without disks(no disks for this client) fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={ 'use_external_resource': True, 'resource_id': 'some', 'vcloud_config': { 'vdc': 'vdc_name' } } ) with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): with self.assertRaises(cfy_exc.NonRecoverableError): volume.creation_validation(ctx=fake_ctx) # good case for external resource fake_client.get_disks = mock.MagicMock(return_value=[ [ self.generate_fake_client_disk('some'), self.generate_fake_vms_disk('some') ] ]) with mock.patch( 'vcloud_plugin_common.VcloudAirClient.get', mock.MagicMock(return_value=fake_client) ): volume.creation_validation(ctx=fake_ctx)
def _regen_relationship_ctx(self): # source source_instance = mock.Mock() source_instance.runtime_properties = {} source = cfy_mocks.MockCloudifyContext() source._instance = source_instance # target target_instance = mock.Mock() target_instance.runtime_properties = {} target = cfy_mocks.MockCloudifyContext() target._instance = target_instance # create context self.fake_ctx = cfy_mocks.MockCloudifyContext(target=target, source=source) current_ctx.set(self.fake_ctx)
def base_test_management(agent_ssl_cert, tmp_path): # Mock the context for all tests original_ctx = current_ctx current_ctx.set( mocks.MockCloudifyContext(tenant={'name': 'default_tenant'})) # Make sure the right env vars are available for the agent agent_env_vars = { constants.MANAGER_FILE_SERVER_URL_KEY: '127.0.0.1', constants.REST_HOST_KEY: '127.0.0.1', constants.REST_PORT_KEY: '80', constants.BROKER_SSL_CERT_PATH: agent_ssl_cert.local_cert_path(), constants.LOCAL_REST_CERT_FILE_KEY: agent_ssl_cert.local_cert_path(), constants.MANAGER_FILE_SERVER_ROOT_KEY: '127.0.0.1/resources' } for key, value in agent_env_vars.items(): os.environ[key] = value old_path = os.getcwd() os.chdir(str(tmp_path)) with patch( 'cloudify_agent.shell.commands.daemons.api_utils.internal.' 'generate_agent_name', new=random_id, ): yield current_ctx # Un-mock the context current_ctx.set(original_ctx) os.chdir(old_path)
def setUp(self): print "\nUsed config: {0}".format(self.service_type) fake_ctx = cfy_mocks.MockCloudifyContext(node_id='test', node_name='test', properties={}) with mock.patch('vcloud_plugin_common.ctx', fake_ctx): self.vca_client = VcloudAirClient().get(config=self.vcloud_config)
def setUp(self): super(VsphereStorageTest, self).setUp() self.logger.debug("\nStorage test started\n") name = self.name_prefix + 'stor' vm_name = storage_config['vm_name'] storage_size = int(storage_config['storage_size']) server_client = vpc.ServerClient().get() vm = server_client.get_server_by_name(vm_name) capability = {server_plugin.VSPHERE_SERVER_ID: vm._moId} endpoint = None instance = None context_capabilities_m = context.ContextCapabilities( endpoint, instance) get_all_m = mock.Mock() get_all_m.values = mock.Mock(return_value=[capability]) context_capabilities_m.get_all = mock.Mock(return_value=get_all_m) self.ctx = mocks.MockCloudifyContext( node_id=name, node_name=name, properties={'storage': { 'storage_size': storage_size }}, capabilities=context_capabilities_m) ctx_patch1 = mock.patch('storage_plugin.storage.ctx', self.ctx) ctx_patch1.start() self.addCleanup(ctx_patch1.stop) ctx_patch2 = mock.patch('vsphere_plugin_common.ctx', self.ctx) ctx_patch2.start() self.addCleanup(ctx_patch2.stop)
def _create_agent(self): old_agent = { 'local': False, 'remote_execution': False, 'ip': '10.0.4.47', 'rest_host': '10.0.4.46', 'distro': 'ubuntu', 'distro_codename': 'trusty', 'basedir': '/home/vagrant', 'user': '******', 'key': '~/.ssh/id_rsa', 'windows': False, 'package_url': 'http://10.0.4.46:53229/packages/agents/' 'ubuntu-trusty-agent.tar.gz', 'version': '3.4', 'broker_config': { 'broker_ip': '10.0.4.46', 'broker_pass': '******', 'broker_user': '******', 'broker_port': '5672', 'broker_ssl_enable': False, 'broker_ssl_cert': '' } } operation_ctx = mocks.MockCloudifyContext( runtime_properties={'rest_token': 'token1234'}) old_context = ctx configuration.prepare_connection(old_agent) current_ctx.set(operation_ctx) configuration.prepare_agent(old_agent, None) current_ctx.set(old_context) return old_agent
def setUp(self): super(VsphereServerTest, self).setUp() name = self.name_prefix + 'server' self.ctx = mocks.MockCloudifyContext( node_id=name, node_name=name, properties={ 'networking': server_config["networking"], 'server': { 'template': server_config['template'], 'cpus': server_config['cpu_count'], 'memory': server_config['memory_in_mb'] }, 'connection_config': { 'datacenter_name': server_config['datacenter_name'], 'resource_pool_name': server_config['resource_pool_name'], 'auto_placement': server_config['auto_placement'] } }, bootstrap_context=mock.Mock()) ctx_patch1 = mock.patch('server_plugin.ctx', self.ctx) ctx_patch1.start() self.addCleanup(ctx_patch1.stop) ctx_patch2 = mock.patch('vsphere_plugin_common.ctx', self.ctx) ctx_patch2.start() self.addCleanup(ctx_patch2.stop)
def test_delete(self): volume_id = '00000000-0000-0000-0000-000000000000' volume_name = 'test-volume' volume_properties = { 'use_external_resource': False, } cinder_client_m = mock.Mock() cinder_client_m.cosmo_delete_resource = mock.Mock() ctx_m = cfy_mocks.MockCloudifyContext(node_id='a', properties=volume_properties) ctx_m.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = volume_id ctx_m.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] = \ volume.VOLUME_OPENSTACK_TYPE ctx_m.instance.runtime_properties[OPENSTACK_NAME_PROPERTY] = \ volume_name volume.delete(cinder_client=cinder_client_m, ctx=ctx_m) cinder_client_m.cosmo_delete_resource.assert_called_once_with( volume.VOLUME_OPENSTACK_TYPE, volume_id) self.assertTrue( OPENSTACK_ID_PROPERTY not in ctx_m.instance.runtime_properties) self.assertTrue( OPENSTACK_TYPE_PROPERTY not in ctx_m.instance.runtime_properties) self.assertTrue( OPENSTACK_NAME_PROPERTY not in ctx_m.instance.runtime_properties)
def test_create_use_existing(self): volume_id = '00000000-0000-0000-0000-000000000000' volume_properties = { 'use_external_resource': True, 'device_name': '/dev/fake', 'resource_id': volume_id, } existing_volume_m = mock.Mock() existing_volume_m.id = volume_id existing_volume_m.status = volume.VOLUME_STATUS_AVAILABLE cinder_client_m = mock.Mock() cinder_client_m.volumes = mock.Mock() cinder_client_m.volumes.create = mock.Mock() cinder_client_m.cosmo_get_if_exists = mock.Mock( return_value=existing_volume_m) cinder_client_m.get_id_from_resource = mock.Mock( return_value=volume_id) ctx_m = cfy_mocks.MockCloudifyContext(node_id='a', properties=volume_properties) volume.create(cinder_client=cinder_client_m, ctx=ctx_m) self.assertFalse(cinder_client_m.volumes.create.called) self.assertEqual( volume_id, ctx_m.instance.runtime_properties[OPENSTACK_ID_PROPERTY]) self.assertEqual( volume.VOLUME_OPENSTACK_TYPE, ctx_m.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY])
def _set_context(self, host='localhost'): old_context = ctx try: os.environ[constants.MANAGER_FILE_SERVER_ROOT_KEY] = \ self.temp_folder os.environ[constants.MANAGER_NAME] = 'cloudify' properties = {} properties['cloudify_agent'] = self._create_agent() properties['agent_status'] = {'agent_alive_crossbroker': True} mock = mocks.MockCloudifyContext(node_id='host_af231', runtime_properties=properties, node_name='host', properties={'cloudify_agent': {}}, brokers=[{ 'networks': { 'default': host } }], managers=[{ 'networks': { 'default': host }, 'hostname': 'cloudify' }]) current_ctx.set(mock) yield finally: current_ctx.set(old_context)
def get_client(self): fake_ctx = cfy_mocks.MockCloudifyContext(node_id='test', node_name='test', properties={}) with mock.patch('vcloud_plugin_common.ctx', fake_ctx): vca_client = VcloudAirClient().get(config=self.vcloud_config) return vca_client
def test_get_management_network_name_without_properties(self): ''' without name in properties ''' fake_ctx = cfy_mocks.MockCloudifyContext(node_id='test', node_name='test', properties={}) with mock.patch('server_plugin.server.ctx', fake_ctx): with self.assertRaises(cfy_exc.NonRecoverableError): server._get_management_network_from_node()
def test_run_one_string(self): fake_netconf = mock.Mock() fake_netconf.send = mock.Mock( side_effect=exceptions.NonRecoverableError("broken connection")) fake_ctx = cfy_mocks.MockCloudifyContext() current_ctx.set(fake_ctx) with self.assertRaises(cfy_exc.NonRecoverableError): rpc._run_one_string(fake_ctx, fake_netconf, "<xml/>", {}, "abc", False, False)
def test_get_management_network_name_in_properties(self): ''' exist some managment network name in properties ''' fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={'management_network': '_management_network'}) with mock.patch('server_plugin.server.ctx', fake_ctx): self.assertEqual('_management_network', server._get_management_network_from_node())
def test_creation_validation_empty_settings(self): fake_ctx = cfy_mocks.MockCloudifyContext(node_id='test', node_name='test', properties={'server': {}}, provider_context={}) with mock.patch('vcloud_plugin_common.VcloudAirClient', self.generate_vca()): with self.assertRaises(cfy_exc.NonRecoverableError): server.creation_validation(ctx=fake_ctx)
def _regen_ctx(self): self.fake_ctx = cfy_mocks.MockCloudifyContext() instance = mock.Mock() instance.runtime_properties = {} self.fake_ctx._instance = instance node = mock.Mock() self.fake_ctx._node = node node.properties = {} node.runtime_properties = {} current_ctx.set(self.fake_ctx)
def generate_relation_context(self): source = mock.Mock() source.node = mock.Mock() target = mock.Mock() target.node = mock.Mock() target.instance.runtime_properties = {} fake_ctx = cfy_mocks.MockCloudifyContext( source=source, target=target ) return fake_ctx
def _get_mock_context_for_run(self): fake_ctx = cfy_mocks.MockCloudifyContext() instance = mock.Mock() instance.runtime_properties = {} fake_ctx._instance = instance node = mock.Mock() fake_ctx._node = node node.properties = {} node.runtime_properties = {} fake_ctx.get_resource = mock.MagicMock(return_value="") return fake_ctx, node, instance
def _create_node_instance_context(self): properties = {} properties['cloudify_agent'] = self._create_agent() properties['agent_status'] = {'agent_alive_crossbroker': True} mock = mocks.MockCloudifyContext( node_id='host_af231', runtime_properties=properties, node_name='host', properties={'cloudify_agent': {}}, bootstrap_context=context.BootstrapContext({'cloudify_agent': {}})) return mock
def _simple_volume_ctx(self): volume_id = '1234-5678' volume_ctx = cfy_mocks.MockCloudifyContext( node_id="node_id", node_name="node_name", properties={}, runtime_properties={ OPENSTACK_ID_PROPERTY: volume_id, } ) current_ctx.set(volume_ctx) return volume_ctx, volume_id
def test_attach(self): volume_id = '00000000-0000-0000-0000-000000000000' server_id = '11111111-1111-1111-1111-111111111111' device_name = '/dev/fake' volume_ctx = cfy_mocks.MockContext({ 'node': cfy_mocks.MockContext( {'properties': { volume.DEVICE_NAME_PROPERTY: device_name }}), 'instance': cfy_mocks.MockContext( {'runtime_properties': { OPENSTACK_ID_PROPERTY: volume_id, }}) }) server_ctx = cfy_mocks.MockContext({ 'node': cfy_mocks.MockContext({'properties': {}}), 'instance': cfy_mocks.MockContext({ 'runtime_properties': { server.OPENSTACK_ID_PROPERTY: server_id } }) }) ctx_m = cfy_mocks.MockCloudifyContext(node_id='a', target=server_ctx, source=volume_ctx) cinderclient_m = mock.Mock() novaclient_m = mock.Mock() novaclient_m.volumes = mock.Mock() novaclient_m.volumes.create_server_volume = mock.Mock() with contextlib.nested( mock.patch.object(NovaClient, 'get', mock.Mock(return_value=novaclient_m)), mock.patch.object(CinderClient, 'get', mock.Mock(return_value=cinderclient_m)), mock.patch.object(volume, 'wait_until_status', mock.Mock(return_value=(None, True)))): server.attach_volume(ctx=ctx_m) novaclient_m.volumes.create_server_volume.assert_called_once_with( server_id, volume_id, device_name) volume.wait_until_status.assert_called_once_with( cinder_client=cinderclient_m, volume_id=volume_id, status=volume.VOLUME_STATUS_IN_USE)
def test_creation_validation_settings(self): fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={'server': { 'catalog': 'public', 'template': 'secret' }}, provider_context={}) with mock.patch('vcloud_plugin_common.VcloudAirClient.get', self.generate_vca()): server.creation_validation(ctx=fake_ctx)
def _get_mock_context_for_run(self): operation = {'name': 'cloudify.interfaces.lifecycle.mock'} fake_ctx = cfy_mocks.MockCloudifyContext(operation=operation) instance = mock.Mock() instance.runtime_properties = {} fake_ctx._instance = instance node = mock.Mock() fake_ctx._node = node node.properties = {} node.runtime_properties = {} node.type_hierarchy = ['ctx.nodes.Root'] fake_ctx.get_resource = mock.MagicMock(return_value="") return fake_ctx, node, instance
def test_creation_validation_settings_wrong_template(self): fake_ctx = cfy_mocks.MockCloudifyContext( node_id='test', node_name='test', properties={'server': { 'catalog': 'public', 'template': 'unknow' }}, provider_context={}) with mock.patch('vcloud_plugin_common.VcloudAirClient.get', self.generate_vca()): with self.assertRaises(cfy_exc.NonRecoverableError): server.creation_validation(ctx=fake_ctx)
def setUp(self): super(ServerWithNetworkTestCase, self).setUp() chars = string.ascii_uppercase + string.digits self.name_prefix = ('plugin_test_{0}_'.format(''.join( random.choice(chars) for _ in range(RANDOM_PREFIX_LENGTH)))) server_test_dict = self.test_config['server'] name = self.name_prefix + 'server' self.network_name = self.test_config['management_network'] port_node_context = cfy_mocks.MockNodeContext( properties={ 'port': { 'network': self.network_name, 'ip_allocation_mode': 'pool', 'primary_interface': True } }) network_node_context = cfy_mocks.MockNodeContext( properties={'network': { 'name': self.network_name }}) self.port_relationship = mock.Mock() self.port_relationship.target = mock.Mock() self.port_relationship.target.node = port_node_context self.network_relationship = mock.Mock() self.network_relationship.target = mock.Mock() self.network_relationship.target.node = network_node_context self.properties = { 'server': { 'name': name, 'catalog': server_test_dict['catalog'], 'template': server_test_dict['template'] }, 'management_network': self.network_name, 'vcloud_config': self.vcloud_config } self.ctx = cfy_mocks.MockCloudifyContext(node_id=name, node_name=name, properties=self.properties) self.ctx.instance.relationships = [] ctx_patch1 = mock.patch('server_plugin.server.ctx', self.ctx) ctx_patch2 = mock.patch('vcloud_plugin_common.ctx', self.ctx) ctx_patch1.start() ctx_patch2.start() self.addCleanup(ctx_patch1.stop) self.addCleanup(ctx_patch2.stop)
def _setup_network(self): network_use_existing = \ self.test_config['combined']['network_use_existing'] existing_network = self.test_config['combined']['network_name'] self.network_name = (existing_network if network_use_existing else self.name_prefix + "network") self.network_ctx = cfy_mocks.MockCloudifyContext( node_id=self.network_name, node_name=self.network_name, properties={ "network": self.test_config['network'], "use_external_resource": network_use_existing, "resource_id": self.network_name, "vcloud_config": self.vcloud_config })