From bd4638905946d042467b5d8027aa8ebe2dfb4586 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 5 Mar 2020 12:58:41 +0100 Subject: [PATCH] tests: use go's test framework in osbuild-dnf-json-tests This allows us to take advantage of the `testing` package. It also gives the resulting test binary common command line arguments (same as `go test`). Tests need to be compiled with `go test -c`, which injects a `Main()` that calls the Test* functions. This is not supported by the golang rpm macros. Thus, build this binary by calling `go test -c` directly, but taking care to pass the same linker flags as the `%gobuild` macro. Mark the test binary with the `integration` build constraint, so that `go test ./...` doesn't pick them up. That's only for unit tests. The idea is to move all other test binaries to this scheme as well. Spec file changes by Lars Karlitski --- Makefile | 2 +- .../{main.go => main_test.go} | 43 ++++++++----------- golang-github-osbuild-composer.spec | 9 +++- 3 files changed, 27 insertions(+), 27 deletions(-) rename cmd/osbuild-dnf-json-tests/{main.go => main_test.go} (68%) diff --git a/Makefile b/Makefile index e20a9405e..a1053f3cc 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ build: go build -o osbuild-upload-aws ./cmd/osbuild-upload-aws/ go build -o osbuild-tests ./cmd/osbuild-tests/ go build -o osbuild-weldr-tests ./cmd/osbuild-weldr-tests/ - go build -o osbuild-dnf-json-tests ./cmd/osbuild-dnf-json-tests/ + go test -c -tags=integration -o osbuild-dnf-json-tests ./cmd/osbuild-dnf-json-tests/main_test.go go build -o osbuild-rcm-tests ./cmd/osbuild-rcm-tests/ .PHONY: install diff --git a/cmd/osbuild-dnf-json-tests/main.go b/cmd/osbuild-dnf-json-tests/main_test.go similarity index 68% rename from cmd/osbuild-dnf-json-tests/main.go rename to cmd/osbuild-dnf-json-tests/main_test.go index 1b58a9c42..968963cfb 100644 --- a/cmd/osbuild-dnf-json-tests/main.go +++ b/cmd/osbuild-dnf-json-tests/main_test.go @@ -1,31 +1,19 @@ // This package contains tests related to dnf-json and rpmmd package. + +// +build integration + package main import ( "fmt" "github.com/osbuild/osbuild-composer/internal/rpmmd" "io/ioutil" - "log" "os" "os/exec" + "testing" "path" ) -func main() { - // Tests that the package wrapping dnf-json works as expected - dir, err := setUpTemporaryRepository() - defer func(dir string) { - err := tearDownTemporaryRepository(dir) - if err != nil { - log.Print("Warning: failed to clean up temporary repository.") - } - }(dir) - if err != nil { - log.Panic("Failed to set up temporary repository:", err) - } - TestFetchChecksum(false, dir) -} - func setUpTemporaryRepository() (string, error) { dir, err := ioutil.TempDir("/tmp", "osbuild-composer-test-") if err != nil { @@ -47,25 +35,30 @@ func tearDownTemporaryRepository(dir string) error { return os.RemoveAll(dir) } -func TestFetchChecksum(quiet bool, dir string) { +func TestFetchChecksum(t *testing.T) { + dir, err := setUpTemporaryRepository() + defer func(dir string) { + err := tearDownTemporaryRepository(dir) + if err != nil { + t.Errorf("Warning: failed to clean up temporary repository.") + } + }(dir) + if err != nil { + t.Fatalf("Failed to set up temporary repository: %v", err) + } + repoCfg := rpmmd.RepoConfig{ Id: "repo", Name: "repo", BaseURL: fmt.Sprintf("file://%s", dir), IgnoreSSL: true, } - if !quiet { - log.Println("Running TestFetchChecksum on:", dir) - } rpmMetadata := rpmmd.NewRPMMD(path.Join(dir, "rpmmd")) _, c, err := rpmMetadata.FetchMetadata([]rpmmd.RepoConfig{repoCfg}, "platform:f31") if err != nil { - log.Panic("Failed to fetch checksum:", err) + t.Fatalf("Failed to fetch checksum: %v", err) } if c["repo"] == "" { - log.Panic("The checksum is empty") - } - if !quiet { - log.Println("TestFetchChecksum: SUCCESS") + t.Errorf("The checksum is empty") } } diff --git a/golang-github-osbuild-composer.spec b/golang-github-osbuild-composer.spec index aec5e2be2..5b87fe09e 100644 --- a/golang-github-osbuild-composer.spec +++ b/golang-github-osbuild-composer.spec @@ -68,9 +68,16 @@ export GOFLAGS=-mod=vendor %gobuild -o _bin/osbuild-worker %{goipath}/cmd/osbuild-worker %gobuild -o _bin/osbuild-tests %{goipath}/cmd/osbuild-tests %gobuild -o _bin/osbuild-weldr-tests %{goipath}/cmd/osbuild-weldr-tests -%gobuild -o _bin/osbuild-dnf-json-tests %{goipath}/cmd/osbuild-dnf-json-tests %gobuild -o _bin/osbuild-image-tests %{goipath}/cmd/osbuild-image-tests +# Build test binaries with `go test -c`, so that they can take advantage of +# golang's testing package. The golang rpm macros don't support building them +# directly. Thus, do it manually, taking care to also include a build id. + +TEST_LDFLAGS="${LDFLAGS:-} -B 0x$(od -N 20 -An -tx1 -w100 /dev/urandom | tr -d ' ')" + +go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-dnf-json-tests %{goipath}/cmd/osbuild-dnf-json-tests + %install install -m 0755 -vd %{buildroot}%{_libexecdir}/osbuild-composer install -m 0755 -vp _bin/osbuild-composer %{buildroot}%{_libexecdir}/osbuild-composer/