From 3af34fba3f896223ade1866f286e0ad1312f2c41 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 20 Mar 2020 08:59:30 -0700 Subject: [PATCH] test: Move temporary repo setup into test/helpers.go This allows it to be shared between the dnf-json tests and weldrcheck. --- cmd/osbuild-dnf-json-tests/main_test.go | 34 +++++-------------------- internal/test/helpers.go | 25 ++++++++++++++++++ internal/weldrcheck/blueprints_test.go | 5 ++-- internal/weldrcheck/utils.go | 27 -------------------- 4 files changed, 34 insertions(+), 57 deletions(-) diff --git a/cmd/osbuild-dnf-json-tests/main_test.go b/cmd/osbuild-dnf-json-tests/main_test.go index 44624825f..13595af35 100644 --- a/cmd/osbuild-dnf-json-tests/main_test.go +++ b/cmd/osbuild-dnf-json-tests/main_test.go @@ -6,40 +6,18 @@ package main import ( "fmt" - "github.com/osbuild/osbuild-composer/internal/rpmmd" - "io/ioutil" - "os" - "os/exec" - "testing" "github.com/stretchr/testify/assert" "path" + "testing" + + "github.com/osbuild/osbuild-composer/internal/rpmmd" + "github.com/osbuild/osbuild-composer/internal/test" ) -func setUpTemporaryRepository() (string, error) { - dir, err := ioutil.TempDir("/tmp", "osbuild-composer-test-") - if err != nil { - return "", err - } - cmd := exec.Command("createrepo_c", path.Join(dir)) - err = cmd.Start() - if err != nil { - return "", err - } - err = cmd.Wait() - if err != nil { - return "", err - } - return dir, nil -} - -func tearDownTemporaryRepository(dir string) error { - return os.RemoveAll(dir) -} - func TestFetchChecksum(t *testing.T) { - dir, err := setUpTemporaryRepository() + dir, err := test.SetUpTemporaryRepository() defer func(dir string) { - err := tearDownTemporaryRepository(dir) + err := test.TearDownTemporaryRepository(dir) assert.Nil(t, err, "Failed to clean up temporary repository.") }(dir) assert.Nilf(t, err, "Failed to set up temporary repository: %v", err) diff --git a/internal/test/helpers.go b/internal/test/helpers.go index cd6360e89..de69a713b 100644 --- a/internal/test/helpers.go +++ b/internal/test/helpers.go @@ -9,6 +9,8 @@ import ( "net/http" "net/http/httptest" "os" + "os/exec" + "path" "strings" "testing" "time" @@ -176,3 +178,26 @@ func IgnoreUuids() cmp.Option { func Ignore(what string) cmp.Option { return cmp.FilterPath(func(p cmp.Path) bool { return p.String() == what }, cmp.Ignore()) } + +// Create a temporary repository +func SetUpTemporaryRepository() (string, error) { + dir, err := ioutil.TempDir("/tmp", "osbuild-composer-test-") + if err != nil { + return "", err + } + cmd := exec.Command("createrepo_c", path.Join(dir)) + err = cmd.Start() + if err != nil { + return "", err + } + err = cmd.Wait() + if err != nil { + return "", err + } + return dir, nil +} + +// Remove the temporary repository +func TearDownTemporaryRepository(dir string) error { + return os.RemoveAll(dir) +} diff --git a/internal/weldrcheck/blueprints_test.go b/internal/weldrcheck/blueprints_test.go index 28ccace53..8dc9086e3 100644 --- a/internal/weldrcheck/blueprints_test.go +++ b/internal/weldrcheck/blueprints_test.go @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/osbuild/osbuild-composer/internal/client" + "github.com/osbuild/osbuild-composer/internal/test" ) // Hold test state to share between tests @@ -40,7 +41,7 @@ func TestMain(m *testing.M) { } // Setup the test repo - dir, err := setUpTemporaryRepository() + dir, err := test.SetUpTemporaryRepository() if err != nil { fmt.Printf("ERROR: Test repo setup failed: %s\n", err) os.Exit(1) @@ -51,7 +52,7 @@ func TestMain(m *testing.M) { rc := m.Run() // Cleanup after the tests - err = tearDownTemporaryRepository(dir) + err = test.TearDownTemporaryRepository(dir) if err != nil { fmt.Printf("ERROR: Failed to clean up temporary repository: %s\n", err) } diff --git a/internal/weldrcheck/utils.go b/internal/weldrcheck/utils.go index 0664c6dd1..a5a51e6b0 100644 --- a/internal/weldrcheck/utils.go +++ b/internal/weldrcheck/utils.go @@ -9,12 +9,8 @@ package weldrcheck import ( "context" "fmt" - "io/ioutil" "net" "net/http" - "os" - "os/exec" - "path" "sort" "strconv" "time" @@ -69,26 +65,3 @@ func setUpTestState(socketPath string, timeout time.Duration) (*TestState, error return &state, nil } - -// Create a temporary repository -func setUpTemporaryRepository() (string, error) { - dir, err := ioutil.TempDir("/tmp", "osbuild-composer-test-") - if err != nil { - return "", err - } - cmd := exec.Command("createrepo_c", path.Join(dir)) - err = cmd.Start() - if err != nil { - return "", err - } - err = cmd.Wait() - if err != nil { - return "", err - } - return dir, nil -} - -// Remove the temporary repository -func tearDownTemporaryRepository(dir string) error { - return os.RemoveAll(dir) -}