diff --git a/Makefile b/Makefile index 325098d60..60e810c66 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ build: go build -o osbuild-tests ./cmd/osbuild-tests/ go test -c -tags=integration -o osbuild-weldr-tests ./internal/weldrcheck/ 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/ + go test -c -tags=integration -o osbuild-rcm-tests ./cmd/osbuild-rcm-tests/main_test.go .PHONY: install install: diff --git a/cmd/osbuild-rcm-tests/main.go b/cmd/osbuild-rcm-tests/main_test.go similarity index 54% rename from cmd/osbuild-rcm-tests/main.go rename to cmd/osbuild-rcm-tests/main_test.go index 6756eedb7..110db1e1a 100644 --- a/cmd/osbuild-rcm-tests/main.go +++ b/cmd/osbuild-rcm-tests/main_test.go @@ -1,18 +1,20 @@ // osbuild-rcm-tests run tests against running osbuild-composer instance that was spawned using the // osbuild-rcm.socket unit. It defines the expected use cases of the RCM API. + +// +build integration + package main import ( "encoding/json" "github.com/google/uuid" - "log" + "github.com/stretchr/testify/require" "net/http" - "os" "strings" + "testing" ) -func main() { - failed := false +func TestRCM(t *testing.T) { // This is the first request the user sends to osbuild. submitBody := ` { @@ -41,40 +43,18 @@ func main() { // Case 1: POST request resp, err := http.Post(socket+endpoint, "application/json", strings.NewReader(submitBody)) - if err != nil { - log.Fatal("Failed to submit a compose: ", err.Error()) - } - if resp.StatusCode != 200 { - log.Print("Error: the ", endpoint, " returned non 200 status. Full response: ", resp) - failed = true - } else { - err = json.NewDecoder(resp.Body).Decode(&submitResponse) - if err != nil { - log.Fatal("Failed to decode JSON response from ", endpoint) - } - log.Print("Success: the ", endpoint, " returned compose UUID: ", submitResponse.UUID) - } + require.Nilf(t, err, "Failed to submit a compose: %v", err) + require.Equalf(t, resp.StatusCode, 200, "Error: the %v returned non 200 status. Full response: %v", endpoint, resp) + err = json.NewDecoder(resp.Body).Decode(&submitResponse) + require.Nilf(t, err, "Failed to decode JSON response from %v", endpoint) // Case 2: GET status statusEndpoint := endpoint + "/" + submitResponse.UUID.String() resp, err = http.Get(socket + statusEndpoint) - if err != nil { - log.Fatal("Failed to get a status: ", err.Error()) - } - if resp.StatusCode != 200 { - log.Print("Error: the ", endpoint, " returned non 200 status. Full response: ", resp) - failed = true - } else { - err = json.NewDecoder(resp.Body).Decode(&statusResponse) - if err != nil { - log.Fatal("Failed to decode JSON response from ", endpoint) - } - log.Print("Success: the ", statusEndpoint, " returned status: ", statusResponse.Status) - } + require.Nilf(t, err, "Failed to get a status: %v", err) + require.Equalf(t, resp.StatusCode, 200, "Error: the %v returned non 200 status. Full response: %v", endpoint, resp) + err = json.NewDecoder(resp.Body).Decode(&statusResponse) + require.Nilf(t, err, "Failed to decode JSON response from %v", endpoint) - // If anything failed return non-zero exit code. - if failed { - os.Exit(1) - } } diff --git a/golang-github-osbuild-composer.spec b/golang-github-osbuild-composer.spec index b64b26863..d08596751 100644 --- a/golang-github-osbuild-composer.spec +++ b/golang-github-osbuild-composer.spec @@ -68,7 +68,6 @@ 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-image-tests %{goipath}/cmd/osbuild-image-tests -%gobuild -o _bin/osbuild-rcm-tests %{goipath}/cmd/osbuild-rcm-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 @@ -78,6 +77,7 @@ 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 go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-weldr-tests %{goipath}/internal/weldrcheck/ +go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-rcm-tests %{goipath}/cmd/osbuild-rcm-tests %install install -m 0755 -vd %{buildroot}%{_libexecdir}/osbuild-composer