cloudapi: run all Koji compose unit tests

Previously, only a subset from all Koji Compose unit test cases were
run. Remove this limitation and run all defined unit tests, which were
copied from `kojiapi`.

In addition, fix unit tests and relevant cloudapi methods to make unit
tests pass.

Add `TestRouteWithReply()` to `test/helpers.go` to allow getting the
compose ID when submitting a new compose. This is needed to make some
unit tests deterministic.

Do not delete values from `fields` slice in `dropFields()` in
`test/helpers.go`. The behavior was previously not consistent.
If the top-level map contained the value, it was deleted from it, but
the nested maps also contained the value, it was not deleted. On the
other hand, if the top level map didn't contain the value, but nested
maps did contain it, the value was deleted from all nested maps.
This commit is contained in:
Tomas Hozza 2022-05-31 14:48:37 +02:00 committed by Tom Gundersen
parent fc8af28231
commit 09534091a9
3 changed files with 166 additions and 108 deletions

View file

@ -75,16 +75,8 @@ func dropFields(obj interface{}, fields ...string) {
switch v := obj.(type) {
// if the interface type is a map attempt to delete the fields
case map[string]interface{}:
for i, field := range fields {
if _, ok := v[field]; ok {
delete(v, field)
// if the field is found remove it from the fields slice
if len(fields) < i-1 {
fields = append(fields[:i], fields[i+1:]...)
} else {
fields = fields[:i]
}
}
for _, field := range fields {
delete(v, field)
}
// call dropFields on the remaining elements since they may contain a map containing the field
for _, val := range v {
@ -102,13 +94,20 @@ func dropFields(obj interface{}, fields ...string) {
func TestRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) {
t.Helper()
_ = TestRouteWithReply(t, api, external, method, path, body, expectedStatus, expectedJSON, ignoreFields...)
}
// TestRouteWithReply tests the given API endpoint and if the test passes, it returns the raw JSON reply.
func TestRouteWithReply(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) (replyJSON []byte) {
t.Helper()
resp := SendHTTP(api, external, method, path, body)
if resp == nil {
t.Skip("This test is for internal testing only")
}
replyJSON, err := ioutil.ReadAll(resp.Body)
var err error
replyJSON, err = ioutil.ReadAll(resp.Body)
require.NoErrorf(t, err, "%s: could not read response body", path)
assert.Equalf(t, expectedStatus, resp.StatusCode, "SendHTTP failed for path %s: %v", path, string(replyJSON))
@ -136,6 +135,8 @@ func TestRoute(t *testing.T, api http.Handler, external bool, method, path, body
dropFields(expected, ignoreFields...)
require.Equal(t, expected, reply)
return
}
func TestTOMLRoute(t *testing.T, api http.Handler, external bool, method, path, body string, expectedStatus int, expectedTOML string, ignoreFields ...string) {