weldrcheck: Setup a temporary repository for testing

This commit is contained in:
Brian C. Lane 2020-03-19 17:33:43 -07:00 committed by Tom Gundersen
parent 3005486cb4
commit 52febbdfa6
2 changed files with 48 additions and 3 deletions

View file

@ -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

View file

@ -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)
}