|
| 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 integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/kubernetes-sigs/blobfuse-csi-driver/test/utils/azure" |
| 28 | + "github.com/kubernetes-sigs/blobfuse-csi-driver/test/utils/credentials" |
| 29 | + "github.com/kubernetes-sigs/blobfuse-csi-driver/test/utils/testutil" |
| 30 | + |
| 31 | + "github.com/stretchr/testify/assert" |
| 32 | +) |
| 33 | + |
| 34 | +func TestIntegrationOnAzurePublicCloud(t *testing.T) { |
| 35 | + // Test on AzurePublicCloud |
| 36 | + creds, err := credentials.CreateAzureCredentialFile(false) |
| 37 | + defer func() { |
| 38 | + err := credentials.DeleteAzureCredentialFile() |
| 39 | + assert.NoError(t, err) |
| 40 | + }() |
| 41 | + assert.NoError(t, err) |
| 42 | + assert.NotNil(t, creds) |
| 43 | + |
| 44 | + testIntegration(t, creds) |
| 45 | +} |
| 46 | + |
| 47 | +func TestIntegrationOnAzureChinaCloud(t *testing.T) { |
| 48 | + if testutil.IsRunningInProw() { |
| 49 | + t.Skipf("Skipping integration test on Azure China Cloud because Prow only tests on Azure Public Cloud at the moment") |
| 50 | + } |
| 51 | + |
| 52 | + // Test on AzureChinaCloud |
| 53 | + creds, err := credentials.CreateAzureCredentialFile(true) |
| 54 | + defer func() { |
| 55 | + err := credentials.DeleteAzureCredentialFile() |
| 56 | + assert.NoError(t, err) |
| 57 | + }() |
| 58 | + |
| 59 | + if err != nil { |
| 60 | + // Skip the test if Azure China Cloud credentials are not supplied |
| 61 | + t.Skipf("Skipping integration test on Azure China Cloud due to the following error %v", err) |
| 62 | + } |
| 63 | + assert.NotNil(t, creds) |
| 64 | + testIntegration(t, creds) |
| 65 | +} |
| 66 | + |
| 67 | +func testIntegration(t *testing.T, creds *credentials.Credentials) { |
| 68 | + os.Setenv("AZURE_CREDENTIAL_FILE", credentials.TempAzureCredentialFilePath) |
| 69 | + |
| 70 | + azureClient, err := azure.GetClient(creds.Cloud, creds.SubscriptionID, creds.AADClientID, creds.TenantID, creds.AADClientSecret) |
| 71 | + assert.NoError(t, err) |
| 72 | + |
| 73 | + ctx := context.Background() |
| 74 | + // Create an empty resource group for integration test |
| 75 | + log.Printf("Creating resource group %s in %s", creds.ResourceGroup, creds.Cloud) |
| 76 | + _, err = azureClient.EnsureResourceGroup(ctx, creds.ResourceGroup, creds.Location, nil) |
| 77 | + assert.NoError(t, err) |
| 78 | + defer func() { |
| 79 | + // Only delete resource group the test created |
| 80 | + if strings.HasPrefix(creds.ResourceGroup, credentials.ResourceGroupPrefix) { |
| 81 | + log.Printf("Deleting resource group %s", creds.ResourceGroup) |
| 82 | + err := azureClient.DeleteResourceGroup(ctx, creds.ResourceGroup) |
| 83 | + assert.NoError(t, err) |
| 84 | + } |
| 85 | + }() |
| 86 | + |
| 87 | + // Execute the script from project root |
| 88 | + err = os.Chdir("../..") |
| 89 | + assert.NoError(t, err) |
| 90 | + // Change directory back to test/integration in preparation for next test |
| 91 | + defer func() { |
| 92 | + err := os.Chdir("test/integration") |
| 93 | + assert.NoError(t, err) |
| 94 | + }() |
| 95 | + |
| 96 | + cwd, err := os.Getwd() |
| 97 | + assert.NoError(t, err) |
| 98 | + assert.True(t, strings.HasSuffix(cwd, "blobfuse-csi-driver")) |
| 99 | + |
| 100 | + // Pass in resource group name, storage account name and cloud type |
| 101 | + cmd := exec.Command("./test/integration/run-tests-all-clouds.sh", creds.ResourceGroup, creds.Cloud) |
| 102 | + cmd.Dir = cwd |
| 103 | + cmd.Stdout = os.Stdout |
| 104 | + cmd.Stderr = os.Stderr |
| 105 | + if err := cmd.Run(); err != nil { |
| 106 | + t.Fatalf("Integration test failed %v", err) |
| 107 | + } |
| 108 | +} |
0 commit comments