Skip to content

Implement HO golang client API ExtractArtifact(filename string) #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions frontend/src/libhoclient/host_orchestrator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type UserArtifactsClient interface {
// Upload artifact into the artifacts repository.
// Artifacts are identified by their SHA256 checksum in the artifacts repository
UploadArtifact(filename string) error
// Extract artifact into the artifacts repository.
// Artifacts are identified by their SHA256 checksum in the artifacts repository
ExtractArtifact(filename string) (*hoapi.Operation, error)
// Creates a directory in the host where user artifacts can be uploaded to.
CreateUploadDir() (string, error)
// Uploads file into the given directory.
Expand Down Expand Up @@ -590,6 +593,26 @@ func (c *HostOrchestratorClientImpl) ExtractFile(uploadDir string, filename stri
return result, nil
}

func (c *HostOrchestratorClientImpl) ExtractArtifact(filename string) (*hoapi.Operation, error) {
checksum, err := sha256Checksum(filename)
if err != nil {
return nil, err
}
res := &hoapi.StatArtifactResponse{}
if err := c.HTTPHelper.NewGetRequest("/v1/userartifacts/" + checksum).JSONResDo(res); err != nil {
return nil, err
}
op := &hoapi.Operation{}
if err := c.HTTPHelper.NewPostRequest("/v1/userartifacts/"+checksum+"/:extract", nil).JSONResDo(op); err != nil {
if apiCallError, ok := err.(*ApiCallError); ok && apiCallError.HTTPStatusCode == 409 {
return nil, nil
} else {
return nil, err
}
}
return op, nil
}

func (c *HostOrchestratorClientImpl) CreateBugReport(group string, opts CreateBugReportOpts, dst io.Writer) error {
op := &hoapi.Operation{}
path := "/cvds/" + group + "/:bugreport"
Expand Down
69 changes: 69 additions & 0 deletions frontend/src/libhoclient/host_orchestrator_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,75 @@ func TestUploadArtifactReceive409WhenUploadingChunks(t *testing.T) {
}
}

func TestExtractArtifactReceive404WhenArtifactDoesNotExist(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
testFile := createTempFile(t, tempDir, "waldo", []byte("waldo"))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch ep := r.Method + " " + r.URL.Path; ep {
case "GET /v1/userartifacts/d2c055002a6cdf8dd9edf90c7a666cb5f7f2d25da8519ec206f56777d74e0c7d":
writeErr(w, http.StatusNotFound)
default:
t.Fatal("unexpected endpoint: " + ep)
}
}))
defer ts.Close()
client := NewHostOrchestratorClient(ts.URL)

if _, err := client.ExtractArtifact(testFile); err == nil {
t.Fatal("Expected error")
}
}

func TestExtractArtifactSucceeds(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
testFile := createTempFile(t, tempDir, "waldo", []byte("waldo"))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch ep := r.Method + " " + r.URL.Path; ep {
case "GET /v1/userartifacts/d2c055002a6cdf8dd9edf90c7a666cb5f7f2d25da8519ec206f56777d74e0c7d":
writeOK(w, hoapi.StatArtifactResponse{})
case "POST /v1/userartifacts/d2c055002a6cdf8dd9edf90c7a666cb5f7f2d25da8519ec206f56777d74e0c7d/:extract":
writeOK(w, hoapi.Operation{Name: "foo"})
default:
t.Fatal("unexpected endpoint: " + ep)
}
}))
defer ts.Close()
client := NewHostOrchestratorClient(ts.URL)

expected := &hoapi.Operation{Name: "foo"}
if op, err := client.ExtractArtifact(testFile); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(expected, op); diff != "" {
t.Fatalf("response mismatch (-want +got):\n%s", diff)
}
}

func TestExtractArtifactReceive409WhenArtifactIsAlreadyExtracted(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
testFile := createTempFile(t, tempDir, "waldo", []byte("waldo"))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch ep := r.Method + " " + r.URL.Path; ep {
case "GET /v1/userartifacts/d2c055002a6cdf8dd9edf90c7a666cb5f7f2d25da8519ec206f56777d74e0c7d":
writeOK(w, hoapi.StatArtifactResponse{})
case "POST /v1/userartifacts/d2c055002a6cdf8dd9edf90c7a666cb5f7f2d25da8519ec206f56777d74e0c7d/:extract":
writeErr(w, http.StatusConflict)
default:
t.Fatal("unexpected endpoint: " + ep)
}
}))
defer ts.Close()
client := NewHostOrchestratorClient(ts.URL)

if op, err := client.ExtractArtifact(testFile); err != nil {
t.Fatal("expected error")
} else if op != nil {
t.Fatal("unexpected operation: " + op.Name)
}
}

func TestCreateCVDWithUserProjectOverride(t *testing.T) {
fakeRes := &hoapi.CreateCVDResponse{CVDs: []*hoapi.CVD{{Name: "1"}}}
token := "foo"
Expand Down
Loading