From 0041ae565566ad2dd7b2de65aa16d7ecbe00d15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Wed, 22 Apr 2020 15:14:08 +0200 Subject: [PATCH] tests/image: move constants to a subpackage The cmd/osbuild-image-tests package is becoming bigger than I would like to. It will be nice to split it to some smaller pieces at some point. This commit does the first step - splits off the first subpackage containing all the constants. --- cmd/osbuild-image-tests/constants-travis.go | 32 ------------------- cmd/osbuild-image-tests/constants.go | 28 ---------------- .../constants/constants-travis.go | 32 +++++++++++++++++++ .../constants/constants.go | 28 ++++++++++++++++ cmd/osbuild-image-tests/context-managers.go | 8 +++-- cmd/osbuild-image-tests/main_test.go | 16 ++++++---- 6 files changed, 74 insertions(+), 70 deletions(-) delete mode 100644 cmd/osbuild-image-tests/constants-travis.go delete mode 100644 cmd/osbuild-image-tests/constants.go create mode 100644 cmd/osbuild-image-tests/constants/constants-travis.go create mode 100644 cmd/osbuild-image-tests/constants/constants.go diff --git a/cmd/osbuild-image-tests/constants-travis.go b/cmd/osbuild-image-tests/constants-travis.go deleted file mode 100644 index f198143db..000000000 --- a/cmd/osbuild-image-tests/constants-travis.go +++ /dev/null @@ -1,32 +0,0 @@ -// +build travis - -package main - -import "os/exec" - -func getOsbuildCommand(store string) *exec.Cmd { - cmd := exec.Command( - "python3", - "-m", "osbuild", - "--libdir", ".", - "--store", store, - "--json", - "-", - ) - cmd.Dir = "osbuild" - return cmd -} - -var testPaths = struct { - imageInfo string - privateKey string - testCasesDirectory string - userData string - metaData string -}{ - imageInfo: "tools/image-info", - privateKey: "test/keyring/id_rsa", - testCasesDirectory: "test/cases", - userData: "test/cloud-init/user-data", - metaData: "test/cloud-init/meta-data", -} diff --git a/cmd/osbuild-image-tests/constants.go b/cmd/osbuild-image-tests/constants.go deleted file mode 100644 index 2a6f17c34..000000000 --- a/cmd/osbuild-image-tests/constants.go +++ /dev/null @@ -1,28 +0,0 @@ -// +build integration,!travis - -package main - -import "os/exec" - -func getOsbuildCommand(store string) *exec.Cmd { - return exec.Command( - "osbuild", - "--store", store, - "--json", - "-", - ) -} - -var testPaths = struct { - imageInfo string - privateKey string - testCasesDirectory string - userData string - metaData string -}{ - imageInfo: "/usr/libexec/osbuild-composer/image-info", - privateKey: "/usr/share/tests/osbuild-composer/keyring/id_rsa", - testCasesDirectory: "/usr/share/tests/osbuild-composer/cases", - userData: "/usr/share/tests/osbuild-composer/cloud-init/user-data", - metaData: "/usr/share/tests/osbuild-composer/cloud-init/meta-data", -} diff --git a/cmd/osbuild-image-tests/constants/constants-travis.go b/cmd/osbuild-image-tests/constants/constants-travis.go new file mode 100644 index 000000000..22eda6bce --- /dev/null +++ b/cmd/osbuild-image-tests/constants/constants-travis.go @@ -0,0 +1,32 @@ +// +build travis + +package constants + +import "os/exec" + +func GetOsbuildCommand(store string) *exec.Cmd { + cmd := exec.Command( + "python3", + "-m", "osbuild", + "--libdir", ".", + "--store", store, + "--json", + "-", + ) + cmd.Dir = "osbuild" + return cmd +} + +var TestPaths = struct { + ImageInfo string + PrivateKey string + TestCasesDirectory string + UserData string + MetaData string +}{ + ImageInfo: "tools/image-info", + PrivateKey: "test/keyring/id_rsa", + TestCasesDirectory: "test/cases", + UserData: "test/cloud-init/user-data", + MetaData: "test/cloud-init/meta-data", +} diff --git a/cmd/osbuild-image-tests/constants/constants.go b/cmd/osbuild-image-tests/constants/constants.go new file mode 100644 index 000000000..17f594614 --- /dev/null +++ b/cmd/osbuild-image-tests/constants/constants.go @@ -0,0 +1,28 @@ +// +build integration,!travis + +package constants + +import "os/exec" + +func GetOsbuildCommand(store string) *exec.Cmd { + return exec.Command( + "osbuild", + "--store", store, + "--json", + "-", + ) +} + +var TestPaths = struct { + ImageInfo string + PrivateKey string + TestCasesDirectory string + UserData string + MetaData string +}{ + ImageInfo: "/usr/libexec/osbuild-composer/image-info", + PrivateKey: "/usr/share/tests/osbuild-composer/keyring/id_rsa", + TestCasesDirectory: "/usr/share/tests/osbuild-composer/cases", + UserData: "/usr/share/tests/osbuild-composer/cloud-init/user-data", + MetaData: "/usr/share/tests/osbuild-composer/cloud-init/meta-data", +} diff --git a/cmd/osbuild-image-tests/context-managers.go b/cmd/osbuild-image-tests/context-managers.go index 37c78302e..c641b3a1f 100644 --- a/cmd/osbuild-image-tests/context-managers.go +++ b/cmd/osbuild-image-tests/context-managers.go @@ -4,7 +4,6 @@ package main import ( "fmt" - "github.com/osbuild/osbuild-composer/internal/common" "io" "io/ioutil" "log" @@ -13,6 +12,9 @@ import ( "runtime" "strconv" "time" + + "github.com/osbuild/osbuild-composer/cmd/osbuild-image-tests/constants" + "github.com/osbuild/osbuild-composer/internal/common" ) // withNetworkNamespace provides the function f with a new network namespace @@ -97,8 +99,8 @@ func withBootedQemuImage(image string, ns netNS, f func() error) error { return withTempFile("", "osbuild-image-tests-cloudinit", func(cloudInitFile *os.File) error { err := writeCloudInitISO( cloudInitFile, - testPaths.userData, - testPaths.metaData, + constants.TestPaths.UserData, + constants.TestPaths.MetaData, ) if err != nil { return err diff --git a/cmd/osbuild-image-tests/main_test.go b/cmd/osbuild-image-tests/main_test.go index c2e1a6029..f404d1bea 100644 --- a/cmd/osbuild-image-tests/main_test.go +++ b/cmd/osbuild-image-tests/main_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/osbuild/osbuild-composer/cmd/osbuild-image-tests/constants" "github.com/osbuild/osbuild-composer/internal/common" ) @@ -48,7 +49,8 @@ func runOsbuild(manifest []byte, store string) (string, error) { // See https://github.com/osbuild/osbuild/issues/351 osbuildMutex.Lock() defer osbuildMutex.Unlock() - cmd := getOsbuildCommand(store) + + cmd := constants.GetOsbuildCommand(store) cmd.Stderr = os.Stderr cmd.Stdin = bytes.NewReader(manifest) @@ -85,7 +87,7 @@ func testImageInfo(t *testing.T, imagePath string, rawImageInfoExpected []byte) err := json.Unmarshal(rawImageInfoExpected, &imageInfoExpected) require.NoErrorf(t, err, "cannot decode expected image info: %#v", err) - cmd := exec.Command(testPaths.imageInfo, imagePath) + cmd := exec.Command(constants.TestPaths.ImageInfo, imagePath) cmd.Stderr = os.Stderr reader, writer := io.Pipe() cmd.Stdout = writer @@ -190,7 +192,7 @@ func testSSH(t *testing.T, address string, privateKey string, ns *netNS) { func testBootUsingQemu(t *testing.T, imagePath string) { err := withNetworkNamespace(func(ns netNS) error { return withBootedQemuImage(imagePath, ns, func() error { - testSSH(t, "localhost", testPaths.privateKey, &ns) + testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns) return nil }) }) @@ -200,7 +202,7 @@ func testBootUsingQemu(t *testing.T, imagePath string) { func testBootUsingNspawnImage(t *testing.T, imagePath string, outputID string) { err := withNetworkNamespace(func(ns netNS) error { return withBootedNspawnImage(imagePath, outputID, ns, func() error { - testSSH(t, "localhost", testPaths.privateKey, &ns) + testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns) return nil }) }) @@ -211,7 +213,7 @@ func testBootUsingNspawnDirectory(t *testing.T, imagePath string, outputID strin err := withNetworkNamespace(func(ns netNS) error { return withExtractedTarArchive(imagePath, func(dir string) error { return withBootedNspawnDirectory(dir, outputID, ns, func() error { - testSSH(t, "localhost", testPaths.privateKey, &ns) + testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns) return nil }) }) @@ -342,7 +344,7 @@ func runTestcase(t *testing.T, testcase testcaseStruct) { // getAllCases returns paths to all testcases in the testcase directory func getAllCases() ([]string, error) { - cases, err := ioutil.ReadDir(testPaths.testCasesDirectory) + cases, err := ioutil.ReadDir(constants.TestPaths.TestCasesDirectory) if err != nil { return nil, fmt.Errorf("cannot list test cases: %#v", err) } @@ -353,7 +355,7 @@ func getAllCases() ([]string, error) { continue } - casePath := fmt.Sprintf("%s/%s", testPaths.testCasesDirectory, c.Name()) + casePath := fmt.Sprintf("%s/%s", constants.TestPaths.TestCasesDirectory, c.Name()) casesPaths = append(casesPaths, casePath) }