github.com/jmclong/azuredisk-csi-driver@v0.7.0/test/e2e/suite_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package e2e 18 19 import ( 20 "context" 21 "fmt" 22 "log" 23 "os" 24 "os/exec" 25 "path" 26 "path/filepath" 27 "strings" 28 "testing" 29 30 "github.com/onsi/ginkgo" 31 "github.com/onsi/ginkgo/reporters" 32 "github.com/onsi/gomega" 33 "github.com/pborman/uuid" 34 "k8s.io/kubernetes/test/e2e/framework" 35 "sigs.k8s.io/azuredisk-csi-driver/pkg/azuredisk" 36 "sigs.k8s.io/azuredisk-csi-driver/test/e2e/driver" 37 "sigs.k8s.io/azuredisk-csi-driver/test/utils/azure" 38 "sigs.k8s.io/azuredisk-csi-driver/test/utils/credentials" 39 "sigs.k8s.io/azuredisk-csi-driver/test/utils/testutil" 40 ) 41 42 const ( 43 kubeconfigEnvVar = "KUBECONFIG" 44 reportDirEnvVar = "ARTIFACTS" 45 testMigrationEnvVar = "TEST_MIGRATION" 46 testWindowsEnvVar = "TEST_WINDOWS" 47 defaultReportDir = "/workspace/_artifacts" 48 inTreeStorageClass = "kubernetes.io/azure-disk" 49 ) 50 51 var ( 52 azurediskDriver *azuredisk.Driver 53 isUsingInTreeVolumePlugin = os.Getenv(driver.AzureDriverNameVar) == inTreeStorageClass 54 isTestingMigration = os.Getenv(testMigrationEnvVar) != "" 55 isWindowsCluster = os.Getenv(testWindowsEnvVar) != "" 56 ) 57 58 type testCmd struct { 59 command string 60 args []string 61 startLog string 62 endLog string 63 } 64 65 var _ = ginkgo.BeforeSuite(func() { 66 // k8s.io/kubernetes/test/e2e/framework requires env KUBECONFIG to be set 67 // it does not fall back to defaults 68 if os.Getenv(kubeconfigEnvVar) == "" { 69 kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") 70 os.Setenv(kubeconfigEnvVar, kubeconfig) 71 } 72 framework.HandleFlags() 73 framework.AfterReadingAllFlags(&framework.TestContext) 74 75 // Default storage driver configuration is CSI. Freshly built 76 // CSI driver is installed for that case. 77 if testutil.IsRunningInProw() && (isTestingMigration || !isUsingInTreeVolumePlugin) { 78 creds, err := credentials.CreateAzureCredentialFile(false) 79 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 80 azureClient, err := azure.GetAzureClient(creds.Cloud, creds.SubscriptionID, creds.AADClientID, creds.TenantID, creds.AADClientSecret) 81 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 82 _, err = azureClient.EnsureResourceGroup(context.Background(), creds.ResourceGroup, creds.Location, nil) 83 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 84 85 // Install Azure Disk CSI Driver on cluster from project root 86 e2eBootstrap := testCmd{ 87 command: "make", 88 args: []string{"e2e-bootstrap"}, 89 startLog: "Installing Azure Disk CSI Driver...", 90 endLog: "Azure Disk CSI Driver installed", 91 } 92 execTestCmd([]testCmd{e2eBootstrap}) 93 94 nodeid := os.Getenv("nodeid") 95 azurediskDriver = azuredisk.NewDriver(nodeid) 96 kubeconfig := os.Getenv(kubeconfigEnvVar) 97 go func() { 98 os.Setenv("AZURE_CREDENTIAL_FILE", credentials.TempAzureCredentialFilePath) 99 azurediskDriver.Run(fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()), kubeconfig) 100 }() 101 } 102 }) 103 104 var _ = ginkgo.AfterSuite(func() { 105 // Default storage driver configuration is CSI. Freshly built 106 // CSI driver is installed for that case. 107 if testutil.IsRunningInProw() { 108 azurediskLog := testCmd{ 109 command: "bash", 110 args: []string{"test/utils/azuredisk_log.sh"}, 111 startLog: "===================azuredisk log===================", 112 endLog: "===================================================", 113 } 114 if isTestingMigration || !isUsingInTreeVolumePlugin { 115 e2eTeardown := testCmd{ 116 command: "make", 117 args: []string{"e2e-teardown"}, 118 startLog: "Uninstalling Azure Disk CSI Driver...", 119 endLog: "Azure Disk CSI Driver uninstalled", 120 } 121 execTestCmd([]testCmd{azurediskLog, e2eTeardown}) 122 123 // install/uninstall Azure Disk CSI Driver deployment scripts test 124 installDriver := testCmd{ 125 command: "bash", 126 args: []string{"deploy/install-driver.sh", "master", "windows,snapshot,local"}, 127 startLog: "===================install Azure Disk CSI Driver deployment scripts test===================", 128 endLog: "===================================================", 129 } 130 uninstallDriver := testCmd{ 131 command: "bash", 132 args: []string{"deploy/uninstall-driver.sh", "master", "windows,snapshot,local"}, 133 startLog: "===================uninstall Azure Disk CSI Driver deployment scripts test===================", 134 endLog: "===================================================", 135 } 136 execTestCmd([]testCmd{installDriver, uninstallDriver}) 137 138 err := credentials.DeleteAzureCredentialFile() 139 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 140 } 141 } 142 }) 143 144 func TestE2E(t *testing.T) { 145 gomega.RegisterFailHandler(ginkgo.Fail) 146 reportDir := os.Getenv(reportDirEnvVar) 147 if reportDir == "" { 148 reportDir = defaultReportDir 149 } 150 r := []ginkgo.Reporter{reporters.NewJUnitReporter(path.Join(reportDir, "junit_01.xml"))} 151 ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "AzureDisk CSI Driver End-to-End Tests", r) 152 } 153 154 func execTestCmd(cmds []testCmd) { 155 err := os.Chdir("../..") 156 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 157 defer func() { 158 err := os.Chdir("test/e2e") 159 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 160 }() 161 162 projectRoot, err := os.Getwd() 163 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 164 gomega.Expect(strings.HasSuffix(projectRoot, "azuredisk-csi-driver")).To(gomega.Equal(true)) 165 166 for _, cmd := range cmds { 167 log.Println(cmd.startLog) 168 cmdSh := exec.Command(cmd.command, cmd.args...) 169 cmdSh.Dir = projectRoot 170 cmdSh.Stdout = os.Stdout 171 cmdSh.Stderr = os.Stderr 172 err = cmdSh.Run() 173 gomega.Expect(err).NotTo(gomega.HaveOccurred()) 174 log.Println(cmd.endLog) 175 } 176 } 177 178 func skipIfTestingInWindowsCluster() { 179 if isWindowsCluster { 180 ginkgo.Skip("test case not supported by Windows clusters") 181 } 182 } 183 184 func skipIfUsingInTreeVolumePlugin() { 185 if isUsingInTreeVolumePlugin { 186 ginkgo.Skip("test case is only available for CSI drivers") 187 } 188 } 189 190 func convertToPowershellCommandIfNecessary(command string) string { 191 if !isWindowsCluster { 192 return command 193 } 194 195 switch command { 196 case "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data": 197 return "echo 'hello world' | Out-File -FilePath C:\\mnt\\test-1\\data.txt; Get-Content C:\\mnt\\test-1\\data.txt | findstr 'hello world'" 198 case "touch /mnt/test-1/data": 199 return "echo $null >> C:\\mnt\\test-1\\data" 200 case "while true; do echo $(date -u) >> /mnt/test-1/data; sleep 1; done": 201 return "while (1) { Add-Content C:\\mnt\\test-1\\data.txt $(Get-Date -Format u); sleep 1 }" 202 case "echo 'hello world' >> /mnt/test-1/data && while true; do sleep 1; done": 203 return "Add-Content C:\\mnt\\test-1\\data.txt 'hello world'; while (1) { sleep 1 }" 204 case "echo 'hello world' > /mnt/test-1/data && echo 'hello world' > /mnt/test-2/data && echo 'hello world' > /mnt/test-3/data && grep 'hello world' /mnt/test-1/data && grep 'hello world' /mnt/test-2/data && grep 'hello world' /mnt/test-3/data": 205 return "echo 'hello world' | Out-File -FilePath C:\\mnt\\test-1\\data.txt; Get-Content C:\\mnt\\test-1\\data.txt | findstr 'hello world'; echo 'hello world' | Out-File -FilePath C:\\mnt\\test-2\\data.txt; Get-Content C:\\mnt\\test-2\\data.txt | findstr 'hello world'; echo 'hello world' | Out-File -FilePath C:\\mnt\\test-3\\data.txt; Get-Content C:\\mnt\\test-3\\data.txt | findstr 'hello world'" 206 } 207 208 return command 209 }