Skip to content

feat(conform): test filter capability - #100

Merged
duglin merged 3 commits into
xregistry:masterfrom
thirschel:thirschel-microsoft-conform-filter-test
Oct 11, 2026
Merged

duglin merged 3 commits into
xregistry:masterfrom
thirschel:thirschel-microsoft-conform-filter-test

Conversation

@thirschel-microsoft

Copy link
Copy Markdown
Contributor

Summary

Add a capability-gated filter check to xr conform.

When a server advertises the filter capability and exposes a usable modeled group, the conformance test verifies that:

  • filtering by an observed group ID returns exactly that group
  • filtering for an absent required ID with <GROUP>id=null returns an empty collection

The test skips without making filter requests when filtering is not advertised or no usable group is available. It runs in the default and all suites only. The smoke and entities suites are unchanged.

The PR also ensures escaped filter URLs are logged correctly and prevents a non-2xx response containing JSON null from causing a client panic.

Related to #55.

Signed-off-by: Tyler Hirschel <thirschel@microsoft.com>

@duglin duglin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry it's so long - the overall flow is ok - it's just the pattern/layout that got me thinking/rambling...

Comment thread cmds/xr/td_registry.go Outdated
for _, id := range ids {
if id != "null" && reSingularID.MatchString(id) &&
!IsNil(gmRes.JSON[id]) {
filterGroupPaths[groupType] = gmPath + "/" + id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 add if 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, I missed that about null. That should let us reuse the existing group and remove the extra loop.

Comment thread cmds/xr/td_registry.go Outdated
}

if !reg.Capabilities.FlagEnabled("filter") {
td.Skip("?filter not supported")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- leaving. Just for consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread cmds/xr/td_registry.go
}

func TestFilter(td *TD) {
td.DependsOn(TestCapabilities)

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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:

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.

#103

Comment thread cmds/xr/td_registry.go Outdated
}
groupPaths, ok := groupPathsAny.(map[string]string)
if !ok {
td.FailNow("reg.stuff.groupPaths != map[string]string")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed that makes sense. Follow up PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmds/xr/td_registry.go Outdated
gm := reg.Model.Groups[candidateType]
candidatePath := groupPaths[candidateType]
candidateID := strings.TrimPrefix(candidatePath, "/"+gm.Plural+"/")
if candidateID == candidatePath || candidateID == "" ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/utils_test.go Outdated
recorder := httptest.NewRecorder()
fileServer.ServeHTTP(recorder, req)
var recorder *httptest.ResponseRecorder
if r.URL.Query().Has("filter") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of TESTNAME/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.
  • 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, ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/utils_test.go Outdated
return strings.ReplaceAll(string(data), specVersionPlaceholder, SPECVERSION)
output := strings.ReplaceAll(string(data), specVersionPlaceholder,
SPECVERSION)
return strings.ReplaceAll(output, outputSpacePlaceholder, " ")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])
}

Comment thread tests/xr_test.go Outdated
}

func TestXRConformFilterNoObservableGroup(t *testing.T) {
const target = "http://localhost:8282/conform-filter-empty"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@thirschel-microsoft
thirschel-microsoft force-pushed the thirschel-microsoft-conform-filter-test branch 2 times, most recently from 6177c1c to 1c277f2 Compare October 2, 2026 19:28
Signed-off-by: Tyler Hirschel <thirschel@microsoft.com>
Comment thread tests/utils_test.go Outdated
return
}

content := strings.ReplaceAll(string(data), specVersionPlaceholder,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use substituteOutput() here as well?

Comment thread cmds/xr/td_registry.go
}

func TestFilter(td *TD) {
td.DependsOn(TestCapabilities)

Copy link
Copy Markdown
Contributor

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:

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.

#103

Comment thread cmds/xr/td_registry.go
return
}

xid, err := ParseXid(groupPaths[SortedKeys(groupPaths)[0]])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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).

Comment thread cmds/xr/td_registry.go Outdated
}
groupPaths, ok := groupPathsAny.(map[string]string)
if !ok {
td.FailNow("reg.stuff.groupPaths != map[string]string")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmds/xr/td_registry.go Outdated
}

matchPath := collectionPath + "?filter=" + filterAttr + "=" +
url.QueryEscape(groupID)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/utils_test.go
func TestFileServerRegisteredOutputs(t *testing.T) {
server := testFileServer()
const path = "/TestFileServerRegisteredOutputs/widgets" +
"?filter=widgetid=NULL&sort=name&inline"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread tests/xr_test.go

func TestXRConformFilter(t *testing.T) {
const target = "http://localhost:8181"
tests := []struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love table driven tests! thanks!

Comment thread cmds/xr/td_registry.go

if reg.Capabilities == nil {
td.Skip("No capabilities found - leaving")
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
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?

Comment thread tests/xr_test.go
}

func TestXRConformFilterResponses(t *testing.T) {
const path = "/TestXRConformFilterResponses"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/xr_test.go Outdated
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)

@duglin duglin Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@duglin
duglin merged commit 10d3ce1 into xregistry:master Oct 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants