-
Notifications
You must be signed in to change notification settings - Fork 41.5k
kubeadm: Add UT for cmd/kubeadm/app/cmd/util/join.go #128283
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
Conversation
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The 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. |
07d2387
to
d1aa906
Compare
kubeConfig: generateValidKubeConfig(), | ||
token: "test-token", | ||
expectError: false, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can there be another negative test case here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetJoinWorkerCommand and GetJoinControlPlaneCommand both call getJoinCommand,
The TestGetJoinControlPlaneCommand below includes the testing scenario, so only one forward case is written here
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test") | |
tmpDir, err := os.MkdirTemp("", "kubeadm-join-test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Down, thanks
err = writeKubeConfigToFile(tt.kubeConfig, configFilePath) | ||
require.NoError(t, err) | ||
|
||
_, err = GetJoinWorkerCommand(configFilePath, tt.token, false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for err != nil should we verify the output string to be the expected one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, fixed, thanks
require.NoError(t, err) | ||
} | ||
|
||
_, err = GetJoinControlPlaneCommand(configFilePath, tt.token, "", true, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we verify the output string for err != nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Down
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test") | |
tmpDir, err := os.MkdirTemp("", "kubeadm-join-test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Down, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of testing the two exported functions
kubernetes/cmd/kubeadm/app/cmd/util/join.go
Lines 40 to 50 in 5ffb052
// GetJoinWorkerCommand returns the kubeadm join command for a given token and | |
// Kubernetes cluster (the current cluster in the kubeconfig file) | |
func GetJoinWorkerCommand(kubeConfigFile, token string, skipTokenPrint bool) (string, error) { | |
return getJoinCommand(kubeConfigFile, token, "", false, skipTokenPrint, false) | |
} | |
// GetJoinControlPlaneCommand returns the kubeadm join command for a given token and | |
// Kubernetes cluster (the current cluster in the kubeconfig file) | |
func GetJoinControlPlaneCommand(kubeConfigFile, token, key string, skipTokenPrint, skipCertificateKeyPrint bool) (string, error) { | |
return getJoinCommand(kubeConfigFile, token, key, true, skipTokenPrint, skipCertificateKeyPrint) | |
} |
just test this function:
func getJoinCommand(kubeConfigFile, token, key string, controlPlane, skipTokenPrint, skipCertificateKeyPrint bool) (string, error) { |
Yes, but I think using two functions as separate entrances would increase the coverage of unit testing, even though it doesn't actually have much effect |
please only test the called function. there is no point to test the parent exported ones. |
Fixed, please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a couple more minor comments.
func writeKubeConfigToFile(config *clientcmdapi.Config, filePath string) error { | ||
configBytes, err := clientcmd.Write(*config) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(filePath, configBytes, 0644) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just move this code in inside the test (i.e. unroll it) because it's used in only one place now.
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
) | ||
|
||
type kubeConfigSpec struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type kubeConfigSpec struct { | |
type testKubeConfigSpec struct { |
to better signify this is a test type.
CertificateAuthorityData string | ||
CertificateAuthorityFile string | ||
Server string | ||
ClusterName string | ||
ContextName string | ||
User string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all these field names can be lowercase (not exported)
User string | ||
} | ||
|
||
func generateKubeConfig(spec kubeConfigSpec) *clientcmdapi.Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func generateKubeConfig(spec kubeConfigSpec) *clientcmdapi.Config { | |
func generateTestKubeConfig(spec kubeConfigSpec) *clientcmdapi.Config { |
Signed-off-by: bzsuni <bingzhe.sun@daocloud.io>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the updates
this looks fine.
/lgtm
/approve
LGTM label has been added. Git tree hash: f8fa3b1ac2dfffb8f9663a8103e344d90f380a30
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: bzsuni, neolit123 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 |
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
Add UT for cmd/kubeadm/app/cmd/util/join.go
Running tool: /usr/local/go/bin/go test -timeout 30s -coverprofile=go-code-cover k8s.io/kubernetes/cmd/kubeadm/app/cmd/util ok k8s.io/kubernetes/cmd/kubeadm/app/cmd/util 0.028s coverage: 81.7% of statements
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: