Skip to content
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

Provider Unit Tests (Provider Lifecycle) #2768

Merged
merged 22 commits into from
Jan 26, 2024
Merged

Conversation

EronWright
Copy link
Contributor

@EronWright EronWright commented Jan 23, 2024

Proposed changes

This PR implements a suite of unit tests for the provider implementation code in provider/pkg/provider/provider.go. Unlike the test suites in tests/, this suite uses a mock Kubernetes client and executes the provider code directly.

This PR focuses on the provider lifecycle (e.g. plugin info and provider configuration):

  • Suite
  • RPC:CheckConfig
    • Strict Mode
    • Yaml Rendering Mode
  • RPC:DiffConfig
    • Kubeconfig Parsing
    • Cluster Change Detection
  • RPC:Configure
    • Secrets Support
    • Connectivity
  • RPC:GetPluginInfo
  • RPC:GetSchema
  • RPC:GetMapping
  • RPC:Cancel

In follow-up PR(s), tests will be developed for the resource lifecycle (eg. Check, Diff, Read, Create, Update, Delete), and for invokes (Invoke, StreamInvoke).

Changes to Implementation Code

Some minor refactoring was necessary to make it possible to substitute fake Kubernetes clients.

  • the type of the RESTMapper field of DynamicClientSet was changed to an interface.
  • the type of the client field of LogClient was changed to an interface.
  • the low-level logic for making a Kubernetes client from a kubeconfig was moved into a function called makeClient.
  • add a makeClient field to kubeProvider as an indirection for test purposes.

Running the Tests

The tests are written as a Ginkgo suite and is integrated with gotest via an ordinary test function called TestSuite in suite_test.go. Here's how go test behaves:

 $ make test_provider
=== RUN   Test_Watcher_Interface_Cancel
--- PASS: Test_Watcher_Interface_Cancel (0.00s)
=== RUN   TestSuite
Running Suite: provider/pkg/provider - /Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/provider
===============================================================================
Random Seed: 1706123566

Will run 39 of 40 specs
••••••••
------------------------------
P [PENDING]
RPC:DiffConfig Kubeconfig Parsing when kubeconfig is a file should return an empty diff
/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/provider/provider_diffconfig_test.go:113
------------------------------
•••••••••••••••••••••••••••••••

Ran 39 of 40 Specs in 0.590 seconds
SUCCESS! -- 39 Passed | 0 Failed | 1 Pending | 0 Skipped
--- PASS: TestSuite (0.59s)
=== RUN   TestHasComputedValue
--- PASS: TestHasComputedValue (0.00s)
...

Here's a full example in a CI context.

One may also use the ginkgo CLI as a drop-in replacement with some niceties.
There's a minor limitation of using go test, it is that the --parallel flag doesn't work and causes Ginkgo to quit. The overall test pass is fast enough without parallelization and so I removed the flag for now. We could switch to using the ginkgo CLI (which runs the standard tests too) as an alternative solution.

Related issues (optional)

Closes #2767

Copy link

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@EronWright EronWright added the impact/no-changelog-required This issue doesn't require a CHANGELOG update label Jan 24, 2024
@EronWright EronWright changed the title [WIP] Provider Unit Tests Provider Unit Tests (Framework, Configuration) Jan 24, 2024
@EronWright EronWright marked this pull request as ready for review January 24, 2024 01:47
@EronWright EronWright requested a review from rquitales January 24, 2024 01:47
@EronWright EronWright requested a review from blampe January 24, 2024 16:58
@EronWright EronWright changed the title Provider Unit Tests (Framework, Configuration) Provider Unit Tests (Provider Lifecycle) Jan 24, 2024
Makefile Show resolved Hide resolved
Copy link
Member

@rquitales rquitales left a comment

Choose a reason for hiding this comment

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

Changes mostly looks good to me. A few comments. I'm still curious how this suite sits amongst our existing vanilla Go unit tests? I'm still hesitant on introducing Ginkgo, but do see utility in BDD for testing the higher level provider calls. I still believe that regular Go table tests should be used for unit testing our lower level functions, and much prefer them over Gingko for such tests.

Thanks for taking the time to mock the clients though!

provider/pkg/provider/provider.go Outdated Show resolved Hide resolved
provider/pkg/provider/provider.go Outdated Show resolved Hide resolved
provider/pkg/provider/suite_test.go Outdated Show resolved Hide resolved
provider/pkg/provider/provider_checkconfig_test.go Outdated Show resolved Hide resolved
provider/pkg/provider/provider_configure_test.go Outdated Show resolved Hide resolved
provider/pkg/provider/provider_plugin_test.go Show resolved Hide resolved
provider/pkg/provider/provider_plugin_test.go Show resolved Hide resolved
@mjeffryes mjeffryes added this to the 0.99 milestone Jan 26, 2024
Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: 35 lines in your changes are missing coverage. Please review.

Comparison is base (66ea94d) 18.41% compared to head (4c10995) 23.12%.
Report is 25 commits behind head on master.

Files Patch % Lines
provider/pkg/provider/provider.go 41.37% 17 Missing ⚠️
provider/pkg/clients/unstructured.go 70.37% 6 Missing and 2 partials ⚠️
provider/pkg/openapi/openapi.go 0.00% 4 Missing ⚠️
provider/pkg/clients/clients.go 40.00% 3 Missing ⚠️
provider/pkg/await/await.go 0.00% 2 Missing ⚠️
provider/pkg/provider/kubeconfig.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2768      +/-   ##
==========================================
+ Coverage   18.41%   23.12%   +4.71%     
==========================================
  Files          47       48       +1     
  Lines        9589     9620      +31     
==========================================
+ Hits         1766     2225     +459     
+ Misses       7724     7240     -484     
- Partials       99      155      +56     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/no-changelog-required This issue doesn't require a CHANGELOG update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Develop a unit test suite for Kubernetes provider
3 participants