forked from google/android-cuttlefish
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
346 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
// Copyright (C) 2024 The Android Open Source Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package orchestration | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"testing" | ||
|
||
"orchestration/e2etesting" | ||
|
||
hoapi "github.com/google/android-cuttlefish/frontend/src/liboperator/api/v1" | ||
"github.com/google/cloud-android-orchestration/pkg/client" | ||
) | ||
|
||
func TestSnapshot(t *testing.T) { | ||
ctx, err := e2etesting.Setup(61003) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
e2etesting.Cleanup(ctx) | ||
}) | ||
dh, err := e2etesting.NewDockerHelper() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
srv := client.NewHostOrchestratorService(ctx.ServiceURL) | ||
uploadDir, err := srv.CreateUploadDir() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := uploadArtifacts(srv, uploadDir); err != nil { | ||
t.Fatal(err) | ||
} | ||
const groupName = "cvd" | ||
cvd, err := createDevice(srv, groupName, uploadDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
log.Printf("cvd: %+v\n", cvd) | ||
cID := ctx.DockerContainerID | ||
adbBin := fmt.Sprintf("/var/lib/cuttlefish-common/user_artifacts/%s/bin/adb", uploadDir) | ||
if err := dh.StartADBServer(cID, adbBin); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := dh.ConnectADB(cID, adbBin, cvd.ADBSerial); err != nil { | ||
t.Fatal(err) | ||
} | ||
const tmpFile = "/data/local/tmp/foo" | ||
// Create temporary file | ||
if err := dh.ExecADBShellCommand(cID, adbBin, cvd.ADBSerial, []string{"touch", tmpFile}); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := dh.ExecADBShellCommand(cID, adbBin, cvd.ADBSerial, []string{"stat", tmpFile}); err != nil { | ||
t.Fatal(err) | ||
} | ||
// Create a snapshot containing the temporary file. | ||
createSnapshotRes, err := createSnapshot(ctx.ServiceURL, groupName, cvd.Name) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Remove temporary file | ||
if err := dh.ExecADBShellCommand(cID, adbBin, cvd.ADBSerial, []string{"rm", tmpFile}); err != nil { | ||
t.Fatal(err) | ||
} | ||
// Double check temporary file does not exist. | ||
err = dh.ExecADBShellCommand(cID, adbBin, cvd.ADBSerial, []string{"stat", tmpFile}) | ||
var exitCodeErr *e2etesting.DockerExecExitCodeError | ||
if !errors.As(err, &exitCodeErr) { | ||
t.Fatal(err) | ||
} | ||
// Stop the device. | ||
if err := stopDevice(ctx.ServiceURL, groupName, cvd.Name); err != nil { | ||
t.Fatal(err) | ||
} | ||
cvds, err := srv.ListCVDs() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
log.Printf("cvds: len: %d: %+v\n", len(cvds), cvds[0]) | ||
// Restore the device from the snapshot. | ||
startErr := startDevice(ctx.ServiceURL, groupName, cvd.Name, createSnapshotRes.SnapshotID) | ||
// startErr := startDevice(ctx.ServiceURL, groupName, cvd.Name, "") | ||
if err := e2etesting.DownloadHostBugReport(srv, groupName); err != nil { | ||
t.Errorf("failed creating bugreport: %s", err) | ||
} | ||
if startErr != nil { | ||
t.Fatal(err) | ||
} | ||
cvds, err = srv.ListCVDs() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// log.Printf("cvds: len: %d: %+v\n", len(cvds), cvds[0]) | ||
// // Verify the temporary file does exist. | ||
// if err := dh.ConnectADB(cID, adbBin, cvd.ADBSerial); err != nil { | ||
// t.Fatal(err) | ||
// } | ||
// if err := dh.ExecADBShellCommand(cID, adbBin, cvd.ADBSerial, []string{"stat", tmpFile}); err != nil { | ||
// t.Fatal(err) | ||
// } | ||
} | ||
|
||
func uploadArtifacts(srv client.HostOrchestratorService, uploadDir string) error { | ||
if err := e2etesting.UploadAndExtract(srv, uploadDir, "images.zip"); err != nil { | ||
return err | ||
} | ||
if err := e2etesting.UploadAndExtract(srv, uploadDir, "cvd-host_package.tar.gz"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func createDevice(srv client.HostOrchestratorService, group_name, artifactsDir string) (*hoapi.CVD, error) { | ||
config := ` | ||
{ | ||
"common": { | ||
"group_name": "` + group_name + `", | ||
"host_package": "@user_artifacts/` + artifactsDir + `" | ||
}, | ||
"instances": [ | ||
{ | ||
"vm": { | ||
"memory_mb": 8192, | ||
"setupwizard_mode": "OPTIONAL", | ||
"cpus": 8, | ||
"enable_virtiofs": "false" | ||
}, | ||
"graphics": { | ||
"gpu_mode": "guest_swiftshader" | ||
}, | ||
"disk": { | ||
"default_build": "@user_artifacts/` + artifactsDir + `" | ||
}, | ||
"streaming": { | ||
"device_id": "cvd-1" | ||
} | ||
} | ||
] | ||
} | ||
` | ||
envConfig := make(map[string]interface{}) | ||
if err := json.Unmarshal([]byte(config), &envConfig); err != nil { | ||
return nil, err | ||
} | ||
createReq := &hoapi.CreateCVDRequest{EnvConfig: envConfig} | ||
res, createErr := srv.CreateCVD(createReq /* buildAPICredentials */, "") | ||
if createErr != nil { | ||
if err := e2etesting.DownloadHostBugReport(srv, group_name); err != nil { | ||
log.Printf("error downloading cvd bugreport: %v", err) | ||
} | ||
return nil, createErr | ||
} | ||
return res.CVDs[0], nil | ||
} | ||
|
||
// TODO(b/370552105): Use HO API objects definitions from HEAD in e2e tests. | ||
type CreateSnapshotResponse struct { | ||
SnapshotID string `json:"snapshot_id"` | ||
} | ||
|
||
// TODO(b/370552105): Use HO API objects definitions from HEAD in e2e tests. | ||
type StartCVDRequest struct { | ||
// Start from the relevant snaphost if not empty. | ||
SnapshotID string `json:"snapshot_id,omitempty"` | ||
} | ||
|
||
func createSnapshot(srvURL, group, name string) (*CreateSnapshotResponse, error) { | ||
helper := client.HTTPHelper{ | ||
Client: http.DefaultClient, | ||
RootEndpoint: srvURL, | ||
} | ||
op := &hoapi.Operation{} | ||
path := fmt.Sprintf("/cvds/%s/%s/snapshots", group, name) | ||
rb := helper.NewPostRequest(path, nil) | ||
if err := rb.JSONResDo(op); err != nil { | ||
return nil, err | ||
} | ||
srv := client.NewHostOrchestratorService(srvURL) | ||
res := &CreateSnapshotResponse{} | ||
if err := srv.WaitForOperation(op.Name, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// TODO(b/370550070) Remove once this method is added to the client implementation. | ||
func stopDevice(srvURL, group, name string) error { | ||
helper := client.HTTPHelper{ | ||
Client: http.DefaultClient, | ||
RootEndpoint: srvURL, | ||
} | ||
op := &hoapi.Operation{} | ||
path := fmt.Sprintf("/cvds/%s/%s/:stop", group, name) | ||
rb := helper.NewPostRequest(path, nil) | ||
if err := rb.JSONResDo(op); err != nil { | ||
return err | ||
} | ||
srv := client.NewHostOrchestratorService(srvURL) | ||
res := &hoapi.EmptyResponse{} | ||
if err := srv.WaitForOperation(op.Name, &res); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// TODO(b/370550070) Remove once this method is added to the client implementation. | ||
func startDevice(srvURL, group, name, snapshotID string) error { | ||
helper := client.HTTPHelper{ | ||
Client: http.DefaultClient, | ||
RootEndpoint: srvURL, | ||
} | ||
// body := &StartCVDRequest{SnapshotID: snapshotID} | ||
body := &StartCVDRequest{} | ||
op := &hoapi.Operation{} | ||
path := fmt.Sprintf("/cvds/%s/%s/:start", group, name) | ||
rb := helper.NewPostRequest(path, body) | ||
if err := rb.JSONResDo(op); err != nil { | ||
return err | ||
} | ||
srv := client.NewHostOrchestratorService(srvURL) | ||
res := &hoapi.EmptyResponse{} | ||
if err := srv.WaitForOperation(op.Name, &res); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |