|
| 1 | +// Copyright (C) 2025 The Android Open Source Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "log" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/android-cuttlefish/e2etests/orchestration/common" |
| 23 | + |
| 24 | + hoapi "github.com/google/android-cuttlefish/frontend/src/host_orchestrator/api/v1" |
| 25 | + hoclient "github.com/google/android-cuttlefish/frontend/src/libhoclient" |
| 26 | +) |
| 27 | + |
| 28 | +const baseURL = "http://0.0.0.0:2080" |
| 29 | + |
| 30 | +func TestReset(t *testing.T) { |
| 31 | + srv := hoclient.NewHostOrchestratorClient(baseURL) |
| 32 | + t.Cleanup(func() { |
| 33 | + if err := common.CollectHOLogs(baseURL); err != nil { |
| 34 | + log.Printf("failed to collect HO logs: %s", err) |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + // Create a cvd |
| 39 | + imageDir, err := common.PrepareArtifact(srv, "../artifacts/images.zip") |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + hostPkgDir, err := common.PrepareArtifact(srv, "../artifacts/cvd-host_package.tar.gz") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + const group_name = "foo" |
| 48 | + config := ` |
| 49 | + { |
| 50 | + "common": { |
| 51 | + "group_name": "` + group_name + `", |
| 52 | + "host_package": "@image_dirs/` + hostPkgDir + `" |
| 53 | +
|
| 54 | + }, |
| 55 | + "instances": [ |
| 56 | + { |
| 57 | + "vm": { |
| 58 | + "memory_mb": 8192, |
| 59 | + "setupwizard_mode": "OPTIONAL", |
| 60 | + "cpus": 8 |
| 61 | + }, |
| 62 | + "disk": { |
| 63 | + "default_build": "@image_dirs/` + imageDir + `" |
| 64 | + }, |
| 65 | + "streaming": { |
| 66 | + "device_id": "cvd-1" |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + ` |
| 72 | + envConfig := make(map[string]interface{}) |
| 73 | + if err := json.Unmarshal([]byte(config), &envConfig); err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + createReq := &hoapi.CreateCVDRequest{ |
| 77 | + EnvConfig: envConfig, |
| 78 | + } |
| 79 | + if _, err := srv.CreateCVD(createReq, &hoclient.AccessTokenBuildAPICreds{}); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + // Make sure there are cvds in the host |
| 84 | + cvds, err := srv.ListCVDs() |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + if len(cvds) == 0 { |
| 89 | + t.Fatal("ListCVDs returned empty list after create-from-images-dirs") |
| 90 | + } |
| 91 | + |
| 92 | + // Reset the host |
| 93 | + if err := srv.Reset(); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + // Check that there are no cvds left in the host |
| 98 | + cvds, err = srv.ListCVDs() |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + if len(cvds) != 0 { |
| 103 | + t.Fatal("ListCVDs returned non-empty list after reset") |
| 104 | + } |
| 105 | +} |
0 commit comments