Repository navigation
feat(conform): test filter capability - #100
Conversation
Signed-off-by: Tyler Hirschel <thirschel@microsoft.com>
duglin
left a comment
There was a problem hiding this comment.
sorry it's so long - the overall flow is ok - it's just the pattern/layout that got me thinking/rambling...
| for _, id := range ids { | ||
| if id != "null" && reSingularID.MatchString(id) && | ||
| !IsNil(gmRes.JSON[id]) { | ||
| filterGroupPaths[groupType] = gmPath + "/" + id |
There was a problem hiding this comment.
Couple of things:
- I think I understand why you try to exclude "null" (?filter=...id=null is problematic), however, and this is kind of cool, ?filter=...id=NULL will find it because we do case insensitive string matching for IDs in filters but not on "null" values. So we may not technically need to skip one with an id of "null" if we do the filter test right
- I think we could move this logic into the next loop since the first 2 checks are the same anyway. So, after
td.Must(!IsNil(v), "Value of %q MUST NOT be nil", id)just addif filterGroupsPaths[groupType] == "" { set it }. Then we don't need to loop twice. And we could add the 'null' check too to the "if" if that turns out to be what we want. Actually, if you move it to after the XID check then you can just add the XID as the filterGroupPaths[] value w/o having to construct it yourself.
There was a problem hiding this comment.
Nice catch, I missed that about null. That should let us reuse the existing group and remove the extra loop.
| } | ||
|
|
||
| if !reg.Capabilities.FlagEnabled("filter") { | ||
| td.Skip("?filter not supported") |
There was a problem hiding this comment.
- leaving. Just for consistency.
| } | ||
|
|
||
| func TestFilter(td *TD) { | ||
| td.DependsOn(TestCapabilities) |
There was a problem hiding this comment.
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.
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.
I think an optional arg with instructions on what to do would help. For example:
td.DependsOn(TestCapabilities, TD_FAILONFAIL) // the default (today)
td.DependsOn(TestCapabilities, TD_FAILONSKIP) // what I think we need here (fail on skip or worse)
td.DependsOn(TestCapabilities, TD_IGNOREFAIL) // If we're ok with a failed dependency
there may be reasons to have other variants too.
| } | ||
| groupPaths, ok := groupPathsAny.(map[string]string) | ||
| if !ok { | ||
| td.FailNow("reg.stuff.groupPaths != map[string]string") |
There was a problem hiding this comment.
I'm thinking more and more that we should make these into "panic" calls since it's a bug in our code and we don't want to run the risk of the "FAIL" be missed in our testing. What do you think?
There was a problem hiding this comment.
Yeah agreed that makes sense. Follow up PR?
| gm := reg.Model.Groups[candidateType] | ||
| candidatePath := groupPaths[candidateType] | ||
| candidateID := strings.TrimPrefix(candidatePath, "/"+gm.Plural+"/") | ||
| if candidateID == candidatePath || candidateID == "" || |
There was a problem hiding this comment.
What are you trying to avoid with this "if" check? It seems like we checked for a valid XID already in TestGroups. Also, in case you want to use it, there's a ParseXid(string) func you can use to parse XIDs and then you can just use xid.GroupID and not have to do the Trim tuff
There was a problem hiding this comment.
I might even be good to call it groupXIDs instead of groupPaths since that's what they are. And you could even store the XIDs in that map instead of strings to ensure they're only parsed once no matter how often they're used.
There was a problem hiding this comment.
Its a good point. I'll parse the selected XID once and use its fields.
| │ ├─ PASS: 'GET /' MUST return 200 | ||
| │ ├─ PASS: 'GET /' MUST return a non-empty body | ||
| │ ├─ PASS: 'GET /' MUST return a JSON body | ||
| │ ├─ PASS: "specversion" ($SPECVERSION) MUST > "1.0" |
There was a problem hiding this comment.
Why is this showing up as $SPECVERSION instead of the real value?
And clearly the check is wrong - I think we should switch to a regexp check instead:
td.ObjReqMustRe(res.JSON, "specversion", "^1\.0.*$")
I think
There was a problem hiding this comment.
The loader replaces $SPECVERSION before comparison but you're right the check is wrong. Ill address it.
| ├─ PASS: TestFilter (skip:1) | ||
| │ ├─ PASS: TestCapabilities (cached) | ||
| │ └─ SKIP: ?filter not supported | ||
| └─ PASS: TestResources (skip:1) |
There was a problem hiding this comment.
I think it would be cool to support something like --branch REGEXP and then only show the tree traversals/branches that include a test that matches REGEXP, and then apply the rest of the -d type of flags to control the depth, skips... under that one branch.
Then, once we support --run AnyTestName it could have an implied --branch AnyTestName since I bet in most cases people don't want to see all of the dependency logs, they only really care about AnyTestName... unless one of the deps fails then we'd show that anyway. This would shrink the output above to just everything under TestResources.
| recorder := httptest.NewRecorder() | ||
| fileServer.ServeHTTP(recorder, req) | ||
| var recorder *httptest.ResponseRecorder | ||
| if r.URL.Query().Has("filter") { |
There was a problem hiding this comment.
I think this might be problematic w.r.t. scaling once we start to test multiple flags at the same time (filter+sort+inline...).
So, for cases where we can't use the real server (which I'm hoping will only be for error testing), what if we did something different:
- made the test use a base URL of localhost:8282/TESTNAME
- have the test "register" the expected output based on the path+query params
- for example,
testServer.SetOutput("/TESTNAME/widgets?filter=widgetid=Null", "{..}")and "setOutput" would store the expected output behind a key ofTESTNAME/widgets?filter=widgetid=Null. Then the server could check the map prior to looking on the filesystem for the data. Then we could store any output (w/ or w/o query params) at will w/o needing specialized code in the server for each variant. - if you really really want to store things in a file, we could have a setOutputFile() variant that takes a path instead of a string.
- for example,
- technically, we could do this for all custom output per test rather than creating new files on disk to keep those inputs near the test-code itself.
e.g.
testServer.SetOutput("/TestFoo/", `{ "specversion", ..." }`)
testServer.SetOutput("/TestFoo/widgets?filter=name=jon", `{}`)
XHTTP(..."localhost:8282/TestFoo/widgets?filter=name=jon"...)
or
const target = "http://localhost:8282/TestFoo"
cliResult := XCLI(t, "conform --run all --skips -d3 -vvv "+target, ...
There was a problem hiding this comment.
Agreed thats easier to follow than filter specific filename rewriting. I'll use it for deliberately broken responses and keep normal cases on the real server.
| return strings.ReplaceAll(string(data), specVersionPlaceholder, SPECVERSION) | ||
| output := strings.ReplaceAll(string(data), specVersionPlaceholder, | ||
| SPECVERSION) | ||
| return strings.ReplaceAll(output, outputSpacePlaceholder, " ") |
There was a problem hiding this comment.
In prep for others later... it might be good to put all $xxx's into an array:
var subs := [][2]string {
{ "$SPECVERSION", SPECVERSION },
{ "$SPACE", " " },
}
for _, sub := range subs {
output = strings.ReplaceAll(string(data), sub[0], sub[1])
}
| } | ||
|
|
||
| func TestXRConformFilterNoObservableGroup(t *testing.T) { | ||
| const target = "http://localhost:8282/conform-filter-empty" |
There was a problem hiding this comment.
I keep bouncing around the PR so sorry if I seem scatter-brained, but I just realized perhaps you didn't know that the server supports setting the capabilities via HTTP, so you can do what you did in the previous test (which I like), setting everything up by inlining all inputs (modelsource, capabilities,...) in the code rather than separate files - then use the real server instead. And save the static file server for error cases (or situations where we can't get the desired output from a real server)
6177c1c to
1c277f2
Compare
Signed-off-by: Tyler Hirschel <thirschel@microsoft.com>
1c277f2 to
ed36cc3
Compare
| return | ||
| } | ||
|
|
||
| content := strings.ReplaceAll(string(data), specVersionPlaceholder, |
There was a problem hiding this comment.
Any reason not to use substituteOutput() here as well?
| } | ||
|
|
||
| func TestFilter(td *TD) { | ||
| td.DependsOn(TestCapabilities) |
There was a problem hiding this comment.
I think an optional arg with instructions on what to do would help. For example:
td.DependsOn(TestCapabilities, TD_FAILONFAIL) // the default (today)
td.DependsOn(TestCapabilities, TD_FAILONSKIP) // what I think we need here (fail on skip or worse)
td.DependsOn(TestCapabilities, TD_IGNOREFAIL) // If we're ok with a failed dependency
there may be reasons to have other variants too.
| return | ||
| } | ||
|
|
||
| xid, err := ParseXid(groupPaths[SortedKeys(groupPaths)[0]]) |
There was a problem hiding this comment.
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).
| } | ||
| groupPaths, ok := groupPathsAny.(map[string]string) | ||
| if !ok { | ||
| td.FailNow("reg.stuff.groupPaths != map[string]string") |
| } | ||
|
|
||
| matchPath := collectionPath + "?filter=" + filterAttr + "=" + | ||
| url.QueryEscape(groupID) |
| func TestFileServerRegisteredOutputs(t *testing.T) { | ||
| server := testFileServer() | ||
| const path = "/TestFileServerRegisteredOutputs/widgets" + | ||
| "?filter=widgetid=NULL&sort=name&inline" |
There was a problem hiding this comment.
Just wondering, do you guys support ?inline w/o a = ? Someone, in the past, mentioned supporting both bools and valued query parameters with the same name might be an issue for you
|
|
||
| func TestXRConformFilter(t *testing.T) { | ||
| const target = "http://localhost:8181" | ||
| tests := []struct { |
There was a problem hiding this comment.
I love table driven tests! thanks!
|
|
||
| if reg.Capabilities == nil { | ||
| td.Skip("No capabilities found - leaving") | ||
| return |
There was a problem hiding this comment.
I think adding a td.Skip() will make TestFilters() report SKIP - you may need to rebase though to get that code.
And in the "leaving" if-statements below too.
Because doesn't it seem like TestFilters should report SKIP rather than PASS if our prereqs aren't met?
| } | ||
|
|
||
| func TestXRConformFilterResponses(t *testing.T) { | ||
| const path = "/TestXRConformFilterResponses" |
There was a problem hiding this comment.
For now this is ok, however we'll eventually be testing all of the various query parameters and at that point it might be easier to just add a single static file (bad) xreg server under tests/files that lies and says it supports filtering, sorting, inlining.... but will obviously not do any of those things. Then we don't need to define the capabilities, model, GET responses for each test like you do below.
| FileServer.SetOutput(t, path+"/model", 200, "{}") | ||
| FileServer.SetOutput(t, path+"/capabilities", 200, "null") | ||
| cliResult := XCLI(t, "conform --run all --skips -d3 -vvv "+target, | ||
| "", substituteOutput(`PASS: http://localhost:8282/TestXRConformFilterResponses (skip:5) |
There was a problem hiding this comment.
if we end up doing substituteOutput often enough we might want to consider adding it as a default thing (like timestamp masking) to XCLI directly. Probably no harm in doing it since the $XXX strings don't occur in the wild.
Signed-off-by: Tyler Hirschel <thirschel@microsoft.com>
Summary
Add a capability-gated filter check to
xr conform.When a server advertises the
filtercapability and exposes a usable modeled group, the conformance test verifies that:<GROUP>id=nullreturns an empty collectionThe test skips without making filter requests when filtering is not advertised or no usable group is available. It runs in the default and
allsuites only. Thesmokeandentitiessuites are unchanged.The PR also ensures escaped filter URLs are logged correctly and prevents a non-2xx response containing JSON
nullfrom causing a client panic.Related to #55.