Skip to content

Conversation

bzsuni
Copy link
Contributor

@bzsuni bzsuni commented Oct 23, 2024

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?

none

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 23, 2024
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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.

@k8s-ci-robot k8s-ci-robot added the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label Oct 23, 2024
@k8s-ci-robot k8s-ci-robot added area/kubeadm sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 23, 2024
@bzsuni bzsuni force-pushed the bz/ut/kubeadm branch 2 times, most recently from 07d2387 to d1aa906 Compare October 23, 2024 10:37
kubeConfig: generateValidKubeConfig(),
token: "test-token",
expectError: false,
},
Copy link
Member

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?

Copy link
Contributor Author

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")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test")
tmpDir, err := os.MkdirTemp("", "kubeadm-join-test")

Copy link
Contributor Author

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)
Copy link
Member

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?

Copy link
Contributor Author

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)
Copy link
Member

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?

Copy link
Contributor Author

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")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
tmpDir, err := os.MkdirTemp("", "kubeadm-images-test")
tmpDir, err := os.MkdirTemp("", "kubeadm-join-test")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Down, thanks

Copy link
Member

@neolit123 neolit123 left a 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

// 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) {

@bzsuni
Copy link
Contributor Author

bzsuni commented Oct 23, 2024

instead of testing the two exported functions

// 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

@neolit123
Copy link
Member

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.

@bzsuni
Copy link
Contributor Author

bzsuni commented Oct 24, 2024

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

Copy link
Member

@neolit123 neolit123 left a 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.

Comment on lines 67 to 73
func writeKubeConfigToFile(config *clientcmdapi.Config, filePath string) error {
configBytes, err := clientcmd.Write(*config)
if err != nil {
return err
}
return os.WriteFile(filePath, configBytes, 0644)
}
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type kubeConfigSpec struct {
type testKubeConfigSpec struct {

to better signify this is a test type.

Comment on lines 32 to 37
CertificateAuthorityData string
CertificateAuthorityFile string
Server string
ClusterName string
ContextName string
User string
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func generateKubeConfig(spec kubeConfigSpec) *clientcmdapi.Config {
func generateTestKubeConfig(spec kubeConfigSpec) *clientcmdapi.Config {

Signed-off-by: bzsuni <bingzhe.sun@daocloud.io>
Copy link
Member

@neolit123 neolit123 left a 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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 28, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: f8fa3b1ac2dfffb8f9663a8103e344d90f380a30

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 28, 2024
@k8s-ci-robot k8s-ci-robot merged commit 9ec52fc into kubernetes:master Oct 28, 2024
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.32 milestone Oct 28, 2024
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. area/kubeadm cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants