From 03ae0007af55057f8bac4ff8e50ef10da58048a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Mon, 16 Mar 2020 15:52:18 +0100 Subject: [PATCH] tests/image: extract constants containing paths We need to use different values for path constants when running the tests on the Travis CI. This is the first step to achieve this. Note that this commit may be reverted when Travis CI is dropped. --- cmd/osbuild-image-tests/constants.go | 18 ++++++++++++++++++ cmd/osbuild-image-tests/context-managers.go | 4 ++-- cmd/osbuild-image-tests/main.go | 16 +++++----------- 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 cmd/osbuild-image-tests/constants.go diff --git a/cmd/osbuild-image-tests/constants.go b/cmd/osbuild-image-tests/constants.go new file mode 100644 index 000000000..5e652796d --- /dev/null +++ b/cmd/osbuild-image-tests/constants.go @@ -0,0 +1,18 @@ +package main + +import "os/exec" + +func getOsbuildCommand(store string) *exec.Cmd { + return exec.Command( + "osbuild", + "--store", store, + "--json", + "-", + ) +} + +var imageInfoPath = "/usr/libexec/osbuild-composer/image-info" +var privateKeyPath = "/usr/share/tests/osbuild-composer/keyring/id_rsa" +var testCasesDirectoryPath = "/usr/share/tests/osbuild-composer/cases" +var userDataPath = "/usr/share/tests/osbuild-composer/cloud-init/user-data" +var metaDataPath = "/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 b766e2b83..4a3459607 100644 --- a/cmd/osbuild-image-tests/context-managers.go +++ b/cmd/osbuild-image-tests/context-managers.go @@ -76,8 +76,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, - "/usr/share/tests/osbuild-composer/cloud-init/user-data", - "/usr/share/tests/osbuild-composer/cloud-init/meta-data", + userDataPath, + metaDataPath, ) if err != nil { return err diff --git a/cmd/osbuild-image-tests/main.go b/cmd/osbuild-image-tests/main.go index 818b10f22..e2933bc92 100644 --- a/cmd/osbuild-image-tests/main.go +++ b/cmd/osbuild-image-tests/main.go @@ -36,12 +36,7 @@ type testcaseStruct struct { // runOsbuild runs osbuild with the specified manifest and store. func runOsbuild(manifest []byte, store string) (string, error) { - cmd := exec.Command( - "osbuild", - "--store", store, - "--json", - "-", - ) + cmd := getOsbuildCommand(store) cmd.Stderr = os.Stderr cmd.Stdin = bytes.NewReader(manifest) @@ -104,7 +99,7 @@ func testImageInfo(imagePath string, rawImageInfoExpected []byte) error { return fmt.Errorf("cannot decode expected image info: %v", err) } - cmd := exec.Command("/usr/libexec/osbuild-composer/image-info", imagePath) + cmd := exec.Command(imageInfoPath, imagePath) cmd.Stderr = os.Stderr reader, writer := io.Pipe() cmd.Stdout = writer @@ -148,7 +143,7 @@ func trySSHOnce(ns netNS) error { ctx, "ssh", "-p", "22", - "-i", "/usr/share/tests/osbuild-composer/keyring/id_rsa", + "-i", privateKeyPath, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "redhat@localhost", @@ -302,8 +297,7 @@ func runTestcase(testcase testcaseStruct) error { // getAllCases returns paths to all testcases in the testcase directory func getAllCases() ([]string, error) { - const casesDirectory = "/usr/share/tests/osbuild-composer/cases" - cases, err := ioutil.ReadDir(casesDirectory) + cases, err := ioutil.ReadDir(testCasesDirectoryPath) if err != nil { return nil, fmt.Errorf("cannot list test cases: %v", err) } @@ -314,7 +308,7 @@ func getAllCases() ([]string, error) { continue } - casePath := fmt.Sprintf("%s/%s", casesDirectory, c.Name()) + casePath := fmt.Sprintf("%s/%s", testCasesDirectoryPath, c.Name()) casesPaths = append(casesPaths, casePath) }