test: Move temporary repo setup into test/helpers.go

This allows it to be shared between the dnf-json tests and weldrcheck.
This commit is contained in:
Brian C. Lane 2020-03-20 08:59:30 -07:00 committed by Tom Gundersen
parent 222d09499c
commit 3af34fba3f
4 changed files with 34 additions and 57 deletions

View file

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

View file

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

View file

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

View file

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