Skip to content

Commit

Permalink
Rebased after added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fkorotkov committed May 20, 2022
1 parent e025c44 commit 0047dc0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Sources/tart/OCI/Registry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class Registry {
}

func ping() async throws {
let (_, response) = try await endpointRequest("GET", "/v2/")
if response.statusCode != 200 {
throw RegistryError.UnexpectedHTTPStatusCode(when: "doing ping", code: response.statusCode)
let response = try await endpointRequest(.GET, "/v2/")
if response.status != .ok {
throw RegistryError.UnexpectedHTTPStatusCode(when: "doing ping", code: response.status.code)
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ class Registry {
return digest
}

public func pullBlobInto(_ digest: String, handler: (ByteBuffer) throws -> Void) async throws {
public func pullBlob(_ digest: String, handler: (ByteBuffer) throws -> Void) async throws {
let response = try await endpointRequest(.GET, "\(namespace)/blobs/\(digest)")
if response.status != .ok {
let body = try await response.body.readTextResponse()
Expand Down
6 changes: 3 additions & 3 deletions Sources/tart/VMDirectory+OCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension VMDirectory {
throw OCIError.FailedToCreateVmFile
}
let configFile = try FileHandle(forWritingTo: configURL)
try await registry.pullBlobInto(configLayers.first!.digest) { buffer in
try await registry.pullBlob(configLayers.first!.digest) { buffer in
configFile.write(Data(buffer: buffer))
}
try configFile.close()
Expand Down Expand Up @@ -70,7 +70,7 @@ extension VMDirectory {
ProgressObserver(progress).log(defaultLogger)

for diskLayer in diskLayers {
try await registry.pullBlobInto(diskLayer.digest) { buffer in
try await registry.pullBlob(diskLayer.digest) { buffer in
let data = Data(buffer: buffer)
try filter.write(data)
progress.completedUnitCount += Int64(data.count)
Expand All @@ -92,7 +92,7 @@ extension VMDirectory {
throw OCIError.FailedToCreateVmFile
}
let nvram = try FileHandle(forWritingTo: nvramURL)
try await registry.pullBlobInto(nvramLayers.first!.digest) { buffer in
try await registry.pullBlob(nvramLayers.first!.digest) { buffer in
nvram.write(Data(buffer: buffer))
}
try nvram.close()
Expand Down
10 changes: 8 additions & 2 deletions Tests/TartTests/RegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ final class RegistryTests: XCTestCase {
XCTAssertEqual("sha256:d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", pushedBlobDigest)

// Pull it
let pulledBlob = try await registry.pullBlob(pushedBlobDigest)
var pulledBlob = Data()
try await registry.pullBlob(pushedBlobDigest) { buffer in
pulledBlob.append(Data(buffer: buffer))
}

// Ensure that both blobs are identical
XCTAssertEqual(pushedBlob, pulledBlob)
Expand All @@ -48,7 +51,10 @@ final class RegistryTests: XCTestCase {
let largeBlobDigest = try await registry.pushBlob(fromData: largeBlobToPush)

// Pull it
let pulledLargeBlob = try await registry.pullBlob(largeBlobDigest)
var pulledLargeBlob = Data()
try await registry.pullBlob(largeBlobDigest) { buffer in
pulledLargeBlob.append(Data(buffer: buffer))
}

// Ensure that both blobs are identical
XCTAssertEqual(largeBlobToPush, pulledLargeBlob)
Expand Down

0 comments on commit 0047dc0

Please sign in to comment.