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

tests: fix unit tests' file path compatibility and error message assertion across Windows and Linux platforms #421

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions pkg/azurefile/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"fmt"
"io/ioutil"
"os"
"reflect"
"runtime"
"testing"

"sigs.k8s.io/azurefile-csi-driver/test/utils/testutil"
)

func skipIfTestingOnWindows(t *testing.T) {
Expand All @@ -34,12 +35,14 @@ func skipIfTestingOnWindows(t *testing.T) {
// TestGetCloudProvider tests the func GetCloudProvider().
// To run this unit test successfully, need to ensure /etc/kubernetes/azure.json nonexistent.
func TestGetCloudProvider(t *testing.T) {
skipIfTestingOnWindows(t)
fakeCredFile := "fake-cred-file.json"
fakeKubeConfig := "fake-kube-config"
emptyKubeConfig := "empty-kube-config"
fakeContent := `
apiVersion: v1
var (
fakeCredFile = testutil.GetWorkDirPath("fake-cred-file.json", t)
fakeKubeConfig = testutil.GetWorkDirPath("fake-kube-config", t)
emptyKubeConfig = testutil.GetWorkDirPath("empty-kube-config", t)
notExistKubeConfig = testutil.GetWorkDirPath("non-exist.json", t)
)

fakeContent := `apiVersion: v1
clusters:
- cluster:
server: https://localhost:8080
Expand Down Expand Up @@ -76,32 +79,43 @@ users:
tests := []struct {
desc string
kubeconfig string
expectedErr error
expectedErr testutil.TestError
}{
{
desc: "[failure] out of cluster, no kubeconfig, no credential file",
kubeconfig: "",
expectedErr: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
desc: "[failure] out of cluster, no kubeconfig, no credential file",
kubeconfig: "",
expectedErr: testutil.TestError{
DefaultError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
WindowsError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathWindows),
},
},
{
desc: "[failure] out of cluster & in cluster, specify a non-exist kubeconfig, no credential file",
kubeconfig: "/tmp/non-exist.json",
expectedErr: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
desc: "[failure] out of cluster & in cluster, specify a non-exist kubeconfig, no credential file",
kubeconfig: notExistKubeConfig,
expectedErr: testutil.TestError{
DefaultError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
WindowsError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathWindows),
},
},
{
desc: "[failure] out of cluster & in cluster, specify a empty kubeconfig, no credential file",
kubeconfig: emptyKubeConfig,
expectedErr: fmt.Errorf("failed to get KubeClient: invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable"),
desc: "[failure] out of cluster & in cluster, specify a empty kubeconfig, no credential file",
kubeconfig: emptyKubeConfig,
expectedErr: testutil.TestError{
DefaultError: fmt.Errorf("failed to get KubeClient: 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",
kubeconfig: fakeKubeConfig,
expectedErr: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
desc: "[failure] out of cluster & in cluster, specify a fake kubeconfig, no credential file",
kubeconfig: fakeKubeConfig,
expectedErr: testutil.TestError{
DefaultError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathLinux),
WindowsError: fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", DefaultCredFilePathWindows),
},
},
{
desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file",
kubeconfig: "",
expectedErr: nil,
expectedErr: testutil.TestError{},
},
}

Expand Down Expand Up @@ -141,7 +155,7 @@ users:
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
}
_, err := GetCloudProvider(test.kubeconfig)
if !reflect.DeepEqual(err, test.expectedErr) {
if !testutil.AssertError(err, &test.expectedErr) {
t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/azurefile/azurefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func TestGetSnapshot(t *testing.T) {
}

func TestIsCorruptedDir(t *testing.T) {
skipIfTestingOnWindows(t)
existingMountPath, err := ioutil.TempDir(os.TempDir(), "csi-mount-test")
if err != nil {
t.Fatalf("failed to create tmp dir: %v", err)
Expand Down
Loading