tests/image: refactor constants

It's not very clear that the constants are indeed constants. This commit moves
them to a new struct. This way it should be more clear that those values are
constants.
This commit is contained in:
Ondřej Budai 2020-04-02 11:09:22 +02:00 committed by Tom Gundersen
parent 640a9fcd92
commit 00f903b879
4 changed files with 34 additions and 18 deletions

View file

@ -17,8 +17,16 @@ func getOsbuildCommand(store string) *exec.Cmd {
return cmd
}
var imageInfoPath = "tools/image-info"
var privateKeyPath = "test/keyring/id_rsa"
var testCasesDirectoryPath = "test/cases"
var userDataPath = "test/cloud-init/user-data"
var metaDataPath = "test/cloud-init/meta-data"
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",
}

View file

@ -13,8 +13,16 @@ func getOsbuildCommand(store string) *exec.Cmd {
)
}
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"
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",
}

View file

@ -94,8 +94,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,
userDataPath,
metaDataPath,
testPaths.userData,
testPaths.metaData,
)
if err != nil {
return err

View file

@ -98,7 +98,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(imageInfoPath, imagePath)
cmd := exec.Command(testPaths.imageInfo, imagePath)
cmd.Stderr = os.Stderr
reader, writer := io.Pipe()
cmd.Stdout = writer
@ -212,7 +212,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
case "qemu-extract":
err := withNetworkNamespace(func(ns netNS) error {
return withBootedQemuImage(imagePath, ns, func() error {
testSSH(t, "localhost", privateKeyPath, &ns)
testSSH(t, "localhost", testPaths.privateKey, &ns)
return nil
})
})
@ -221,7 +221,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
case "nspawn":
err := withNetworkNamespace(func(ns netNS) error {
return withBootedNspawnImage(imagePath, outputID, ns, func() error {
testSSH(t, "localhost", privateKeyPath, &ns)
testSSH(t, "localhost", testPaths.privateKey, &ns)
return nil
})
})
@ -231,7 +231,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
err := withNetworkNamespace(func(ns netNS) error {
return withExtractedTarArchive(imagePath, func(dir string) error {
return withBootedNspawnDirectory(dir, outputID, ns, func() error {
testSSH(t, "localhost", privateKeyPath, &ns)
testSSH(t, "localhost", testPaths.privateKey, &ns)
return nil
})
})
@ -293,7 +293,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(testCasesDirectoryPath)
cases, err := ioutil.ReadDir(testPaths.testCasesDirectory)
if err != nil {
return nil, fmt.Errorf("cannot list test cases: %#v", err)
}
@ -304,7 +304,7 @@ func getAllCases() ([]string, error) {
continue
}
casePath := fmt.Sprintf("%s/%s", testCasesDirectoryPath, c.Name())
casePath := fmt.Sprintf("%s/%s", testPaths.testCasesDirectory, c.Name())
casesPaths = append(casesPaths, casePath)
}