def test_get_date_schema2(self, fixture_registry_url, fixture_repositories, fixture_tags, fixture_digest, fixture_config_payload, fixture_image_date, fixture_blob_payload): mock_res = mock.MagicMock() mock_res.json = mock.MagicMock(return_value=fixture_blob_payload) mock_res.status_code = 200 # get the date use case with mock.patch('requests.get', return_value=mock_res) as mo: image = Image(Client(fixture_registry_url), fixture_repositories["repositories"][0], fixture_tags[0], digest=fixture_digest, data=fixture_config_payload) date = image.get_date() assert date and date == fixture_image_date assert image.date == date # could not get the date use case mock_res.json = mock.MagicMock(return_value={}) with mock.patch('requests.get', return_value=mock_res) as mo: image = Image(Client(fixture_registry_url), fixture_repositories["repositories"][0], fixture_tags[0], digest=fixture_digest, data=fixture_config_payload) with pytest.raises(DRegCliException) as excinfo: image.get_date() assert str(excinfo.value) == "Image date not found"
def get_client(self): if self.active: client = Client(self.host) client.set_auth(self.login, self.pwd) else: client = None return client
def run(self, url, json_output, user=False, debug=False): if not json_output: print(self.Meta.command) self.client = Client(url, verbose=not json_output) if debug: self.client.set_debug(True) if user: login, password = user.split(':') self.client.set_auth(login, password)
def test_init(self, fixture_registry_url): def assert_client(verbose): assert client.url == fixture_registry_url assert client.verbose == verbose assert not client.auth assert isinstance(client.request_kwargs, dict) \ and not client.request_kwargs client = Client(fixture_registry_url) assert_client(False) client = Client(fixture_registry_url, verbose=False) assert_client(False) client = Client(fixture_registry_url, verbose=True) assert_client(True)
def test_decorate_headers(self, fixture_registry_url, fixture_auth_token): client = Client(fixture_registry_url, verbose=False) client.auth = {'token': fixture_auth_token} headers = {'foobar': 'foobar2000'} original_headers = headers.copy() expected_headers = headers.copy() expected_headers['Authorization'] = \ Client.Meta.auth_bearer_pattern.format(token=fixture_auth_token) decorated_headers = client._auth_decorate_headers(headers) # header should be unchanged assert headers == original_headers # decorated headers should be the expected decorated_headers == expected_headers
def test_tags( self, fixture_registry_url, fixture_repository, fixture_tags, capsys ): # compute expected console result from expected fixture_tags client = Client(fixture_registry_url, verbose=False) repository = Repository(client, fixture_repository) expected_tags_lines = tabulate([ [t, CommandHandler().date2str(repository.image(t).get_date())] for t in fixture_tags ], headers=['Tag', 'Date']).split("\n") with mock.patch( 'sys.argv', ['dregcli', 'tags', fixture_registry_url, fixture_repository] ): console_main() out_lines = tools.get_output_lines(capsys) assert out_lines[0] == 'tags' # tag lines result: last tags count ouput lines output_tags = out_lines[len(expected_tags_lines) * -1:] assert all([ot in expected_tags_lines for ot in output_tags])
def test_tags_json( self, fixture_registry_url, fixture_repository, fixture_tags, capsys ): # compute expected json result from expected fixture_tags client = Client(fixture_registry_url, verbose=False) repository = Repository(client, fixture_repository) expected_json_result = { 'result': [ { 'tag': t, 'date': CommandHandler().date2str( repository.image(t).get_date() ), } for t in fixture_tags ], } with mock.patch( 'sys.argv', ['dregcli', 'tags', fixture_registry_url, fixture_repository, '-j'] ): console_main() out_json = tools.get_output_json(capsys) assert out_json and 'result' in expected_json_result \ and all(t in expected_json_result['result'] for t in out_json['result'])
class CommandHandler(object): def __init__(self): self.client = None def date2str(self, dt): return dt.strftime(DATE_FORMAT) def run(self, url, json_output, user=False, debug=False): if not json_output: print(self.Meta.command) self.client = Client(url, verbose=not json_output) if debug: self.client.set_debug(True) if user: login, password = user.split(':') self.client.set_auth(login, password)
def test_display(self, fixture_registry_url, capsys): client = Client(fixture_registry_url, verbose=True) client.display("hello", "world") out_lines = tools.get_output_lines(capsys) assert out_lines == ["hello world"] client = Client(fixture_registry_url, verbose=False) client.display("hello") out_lines = tools.get_output_lines(capsys) assert out_lines == []
def test_get_token(self, fixture_registry_url, fixture_auth, fixture_auth_token): expected_realm = "https://myhost/v2/token" expected_service = "docker-registry.myhost.com" expected_scope = "registry:catalog:*" authenticate_header_val = \ 'Bearer realm="{realm}",' \ 'service="{service}",' \ 'scope="{scope}"'.format( realm=expected_realm, service=expected_service, scope=expected_scope ) headers = {'Www-Authenticate': authenticate_header_val} mock_response = mock.MagicMock() mock_response.status_code = 200 mock_response.headers = headers auth_get_token_inner_get_result = mock.MagicMock() auth_get_token_inner_get_result.status_code = 200 auth_get_token_inner_get_result.json = mock.MagicMock( return_value={'token': fixture_auth_token}) client = Client(fixture_registry_url, verbose=False) client.auth = fixture_auth with mock.patch('requests.get', return_value=auth_get_token_inner_get_result) as mo: client._auth_get_token(mock_response) assert 'token' in client.auth and \ client.auth['token'] == fixture_auth_token no_token_msg = "Get token request: no token found in response" auth_get_token_inner_get_result.json = mock.MagicMock( return_value={'token': ''}) with mock.patch('requests.get', return_value=auth_get_token_inner_get_result) as mo: with pytest.raises(DRegCliException) as excinfo: client._auth_get_token(mock_response) assert str(excinfo.value) == no_token_msg # no token use case auth_get_token_inner_get_result.json = mock.MagicMock(return_value={}) with mock.patch('requests.get', return_value=auth_get_token_inner_get_result) as mo: with pytest.raises(DRegCliException) as excinfo: client._auth_get_token(mock_response) assert str(excinfo.value) == no_token_msg
def test_debug(self, fixture_registry_url): client = Client(fixture_registry_url) assert not client._debug client.set_debug(True) assert client._debug client.set_debug(False) assert not client._debug
def test_get_date_schema1(self, fixture_registry_url, fixture_repositories, fixture_tags, fixture_digest, fixture_schema1_history, fixture_image_date): image = Image(Client(fixture_registry_url), fixture_repositories["repositories"][0], fixture_tags[0], digest=fixture_digest, data=fixture_schema1_history) date = image.get_date() # regarding fixture_schema1_history fixture_image_date_str should be # the latest assert date and date == fixture_image_date assert image.date == date
def test_image( self, fixture_registry_url, fixture_repository, fixture_tags, fixture_digest, fixture_image_url, fixture_image_json ): expected_url = fixture_registry_url + fixture_image_url mock_res = mock.MagicMock() mock_res.status_code = 200 mock_res.json = mock.MagicMock( return_value=fixture_image_json ) # mock response headers response_headers = {} response_headers[Repository.Meta.manifest_response_header_digest] = \ fixture_digest mock_res.headers.__getitem__.side_effect = response_headers.__getitem__ mock_res.headers.get.side_effect = response_headers.get # mock get mock_res.headers.__iter__.side_effect = response_headers.__iter__ expected_image_name = "{repo}:{tag}".format( repo=fixture_repository, tag=fixture_tags[0] ) with mock.patch('requests.get', return_value=mock_res) as mo: repository = Repository( Client(fixture_registry_url), fixture_repository ) image = repository.image(fixture_tags[0]) mo.assert_called_once_with( expected_url, headers=Repository.Meta.manifests_headers ) assert type(image) == Image and \ image.name == fixture_repository and \ image.tag == fixture_tags[0] and \ image.digest == fixture_digest and \ image.data == fixture_image_json and \ str(image) == "{repo}:{tag}".format( repo=fixture_repository, tag=fixture_tags[0])
def test_repositories(self, fixture_registry_url, fixture_repositories_url, fixture_repositories): mock_res = mock.MagicMock() mock_res.status_code = 200 mock_res.json = mock.MagicMock(return_value=fixture_repositories) with mock.patch('requests.get', return_value=mock_res) as mo: client = Client(fixture_registry_url, verbose=False) repositories = self.repositories( mo, client, fixture_registry_url + fixture_repositories_url) expected_repos = fixture_repositories['repositories'] assert isinstance(repositories, list) and \ len(repositories) == len(expected_repos) and \ all([type(r) == Repository for r in repositories]) and \ all([r.client == client for r in repositories]) and \ [r.name for r in repositories] == expected_repos and \ [str(r) for r in repositories] == expected_repos
def test_tags( self, fixture_registry_url, fixture_repository, fixture_tags_url, fixture_tags_json ): expected_url = fixture_registry_url + fixture_tags_url mock_res = mock.MagicMock() mock_res.status_code = 200 mock_res.json = mock.MagicMock(return_value=fixture_tags_json) with mock.patch('requests.get', return_value=mock_res) as mo: repository = Repository( Client(fixture_registry_url), fixture_repository ) tags = repository.tags() assert isinstance(tags, list) and \ tags == fixture_tags_json['tags']
def test_init( self, fixture_registry_url, fixture_repositories, fixture_tags, fixture_digest, fixture_config_payload, ): image = Image(Client(fixture_registry_url), fixture_repositories["repositories"][0], fixture_tags[0], digest=fixture_digest, data=fixture_config_payload) assert image and \ image.data == fixture_config_payload and \ image.tag == fixture_tags[0] and \ image.digest == fixture_digest and \ image.schema_version == \ fixture_config_payload['schemaVersion'] and \ image.config_digest == fixture_config_payload['config']['digest']
def test_delete( self, fixture_registry_url, fixture_repositories, fixture_tags, fixture_digest, fixture_delete_url, ): mock_res = mock.MagicMock() mock_res.status_code = 202 # 202 for delete with mock.patch('requests.delete', return_value=mock_res) as mo: image = Image(Client(fixture_registry_url), fixture_repositories["repositories"][0], fixture_tags[0], digest=fixture_digest) image.delete() mo.assert_called_once_with( fixture_delete_url, headers=Repository.Meta.manifests_headers, )
def test_images_json( self, fixture_registry_url, fixture_repository, fixture_tags, capsys ): # compute expected json result from expected fixture_tags client = Client(fixture_registry_url, verbose=False) repository = Repository(client, fixture_repository) with mock.patch( 'sys.argv', [ 'dregcli', 'images', fixture_registry_url, fixture_repository, '-j' ] ): console_main() out_json = tools.get_output_json(capsys) # 1388 commit and latest should be in same layer # 1385 1386 and 1387 should each on separate layer for result_item in out_json['result']: tags = result_item['tags'] tags_count = len(tags) assert tags_count in (1, 2) for t in tags: pipeline_ref = t.split('-')[-1] if tags_count == 1: assert pipeline_ref in ('1385', '1386', '1387') else: assert pipeline_ref in ('latest', '1388')
def test_request(self, fixture_registry_url, fixture_repositories_url, fixture_auth, fixture_auth_token): expected_url = fixture_registry_url + fixture_repositories_url expected_code = 200 client = Client(fixture_registry_url, verbose=False) client.auth = fixture_auth client._auth_get_token = mock.MagicMock( return_value=fixture_auth_token) client.decorate_headers = mock.MagicMock(return_value={}) # default mock_res = mock.MagicMock() mock_res.json = mock.MagicMock(return_value=fixture_repositories) mock_res.status_code = 200 with mock.patch('requests.get', return_value=mock_res) as mo: res = client._request(expected_url) assert res.status_code == expected_code and \ res.json() == fixture_repositories
def test_request(self, fixture_registry_url, fixture_repositories_url, fixture_repositories): expected_url = fixture_registry_url + fixture_repositories_url expected_code = 200 client = Client(fixture_registry_url, verbose=False) headers = {} # default mock_res = mock.MagicMock() mock_res.json = mock.MagicMock(return_value=fixture_repositories) mock_res.status_code = 200 with mock.patch('requests.get', return_value=mock_res) as mo: res = client._request(expected_url) mo.assert_called_once_with(expected_url, headers=headers) assert res.status_code == expected_code and \ res.json() == fixture_repositories # headers headers = {'foobar': 'foobar2000'} with mock.patch('requests.get', return_value=mock_res) as mo: res = client._request(expected_url, headers=headers) mo.assert_called_once_with(expected_url, headers=headers) assert res.status_code == expected_code and \ res.json() == fixture_repositories # method headers = {} with mock.patch('requests.post', return_value=mock_res) as mo: res = client._request(expected_url, method=requests.post, verb='POST') mo.assert_called_once_with( expected_url, headers=headers, ) assert res.status_code == expected_code and \ res.json() == fixture_repositories # expected_code matching expected_code = 202 mock_res.status_code = expected_code with mock.patch('requests.get', return_value=mock_res) as mo: res = client._request(expected_url, expected_code=expected_code) mo.assert_called_once_with( expected_url, headers=headers, ) assert res.status_code == expected_code and \ res.json() == fixture_repositories # expected code mismatch expected_code = 202 other_code = 404 # other result than expected code other_code_msg = tools.get_error_status_message(other_code) mock_res.status_code = other_code with mock.patch('requests.get', return_value=mock_res) as mo: with pytest.raises(DRegCliException) as excinfo: res = client._request(expected_url, expected_code=expected_code) mo.assert_called_once_with( expected_url, headers=headers, ) assert res.status_code == expected_code and \ res.json() == fixture_repositories assert str(excinfo.value) == other_code_msg
def test_set_auth(self, fixture_registry_url, fixture_auth): client = Client(fixture_registry_url) client.set_auth(fixture_auth.get('login'), fixture_auth.get('password')) assert client.auth == fixture_auth
def test_set_auth(self, fixture_registry_url, fixture_auth): client = Client(fixture_registry_url) client.auth = fixture_auth client.reset_auth() assert not client.auth and isinstance(client.auth, bool)
def fixture_client(fixture_registry_url): return Client(fixture_registry_url)