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:
parent
4a0fea8b6d
commit
bd46389059
3 changed files with 27 additions and 27 deletions
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue