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 <lars@karlitski.net>
This commit is contained in:
Alexander Todorov 2020-03-05 12:58:41 +01:00 committed by Tom Gundersen
parent 4a0fea8b6d
commit bd46389059
3 changed files with 27 additions and 27 deletions

View file

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

View file

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

View file

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