def test_associate(self, pulp, dist_id): if dist_id == 'bar': flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').never()) with pytest.raises(errors.DockPulpConfigError): pulp.associate(dist_id, 'testrepo') else: flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return( None)) response = pulp.associate(dist_id, 'testrepo') assert response is None
def test_disassociate(self, pulp): repo = 'testrepo' dist_id = 'foo' dl = '/pulp/api/v2/repositories/%s/distributors/%s/' % (repo, dist_id) tid = '123' url = '/pulp/api/v2/tasks/%s/' % tid t = {'state': 'finished'} flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'delete', dl).once().and_return(tid)) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).once().and_return(t)) response = pulp.disassociate('foo', 'testrepo') assert response is None
def test_sync(self, pulp): repoinfoold = [{ 'id': 'redhat-foobar', 'images': {}, 'manifests': { '123456': 'foobar' } }] repoinfonew = [{ 'id': 'redhat-foobar', 'images': {}, 'manifests': { '123456': 'foobar', '567890': 'latest' } }] pulp_filter = {'unit': {'$or': [{'digest': '567890'}]}} flexmock(pulp) (pulp.should_receive('listRepos').with_args( repos='redhat-foobar', content=True).twice().and_return(repoinfoold, repoinfonew).one_by_one()) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return('123') ) (pulp.should_receive('watch').with_args('123').once().and_return(None)) (pulp.should_receive('copy_filters').with_args( 'redhat-everything', 'redhat-foobar', pulp_filter).once().and_return(None)) imgs, manifests = pulp.syncRepo(env='syncenv', repo='foobar', feed='fb', upstream_name='foobar') assert imgs == [] assert manifests == ['567890']
def test_updateRedirect(self, pulp, rid, redirect, dist, download, auto_publish): update = { 'redirect-url': redirect, 'rel-url': 'content/', 'auto_publish': auto_publish } blob = [{ 'distributor_type_id': 'testdist', 'id': 'test' }, { 'distributor_type_id': 'docker_rsync_distributor', 'id': 'rsync_test' }] t = {'state': 'finished'} if dist: update.setdefault('notes', {}) update['notes'] = {'distribution': dist} if download is not None: update.setdefault('notes', {}) update['notes']['include_in_download_service'] = download flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, '111', t, '123', '124', t, t).one_by_one()) assert pulp.updateRepo(rid, update) is None
def test_getTaskRetries(self, pulp, tid, url): result = 'task_received' flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).twice().and_raise( requests.ConnectionError).and_return(result)) assert pulp.getTask(tid) == result
def test_checkBlobsFail(self, pulp, repo, blob): blobs = [] blobs.append(blob) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_raise( requests.exceptions.ConnectionError)) response = pulp.checkBlobs(repo, blobs) assert response['error']
def test_checkLayersFail(self, pulp, repo, image): images = [] images.append(image) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_raise( requests.exceptions.ConnectionError)) response = pulp.checkLayers(repo, images) assert response['error']
def test_associate(self, pulp, dist_id, type_id): if dist_id == 'bar': flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').never()) with pytest.raises(errors.DockPulpConfigError): pulp.associate(dist_id, 'testrepo') else: url = '/pulp/api/v2/repositories/testrepo/distributors/' data = { "distributor_type_id": "docker_distributor_web", "distributor_config": {} } if type_id: data['distributor_type_id'] = type_id flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().with_args( 'post', url, data=json.dumps(data)).and_return(None)) response = pulp.associate(dist_id, 'testrepo', type_id) assert response is None
def test_updateRedirect(self, pulp, rid, redirect): update = {'redirect-url': redirect} blob = [] did = {'distributor_type_id': 'testdist', 'id': 'test'} t = {'state': 'finished'} blob.append(did) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, '111', t).one_by_one()) assert pulp.updateRepo(rid, update) is None
def test_checkBlobsMismatch(self, pulp, repo, blob): blobs = [] blobs.append('sha256:%s' % blob) req = testResponse() flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return(req)) flexmock(hashlib) (hashlib.should_receive('sha256').once().and_return(testHash('test'))) response = pulp.checkBlobs(repo, blobs) assert response['error']
def test_updateAutoPublishFail(self, pulp, auto_publish): rid = 'test-repo' update = {'auto_publish': auto_publish} blob = [] did = {'distributor_type_id': 'testdist', 'id': 'test'} t = {'state': 'finished'} blob.append(did) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, '111', t).one_by_one()) with pytest.raises(ValueError): pulp.updateRepo(rid, update)
def test_checkLayers(self, pulp, repo, image): images = [] images.append(image) req = requests.Response() flexmock(req, raw="rawtest") flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return(req)) flexmock(tarfile) (tarfile.should_receive('open').once().and_return(tarfile.TarFile)) flexmock(tarfile.TarFile) (tarfile.TarFile.should_receive('close').once().and_return()) response = pulp.checkLayers(repo, images) assert not response['error']
def test_create_hidden(self, restricted_pulp, repo_id): if repo_id == 'redhat-foo-bar': with pytest.raises(errors.DockPulpError): restricted_pulp.createRepo(repo_id=repo_id, url='/foo/bar', library=True) return flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return(None)) response = restricted_pulp.createRepo(repo_id=repo_id, url='/foo/bar', library=True) assert response['id'] == repo_id
def test_listHistory(self, pulp, repos, content, history, label): blob = { 'notes': { '_repo-type': 'docker-repo' }, 'id': 'testid', 'description': 'testdesc', 'display_name': 'testdisp', 'distributors': [], 'scratchpad': {} } units = [{ 'unit_type_id': 'docker_manifest', 'metadata': { 'fs_layers': [{ 'blob_sum': 'test' }], 'digest': 'testdig', 'tag': 'testtag', 'schema_version': 1 } }, { 'unit_type_id': 'docker_image', 'metadata': { 'image_id': 'v1idtest' } }] labels = {'config': {'Labels': {'label1': 'label2'}}} v1Compatibility = { 'parent': 'testparent', 'id': 'testid', 'config': { 'Labels': { 'testlab1': 'testlab2' } } } data = {'history': [{'v1Compatibility': json.dumps(v1Compatibility)}]} flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, units, labels, data).one_by_one()) history = pulp.listRepos(repos, content, history, label) for key in history[0]['manifests']: assert history[0]['manifests'][key]['v1parent'] == v1Compatibility[ 'parent'] assert history[0]['manifests'][key]['v1id'] == v1Compatibility[ 'id'] assert history[0]['manifests'][key]['v1labels']['testlab1'] == \ v1Compatibility['config']['Labels']['testlab1'] for key in history[0]['v1_labels']: assert history[0]['v1_labels'][key] == labels['config']['Labels']
def test_deleteRepo(self, pulp, publish): repo = 'foobar' flexmock(Pulp) if publish: (Pulp.should_receive('emptyRepo').with_args( repo).once().and_return(None)) (Pulp.should_receive('crane').with_args( repo, force_refresh=True).twice().and_return(None)) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'delete', '/pulp/api/v2/repositories/%s/' % repo).once().and_return(123)) (Pulp.should_receive('watch').with_args(123).once().and_return(None)) pulp.deleteRepo(repo, publish)
def test_listSigstore(self, pulp): repoid = pulp.getSigstore() blob = { 'notes': { '_repo-type': 'iso' }, 'id': repoid, 'description': 'testdesc', 'display_name': 'testdisp', 'distributors': [], 'scratchpad': {} } units = [{'unit_type_id': 'iso', 'metadata': {'name': 'testname'}}] flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, units).one_by_one()) response = pulp.listRepos(repoid, content=True) assert response[0]['sigstore'][0] == 'testname'
def test_listSchema2(self, pulp): blob = { 'notes': { '_repo-type': 'docker-repo', 'include_in_download_service': 'False' }, 'id': 'testid', 'description': 'testdesc', 'display_name': 'testdisp', 'distributors': [], 'scratchpad': {} } units = [{ 'unit_type_id': 'docker_manifest', 'metadata': { 'fs_layers': [{ 'blob_sum': 'test_layer' }], 'digest': 'testdig', 'tag': 'testtag', 'config_layer': 'test_config', 'schema_version': 2 } }, { 'unit_type_id': 'docker_blob', 'metadata': { 'digest': 'test_config' } }, { 'unit_type_id': 'docker_blob', 'metadata': { 'digest': 'test_layer' } }] flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').and_return( blob, units).one_by_one()) response = pulp.listRepos('testid', content=True) assert response[0]['manifests']['testdig']['config'] == 'test_config' assert response[0]['manifests']['testdig']['layers'][0] == 'test_layer' assert response[0]['manifests']['testdig']['schema_version'] == 2 assert response[0]['include_in_download_service'] == 'False'
def test_sync(self, pulp): repoinfoold = [{ 'id': 'redhat-foobar', 'images': {}, 'manifests': { '123456': 'foobar' }, 'manifest_lists': {} }] repoinfonew = [{ 'id': 'redhat-foobar', 'images': {}, 'manifests': { '123456': 'foobar', '567890': 'latest' }, 'manifest_lists': {} }] origin_repo = pulp.getOriginPrefix() + 'redhat-foobar' flexmock(pulp) (pulp.should_receive('listRepos').with_args( repos='redhat-foobar', content=True).twice().and_return(repoinfoold, repoinfonew).one_by_one()) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return('123') ) (pulp.should_receive('watch').with_args('123').once().and_return(None)) (pulp.should_receive('createOriginRepo').with_args( origin_repo).once().and_return(None)) (pulp.should_receive('copy_filters').with_args( origin_repo, 'redhat-foobar').once().and_return(None)) imgs, manifests, manifest_lists = pulp.syncRepo(env='syncenv', repo='foobar', feed='fb', upstream_name='foobar') assert imgs == [] assert manifests == ['567890'] assert manifest_lists == []
def test_remove_filters(self, pulp): repo = 'foobar' type_ids = [ 'docker_image', 'docker_manifest', 'docker_blob', 'docker_tag', 'docker_manifest_list' ] data = { 'criteria': { 'type_ids': type_ids, 'filters': {}, }, 'override_config': {}, 'limit': 1 } flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'post', '/pulp/api/v2/repositories/%s/actions/unassociate/' % repo, data=json.dumps(data)).once().and_return(123)) flexmock(Pulp) (Pulp.should_receive('watch').with_args(123).once().and_return(None)) pulp.remove_filters(repo)
def test_createRepo(self, pulp, repo_id, url, registry_id, sig, distributors, productline, library, distribution): flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return(None)) response = pulp.createRepo(repo_id=repo_id, url=url, registry_id=registry_id, sig=sig, distributors=distributors, productline=productline, library=library, distribution=distribution) if not repo_id.startswith(pulp.getPrefix()): repo_id = pulp.getPrefix() + repo_id if registry_id is None: if productline: pindex = repo_id.find(productline) registry_id = productline + '/' + repo_id[pindex + len(productline) + 1:] elif library: registry_id = repo_id.replace(pulp.getPrefix(), '') else: registry_id = repo_id.replace(pulp.getPrefix(), '').replace('-', '/', 1) rurl = url if rurl and not rurl.startswith('http'): rurl = pulp.cdnhost + url assert response['id'] == repo_id assert response['display_name'] == repo_id if sig: assert response['notes']['signatures'] == pulp.getSignature(sig) if distributors: assert response['distributors'][0]['distributor_config']['repo-registry-id'] \ == registry_id assert response['distributors'][0]['distributor_config'][ 'redirect-url'] == rurl
def test_listHistory(self, pulp, repos, content, history, label): blob = { 'notes': { '_repo-type': 'docker-repo' }, 'id': 'testid', 'description': 'testdesc', 'display_name': 'testdisp', 'distributors': [], 'scratchpad': {} } units = [{ 'unit_type_id': 'docker_manifest', 'metadata': { 'fs_layers': [{ 'blob_sum': 'test' }], 'digest': 'testdig', 'tag': 'testtag', 'schema_version': 1 } }, { 'unit_type_id': 'docker_image', 'metadata': { 'image_id': 'v1idtest' } }] labels = {'config': {'Labels': {'label1': 'label2'}}} v1Compatibility = { 'parent': 'testparent', 'id': 'testid', 'config': { 'Labels': { 'testlab1': 'testlab2' } } } data = {'history': [{'v1Compatibility': json.dumps(v1Compatibility)}]} params = {'details': True} unitdata = { 'criteria': { 'type_ids': [ 'docker_image', 'docker_manifest', 'docker_blob', 'docker_tag', 'docker_manifest_list', 'iso' ], 'filters': { 'unit': {} } } } flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', '/pulp/api/v2/repositories/test-repo/', params=params).and_return(blob).once().ordered()) (RequestsHttpCaller.should_receive('__call__').with_args( 'post', '/pulp/api/v2/repositories/testid/search/units/', data=json.dumps(unitdata)).and_return(units).once().ordered()) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', '/pulp/docker/v1/testid/v1idtest/json').and_return( labels).once().ordered()) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', '/pulp/docker/v2/testid/manifests/1/testdig').once().and_return( data).ordered()) history = pulp.listRepos(repos, content, history, label) for key in history[0]['manifests']: assert history[0]['manifests'][key]['v1parent'] == v1Compatibility[ 'parent'] assert history[0]['manifests'][key]['v1id'] == v1Compatibility[ 'id'] assert history[0]['manifests'][key]['v1labels']['testlab1'] == \ v1Compatibility['config']['Labels']['testlab1'] for key in history[0]['v1_labels']: assert history[0]['v1_labels'][key] == labels['config']['Labels']
def test_getTaskRetriesFail(self, pulp, tid, url): flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).twice().and_raise(requests.ConnectionError)) with pytest.raises(requests.ConnectionError): pulp.getTask(tid)
def test_createRepo(self, pulp, repo_id, url, registry_id, distributors, productline, library, distribution, repotype, importer_type_id, rel_url, download): if distribution == 'test': if (url != '/content/test/foo-test/bar' and url is not None) or \ ((productline is not None or library) and productline != 'foo-test'): with pytest.raises(errors.DockPulpError): pulp.createRepo(repo_id=repo_id, url=url, registry_id=registry_id, distributors=distributors, productline=productline, library=library, distribution=distribution, repotype=repotype, importer_type_id=importer_type_id, rel_url=rel_url, download=download) return if distribution == 'beta' and productline and productline.endswith( 'test'): with pytest.raises(errors.DockPulpError): pulp.createRepo(repo_id=repo_id, url=url, registry_id=registry_id, distributors=distributors, productline=productline, library=library, distribution=distribution, repotype=repotype, importer_type_id=importer_type_id, rel_url=rel_url, download=download) return if not registry_id and (repo_id == 'tags' or productline == 'blobs'): with pytest.raises(errors.DockPulpError): pulp.createRepo(repo_id=repo_id, url=url, registry_id=registry_id, distributors=distributors, productline=productline, library=library, distribution=distribution, repotype=repotype, importer_type_id=importer_type_id, rel_url=rel_url, download=download) return flexmock(Pulp) (Pulp.should_receive('createOriginRepo').once().and_return(None)) flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').once().and_return(None)) response = pulp.createRepo(repo_id=repo_id, url=url, registry_id=registry_id, distributors=distributors, productline=productline, library=library, distribution=distribution, repotype=repotype, importer_type_id=importer_type_id, rel_url=rel_url, download=download) if not repo_id.startswith(pulp.getPrefix()): repo_id = pulp.getPrefix() + repo_id if registry_id is None: if productline: pindex = repo_id.find(productline) registry_id = productline + '/' + repo_id[pindex + len(productline) + 1:] elif library: registry_id = repo_id.replace(pulp.getPrefix(), '') else: registry_id = repo_id.replace(pulp.getPrefix(), '').replace('-', '/', 1) rurl = url if rurl and not rurl.startswith('http'): rurl = pulp.cdnhost + url assert response['id'] == repo_id assert response['display_name'] == repo_id if distribution: assert response['notes']['distribution'] == distribution sig = pulp.getDistributionSig(distribution) assert response['notes']['signatures'] == pulp.getSignature(sig) if distributors: for distributor in response['distributors']: if distributor[ 'distributor_type_id'] == 'docker_distributor_web': assert distributor['distributor_config'][ 'repo-registry-id'] == registry_id assert distributor['distributor_config'][ 'redirect-url'] == rurl if repotype: assert response['notes']['_repo-type'] == repotype if importer_type_id: assert response['importer_type_id'] == importer_type_id if rel_url: assert response['notes']['relative_url'] == rel_url if distributors: for distributor in response['distributors']: if distributor[ 'distributor_type_id'] == 'docker_rsync_distributor': assert distributor['distributor_config'][ 'repo_relative_path'] == rel_url if download is not None: if download: assert response['notes'][ 'include_in_download_service'] == "True" else: assert response['notes'][ 'include_in_download_service'] == "False"
def test_getTaskRetriesFail(self, pulp, status_code): rq = RequestsHttpCaller('http://httpbin.org/') flexmock(Retry).new_instances = fake_retry with pytest.raises(requests.exceptions.RetryError): rq('get', '/status/%s' % status_code)
def test_switched_pulp(self): # Test for dockpulp switchover with nested(NamedTemporaryFile(mode='wt'), NamedTemporaryFile(mode='wt'), NamedTemporaryFile(mode='wt')) as (fp, df, dn): name = 'test' fp.write( dedent(""" [pulps] {name} = foo [registries] {name} = foo [filers] {name} = foo [redirect] {name} = no [distributors] {name} = foo [release_order] {name} = foo [switch_ver] version = 10.0 [switch_release] {name} = bar """).format(name=name)) fp.flush() df.write( dedent(""" { "foo":{ "distributor_type_id": "docker_distributor_web", "distributor_config": {} }, "switch":{ "distributor_type_id": "docker_distributor_web", "distributor_config": {} } } """)) df.flush() dn.write( dedent(""" { "foo":{ "signature": "", "name_enforce": "", "content_enforce": "", "name_restrict": [] } } """)) dn.flush() url = '/pulp/api/v2/status' response = {'versions': {'platform_version': '10.0'}} flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).and_return(response)) switched_pulp = Pulp(env=name, config_file=fp.name, config_distributors=df.name, config_distributions=dn.name) assert switched_pulp.release_order == switched_pulp.switch_release response = {'versions': {'platform_version': '1.0'}} (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).and_return(response)) switched_pulp = Pulp(env=name, config_file=fp.name, config_distributors=df.name, config_distributions=dn.name) assert switched_pulp.release_order != switched_pulp.switch_release
def test_getTask(self, pulp, tid, url): result = 'task_received' flexmock(RequestsHttpCaller) (RequestsHttpCaller.should_receive('__call__').with_args( 'get', url).once().and_return(result)) assert pulp.getTask(tid) == result