From b3ce32fbf0e64785b9ccbbbbb42b17f1a24321df Mon Sep 17 00:00:00 2001 From: weizhichen Date: Tue, 9 Jan 2024 14:40:26 +0000 Subject: [PATCH 1/4] fix ut --- pkg/blob/azure_test.go | 65 ++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/pkg/blob/azure_test.go b/pkg/blob/azure_test.go index 4ba69d1a7..4e00a7f50 100644 --- a/pkg/blob/azure_test.go +++ b/pkg/blob/azure_test.go @@ -27,6 +27,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network" "github.com/Azure/azure-sdk-for-go/storage" "github.com/golang/mock/gomock" + "k8s.io/client-go/kubernetes" "github.com/Azure/go-autorest/autorest/azure" "github.com/stretchr/testify/assert" @@ -61,12 +62,7 @@ kind: Config users: - name: foo-user user: - exec: - apiVersion: client.authentication.k8s.io/v1alpha1 - args: - - arg-1 - - arg-2 - command: foo-command + token: 2fef7f7c64127579b48d61434c44ad46d87793169ee6a4199af3ce16a3cf5be3371 ` err := createTestFile(emptyKubeConfig) @@ -87,55 +83,41 @@ users: nodeID string userAgent string allowEmptyCloudConfig bool - expectedErr error + expectedErrorMessage string + expectError bool }{ { - desc: "out of cluster, no kubeconfig, no credential file", - kubeconfig: "", + desc: "[success] out of cluster, no kubeconfig, no credential file", nodeID: "", allowEmptyCloudConfig: true, - expectedErr: nil, + expectError: false, }, { - desc: "[failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", - kubeconfig: "", + desc: "[failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", nodeID: "", allowEmptyCloudConfig: false, - expectedErr: nil, + expectError: true, + expectedErrorMessage: "no cloud config provided, error: open /etc/kubernetes/azure.json: no such file or directory", }, { - desc: "[failure] out of cluster & in cluster, specify a non-exist kubeconfig, no credential file", - kubeconfig: "/tmp/non-exist.json", - nodeID: "", - allowEmptyCloudConfig: true, - expectedErr: nil, - }, - { - desc: "[failure] out of cluster & in cluster, specify a empty kubeconfig, no credential file", - kubeconfig: emptyKubeConfig, - nodeID: "", - allowEmptyCloudConfig: true, - expectedErr: fmt.Errorf("invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable"), - }, - { - desc: "[failure] out of cluster & in cluster, specify a fake kubeconfig, no credential file", + desc: "[success] out of cluster & in cluster, specify a fake kubeconfig, no credential file", createFakeKubeConfig: true, kubeconfig: fakeKubeConfig, nodeID: "", allowEmptyCloudConfig: true, - expectedErr: nil, + expectError: false, }, { desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file", createFakeCredFile: true, - kubeconfig: "", nodeID: "", userAgent: "useragent", allowEmptyCloudConfig: true, - expectedErr: nil, + expectError: false, }, } + var kubeClient kubernetes.Interface for _, test := range tests { if test.createFakeKubeConfig { if err := createTestFile(fakeKubeConfig); err != nil { @@ -150,6 +132,13 @@ users: if err := os.WriteFile(fakeKubeConfig, []byte(fakeContent), 0666); err != nil { t.Error(err) } + + kubeClient, err = util.GetKubeClient(test.kubeconfig, 25.0, 50, "") + if err != nil { + t.Error(err) + } + } else { + kubeClient = nil } if test.createFakeCredFile { if err := createTestFile(fakeCredFile); err != nil { @@ -169,17 +158,13 @@ users: } os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile) } - kubeClient, err := util.GetKubeClient(test.kubeconfig, 25.0, 50, "") - if err != nil { - if !reflect.DeepEqual(err, test.expectedErr) && test.expectedErr != nil && !strings.Contains(err.Error(), test.expectedErr.Error()) { - t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr) - } - continue - } + cloud, err := GetCloudProvider(context.Background(), kubeClient, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig) - if !reflect.DeepEqual(err, test.expectedErr) && test.expectedErr != nil && !strings.Contains(err.Error(), test.expectedErr.Error()) { - t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr) + assert.Equal(t, test.expectError, err != nil) + if test.expectError { + assert.Equal(t, test.expectedErrorMessage, err.Error()) } + if cloud == nil { t.Errorf("return value of getCloudProvider should not be nil even there is error") } else { From 2d3abc1aee7d8653c9632ee528a7e9c35cc020a0 Mon Sep 17 00:00:00 2001 From: weizhichen Date: Wed, 10 Jan 2024 03:05:47 +0000 Subject: [PATCH 2/4] fix ut --- pkg/blob/azure_test.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/blob/azure_test.go b/pkg/blob/azure_test.go index 04d993b3d..7da2ca295 100644 --- a/pkg/blob/azure_test.go +++ b/pkg/blob/azure_test.go @@ -22,6 +22,7 @@ import ( "os" "reflect" "strings" + "syscall" "testing" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network" @@ -82,21 +83,19 @@ users: nodeID string userAgent string allowEmptyCloudConfig bool - expectedErrorMessage string - expectError bool + expectedErr error }{ { desc: "[success] out of cluster, no kubeconfig, no credential file", nodeID: "", allowEmptyCloudConfig: true, - expectError: false, + expectedErr: nil, }, { desc: "[failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", nodeID: "", allowEmptyCloudConfig: false, - expectError: true, - expectedErrorMessage: "no cloud config provided, error: open /etc/kubernetes/azure.json: no such file or directory", + expectedErr: syscall.ENOENT, }, { desc: "[success] out of cluster & in cluster, specify a fake kubeconfig, no credential file", @@ -104,7 +103,7 @@ users: kubeconfig: fakeKubeConfig, nodeID: "", allowEmptyCloudConfig: true, - expectError: false, + expectedErr: nil, }, { desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file", @@ -112,7 +111,7 @@ users: nodeID: "", userAgent: "useragent", allowEmptyCloudConfig: true, - expectError: false, + expectedErr: nil, }, } @@ -159,10 +158,7 @@ users: } cloud, err := GetCloudProvider(context.Background(), kubeClient, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig) - assert.Equal(t, test.expectError, err != nil) - if test.expectError { - assert.Equal(t, test.expectedErrorMessage, err.Error()) - } + assert.ErrorIs(t, err, test.expectedErr) if cloud == nil { t.Errorf("return value of getCloudProvider should not be nil even there is error") From 2f52ae21931c99a3ef61bb1f14a1bfd76793a23e Mon Sep 17 00:00:00 2001 From: Weizhi Chen Date: Thu, 11 Jan 2024 00:33:40 +0800 Subject: [PATCH 3/4] fix ut windows --- pkg/blob/azure_test.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/blob/azure_test.go b/pkg/blob/azure_test.go index 7da2ca295..8aab9dc51 100644 --- a/pkg/blob/azure_test.go +++ b/pkg/blob/azure_test.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "reflect" + "runtime" "strings" "syscall" "testing" @@ -92,11 +93,17 @@ users: expectedErr: nil, }, { - desc: "[failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", + desc: "[linux][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", nodeID: "", allowEmptyCloudConfig: false, expectedErr: syscall.ENOENT, }, + { + desc: "[windows][failure][disallowEmptyCloudConfig] out of cluster, no kubeconfig, no credential file", + nodeID: "", + allowEmptyCloudConfig: false, + expectedErr: syscall.ENOTDIR, + }, { desc: "[success] out of cluster & in cluster, specify a fake kubeconfig, no credential file", createFakeKubeConfig: true, @@ -117,6 +124,12 @@ users: var kubeClient kubernetes.Interface for _, test := range tests { + if strings.HasPrefix(test.desc, "[linux]") && runtime.GOOS == "windows" { + continue + } + if strings.HasPrefix(test.desc, "[windows]") && runtime.GOOS == "linux" { + continue + } if test.createFakeKubeConfig { if err := createTestFile(fakeKubeConfig); err != nil { t.Error(err) From 23d05706e50467bdb1a4666d620901bb458b9228 Mon Sep 17 00:00:00 2001 From: weizhichen Date: Thu, 11 Jan 2024 04:30:02 +0000 Subject: [PATCH 4/4] fix --- pkg/blob/azure_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/blob/azure_test.go b/pkg/blob/azure_test.go index 8aab9dc51..3dda37553 100644 --- a/pkg/blob/azure_test.go +++ b/pkg/blob/azure_test.go @@ -124,10 +124,10 @@ users: var kubeClient kubernetes.Interface for _, test := range tests { - if strings.HasPrefix(test.desc, "[linux]") && runtime.GOOS == "windows" { + if strings.HasPrefix(test.desc, "[linux]") && runtime.GOOS != "linux" { continue } - if strings.HasPrefix(test.desc, "[windows]") && runtime.GOOS == "linux" { + if strings.HasPrefix(test.desc, "[windows]") && runtime.GOOS != "windows" { continue } if test.createFakeKubeConfig {