From 52febbdfa69a094b82185b592d7089a7ce2b7ed9 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 19 Mar 2020 17:33:43 -0700 Subject: [PATCH] weldrcheck: Setup a temporary repository for testing --- internal/weldrcheck/blueprints_test.go | 21 ++++++++++++++++-- internal/weldrcheck/utils.go | 30 +++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/internal/weldrcheck/blueprints_test.go b/internal/weldrcheck/blueprints_test.go index 1e9e47fbb..28ccace53 100644 --- a/internal/weldrcheck/blueprints_test.go +++ b/internal/weldrcheck/blueprints_test.go @@ -33,12 +33,29 @@ var testState *TestState // Also makes sure there is a running server to test against func TestMain(m *testing.M) { var err error - testState, err = setupTestState("/run/weldr/api.socket", 60*time.Second) + testState, err = setUpTestState("/run/weldr/api.socket", 60*time.Second) if err != nil { fmt.Printf("ERROR: Test setup failed: %s\n", err) os.Exit(1) } - os.Exit(m.Run()) + + // Setup the test repo + dir, err := setUpTemporaryRepository() + if err != nil { + fmt.Printf("ERROR: Test repo setup failed: %s\n", err) + os.Exit(1) + } + testState.repoDir = dir + + // Run the tests + rc := m.Run() + + // Cleanup after the tests + err = tearDownTemporaryRepository(dir) + if err != nil { + fmt.Printf("ERROR: Failed to clean up temporary repository: %s\n", err) + } + os.Exit(rc) } // POST a new TOML blueprint diff --git a/internal/weldrcheck/utils.go b/internal/weldrcheck/utils.go index 9fec45263..0664c6dd1 100644 --- a/internal/weldrcheck/utils.go +++ b/internal/weldrcheck/utils.go @@ -9,8 +9,12 @@ package weldrcheck import ( "context" "fmt" + "io/ioutil" "net" "net/http" + "os" + "os/exec" + "path" "sort" "strconv" "time" @@ -21,6 +25,7 @@ import ( type TestState struct { socket *http.Client apiVersion int + repoDir string } // isStringInSlice returns true if the string is present, false if not @@ -34,7 +39,7 @@ func isStringInSlice(slice []string, s string) bool { return false } -func setupTestState(socketPath string, timeout time.Duration) (*TestState, error) { +func setUpTestState(socketPath string, timeout time.Duration) (*TestState, error) { var state TestState state.socket = &http.Client{ // TODO This may be too short/simple for downloading images @@ -64,3 +69,26 @@ 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) +}