From 5a01d6b339c6669caf7b8da2c6eb138ef79c7640 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Thu, 5 May 2022 13:19:20 +0200 Subject: [PATCH] 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). --- internal/dnfjson/dnfjson_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/dnfjson/dnfjson_test.go b/internal/dnfjson/dnfjson_test.go index bdd3b76d0..b8f821204 100644 --- a/internal/dnfjson/dnfjson_test.go +++ b/internal/dnfjson/dnfjson_test.go @@ -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()