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

Improve coverage and testability of config.ts #2288

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

brendandburns
Copy link
Contributor

No description provided.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 4, 2025
Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM minus the console.log().

it('should try to load from WSL on Windows with wsl.exe not working', () => {
const kc = new KubeConfig();
const commands: { command: string; args: string[] }[] = [];
(kc as any).spawnSync = (cmd: string, args: string[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to change this PR, but just something for future consideration - all supported Node release lines do have this type of mocking and spying built in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, it didn't show up when I looked around for mocking child_process but now I found it and I refactored the test to use this instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I'm actually surprised that the CI is passing. Mocking an ES module like that should not work because they have immutable bindings. When I pull this PR down locally, these tests fail.

The approach here would work with the previous state of the PR though, where you were mocking the KubeConfig object's method. I think we should go back to that.

ESM mocking is quite the can of worms. There is support for that in newer versions of Node (and some ecosystem modules as well), but they build on top of Node's ESM loaders, which have been extremely unstable and are still experimental.

Copy link
Contributor

Choose a reason for hiding this comment

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

I dug into this a bit more. The mock is surprisingly working. The reason the tests are failing on my local machine is that loadFromDefault() is returning early here, so the mocked spawnSync() calls never happen.

Copy link
Contributor

@cjihrig cjihrig Mar 5, 2025

Choose a reason for hiding this comment

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

This gets the new tests passing for me locally:

diff --git a/src/config_test.ts b/src/config_test.ts
index 9bb31dd5..30f4d49f 100644
--- a/src/config_test.ts
+++ b/src/config_test.ts
@@ -1613,6 +1613,17 @@ describe('KubeConfig', () => {
     });
 
     describe('BufferOrFile', () => {
+        let originalEnv;
+
+        before(() => {
+            originalEnv = process.env;
+            process.env = {};
+        });
+
+        after(() => {
+            process.env = originalEnv;
+        });
+
         it('should load from root if present', () => {
             const data = 'some data for file';
             const arg: any = {

Looking at the code, mocking fs.accessSync() to throw might also get the job done.

@brendandburns
Copy link
Contributor Author

Comments addressed, please re-check.

@@ -322,6 +326,7 @@ describe('KubeConfig', () => {
};

assertRequestOptionsEqual(opts, expectedOptions);
strictEqual((requestContext.getAgent()! as any).options.servername, 'kube.example2.com');
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this rather an https.Agent than an any?
This would remove the unnecessary type casting but you probably still need the bang-operator or a different check.
Not sure if it's worth it, just wanted to mention that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

options isn't exposed as public (at least not in the typings), so Typescript doesn't like it. The cast as any is mostly to get the typescript compiler to leave it alone.

@mstruebing
Copy link
Member

/lgtm

I wasn't ware of this form of mocking either, looks very cool!
LGTM but I will let @cjihrig re-review as he is much deeper into the mocking and Node/ESM stuff.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 5, 2025
@k8s-ci-robot k8s-ci-robot merged commit e27eb26 into kubernetes-client:main Mar 5, 2025
8 checks passed
@mstruebing
Copy link
Member

Opps - sorry, I wasn't aware that /lgtm will merge it right away, but yeah, it already has the approve label because @brendandburns is in the org. :/

cjihrig added a commit to cjihrig/javascript that referenced this pull request Mar 5, 2025
The tests in kubernetes-client#2288 can fail depending on the environment
variables on the system. This commit takes the environment out
of the equation.

Refs: kubernetes-client#2288
cjihrig added a commit to cjihrig/javascript that referenced this pull request Mar 5, 2025
The tests in kubernetes-client#2288 can fail depending on the environment
variables on the system. This commit takes the environment out
of the equation.

Refs: kubernetes-client#2288
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants