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>
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
// 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"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
"path"
|
|
)
|
|
|
|
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()
|
|
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,
|
|
}
|
|
rpmMetadata := rpmmd.NewRPMMD(path.Join(dir, "rpmmd"))
|
|
_, c, err := rpmMetadata.FetchMetadata([]rpmmd.RepoConfig{repoCfg}, "platform:f31")
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch checksum: %v", err)
|
|
}
|
|
if c["repo"] == "" {
|
|
t.Errorf("The checksum is empty")
|
|
}
|
|
}
|