def test_build_manifest_missing_parent(fake_session, registry_model): storage = DistributedStorage({'local_us': FakeStorage(None)}, ['local_us']) repository_ref = registry_model.lookup_repository('devtable', 'complex') builder = create_manifest_builder(repository_ref, storage, docker_v2_signing_key) assert builder.start_layer( 'somelayer', json.dumps({ 'id': 'somelayer', 'parent': 'someparent' }), 'local_us', None, 60) is None
def test_build_manifest_missing_parent(fake_session, registry_model): storage = DistributedStorage({"local_us": FakeStorage(None)}, ["local_us"]) repository_ref = registry_model.lookup_repository("devtable", "complex") builder = create_manifest_builder(repository_ref, storage, docker_v2_signing_key) assert ( builder.start_layer( "somelayer", json.dumps({"id": "somelayer", "parent": "someparent"}), "local_us", None, 60, ) is None )
def create_repository(namespace_name, repo_name): # Verify that the repository name is valid. if not REPOSITORY_NAME_REGEX.match(repo_name): abort( 400, message= "Invalid repository name. Repository names cannot contain slashes." ) logger.debug("Looking up repository %s/%s", namespace_name, repo_name) repository_ref = registry_model.lookup_repository(namespace_name, repo_name) if repository_ref is None and get_authenticated_user() is None: logger.debug("Attempt to create repository %s/%s without user auth", namespace_name, repo_name) abort( 401, message= 'Cannot create a repository as a guest. Please login via "docker login" first.', issue="no-login", ) elif repository_ref: modify_perm = ModifyRepositoryPermission(namespace_name, repo_name) if not modify_perm.can(): abort( 403, message= "You do not have permission to modify repository %(namespace)s/%(repository)s", issue="no-repo-write-permission", namespace=namespace_name, repository=repo_name, ) elif repository_ref.kind != "image": msg = ( "This repository is for managing %s resources and not container images." % repository_ref.kind) abort(405, message=msg, namespace=namespace_name) else: create_perm = CreateRepositoryPermission(namespace_name) if not create_perm.can(): logger.warning( "Attempt to create a new repo %s/%s with insufficient perms", namespace_name, repo_name, ) msg = 'You do not have permission to create repositories in namespace "%(namespace)s"' abort(403, message=msg, issue="no-create-permission", namespace=namespace_name) # Attempt to create the new repository. logger.debug( "Creating repository %s/%s with owner: %s", namespace_name, repo_name, get_authenticated_user().username, ) repository_ref = model.repository.create_repository( namespace_name, repo_name, get_authenticated_user()) if get_authenticated_user(): user_event_data = { "action": "push_start", "repository": repo_name, "namespace": namespace_name, } event = userevents.get_event(get_authenticated_user().username) event.publish_event_data("docker-cli", user_event_data) # Start a new builder for the repository and save its ID in the session. assert repository_ref builder = create_manifest_builder(repository_ref, storage, docker_v2_signing_key) logger.debug("Started repo push with manifest builder %s", builder) if builder is None: abort(404, message="Unknown repository", issue="unknown-repo") session["manifest_builder"] = builder.builder_id return make_response("Created", 201)
def test_build_manifest(layers, fake_session, registry_model): repository_ref = registry_model.lookup_repository("devtable", "complex") storage = DistributedStorage({"local_us": FakeStorage(None)}, ["local_us"]) settings = BlobUploadSettings("2M", 3600) app_config = {"TESTING": True} builder = create_manifest_builder(repository_ref, storage, docker_v2_signing_key) assert ( lookup_manifest_builder(repository_ref, "anotherid", storage, docker_v2_signing_key) is None ) assert ( lookup_manifest_builder(repository_ref, builder.builder_id, storage, docker_v2_signing_key) is not None ) blobs_by_layer = {} for layer_id, parent_id, layer_bytes in layers: # Start a new layer. assert builder.start_layer( layer_id, json.dumps({"id": layer_id, "parent": parent_id}), "local_us", None, 60 ) checksum = hashlib.sha1(layer_bytes).hexdigest() # Assign it a blob. with upload_blob(repository_ref, storage, settings) as uploader: uploader.upload_chunk(app_config, BytesIO(layer_bytes)) blob = uploader.commit_to_blob(app_config) blobs_by_layer[layer_id] = blob builder.assign_layer_blob(builder.lookup_layer(layer_id), blob, [checksum]) # Validate the checksum. assert builder.validate_layer_checksum(builder.lookup_layer(layer_id), checksum) # Commit the manifest to a tag. tag = builder.commit_tag_and_manifest("somenewtag", builder.lookup_layer(layers[-1][0])) assert tag assert tag in builder.committed_tags # Mark the builder as done. builder.done() # Verify the legacy image for the tag. found = registry_model.get_repo_tag(repository_ref, "somenewtag", include_legacy_image=True) assert found assert found.name == "somenewtag" assert found.legacy_image.docker_image_id == layers[-1][0] # Verify the blob and manifest. manifest = registry_model.get_manifest_for_tag(found) assert manifest parsed = manifest.get_parsed_manifest() assert len(list(parsed.layers)) == len(layers) for index, (layer_id, parent_id, layer_bytes) in enumerate(layers): assert list(parsed.blob_digests)[index] == blobs_by_layer[layer_id].digest assert list(parsed.layers)[index].v1_metadata.image_id == layer_id assert list(parsed.layers)[index].v1_metadata.parent_image_id == parent_id assert parsed.leaf_layer_v1_image_id == layers[-1][0]