Skip to content

🐛 Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management #650

Open
adity1raut wants to merge 1 commit intokubestellar:mainfrom
adity1raut:TestingKubeconfig
Open

🐛 Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management #650
adity1raut wants to merge 1 commit intokubestellar:mainfrom
adity1raut:TestingKubeconfig

Conversation

@adity1raut
Copy link
Copy Markdown
Contributor

…st cases for context management

Summary

  • Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management

Related issue(s)

Fixes #632

…st cases for context management

Signed-off-by: adity1raut <araut7798@gmail.com>
Copilot AI review requested due to automatic review settings January 14, 2026 03:28
@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Jan 14, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign mikespreitzer for approval. For more information see the Code Review Process.

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

Details 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

@github-actions
Copy link
Copy Markdown

Welcome to KubeStellar! 🚀 Thank you for submitting this Pull Request.

Before your PR can be merged, please ensure:

DCO Sign-off - All commits must be signed off with git commit -s to certify the Developer Certificate of Origin

PR Title - Must start with an emoji: ✨ (feature), 🐛 (bug fix), 📖 (docs), 🌱 (infra/tests), ⚠️ (breaking change)

Getting Started with KubeStellar:

Contributor Resources:


🌟 Help KubeStellar Grow - We Need Adopters!

Our roadmap is driven entirely by adopter feedback. Whether you're using KubeStellar yourself or know someone who could benefit from multi-cluster Kubernetes:

📋 Take our Multi-Cluster Survey - Share your use cases and help shape our direction!


A maintainer will review your PR soon. Feel free to ask questions in the comments or on Slack!

@kubestellar-prow kubestellar-prow bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 14, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

Hi @adity1raut. Thanks for your PR.

I'm waiting for a kubestellar member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubestellar-prow kubestellar-prow bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 14, 2026
@adity1raut
Copy link
Copy Markdown
Contributor Author

/cc @MikeSpreitzer

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the kubeconfig test file (pkg/kubeconfig/kubeconfig_test.go) by completely rewriting it with a more comprehensive and structured approach to testing kubeconfig context management functionality.

Changes:

  • Replaced minimal test coverage with comprehensive table-driven tests for all public functions
  • Added tests for adjustConfigKeys, merge, AssignControlPlaneToContext, DeleteAll, RenameKey, SwitchContext, SetHostingClusterContext, GetHostingClusterContext, IsHostingClusterContextSet, and SwitchToHostingClusterContext
  • Expanded test scenarios to cover positive cases, negative cases, and edge cases for each function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +122 to +127
validate: func(t *testing.T, kconf *clientcmdapi.Config, cpName string) {
expectedContext := certs.GenerateContextName(cpName)
if kconf.CurrentContext != expectedContext {
t.Errorf("expected current context %s, got %s", expectedContext, kconf.CurrentContext)
}
},
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The validation for the K3s control plane type test case is incomplete. It only checks the CurrentContext, but should also verify that the clusters, authInfos, and contexts have been properly renamed (similar to the OCM test case validation). The adjustConfigKeys function renames all three maps for K3s type, so all should be validated.

Copilot uses AI. Check for mistakes.
Comment on lines +56 to +73
validate: func(t *testing.T, kconf *clientcmdapi.Config, cpName string) {
expectedCluster := certs.GenerateClusterName(cpName)
expectedAuthInfo := certs.GenerateAuthInfoAdminName(cpName)
expectedContext := certs.GenerateContextName(cpName)

if _, ok := kconf.Clusters[expectedCluster]; !ok {
t.Errorf("expected cluster %s not found", expectedCluster)
}
if _, ok := kconf.AuthInfos[expectedAuthInfo]; !ok {
t.Errorf("expected authInfo %s not found", expectedAuthInfo)
}
if _, ok := kconf.Contexts[expectedContext]; !ok {
t.Errorf("expected context %s not found", expectedContext)
}
if kconf.CurrentContext != expectedContext {
t.Errorf("expected current context %s, got %s", expectedContext, kconf.CurrentContext)
}
},
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The test validation should also verify that the context entry has the correct Cluster and AuthInfo references. The adjustConfigKeys function creates a new context with specific Cluster and AuthInfo values (lines 66-69 of kubeconfig.go), but the test only checks if the context exists, not if it has the correct internal structure.

Copilot uses AI. Check for mistakes.
Comment on lines +283 to +295
name: "successfully assign control plane to existing context",
kconf: &clientcmdapi.Config{
Contexts: map[string]*clientcmdapi.Context{
"test-context": {
Cluster: "test-cluster",
AuthInfo: "test-user",
},
},
},
cpName: "test-cp",
ctxName: "test-context",
expectError: false,
},
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The successful test case for AssignControlPlaneToContext does not validate that the control plane was actually assigned. The test should verify that the context's Extensions field contains the control plane name after the assignment.

