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.
This commit is contained in:
Ondřej Budai 2020-04-22 15:14:08 +02:00 committed by Tom Gundersen
parent b109ec878e
commit 0041ae5655
6 changed files with 74 additions and 70 deletions

View file

@ -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",
}

View file

@ -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",
}

View file

@ -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",
}

View file

@ -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",
}

View file

@ -4,7 +4,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/osbuild/osbuild-composer/internal/common"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -13,6 +12,9 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"time" "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 // 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 { return withTempFile("", "osbuild-image-tests-cloudinit", func(cloudInitFile *os.File) error {
err := writeCloudInitISO( err := writeCloudInitISO(
cloudInitFile, cloudInitFile,
testPaths.userData, constants.TestPaths.UserData,
testPaths.metaData, constants.TestPaths.MetaData,
) )
if err != nil { if err != nil {
return err return err

View file

@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/osbuild/osbuild-composer/cmd/osbuild-image-tests/constants"
"github.com/osbuild/osbuild-composer/internal/common" "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 // See https://github.com/osbuild/osbuild/issues/351
osbuildMutex.Lock() osbuildMutex.Lock()
defer osbuildMutex.Unlock() defer osbuildMutex.Unlock()
cmd := getOsbuildCommand(store)
cmd := constants.GetOsbuildCommand(store)
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdin = bytes.NewReader(manifest) cmd.Stdin = bytes.NewReader(manifest)
@ -85,7 +87,7 @@ func testImageInfo(t *testing.T, imagePath string, rawImageInfoExpected []byte)
err := json.Unmarshal(rawImageInfoExpected, &imageInfoExpected) err := json.Unmarshal(rawImageInfoExpected, &imageInfoExpected)
require.NoErrorf(t, err, "cannot decode expected image info: %#v", err) 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 cmd.Stderr = os.Stderr
reader, writer := io.Pipe() reader, writer := io.Pipe()
cmd.Stdout = writer 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) { func testBootUsingQemu(t *testing.T, imagePath string) {
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withBootedQemuImage(imagePath, ns, func() error { return withBootedQemuImage(imagePath, ns, func() error {
testSSH(t, "localhost", testPaths.privateKey, &ns) testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns)
return nil return nil
}) })
}) })
@ -200,7 +202,7 @@ func testBootUsingQemu(t *testing.T, imagePath string) {
func testBootUsingNspawnImage(t *testing.T, imagePath string, outputID string) { func testBootUsingNspawnImage(t *testing.T, imagePath string, outputID string) {
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withBootedNspawnImage(imagePath, outputID, ns, func() error { return withBootedNspawnImage(imagePath, outputID, ns, func() error {
testSSH(t, "localhost", testPaths.privateKey, &ns) testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns)
return nil return nil
}) })
}) })
@ -211,7 +213,7 @@ func testBootUsingNspawnDirectory(t *testing.T, imagePath string, outputID strin
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withExtractedTarArchive(imagePath, func(dir string) error { return withExtractedTarArchive(imagePath, func(dir string) error {
return withBootedNspawnDirectory(dir, outputID, ns, func() error { return withBootedNspawnDirectory(dir, outputID, ns, func() error {
testSSH(t, "localhost", testPaths.privateKey, &ns) testSSH(t, "localhost", constants.TestPaths.PrivateKey, &ns)
return nil return nil
}) })
}) })
@ -342,7 +344,7 @@ func runTestcase(t *testing.T, testcase testcaseStruct) {
// getAllCases returns paths to all testcases in the testcase directory // getAllCases returns paths to all testcases in the testcase directory
func getAllCases() ([]string, error) { func getAllCases() ([]string, error) {
cases, err := ioutil.ReadDir(testPaths.testCasesDirectory) cases, err := ioutil.ReadDir(constants.TestPaths.TestCasesDirectory)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot list test cases: %#v", err) return nil, fmt.Errorf("cannot list test cases: %#v", err)
} }
@ -353,7 +355,7 @@ func getAllCases() ([]string, error) {
continue continue
} }
casePath := fmt.Sprintf("%s/%s", testPaths.testCasesDirectory, c.Name()) casePath := fmt.Sprintf("%s/%s", constants.TestPaths.TestCasesDirectory, c.Name())
casesPaths = append(casesPaths, casePath) casesPaths = append(casesPaths, casePath)
} }