Skip to content

Reorganise web test code for use outside repo#17457

Closed
camscale wants to merge 3 commits intomasterfrom
camh/export-lib-web-tests
Closed

Reorganise web test code for use outside repo#17457
camscale wants to merge 3 commits intomasterfrom
camh/export-lib-web-tests

Conversation

@camscale
Copy link
Copy Markdown
Contributor

@camscale camscale commented Oct 17, 2022

Reorganise the test code in lib/web to make parts available to tests running elsewhere, in particular the enterprise repository for components that are only available in the enterprise edition.

Three changes are:

  • Export webSuite as TestWebSuite and move into the lib/web package proper, rather than only being in the _test.go files. This allows other packages to use this test setup code.
  • Export a bunch of names in the lib/web package so code and tests can be moved outside of this package. This is just enough to get the OIDC/SAML connectors moved, not a comprehensive look at what would need to be exported in general.
  • Add functional options to NewTestWebSuite so parts of the test suite can be configured appropriately for external use.

This is the first of a series of PRs to move the enterprise connectors to the enterprise repository.

Issues: https://github.com/gravitational/teleport.e/issues/525


Note: The commits are broken up into reviewable pieces (as much as this renaming and code movement are reviewable). Reviewing the individual commits may be easier than reviewing the whole lot at once.

Extract the `webSuite` type from apiserver_test.go and bring it into the
lib/web package so that it can be used from outside the lib/web package.
This will allow the movement of the enterprise SSO code to the
enterprise repository, retaining the existing tests.

`webSuite` has been renamed to `TestWebSuite` to highlight that it
exists for testing purposes and is not part of the lib/web package for
non-testing purposes. Ideally this would be moved to a separate fixtures
package but circular dependencies prevent that for now - larger changes
would be needed.

Some ancillary types and functions have also been extracted as they were
needed by `TestWebSuite`.
Export some names in the `lib/auth` package so that SSO auth plugins can
be implemented from outside this package:

  * struct `ssoRequestParams` (including fields)
  * struct `ssoCallbackResponse` (including fields)
  * fields of struct `TestWebSuite`
  * func `parseSSORequestParams`
  * func `ssoSetWebSessionAndRedirectURL`
  * func `redirectURLWithError`
  * var `ssoLoginConsoleErr`
  * func `addCSRFCookieToReq` (for TestSAML)
  * method `TestWebSuite.clientNoRedirects` (for TestSAML)
  * type `CachedSessionLingeringThreshold` (for TestSAML)
Add the ability to configure TestWebSuite so it can be used outside of
the lib/web package. This currently includes:

  * Specifying the assets directory to serve, instead of the hard-coded
    default of `../../webassets/teleport`, which only works for tests at
    a particular level of the hierarchy.
  * Specifying a plugin registry for the web.Handler so that external
    plugins can set up the test suite. This is needed to move the
    enterprise SSO connectors to the enterprise repo.
@github-actions github-actions Bot requested review from Tener and strideynet October 17, 2022 05:06
@camscale camscale requested a review from r0mant October 17, 2022 05:06
Comment thread lib/web/websuite.go
@@ -0,0 +1,750 @@
package web
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.

Unsure what others will think - but I'm quite uncomfortable with test helpers making their way into the packages themselves. The lines quickly become blurred as to whats for the test and whats actually making this code tick in production (e.g something like AddCSRFCookieToReq). Could we do something like lib/web/test instead ?

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.

I am not all that comfortable with it myself. I did try to split it into a separate package, but circular dependencies on creating the web.Handler (from the web.Config) made that quite difficult (package web would need to use this new webfixture package, and it would need to use web to stand up the fixture). It might be possible to abstract that through an interface, but the Config struct is pretty unwieldy in its breadth.

I only went this route when I noted that lib/auth/helper.go contained TestAuthServer and similar.

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.

And sorry, I meant to note that in the PR description.

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.

One (imperfect) solution is to hide the testing utilities behind a build tag, which would only be used for testing. We may also have luck with the internal directory (https://go.dev/doc/go1.4#internalpackages), but I'm not sure we can use it in our case.

I also wonder if we may simply copy the code, either manually or with go:generate magic.

@zmb3
Copy link
Copy Markdown
Collaborator

zmb3 commented Oct 17, 2022

The lib/web test suite is where most of the slowest tests in the codebase live. I'd be happy to see us rethink testing here and come up with a new approach for your migration rather than allowing some of the poorer tests to propagate further through the code.

Copy link
Copy Markdown
Collaborator

@r0mant r0mant left a comment

Choose a reason for hiding this comment

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

@camscale What parts of the web suite do we need to test SAML/OIDC after they move to teleport.e? Do we need to expose the full web suite?

Looking at this test here it just needs a basic setup like auth + web handler: https://github.com/gravitational/teleport.e/pull/562/files#diff-c21568611c884c7dc907ee38c89cd62d92d99b2ee44182a73f12e1984a3cdbdf.

Can enterprise web package just have a more lightweight version of the web suite that would allow us to test what's needed for SAML/OIDC handlers? Then we wouldn't need to expose it here.

@camscale
Copy link
Copy Markdown
Contributor Author

camscale commented Oct 17, 2022

@strideynet @Tener @zmb3 @r0mant Thanks for your comments. Rolled-up response here:

What parts of the web suite do we need to test SAML/OIDC after they move to teleport.e? Do we need to expose the full web suite?

We likely don't need the whole web suite - that was my first approach, trying to stand up a fake piecemeal but I very quickly got lost as a newbie to the code base and didn't get anything to work. Having been around this multiple times now, I may have better success trying that approach again.

I also wonder if we may simply copy the code, either manually

That was approach # 2, which Roman preferred not be done (and I agree - i felt dirty cutting and pasting that code)

I'd be happy to see us rethink testing here and come up with a new approach for your migration

That was part of attempt # 1 but I'm too new to the code base to understand why things were done the way they were and what the new approach would look like. I think this is the right idea, but I think it needs a bit more experience with the code base to do it well.

One (imperfect) solution is to hide the testing utilities behind a build tag

That would work, but I think it might be too annoying - it would require you run go test --tags test each time instead of just go test, and that would be required from any package that uses the test suite not just this one.

I will have another crack at simplifying the requirements for TestSAML to a smaller stub/fake that does not require much if any to be exposed or copied from lib/web.

@r0mant
Copy link
Copy Markdown
Collaborator

r0mant commented Oct 18, 2022

@camscale

That was approach # 2, which Roman preferred not be done (and I agree - i felt dirty cutting and pasting that code)

FWIW I think that just cut/paste our entire OSS web suite is indeed what we don't want to do, however I don't see anything wrong with just building a smaller web suite in enterprise that borrows the parts it needs from the OSS one. We do it in many places in Teleport, a lot of packages have their own test suites that set up services that they need, and most of the time it's similar since they all need auth, web, etc.

I will have another crack at simplifying the requirements for TestSAML to a smaller stub/fake that does not require much if any to be exposed or copied from lib/web.

This approach sounds good to me.

@camscale
Copy link
Copy Markdown
Contributor Author

Clsoed in favour of #17530

@camscale camscale closed this Oct 18, 2022
@camscale camscale deleted the camh/export-lib-web-tests branch October 19, 2022 03:20
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.

5 participants