sigs.k8s.io/azuredisk-csi-driver@v1.30.1/test/sanity/sanity_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 sanity
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"log"
    23  	"os"
    24  	"os/exec"
    25  	"strings"
    26  	"testing"
    27  
    28  	"sigs.k8s.io/azuredisk-csi-driver/test/utils/azure"
    29  	"sigs.k8s.io/azuredisk-csi-driver/test/utils/credentials"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  const (
    35  	nodeid = "sanity-test-node"
    36  	vmType = "standard"
    37  )
    38  
    39  var useDriverV2 = flag.Bool("temp-use-driver-v2", false, "A temporary flag to enable early test and development of Azure Disk CSI Driver V2. This will be removed in the future.")
    40  
    41  func TestSanity(t *testing.T) {
    42  	// Set necessary env vars for creating azure credential file
    43  	t.Setenv("AZURE_VM_TYPE", vmType)
    44  
    45  	creds, err := credentials.CreateAzureCredentialFile()
    46  	defer func() {
    47  		err := credentials.DeleteAzureCredentialFile()
    48  		assert.NoError(t, err)
    49  	}()
    50  	assert.NoError(t, err)
    51  	assert.NotNil(t, creds)
    52  
    53  	// Set necessary env vars for sanity test
    54  	t.Setenv("AZURE_CREDENTIAL_FILE", credentials.TempAzureCredentialFilePath)
    55  	t.Setenv("nodeid", nodeid)
    56  
    57  	azureClient, err := azure.GetAzureClient(creds.Cloud, creds.SubscriptionID, creds.AADClientID, creds.TenantID, creds.AADClientSecret)
    58  	assert.NoError(t, err)
    59  
    60  	ctx := context.Background()
    61  	// Create a resource group with a VM for sanity test
    62  	log.Printf("Creating resource group %s in %s", creds.ResourceGroup, creds.Cloud)
    63  	_, err = azureClient.EnsureResourceGroup(ctx, creds.ResourceGroup, creds.Location, nil)
    64  	assert.NoError(t, err)
    65  	defer func() {
    66  		// Only delete resource group the test created
    67  		if strings.HasPrefix(creds.ResourceGroup, credentials.ResourceGroupPrefix) {
    68  			log.Printf("Deleting resource group %s", creds.ResourceGroup)
    69  			err := azureClient.DeleteResourceGroup(ctx, creds.ResourceGroup)
    70  			assert.NoError(t, err)
    71  		}
    72  	}()
    73  
    74  	log.Printf("Creating a VM in %s", creds.ResourceGroup)
    75  	_, err = azureClient.EnsureVirtualMachine(ctx, creds.ResourceGroup, creds.Location, nodeid)
    76  	assert.NoError(t, err)
    77  
    78  	// Execute the script from project root
    79  	err = os.Chdir("../..")
    80  	assert.NoError(t, err)
    81  	// Change directory back to test/sanity
    82  	defer func() {
    83  		err := os.Chdir("test/sanity")
    84  		assert.NoError(t, err)
    85  	}()
    86  
    87  	projectRoot, err := os.Getwd()
    88  	assert.NoError(t, err)
    89  	assert.True(t, strings.HasSuffix(projectRoot, "azuredisk-csi-driver"))
    90  
    91  	args := make([]string, 0)
    92  	if *useDriverV2 {
    93  		args = append(args, "v2")
    94  	}
    95  
    96  	cmd := exec.Command("./test/sanity/run-tests-all-clouds.sh", args...)
    97  	cmd.Dir = projectRoot
    98  	cmd.Stdout = os.Stdout
    99  	cmd.Stderr = os.Stderr
   100  	if err := cmd.Run(); err != nil {
   101  		t.Fatalf("Sanity test failed %v", err)
   102  	}
   103  }