def test_update_flow_dns_exception(self): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') domains_new = domain.Domain(domain='mycdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_old = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') service_new = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_new], origins=[current_origin], flavor_id='cdn') kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'auth_token': json.dumps(str(uuid.uuid4())), 'service_id': json.dumps(service_id), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'service_old': json.dumps(service_old.to_dict()), 'service_obj': json.dumps(service_new.to_dict()), 'context_dict': context_utils.RequestContext().to_dict() } (service_controller, storage_controller, dns_controller, ssl_cert_controller) = self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, ssl_cert_controller, memoized_controllers.task_controllers): self.patch_update_flow(service_controller, storage_controller, dns_controller) dns_controller.update = mock.Mock() dns_controller.update._mock_return_value = { 'cdn_provider': { 'error': 'Whoops!', 'error_class': 'tests.unit.distributed_task' '.taskflow.test_flows.DNSException' } } engines.run(update_service.update_service(), store=kwargs)
def test_update_flow_dns_exception_with_retry_and_succeed( self, mock_creds): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') domains_new = domain.Domain(domain='mycdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_old = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') service_new = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_new], origins=[current_origin], flavor_id='cdn') kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'auth_token': json.dumps(str(uuid.uuid4())), 'service_id': json.dumps(service_id), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'service_old': json.dumps(service_old.to_dict()), 'service_obj': json.dumps(service_new.to_dict()), 'context_dict': context_utils.RequestContext().to_dict() } service_controller, storage_controller, dns_controller = \ self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, memoized_controllers.task_controllers): self.patch_update_flow(service_controller, storage_controller, dns_controller) dns_controller.update = mock.Mock() dns_responder_returns = self.dns_exceptions_and_succeed() dns_controller.update._mock_side_effect = ( dns_responder for dns_responder in dns_responder_returns) engines.run(update_service.update_service(), store=kwargs)
def format_result(result): service_id = result.get('service_id') name = str(result.get('service_name')) origins = [o for o in result.get('origins', [])] domains = [d for d in result.get('domains', [])] origins = [origin.Origin(o['origin'], hostheadertype='domain', hostheadervalue='blog.mywebsite.com', port=o.get('port', 80), ssl=o.get('ssl', False)) for o in origins] domains = [domain.Domain(d['domain'], d['protocol'], d['certificate']) for d in domains] flavor_id = result.get('flavor_id') s = service.Service(service_id, name, domains, origins, flavor_id) provider_detail_results = result.get('provider_details') or {} provider_details_dict = {} for provider_name in provider_detail_results: provider_detail_dict = json.loads( provider_detail_results[provider_name]) provider_service_id = provider_detail_dict.get('id', None) access_urls = provider_detail_dict.get('access_urls', []) status = provider_detail_dict.get('status', u'unknown') provider_detail_obj = provider_details.ProviderDetail( provider_service_id=provider_service_id, access_urls=access_urls, status=status) provider_details_dict[provider_name] = provider_detail_obj s.provider_details = provider_details_dict return s
def test_services_action_with_domain_existing_domain(self): project_id = str(uuid.uuid4()) service_id = str(uuid.uuid4()) domains = domain.Domain(domain='happy.strawberries.com') current_origin = origin.Origin(origin='poppy.org') mock_service = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains], origins=[current_origin], flavor_id='cdn', project_id=project_id) DefaultServicesController.get_service_by_domain_name = mock.Mock( return_value=mock_service) response = self.app.post('/v1.0/admin/services/action', params=json.dumps({ 'action': 'enable', 'domain': 'happy.strawberries.com' }), headers={ 'Content-Type': 'application/json', 'X-Project-ID': project_id }, expect_errors=True) self.assertEqual(response.status_code, 202)
def test_delete_flow_normal(self, mock_creds, mock_dns_client): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_obj = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'service_id': json.dumps(service_id), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'provider_details': json.dumps( dict([(k, v.to_dict()) for k, v in service_obj.provider_details.items()])) } service_controller, storage_controller, dns_controller = \ self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, memoized_controllers.task_controllers): self.patch_delete_flow(service_controller, storage_controller, dns_controller) engines.run(delete_service.delete_service(), store=kwargs)
def test_update_remove_domains_provider_error(self): domains_new = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com'), domain.Domain('pictures.domain.com') ] service_new = service.Service(service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'error': 'Create service failed' } }] dns_details = self.controller.update(self.service_old, service_new, responders) access_urls_map = {} for provider_name in dns_details: access_urls_map[provider_name] = {} access_urls_list = dns_details[provider_name]['access_urls'] for access_urls in access_urls_list: access_urls_map[provider_name][access_urls['domain']] = ( access_urls['operator_url']) for responder in responders: for provider_name in responder: for domain_old in self.domains_old: self.assertIsNotNone( access_urls_map[provider_name][domain_old.domain])
def test_create_service(self): service_name = 'NewService' myorigins = [] mydomains = [] myorigins.append(origin.Origin('mysite.com')) myorigins.append(origin.Origin('yoursite.io', port=80, ssl=True)) mydomains.append(domain.Domain('oursite.org')) mydomains.append(domain.Domain('wiki.cc')) myservice = service.Service(service_name, mydomains, myorigins) # test all properties # name self.assertEqual(myservice.name, service_name) self.assertRaises(AttributeError, setattr, myservice, 'name', service_name) # domains self.assertEqual(myservice.domains, mydomains) self.assertRaises(AttributeError, setattr, myservice, 'domains', []) # origins self.assertEqual(myservice.origins, myorigins) self.assertRaises(AttributeError, setattr, myservice, 'origins', [])
def load_from_json(json_data): service_id = json_data.get('service_id', uuid.uuid4()) name = json_data.get("name") origins = json_data.get("origins", []) domains = json_data.get("domains", []) flavor_id = json_data.get("flavor_id") restrictions = json_data.get("restrictions", []) project_id = json_data.get("project_id") pd = json_data.get("provider_details", {}) log_delivery = json_data.get("log_delivery", {}) # load caching rules json string from input origins = [origin.load_from_json(o) for o in origins] domains = [domain.load_from_json(d) for d in domains] restrictions = [restriction.load_from_json(r) for r in restrictions] log_delivery = ld.load_from_json(log_delivery) # convert caching rule json string list into object list caching = json_data.get("caching", []) caching = [cachingrule.load_from_json(c) for c in caching] r = service.Service(service_id=service_id, name=name, domains=domains, origins=origins, flavor_id=flavor_id, caching=caching, restrictions=restrictions, log_delivery=log_delivery, project_id=project_id) r.provider_details = dict([(k, provider_details.load_from_json(v)) for k, v in pd.items()]) return r
def test_post_job_positive(self, job_tuple): job_type, cert_type = job_tuple # mock ssl storage returning a cert self.mock_storage.certificates_controller.\ get_certs_by_domain.return_value = [ mock.Mock() ] # mock service storage returning a service with domain with # correct protocol + cert self.mock_storage.services_controller. \ get_service_details_by_domain_name.return_value = service.Service( 'service_id', 'name', [ domain.Domain( "www.example.com", protocol='https', certificate=cert_type ) ], [], 'flavor_id', project_id='project_id' ) san_mapping_queue = self.manager_driver.providers[ 'akamai'].obj.san_mapping_queue cert_key = ('san cert' if cert_type == 'san' else 'sni_cert') san_mapping_queue.traverse_queue.return_value = [ json.dumps({ "domain_name": "www.example.com", "flavor_id": "flavor_id", "project_id": "project_id", "cert_type": cert_type, "cert_details": { "Akamai": { "extra_info": { cert_key: "{0}.example.com".format(cert_type), "akamai_spsId": 1 } } }, 'property_activated': (True if job_type == "akamai_check_and_update_cert_status" else False) }) ] run_list, ignore_list = self.bgc.post_job(job_type, {'project_id': 'project_id'}) self.assertEqual(1, len(run_list)) self.assertEqual(0, len(ignore_list)) self.assertTrue( self.bgc.distributed_task_controller.submit_task.called)
def test_update_add_domains(self): subdomain = mock.Mock() subdomain.add_records = mock.Mock() self.client.find = mock.Mock(return_value=subdomain) domains_new = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com'), domain.Domain('pictures.domain.com') ] service_new = service.Service(service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [{ 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'pictures.domain.com', 'href': u'pictures.domain.com.global.prod.fastly.net', 'rel': 'access_url', 'certificate': 'san', 'old_operator_url': 'old.operator.url.cdn99.mycdn.com' }] } }] dns_details = self.controller.update(self.service_old, service_new, responders) access_urls_map = {} for provider_name in dns_details: access_urls_map[provider_name] = {} access_urls_list = dns_details[provider_name]['access_urls'] for access_urls in access_urls_list: access_urls_map[provider_name][access_urls['domain']] = ( access_urls['operator_url']) for responder in responders: for provider_name in responder: for domain_new in domains_new: self.assertIsNotNone( access_urls_map[provider_name][domain_new.domain])
def test_update_add_domains_keeps_log_delivery(self): subdomain = mock.Mock() subdomain.add_records = mock.Mock() self.client.find = mock.Mock(return_value=subdomain) domains_new = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com'), domain.Domain('pictures.domain.com') ] service_new = service.Service( service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard', log_delivery=log_delivery.LogDelivery(enabled=True)) self.service_old.log_delivery = log_delivery.LogDelivery(enabled=True) responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [{ 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'pictures.domain.com', 'href': u'pictures.domain.com.global.prod.fastly.net', 'rel': 'access_url' }] } }] dns_details = self.controller.update(self.service_old, service_new, responders) access_urls_map = {} for provider_name in dns_details: access_urls_map[provider_name] = {} access_urls_list = dns_details[provider_name]['access_urls'] for access_urls in access_urls_list: if 'operator_url' in access_urls: access_urls_map[provider_name][access_urls['domain']] = ( access_urls['operator_url']) if 'log_delivery' in access_urls: for ld_url in access_urls['log_delivery']: self.assertIsNotNone(ld_url['internalURL']) self.assertIsNotNone(ld_url['publicURL']) for responder in responders: for provider_name in responder: for domain_new in domains_new: self.assertIsNotNone( access_urls_map[provider_name][domain_new.domain])
def test_service_state_flow_dns_exception(self, mock_creds): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_obj = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') enable_kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'state': 'enable', 'service_obj': json.dumps(service_obj.to_dict()), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'context_dict': context_utils.RequestContext().to_dict() } disable_kwargs = enable_kwargs.copy() disable_kwargs['state'] = 'disable' service_controller, storage_controller, dns_controller = \ self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, memoized_controllers.task_controllers): self.patch_service_state_flow(service_controller, storage_controller, dns_controller) dns_controller.enable = mock.Mock() dns_controller.enable._mock_return_value = { 'cdn_provider': { 'error': 'Whoops!', 'error_class': 'tests.unit.distributed_task' '.taskflow.test_flows.DNSException' } } dns_controller.disable = mock.Mock() dns_controller.disable._mock_return_value = { 'cdn_provider': { 'error': 'Whoops!', 'error_class': 'tests.unit.distributed_task' '.taskflow.test_flows.DNSException' } } engines.run(update_service_state.enable_service(), store=enable_kwargs) engines.run(update_service_state.disable_service(), store=disable_kwargs)
def test_set_valid_status(self, status): myservice = service.Service(self.service_id, self.service_name, self.mydomains, self.myorigins, self.flavor_id) myservice.status = status self.assertEqual(myservice.status, status)
def setUp(self): super(TestServicesUpdate, self).setUp() pyrax_cloud_dns_patcher = mock.patch('pyrax.cloud_dns') pyrax_cloud_dns_patcher.start() self.addCleanup(pyrax_cloud_dns_patcher.stop) pyrax_set_credentials_patcher = mock.patch('pyrax.set_credentials') pyrax_set_credentials_patcher.start() self.addCleanup(pyrax_set_credentials_patcher.stop) pyrax_set_setting_patcher = mock.patch('pyrax.set_setting') pyrax_set_setting_patcher.start() self.addCleanup(pyrax_set_setting_patcher.stop) rs_options_patcher = mock.patch.object( driver, 'RACKSPACE_OPTIONS', new=RACKSPACE_OPTIONS ) rs_options_patcher.start() self.addCleanup(rs_options_patcher.stop) self.client = mock.Mock() provider = driver.DNSProvider(self.conf) self.controller = provider.services_controller self.controller.client = self.client self.domains_old = [domain.Domain('test.domain.com'), domain.Domain('blog.domain.com')] self.origins_old = [] fastly_access_urls_old = [ { u'provider_url': u'test.domain.com.global.prod.fastly.net', u'domain': u'test.domain.com', u'operator_url': u'test.domain.com.cdn80.mycdn.com' }, { u'provider_url': u'blog.domain.com.global.prod.fastly.net', u'domain': u'blog.domain.com', u'operator_url': u'blog.domain.com.cdn80.mycdn.com' }] fastly_provider_details_old = mock.Mock() fastly_provider_details_old.access_urls = fastly_access_urls_old provider_details_old = { 'Fastly': fastly_provider_details_old } self.service_old = service.Service(service_id=uuid.uuid4(), name='myservice', domains=self.domains_old, origins=self.origins_old, flavor_id='standard') self.service_old.provider_details = provider_details_old
def test_set_invalid_status(self, status): myservice = service.Service( self.service_id, self.service_name, self.mydomains, self.myorigins, self.flavor_id) self.assertRaises(ValueError, setattr, myservice, 'status', status)
def get_service_details_by_domain_name(self, domain_name, project_id=None): r = service.Service( str(uuid.uuid4()), str(uuid.uuid4()), [domain.Domain('wiki.cc', 'https', 'shared')], [origin.Origin('mysite.com')], "strawberry") r.provider_details = MockProviderDetails(r.service_id) return r
def test_update_flow_normal(self): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') domains_new = domain.Domain(domain='mycdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_old = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') service_new = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_new], origins=[current_origin], flavor_id='cdn') kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'auth_token': json.dumps(str(uuid.uuid4())), 'service_id': json.dumps(service_id), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'service_old': json.dumps(service_old.to_dict()), 'service_obj': json.dumps(service_new.to_dict()), 'context_dict': context_utils.RequestContext().to_dict() } (service_controller, storage_controller, dns_controller, ssl_cert_controller) = self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, ssl_cert_controller, memoized_controllers.task_controllers): self.patch_update_flow(service_controller, storage_controller, dns_controller) engines.run(update_service.update_service(), store=kwargs)
def test_update_remove_domains_with_subdomain_not_found_exception(self): subdomain = mock.Mock() subdomain.add_records = mock.Mock() client = mock.Mock() client.find = mock.Mock( side_effect=exc.NotFound('Subdomain not found')) records = [mock.Mock(), mock.Mock()] client.search_records = mock.Mock(return_value=records) self.controller.client = client domains_new = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com') ] service_updates = service.Service( service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [{ 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }] } }] dns_details = self.controller.update(self.service_old, service_updates, responders) access_urls_map = {} for provider_name in dns_details: access_urls_map[provider_name] = {} access_urls_list = dns_details[provider_name]['access_urls'] for access_urls in access_urls_list: if 'operator_url' in access_urls: access_urls_map[provider_name][access_urls['domain']] = ( access_urls['operator_url']) for responder in responders: for provider_name in responder: for domain_new in domains_new: self.assertIsNotNone( access_urls_map[provider_name][domain_new.domain])
def test_post_job_domain_type_modified_on_service(self, cert_type): self.mock_storage.certificates_controller.\ get_certs_by_domain.return_value = [ mock.Mock() ] # simulate domain being changed from https+san to http self.mock_storage.services_controller. \ get_service_details_by_domain_name.return_value = service.Service( 'service_id', 'name', [ domain.Domain( "www.example.com", protocol='http', ) ], [], 'flavor_id', project_id='project_id' ) cert_key = ('san cert' if cert_type == 'san' else 'sni_cert') san_mapping_queue = self.manager_driver.providers[ 'akamai'].obj.san_mapping_queue san_mapping_queue.traverse_queue.return_value = [ json.dumps({ "domain_name": "www.example.com", "flavor_id": "flavor_id", "project_id": "project_id", "cert_type": cert_type, "cert_details": { "Akamai": { "extra_info": { cert_key: "san.example.com", "akamai_spsId": 1 } } }, 'property_activated': True }) ] run_list, ignore_list = self.bgc.post_job( "akamai_update_papi_property_for_mod_{0}".format(cert_type), {'project_id': 'project_id'} ) self.assertEqual(0, len(run_list)) self.assertEqual(1, len(ignore_list)) self.assertEqual( False, self.bgc.distributed_task_controller.submit_task.called )
def test_update_add_domains_with_dns_exception(self): subdomain = mock.Mock() subdomain.add_records = mock.Mock() client = mock.Mock() client.find = mock.Mock( side_effect=Exception('DNS Exception')) self.controller.client = client domains_new = [domain.Domain('test.domain.com'), domain.Domain('blog.domain.com'), domain.Domain('pictures.domain.com')] service_updates = service.Service( service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [ { 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'pictures.domain.com', 'href': u'pictures.domain.com.global.prod.fastly.net', 'rel': 'access_url' } ]} }] dns_details = self.controller.update(self.service_old, service_updates, responders) for responder in responders: for provider_name in responder: self.assertIsNotNone(dns_details[provider_name]['error']) self.assertIsNotNone( dns_details[provider_name]['error_detail']) self.assertIsNotNone( dns_details[provider_name]['error_class'] )
def format_result(result): service_id = result.get('service_id') name = str(result.get('service_name')) origins = [o for o in result.get('origins', [])] domains = [d for d in result.get('domains', [])] origins = [ origin.Origin(o['origin'], hostheadertype='domain', hostheadervalue='blog.mywebsite.com', port=o.get('port', 80), ssl=o.get('ssl', False)) for o in origins ] domains = [ domain.Domain(d['domain'], d['protocol'], d['certificate']) for d in domains ] restrictions = [ restriction.Restriction(r.get('name'), r.get( 'access', 'whitelist'), [ rule.Rule(r_rule.get('name'), referrer=r_rule.get('referrer'), client_ip=r_rule.get('client_ip'), geography=r_rule.get('geography'), request_url=r_rule.get('request_url', "/*") or "/*") for r_rule in r['rules'] ]) for r in result.get('restrictions', []) ] flavor_id = result.get('flavor_id') s = service.Service(service_id, name, domains, origins, flavor_id, restrictions=restrictions) provider_detail_results = result.get('provider_details') or {} provider_details_dict = {} for provider_name in provider_detail_results: provider_detail_dict = json.loads( provider_detail_results[provider_name]) provider_service_id = provider_detail_dict.get('id', None) access_urls = provider_detail_dict.get('access_urls', []) status = provider_detail_dict.get('status', u'unknown') provider_detail_obj = provider_details.ProviderDetail( provider_service_id=provider_service_id, access_urls=access_urls, status=status) provider_details_dict[provider_name] = provider_detail_obj s.provider_details = provider_details_dict return s
def test_init_from_dict_whitespaces(self): caching = [cachingrule.CachingRule('images ', 3600)] restrictions = [restriction.Restriction('client_ip ', 'whitelist')] name = 'tests ' myservice = service.Service( self.service_id, name, self.mydomains, self.myorigins, self.flavor_id, caching, restrictions) cloned_service = service.Service.init_from_dict(self.project_id, myservice.to_dict()) self.assertNotEqual(cloned_service.name, myservice.name.strip()) self.assertNotEqual([x.name for x in cloned_service.caching], [y.name for y in myservice.caching]) self.assertNotEqual([x.name for x in cloned_service.restrictions], [y.name for y in myservice.restrictions])
def test_init_from_dict_method(self): # this should generate a service copy from my service myservice = service.Service( self.service_id, self.service_name, self.mydomains, self.myorigins, self.flavor_id, self.mycaching, self.myrestrictions) cloned_service = service.Service.init_from_dict(self.project_id, myservice.to_dict()) self.assertEqual(cloned_service.service_id, myservice.service_id) self.assertEqual(cloned_service.name, myservice.name) self.assertEqual(cloned_service.flavor_id, myservice.flavor_id) self.assertEqual([domain.to_dict() for domain in cloned_service.domains], [domain.to_dict() for domain in myservice.domains]) self.assertEqual([origin.to_dict() for origin in cloned_service.origins], [origin.to_dict() for origin in myservice.origins]) self.assertEqual([caching.to_dict()['rules'] for caching in cloned_service.caching], [caching.to_dict()['rules'] for caching in myservice.caching]) self.assertEqual([caching.to_dict()['name'] for caching in cloned_service.caching], [caching.to_dict()['name'] for caching in myservice.caching]) self.assertEqual([caching.to_dict()['ttl'] for caching in cloned_service.caching], [caching.to_dict()['ttl'] for caching in myservice.caching]) self.assertEqual([restrictions.to_dict()['rules'] for restrictions in cloned_service.restrictions], [restrictions.to_dict()['rules'] for restrictions in myservice.restrictions]) self.assertEqual([restrictions.to_dict()['name'] for restrictions in cloned_service.restrictions], [restrictions.to_dict()['name'] for restrictions in myservice.restrictions])
def test_update_add_domains_https_upgrade_regex_exception(self, re_mock): re_mock.return_value.groups.return_value = (None, ) subdomain = mock.Mock() subdomain.add_records = mock.Mock() self.client.find = mock.Mock(return_value=subdomain) domains_new = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com') ] self.service_old.domains = domains_new service_new = service.Service(service_id=self.service_old.service_id, name='myservice', domains=domains_new, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [{ 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url', 'certificate': 'san', 'old_operator_url': 'old.operator.url.cdn99.mycdn.com' }] } }] dns_details = self.controller.update(self.service_old, service_new, responders) self.assertTrue('error' in dns_details['Fastly']) self.assertTrue('error_detail' in dns_details['Fastly']) self.assertTrue('error_class' in dns_details['Fastly']) self.assertTrue('ValueError' in dns_details['Fastly']['error_class'])
def test_create(self): myservice = service.Service(self.service_id, self.service_name, self.mydomains, self.myorigins, self.flavor_id, self.mycaching, self.myrestrictions, self.log_delivery) # test all properties # id self.assertEqual(myservice.service_id, self.service_id) # name self.assertEqual(myservice.name, self.service_name) changed_service_name = 'ChangedServiceName' myservice.name = changed_service_name self.assertEqual(myservice.name, changed_service_name) # flavor_id # self.assertEqual(myservice.flavor_id, self.flavor_id) # domains self.assertEqual(myservice.domains, self.mydomains) self.assertEqual(myservice.domains[0].protocol, 'http') self.assertEqual(myservice.domains[1].protocol, 'https') myservice.domains = [] self.assertEqual(myservice.domains, []) # origins self.assertEqual(myservice.origins, self.myorigins) myservice.origins = [] self.assertEqual(myservice.origins, []) # flavor_id self.assertEqual(myservice.flavor_id, self.flavor_id) myservice.flavor_id = "standard" self.assertEqual(myservice.flavor_id, "standard") self.assertEqual(myservice.restrictions, self.myrestrictions) myservice.restrictions = [] self.assertEqual(myservice.restrictions, []) # caching rules self.assertEqual(myservice.caching, self.mycaching) myservice.caching = [] self.assertEqual(myservice.caching, []) # status self.assertEqual(myservice.status, u'create_in_progress')
def test_service_state_flow_dns_exception_retry_and_succeed(self): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_obj = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') enable_kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'state': 'enable', 'service_obj': json.dumps(service_obj.to_dict()), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'context_dict': context_utils.RequestContext().to_dict() } disable_kwargs = enable_kwargs.copy() disable_kwargs['state'] = 'disable' (service_controller, storage_controller, dns_controller, ssl_cert_controller) = self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, ssl_cert_controller, memoized_controllers.task_controllers): self.patch_service_state_flow(service_controller, storage_controller, dns_controller) dns_responder_returns = self.dns_exceptions_and_succeed() dns_controller.enable._mock_side_effect = ( dns_responder for dns_responder in dns_responder_returns) dns_controller.disable._mock_side_effect = ( dns_responder for dns_responder in dns_responder_returns) engines.run(update_service_state.enable_service(), store=enable_kwargs) engines.run(update_service_state.disable_service(), store=disable_kwargs)
def test_delete_flow_dns_exception(self, mock_creds, mock_dns_client): service_id = str(uuid.uuid4()) domains_old = domain.Domain(domain='cdn.poppy.org') current_origin = origin.Origin(origin='poppy.org') service_obj = service.Service(service_id=service_id, name='poppy cdn service', domains=[domains_old], origins=[current_origin], flavor_id='cdn') kwargs = { 'project_id': json.dumps(str(uuid.uuid4())), 'service_id': json.dumps(service_id), 'time_seconds': [i * self.time_factor for i in range(self.total_retries)], 'provider_details': json.dumps( dict([(k, v.to_dict()) for k, v in service_obj.provider_details.items()])) } service_controller, storage_controller, dns_controller = \ self.all_controllers() with MonkeyPatchControllers(service_controller, dns_controller, storage_controller, memoized_controllers.task_controllers): self.patch_delete_flow(service_controller, storage_controller, dns_controller) dns_controller.delete = mock.Mock() dns_controller.delete._mock_return_value = { 'cdn_provider': { 'error': 'Whoops!', 'error_class': 'tests.unit.distributed_task' '.taskflow.test_flows.DNSException' } } engines.run(delete_service.delete_service(), store=kwargs)
def test_update_add_domains_with_no_domains_in_update(self): subdomain = mock.Mock() subdomain.add_records = mock.Mock() client = mock.Mock() self.controller.client = client service_updates = service.Service( service_id=self.service_old.service_id, name='myservice', domains=[], origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [{ 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'pictures.domain.com', 'href': u'pictures.domain.com.global.prod.fastly.net', 'rel': 'access_url' }] } }] dns_details = self.controller.update(self.service_old, service_updates, responders) access_urls_map = {} for provider_name in self.service_old.provider_details: provider_detail = self.service_old.provider_details[provider_name] access_urls = provider_detail.access_urls access_urls_map[provider_name] = {'access_urls': access_urls} self.assertEqual(access_urls_map, dns_details)
def test_update_same_domains(self): service_updates = service.Service( service_id=self.service_old.service_id, name='myservice', domains=self.domains_old, origins=[], flavor_id='standard') responders = [{ 'Fastly': { 'id': str(uuid.uuid4()), 'links': [ { 'domain': u'blog.domain.com', 'href': u'blog.domain.com.global.prod.fastly.net', 'rel': 'access_url' }, { 'domain': u'test.domain.com', 'href': u'test.domain.com.global.prod.fastly.net', 'rel': 'access_url' } ]} }] dns_details = self.controller.update(self.service_old, service_updates, responders) access_urls_map = {} for provider_name in dns_details: access_urls_map[provider_name] = {} access_urls_list = dns_details[provider_name]['access_urls'] for access_urls in access_urls_list: access_urls_map[provider_name][access_urls['domain']] = ( access_urls['operator_url']) for responder in responders: for provider_name in responder: for domain_old in self.domains_old: self.assertIsNotNone( access_urls_map[provider_name][domain_old.domain])
def setUp(self, mock_set_credentials): super(TestServicesUpdate, self).setUp() self.client = mock.Mock() provider = driver.DNSProvider(self.conf) self.controller = provider.services_controller self.controller.client = self.client self.domains_old = [ domain.Domain('test.domain.com'), domain.Domain('blog.domain.com') ] self.origins_old = [] fastly_access_urls_old = [{ u'provider_url': u'test.domain.com.global.prod.fastly.net', u'domain': u'test.domain.com', u'operator_url': u'test.domain.com.cdn80.mycdn.com' }, { u'provider_url': u'blog.domain.com.global.prod.fastly.net', u'domain': u'blog.domain.com', u'operator_url': u'blog.domain.com.cdn80.mycdn.com' }] fastly_provider_details_old = mock.Mock() fastly_provider_details_old.access_urls = fastly_access_urls_old provider_details_old = {'Fastly': fastly_provider_details_old} self.service_old = service.Service(service_id=uuid.uuid4(), name='myservice', domains=self.domains_old, origins=self.origins_old, flavor_id='standard') self.service_old.provider_details = provider_details_old