dnfjson: skip dnf-json tests if dnf python module isn't available

On systems where `dnf` and the Python module aren't available, skip the
unit tests that call into the `dnf-json` script.
A test flag, `-force-dnf` is added to avoid this check and run the tests
unconditionally.  This is useful for cases where the sniff check might
fail for wrong reasons or, more importantly, for cases where we want to
be sure the tests are ran and consider a missing `dnf` module to be an
error state (e.g., in CI).
This commit is contained in:
Achilleas Koutsou 2022-05-05 13:19:20 +02:00 committed by Tom Gundersen
parent 387b982a79
commit 5a01d6b339

View file

@ -1,7 +1,10 @@
package dnfjson
import (
"flag"
"fmt"
"os"
"os/exec"
"testing"
"github.com/osbuild/osbuild-composer/internal/mocks/rpmrepo"
@ -9,7 +12,26 @@ import (
"github.com/stretchr/testify/assert"
)
var forceDNF = flag.Bool("force-dnf", false, "force dnf testing, making them fail instead of skip if dnf isn't installed")
func dnfInstalled() bool {
cmd := exec.Command("python3", "-c", "import dnf")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "failed to import dnf: %s\n", err.Error())
return false
}
return true
}
func TestDepsolver(t *testing.T) {
if !*forceDNF {
// dnf tests aren't forced: skip them if the dnf sniff check fails
if !dnfInstalled() {
t.Skip()
}
}
s := rpmrepo.NewTestServer()
defer s.Close()