Copilot uses AI. Check for mistakes.
Comment on lines +252 to +256
validate: func(t *testing.T, result *clientcmdapi.Config) {
if result.Extensions == nil {
t.Error("expected extensions to be set")
}
},
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The validation for the merge test case "merge without hosting cluster context - should set it" is incomplete. While it checks that Extensions is not nil, it should also verify that the hosting cluster context was actually set to the correct value (base-context) by calling GetHostingClusterContext or checking the extension data directly.

Copilot uses AI. Check for mistakes.
Comment on lines +662 to +671
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, err := GetHostingClusterContext(tt.kconf)
if (err != nil) != tt.expectError {
t.Errorf("expected error: %v, got: %v", tt.expectError, err)
}
if err == nil && ctx != tt.expectedCtx {
t.Errorf("expected context %s, got %s", tt.expectedCtx, ctx)
}
})
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The test defines an errorContains field but never validates it. The test should check that the error message contains the expected substring when an error occurs. Add validation like: if err != nil && tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) { ... }

Copilot uses AI. Check for mistakes.
CurrentContext: "base-context",
Extensions: map[string]runtime.Object{
ExtensionKubeflexKey: &runtime.Unknown{
Raw: []byte(`{"hostingClusterContextName":"base-context"}`),
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The JSON field name used in the test is incorrect. According to the KubeflexExtensions struct definition, the JSON tag for HostingClusterContextName is "hosting-cluster-ctx-name" (kebab-case), but this test uses "hostingClusterContextName" (camelCase). This should be changed to match the struct definition.

Suggested change
Raw: []byte(`{"hostingClusterContextName":"base-context"}`),
Raw: []byte(`{"hosting-cluster-ctx-name":"base-context"}`),

Copilot uses AI. Check for mistakes.
Contexts: map[string]*clientcmdapi.Context{},
Extensions: map[string]runtime.Object{
ExtensionKubeflexKey: &runtime.Unknown{
Raw: []byte(`{"hostingClusterContextName":"non-existent"}`),
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The JSON field name used in the test is incorrect. According to the KubeflexExtensions struct definition, the JSON tag for HostingClusterContextName is "hosting-cluster-ctx-name" (kebab-case), but this test uses "hostingClusterContextName" (camelCase). This should be changed to match the struct definition.

Suggested change
Raw: []byte(`{"hostingClusterContextName":"non-existent"}`),
Raw: []byte(`{"hosting-cluster-ctx-name":"non-existent"}`),

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +100
validate: func(t *testing.T, kconf *clientcmdapi.Config, cpName string) {
expectedContext := certs.GenerateContextName(cpName)
if kconf.CurrentContext != expectedContext {
t.Errorf("expected current context %s, got %s", expectedContext, kconf.CurrentContext)
}
},
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The validation for the VCluster control plane type test case is incomplete. It only checks the CurrentContext, but should also verify that the clusters, authInfos, and contexts have been properly renamed (similar to the OCM test case validation). The adjustConfigKeys function renames all three maps for VCluster type, so all should be validated.

Copilot uses AI. Check for mistakes.
@clubanderson
Copy link
Copy Markdown
Contributor

❌ PR Title Verification Failed

Your PR title does not follow the required format.

Current title: 🌱 other : Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management

Required Format

PR titles must start with one of these emoji prefixes:

Emoji Meaning
⚠️ Breaking change
Non-breaking feature
🐛 Patch fix / Bug fix
📖 Documentation
🚀 Release
🌱 Infra/Tests/Other

How to Fix

Edit your PR title to start with the appropriate emoji. For example:

  • ✨ Add new feature for user authentication
  • 🐛 Fix crash when loading empty config
  • 📖 Update installation guide

You can edit the title by clicking the Edit button next to your PR title.


This comment was posted to help you fix the PR title format.

@adity1raut adity1raut changed the title 🌱 other : Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management 🌱 improve the test coverage of the kubeconfig Jan 14, 2026
@adity1raut adity1raut changed the title 🌱 improve the test coverage of the kubeconfig 🐛 Refactor kubeconfig tests: enhance structure and add comprehensive test cases for context management Jan 14, 2026
@clubanderson
Copy link
Copy Markdown
Contributor

/ok-to-test

@kubestellar-prow kubestellar-prow bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Improve Unit Test Coverage Across the Project

3 participants