Skip to content

Commit

Permalink
Remove unit tests.
Browse files Browse the repository at this point in the history
- After setting up integration tests these unit tests are no longer
  needed
  • Loading branch information
ser-io committed Jun 12, 2024
1 parent fe8149b commit c2e8ccd
Showing 1 changed file with 0 additions and 340 deletions.
340 changes: 0 additions & 340 deletions frontend/src/host_orchestrator/orchestrator/createcvdaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,12 @@
package orchestrator

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/google/android-cuttlefish/frontend/src/host_orchestrator/orchestrator/cvd"
orchtesting "github.com/google/android-cuttlefish/frontend/src/host_orchestrator/orchestrator/testing"
apiv1 "github.com/google/android-cuttlefish/frontend/src/liboperator/api/v1"
"github.com/google/android-cuttlefish/frontend/src/liboperator/operator"

"github.com/google/go-cmp/cmp"
)

func TestCreateCVDInvalidRequestsEmptyFields(t *testing.T) {
Expand Down Expand Up @@ -84,335 +73,6 @@ func TestCreateCVDInvalidRequestsEmptyFields(t *testing.T) {
}
}

func TestCreateCVDSameTargetArtifactsIsDownloadedOnce(t *testing.T) {
dir := orchtesting.TempDir(t)
defer orchtesting.RemoveDir(t, dir)
fetchCVDExecCounter := 0
execContext := func(ctx context.Context, name string, args ...string) *exec.Cmd {
if len(args) > 3 && filepath.Base(args[2]) == "fetch_cvd" {
fetchCVDExecCounter += 1
}
return exec.Command("true")
}
paths := IMPaths{ArtifactsRootDir: dir + "/artifacts"}
om := NewMapOM()
buildAPI := &fakeBuildAPI{}
artifactsFetcher := newBuildAPIArtifactsFetcher(buildAPI)
cvdExecContext := newCVDExecContext(execContext, fakeCVDUser)
cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(cvdExecContext, "")
opts := CreateCVDActionOpts{
Request: &apiv1.CreateCVDRequest{CVD: &apiv1.CVD{BuildSource: androidCISource("1", "foo")}},
HostValidator: &AlwaysSucceedsValidator{},
Paths: paths,
OperationManager: om,
ExecContext: execContext,
BuildAPI: buildAPI,
ArtifactsFetcher: artifactsFetcher,
CVDBundleFetcher: cvdBundleFetcher,
CVDUser: fakeCVDUser,
}
action := NewCreateCVDAction(opts)

op1, err := action.Run()
orchtesting.FatalIfNil(t, err)
op2, err := action.Run()
orchtesting.FatalIfNil(t, err)

_, err = om.Wait(op1.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
_, err = om.Wait(op2.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
if fetchCVDExecCounter == 0 {
t.Error("`fetch_cvd` was never executed")
}
if fetchCVDExecCounter > 1 {
t.Errorf("`fetch_cvd` was executed more than once, it was <<%d>> times", fetchCVDExecCounter)
}
}

func TestCreateCVDVerifyRootDirectoriesAreCreated(t *testing.T) {
dir := orchtesting.TempDir(t)
defer orchtesting.RemoveDir(t, dir)
execContext := execCtxAlwaysSucceeds
paths := IMPaths{ArtifactsRootDir: dir + "/artifacts"}
om := NewMapOM()
buildAPI := &fakeBuildAPI{}
artifactsFetcher := newBuildAPIArtifactsFetcher(buildAPI)
cvdExecContext := newCVDExecContext(execContext, fakeCVDUser)
cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(cvdExecContext, "")
opts := CreateCVDActionOpts{
Request: &apiv1.CreateCVDRequest{CVD: &apiv1.CVD{BuildSource: androidCISource("1", "foo")}},
HostValidator: &AlwaysSucceedsValidator{},
Paths: paths,
OperationManager: om,
ExecContext: execContext,
BuildAPI: buildAPI,
ArtifactsFetcher: artifactsFetcher,
CVDBundleFetcher: cvdBundleFetcher,
CVDUser: fakeCVDUser,
}
action := NewCreateCVDAction(opts)

op, err := action.Run()

orchtesting.FatalIfNil(t, err)
_, err = om.Wait(op.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
stats, err := os.Stat(paths.ArtifactsRootDir)
orchtesting.FatalIfNil(t, err)
if diff := cmp.Diff("drwxrwxr--", stats.Mode().String()); diff != "" {
t.Errorf("mode mismatch (-want +got):\n%s", diff)
}
}

func TestCreateCVDVerifyStartCVDCmdArgs(t *testing.T) {
dir := orchtesting.TempDir(t)
defer orchtesting.RemoveDir(t, dir)
goldenPrefixFmt := fmt.Sprintf("sudo -u fakecvduser "+
"ANDROID_HOST_OUT=%[1]s/artifacts/%%[1]s %[2]s --group_name=cvd start --daemon --report_anonymous_usage_stats=y"+
" --base_instance_num=1 --system_image_dir=%[1]s/artifacts/%%[1]s", dir, cvd.CVDBin)
tests := []struct {
name string
req apiv1.CreateCVDRequest
exp string
}{
{
name: "android ci build default",
req: apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
AndroidCIBuildSource: &apiv1.AndroidCIBuildSource{},
},
},
},
exp: fmt.Sprintf(goldenPrefixFmt,
fmt.Sprintf("%s_%s__cvd", fakeLatesGreenBuildID, mainBuildDefaultTarget)),
},
{
name: "android ci build specific main build",
req: apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
AndroidCIBuildSource: &apiv1.AndroidCIBuildSource{
MainBuild: &apiv1.AndroidCIBuild{
BuildID: "1",
Target: "foo",
},
},
},
},
},
exp: fmt.Sprintf(goldenPrefixFmt, "1_foo__cvd"),
},
{
name: "android ci build specific kernel build",
req: apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
AndroidCIBuildSource: &apiv1.AndroidCIBuildSource{
MainBuild: &apiv1.AndroidCIBuild{
BuildID: "1",
Target: "foo",
},
KernelBuild: &apiv1.AndroidCIBuild{
BuildID: "137",
Target: "bar",
},
},
},
},
},
exp: fmt.Sprintf(goldenPrefixFmt, "1_foo__cvd") +
" --kernel_path=" + dir + "/artifacts/137_bar__kernel/bzImage" +
" --initramfs_path=" + dir + "/artifacts/137_bar__kernel/initramfs.img",
},
{
name: "android ci build specific bootloader build",
req: apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
AndroidCIBuildSource: &apiv1.AndroidCIBuildSource{
MainBuild: &apiv1.AndroidCIBuild{
BuildID: "1",
Target: "foo",
},
BootloaderBuild: &apiv1.AndroidCIBuild{
BuildID: "137",
Target: "bar",
},
},
},
},
},
exp: fmt.Sprintf(goldenPrefixFmt, "1_foo__cvd") +
" --bootloader=" + dir + "/artifacts/137_bar__bootloader/u-boot.rom",
},
{
name: "android ci build with system image build",
req: apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
AndroidCIBuildSource: &apiv1.AndroidCIBuildSource{
MainBuild: &apiv1.AndroidCIBuild{
BuildID: "1",
Target: "foo",
},
SystemImageBuild: &apiv1.AndroidCIBuild{
BuildID: "137",
Target: "bar",
},
},
},
},
},
exp: fmt.Sprintf(goldenPrefixFmt, fakeUUID+"__custom_cvd"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var usedCmdName string
var usedCmdArgs []string
execContext := func(cxt context.Context, name string, args ...string) *exec.Cmd {
if containsStr(args, "start") {
usedCmdName = name
usedCmdArgs = args
}
return exec.Command("true")
}
paths := IMPaths{ArtifactsRootDir: dir + "/artifacts"}
om := NewMapOM()
buildAPI := &fakeBuildAPI{}
artifactsFetcher := newBuildAPIArtifactsFetcher(buildAPI)
cvdExecContext := newCVDExecContext(execContext, fakeCVDUser)
cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(cvdExecContext, "")
opts := CreateCVDActionOpts{
Request: &tc.req,
HostValidator: &AlwaysSucceedsValidator{},
Paths: paths,
OperationManager: om,
ExecContext: execContext,
BuildAPI: buildAPI,
ArtifactsFetcher: artifactsFetcher,
CVDBundleFetcher: cvdBundleFetcher,
UUIDGen: fakeUUIDGen,
CVDUser: fakeCVDUser,
}
action := NewCreateCVDAction(opts)

op, err := action.Run()

orchtesting.FatalIfNil(t, err)
_, err = om.Wait(op.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
got := usedCmdName + " " + strings.Join(usedCmdArgs, " ")
if diff := cmp.Diff(tc.exp, got); diff != "" {
t.Errorf("command line mismatch (-want +got):\n%s", diff)
}
// Cleanup
os.RemoveAll(dir)
os.MkdirAll(dir, 0774)
})
}
}

type fakeUADirRes struct {
Dir string
}

func (r *fakeUADirRes) GetDirPath(string) string { return r.Dir }

func TestCreateCVDFromUserBuildVerifyStartCVDCmdArgs(t *testing.T) {
dir := orchtesting.TempDir(t)
defer orchtesting.RemoveDir(t, dir)
ioutil.WriteFile(dir+"/vbmeta.img", []byte{}, 0755)
ioutil.WriteFile(dir+"/vbmeta_system.img", []byte{}, 0755)
expected := fmt.Sprintf("sudo -u fakecvduser "+
"ANDROID_HOST_OUT=%[1]s %[2]s --group_name=cvd start --daemon --report_anonymous_usage_stats=y"+
" --base_instance_num=1 --system_image_dir=%[1]s", dir, cvd.CVDBin)
var usedCmdName string
var usedCmdArgs []string
execContext := func(cxt context.Context, name string, args ...string) *exec.Cmd {
if containsStr(args, "start") {
usedCmdName = name
usedCmdArgs = args
}
return exec.Command("true")
}
paths := IMPaths{ArtifactsRootDir: dir + "/artifacts"}
om := NewMapOM()
buildAPI := &fakeBuildAPI{}
artifactsFetcher := newBuildAPIArtifactsFetcher(buildAPI)
cvdExecContext := newCVDExecContext(execContext, fakeCVDUser)
cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(cvdExecContext, "")
req := apiv1.CreateCVDRequest{
CVD: &apiv1.CVD{
BuildSource: &apiv1.BuildSource{
UserBuildSource: &apiv1.UserBuildSource{ArtifactsDir: "baz"},
},
},
}
opts := CreateCVDActionOpts{
Request: &req,
HostValidator: &AlwaysSucceedsValidator{},
Paths: paths,
OperationManager: om,
ExecContext: execContext,
BuildAPI: buildAPI,
ArtifactsFetcher: artifactsFetcher,
CVDBundleFetcher: cvdBundleFetcher,
UserArtifactsDirResolver: &fakeUADirRes{dir},
UUIDGen: fakeUUIDGen,
CVDUser: fakeCVDUser,
}
action := NewCreateCVDAction(opts)

op, err := action.Run()

orchtesting.FatalIfNil(t, err)
_, err = om.Wait(op.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
got := usedCmdName + " " + strings.Join(usedCmdArgs, " ")
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("command line mismatch (-want +got):\n%s", diff)
}
// Cleanup
os.RemoveAll(dir)
os.MkdirAll(dir, 0774)
}

func TestCreateCVDFailsDueCVDSubCommandExecution(t *testing.T) {
dir := orchtesting.TempDir(t)
defer orchtesting.RemoveDir(t, dir)
execContext := execCtxCvdSubcmdFails
paths := IMPaths{ArtifactsRootDir: dir + "/artifacts"}
om := NewMapOM()
buildAPI := &fakeBuildAPI{}
artifactsFetcher := newBuildAPIArtifactsFetcher(buildAPI)
cvdExecContext := newCVDExecContext(execContext, fakeCVDUser)
cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(cvdExecContext, "")
opts := CreateCVDActionOpts{
Request: &apiv1.CreateCVDRequest{CVD: &apiv1.CVD{BuildSource: androidCISource("1", "foo")}},
HostValidator: &AlwaysSucceedsValidator{},
Paths: paths,
OperationManager: om,
ExecContext: execContext,
BuildAPI: buildAPI,
ArtifactsFetcher: artifactsFetcher,
CVDBundleFetcher: cvdBundleFetcher,
CVDUser: fakeCVDUser,
}
action := NewCreateCVDAction(opts)

op, err := action.Run()

orchtesting.FatalIfNil(t, err)
res, err := om.Wait(op.Name, 1*time.Second)
orchtesting.FatalIfNil(t, err)
if res.Error == nil {
t.Error("expected error")
}
}

type AlwaysFailsValidator struct{}

func (AlwaysFailsValidator) Validate() error {
Expand Down

0 comments on commit c2e8ccd

Please sign in to comment.