test: run api tests against lorax
osbuild-composer's tests can be run against lorax-composer's api in order to ensure parity with lorax. This can be accomplished by passing the environment variable LORAX=true into the tests. Otherwise, the tests run as usual against osbuild-composer's api.
This commit is contained in:
parent
7cf8c2e875
commit
cefea2d387
1 changed files with 45 additions and 3 deletions
|
|
@ -2,10 +2,13 @@ package weldr_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -26,16 +29,55 @@ var packages = rpmmd.PackageList{
|
||||||
{Name: "package2"},
|
{Name: "package2"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRoute(t *testing.T, api *weldr.API, method, path, body string, expectedStatus int, expectedJSON string) {
|
func externalRequest(method, path, body string) *http.Response {
|
||||||
|
client := http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
|
||||||
|
return net.Dial("unix", "/run/weldr/api.socket")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(method, "http://localhost"+path, bytes.NewReader([]byte(body)))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if method == "POST" {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func internalRequest(api *weldr.API, method, path, body string) *http.Response {
|
||||||
req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body)))
|
req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body)))
|
||||||
|
|
||||||
if method == "POST" {
|
if method == "POST" {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
api.ServeHTTP(resp, req)
|
api.ServeHTTP(resp, req)
|
||||||
|
|
||||||
if resp.Code != expectedStatus {
|
return resp.Result()
|
||||||
t.Errorf("%s: expected status %v, but got %v", path, expectedStatus, resp.Code)
|
}
|
||||||
|
|
||||||
|
func testRoute(t *testing.T, api *weldr.API, method, path, body string, expectedStatus int, expectedJSON string) {
|
||||||
|
var resp *http.Response
|
||||||
|
|
||||||
|
if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 {
|
||||||
|
resp = externalRequest(method, path, body)
|
||||||
|
} else {
|
||||||
|
resp = internalRequest(api, method, path, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != expectedStatus {
|
||||||
|
t.Errorf("%s: expected status %v, but got %v", path, expectedStatus, resp.StatusCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue