github.com/csi-driver/azuredisk-csi-driver@v0.7.0/test/integration/integration_test.go (about)

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