Repository navigation
feat(conform): test filter capability #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| // "fmt" | ||
| "net/url" | ||
| "regexp" | ||
|
|
||
| "github.com/xregistry/server/cmds/xr/xrlib" | ||
|
|
@@ -23,6 +23,7 @@ func TestRegistry(td *TD) { | |
| td.Run(TestCapabilities) | ||
| td.Run(TestRegistryRoot) | ||
| td.Run(TestGroups) | ||
| td.Run(TestFilter) | ||
| td.Run(TestResources) | ||
| } | ||
|
|
||
|
|
@@ -56,6 +57,7 @@ func TestCapabilities(td *TD) { | |
|
|
||
| if reg.Capabilities == nil { | ||
| td.Skip("No capabilities found - leaving") | ||
| td.Skip() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -222,7 +224,8 @@ func TestGroups(td *TD) { | |
| MakeURL(reg.GetStuffAsString("self"), | ||
| gm.Plural, MustString(gRes.JSON[gm.Singular+"id"]))) | ||
| gTD.ObjReqMustEq(gRes.JSON, "xid", | ||
| MakeURL("/", gm.Plural, MustString(gRes.JSON[gm.Singular+"id"]))) | ||
| MakeURL("/", gm.Plural, | ||
| MustString(gRes.JSON[gm.Singular+"id"]))) | ||
| gTD.ObjReqMustGe(gRes.JSON, "epoch", 0) | ||
| gTD.ObjMayExist(gRes.JSON, "name", "") | ||
| gTD.ObjMayExist(gRes.JSON, "description", "") | ||
|
|
@@ -240,6 +243,68 @@ func TestGroups(td *TD) { | |
| reg.SetStuff("groupPaths", groupPaths) | ||
| } | ||
|
|
||
| func TestFilter(td *TD) { | ||
| td.DependsOn(TestCapabilities) | ||
| reg := td.GetRegistry() | ||
|
|
||
| if reg.Capabilities == nil { | ||
| td.Skip("No capabilities found - leaving") | ||
| td.Skip() | ||
| return | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding a td.Skip() will make TestFilters() report SKIP - you may need to rebase though to get that code. |
||
| } | ||
|
|
||
| if !reg.Capabilities.FlagEnabled("filter") { | ||
| td.Skip("?filter not supported - leaving") | ||
| td.Skip() | ||
| return | ||
| } | ||
|
|
||
| td.DependsOn(TestGroups) | ||
|
|
||
| groupPathsAny, ok := reg.GetStuff("groupPaths") | ||
| if !ok { | ||
| td.Skip("No observable Group Types defined - leaving") | ||
| td.Skip() | ||
| return | ||
| } | ||
| groupPaths, ok := groupPathsAny.(map[string]string) | ||
| PanicIf(!ok, "reg.stuff.groupPaths != map[string]string") | ||
|
|
||
| if len(groupPaths) == 0 { | ||
| td.Skip("No observable Group Types defined - leaving") | ||
| td.Skip() | ||
| return | ||
| } | ||
|
|
||
| xid, err := ParseXid(groupPaths[SortedKeys(groupPaths)[0]]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point it might be easier to just store the XID in groupPaths instead of the string (ie. let TestGroups do the ParseXid instead of all users of it). |
||
| PanicIf(err != nil, "reg.stuff.groupPaths has an invalid XID: %s", err) | ||
|
|
||
| gm := reg.Model.Groups[xid.Group] | ||
| filterAttr := gm.Singular + "id" | ||
| collectionPath := "/" + xid.Group | ||
| matchID := xid.GroupID | ||
| if matchID == "null" { | ||
| matchID = "NULL" | ||
| } | ||
|
|
||
| matchPath := collectionPath + "?filter=" + filterAttr + "=" + | ||
| url.QueryEscape(matchID) | ||
| matchRes := td.HTTPGetJSON(matchPath) | ||
| if matchRes != nil { | ||
|
duglin marked this conversation as resolved.
Resolved
|
||
| td.MustEqual([]string{xid.GroupID}, SortedKeys(matchRes.JSON), | ||
| "'GET %s' MUST return exactly group %q", matchPath, xid.GroupID) | ||
| td.Must(!IsNil(matchRes.JSON[xid.GroupID]), | ||
| "'GET %s' group %q MUST NOT be nil", matchPath, xid.GroupID) | ||
| } | ||
|
|
||
| absentPath := collectionPath + "?filter=" + filterAttr + "=null" | ||
| absentRes := td.HTTPGetJSON(absentPath) | ||
| if absentRes != nil { | ||
| td.MustEqual(0, len(absentRes.JSON), | ||
| "'GET %s' MUST return an empty collection", absentPath) | ||
| } | ||
| } | ||
|
|
||
| func TestResources(td *TD) { | ||
| td.DependsOn(TestGroups) | ||
| reg := td.GetRegistry() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,11 @@ package main | |
|
|
||
| import ( | ||
| "bytes" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/xregistry/server/cmds/xr/xrlib" | ||
|
|
@@ -147,6 +150,142 @@ Pass: 2 Fail: 0 Warn: 0 Skip: 0 | |
| XEqual(t, "Stdout", stdout, "") | ||
| } | ||
|
|
||
| func TestTDHTTPGetJSON(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| code int | ||
| body string | ||
| status int | ||
| }{ | ||
| {"object", 200, `{"null":{}}`, PASS}, | ||
| {"empty object", 200, `{}`, PASS}, | ||
| {"wrong success status", 201, `{}`, FAIL}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future we may need to allow 201's not just 200's when we're testing creates. |
||
| {"error with null body", 500, `null`, FAIL}, | ||
| {"empty body", 200, ``, FAIL}, | ||
| {"null", 200, `null`, FAIL}, | ||
| {"array", 200, `[]`, FAIL}, | ||
| {"string", 200, `"not an object"`, FAIL}, | ||
| {"malformed", 200, `{"null":`, FAIL}, | ||
| {"transport", 200, `{}`, FAIL}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc( | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(test.code) | ||
| _, _ = w.Write([]byte(test.body)) | ||
| })) | ||
| defer server.Close() | ||
| if test.name == "transport" { | ||
| server.Close() | ||
| } | ||
|
|
||
| td := newTestTD("HTTP GET") | ||
| td.SetRegistry(xrlib.DefineRegistry(server.URL)) | ||
| const path = "/widgets?filter=widgetid=NULL" | ||
| res := td.HTTPGetJSON(path) | ||
|
|
||
| XEqual(t, "Root status", td.Status, test.status) | ||
| XEqual(t, "Request subtests", len(td.Logs), 1) | ||
| requestTD := td.Logs[0].Subtest | ||
| XEqual(t, "Request name", requestTD.TestName, "GET "+path) | ||
| XEqual(t, "Request status", requestTD.Status, test.status) | ||
| if test.status == PASS { | ||
| XCheck(t, res != nil, "Expected a JSON response") | ||
| XEqual(t, "Response status", res.Code, 200) | ||
| XEqual(t, "Response body", string(res.Body), test.body) | ||
| XCheck(t, res.JSON != nil, "Expected a JSON object") | ||
| } else { | ||
| XCheck(t, res == nil, "A failed request must return nil") | ||
| } | ||
| td.Pass("continued") | ||
| XEqual(t, "Final status", td.Status, test.status) | ||
| }) | ||
| } | ||
|
|
||
| t.Run("independent requests", func(t *testing.T) { | ||
| var requests atomic.Int32 | ||
| server := httptest.NewServer(http.HandlerFunc( | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| if requests.Add(1) == 1 { | ||
| _, _ = w.Write([]byte("null")) | ||
| } else { | ||
| _, _ = w.Write([]byte("{}")) | ||
| } | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| td := newTestTD("HTTP GET") | ||
| td.SetRegistry(xrlib.DefineRegistry(server.URL)) | ||
| first := td.HTTPGetJSON("/widgets?filter=widgetid=NULL") | ||
| second := td.HTTPGetJSON("/widgets?filter=widgetid=null") | ||
| XCheck(t, first == nil, "The first request must fail") | ||
| XCheck(t, second != nil, "The second request must still run") | ||
| XEqual(t, "Requests", requests.Load(), int32(2)) | ||
| XEqual(t, "First status", td.Logs[0].Subtest.Status, FAIL) | ||
| XEqual(t, "Second status", td.Logs[1].Subtest.Status, PASS) | ||
| }) | ||
|
|
||
| t.Run("failfast", func(t *testing.T) { | ||
| var requests atomic.Int32 | ||
| server := httptest.NewServer(http.HandlerFunc( | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| requests.Add(1) | ||
| _, _ = w.Write([]byte("null")) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| td := newTestTD("HTTP GET") | ||
| td.SetRegistry(xrlib.DefineRegistry(server.URL)) | ||
| td.Config.FailFast = true | ||
| continued := false | ||
| td.Run(func(td *TD) { | ||
| td.HTTPGetJSON("/widgets?filter=widgetid=NULL") | ||
| continued = true | ||
| td.HTTPGetJSON("/widgets?filter=widgetid=null") | ||
| }) | ||
| XEqual(t, "Root status", td.Status, FAIL) | ||
| XEqual(t, "Requests", requests.Load(), int32(1)) | ||
| XEqual(t, "Continued", continued, false) | ||
| }) | ||
| } | ||
|
|
||
| func TestCapabilitiesMissingResponseStatus(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| code int | ||
| body string | ||
| status int | ||
| }{ | ||
| {"no capabilities", 200, "null", SKIP}, | ||
| {"malformed capabilities", 200, "{", FAIL}, | ||
| {"capabilities HTTP error", 500, "null", FAIL}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var requests atomic.Int32 | ||
| server := httptest.NewServer(http.HandlerFunc( | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| requests.Add(1) | ||
| w.WriteHeader(test.code) | ||
| _, _ = w.Write([]byte(test.body)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| root := newTestTD("Capabilities") | ||
| root.SetRegistry(xrlib.DefineRegistry(server.URL)) | ||
| td := root.Run(TestCapabilities) | ||
| XEqual(t, "Capabilities status", td.Status, test.status) | ||
| XEqual(t, "Requests", requests.Load(), int32(1)) | ||
| XEqual(t, "Diagnostic", | ||
| td.Logs[len(td.Logs)-1].Text, | ||
| "No capabilities found - leaving") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRunConformIsolatesOutputAndConfigState(t *testing.T) { | ||
| staleRun := NewTD(nil, "stale") | ||
| staleRun.Fail("stale failure") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package xrlib | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestHttpDoNullErrorBody(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc( | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| _, _ = io.WriteString(w, "null") | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| res, xErr := HttpDo(false, http.MethodGet, | ||
| server.URL+"/", nil, nil) | ||
| if res == nil || res.Code != http.StatusInternalServerError { | ||
| t.Fatalf("unexpected response: %#v", res) | ||
| } | ||
| if xErr == nil { | ||
| t.Fatal("expected a non-nil error") | ||
| } | ||
| if string(res.Body) != "null" { | ||
| t.Fatalf("unexpected response body: %q", res.Body) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now td.DependsOn() only stops if the dependency fails... I'm wondering if it should stop (with a 'skip' status) if the dependency was skipped. In this case if TestCapabilities is skipped because they don't expose capabilities, then it seems like we should immediate stop this one too and then you can remove the "if reg.Capabilities == nil ..." block below too. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense for a wholly skipped dependency. We'd need to be able to differentiate that from a passing test with some skipped checks before changing
DependsOn.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an optional arg with instructions on what to do would help. For example:
there may be reasons to have other variants too.
#103