gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.5k stars 1.33k forks source link

no Auth Provider found for name "azure" #1017

Open edvinas31 opened 3 years ago

edvinas31 commented 3 years ago

Hello, all. I would like to share my question rather than issue. I have created AKS cluster successfully using terraform with following code:

resource "azurerm_subnet" "aks_subnet" {
  name                  = var.subnet_name
  resource_group_name   = var.resource_group_name
  virtual_network_name  = var.vnet
  address_prefixes      = var.address_prefixes
}

resource "azurerm_kubernetes_cluster" "aks" {
  name                            = var.k8s_cluster_name
  dns_prefix                      = var.k8s_cluster_name
  location                        = var.resource_group_location
  resource_group_name             = var.resource_group_name
  kubernetes_version              = var.kubernetes_version
default_node_pool {
    name                  = var.k8s_default_np_name
    orchestrator_version  = var.kubernetes_version
    node_count            = var.node_count_default_np
    vm_size               = var.k8s_vm_size_system_np
    vnet_subnet_id        = azurerm_subnet.aks_subnet.id
  }  

identity {
    type = var.identity_type
  }

  role_based_access_control {
    enabled = var.role_based_access_control

    azure_active_directory {
      managed = var.ad_managed
      admin_group_object_ids = var.admin_group_object_ids
    }
  }
network_profile {
    load_balancer_sku  = var.load_balancer_sku
    outbound_type      = var.outbound_type
    network_plugin     = var.network_plugin
    network_policy     = var.network_policy
  }
}

resource "azurerm_kubernetes_cluster_node_pool" "user" {
  name                  = var.k8s_user_np_name
  kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
  vm_size               = var.vm_size
  node_count            = var.node_count
}

# ---------------------------------------------------------------------------------------------------------------------
# CREATE KUBECONFIG FILE
# ---------------------------------------------------------------------------------------------------------------------

resource "local_file" "kubeconfig" {
  content  = azurerm_kubernetes_cluster.aks.kube_config_raw
  filename = "../../../../kubeconfig"

  depends_on = [
    azurerm_kubernetes_cluster.aks
  ]
}

And I wanted to do use terratest so I found an aks terratest https://github.com/gruntwork-io/terratest/blob/master/test/azure/terraform_azure_aks_example_test.go example, which tests deployment:

package test

import (
    "crypto/tls"
    "fmt"
    "path/filepath"
    "strings"
    "terraform-test/helpers"
    "testing"
    "time"

    http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
    "github.com/gruntwork-io/terratest/modules/k8s"
    "github.com/gruntwork-io/terratest/modules/random"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/require"
)

func TestK8sExample(t *testing.T) {

    // Deploy VNET
    vnetOpts := createK8sVnetOpts(t)
    defer terraform.Destroy(t, vnetOpts)
    terraform.InitAndApply(t, vnetOpts)

    varAddressPrefixes := []string{"10.1.0.0/23"}
    varNodeCount := 3
    varVmSize := "standard_d2s_v4"

    // Deploy Kubernetes
    kubernetesOpts := &terraform.Options{
        TerraformDir:    "../../examples/azure/kubernetes",
        TerraformBinary: "terragrunt",
        Vars: map[string]interface{}{
            "vnet":                    terraform.OutputRequired(t, vnetOpts, "vnet"),
            "resource_group_name":     terraform.OutputRequired(t, vnetOpts, "resource_group_name"),
            "resource_group_location": terraform.OutputRequired(t, vnetOpts, "resource_group_location"),
            "address_prefixes":        varAddressPrefixes,
            "node_count":              varNodeCount,
            "vm_size":                 varVmSize,
        },
    }
    defer terraform.Destroy(t, kubernetesOpts)
    terraform.InitAndApply(t, kubernetesOpts)

    TestTerraformAzureAKSExample(t)

}

func createK8sVnetOpts(t *testing.T) *terraform.Options {
    varResourceGroup := helpers.GenerateRgName()
    varResourceGroupLocation := "northeurope"
    varVnetName := helpers.GenerateVnetName()
    varVnetAddressSpace := []string{"10.1.0.0/23"}

    return &terraform.Options{
        TerraformDir:    "../../examples/azure/network/vnet",
        TerraformBinary: "terragrunt",
        Vars: map[string]interface{}{
            "rg_name":            varResourceGroup,
            "rg_location":        varResourceGroupLocation,
            "vnet_name":          varVnetName,
            "vnet_address_space": varVnetAddressSpace,
        },
    }
}

func TestTerraformAzureAKSExample(t *testing.T) {
    t.Parallel()

    // Path to the Kubernetes resource config we will test
    kubeResourcePath, err := filepath.Abs("../../examples/azure/kubernetes/terraform-azure-aks-example/nginx-deployment.yml")
    require.NoError(t, err)

    // To ensure we can reuse the resource config on the same cluster to test different scenarios, we setup a unique
    // namespace for the resources for this test.
    // Note that namespaces must be lowercase.
    namespaceName := strings.ToLower(random.UniqueId())

    // Setup the kubectl config and context. Here we choose to use the defaults, which is:
    // - HOME/.kube/config for the kubectl config file
    // - Current context of the kubectl config file
    options := k8s.NewKubectlOptions("", "../../examples/azure/kubeconfig", namespaceName)

    k8s.CreateNamespace(t, options, namespaceName)
    // ... and make sure to delete the namespace at the end of the test
    defer k8s.DeleteNamespace(t, options, namespaceName)

    // At the end of the test, run `kubectl delete -f RESOURCE_CONFIG` to clean up any resources that were created.
    defer k8s.KubectlDelete(t, options, kubeResourcePath)

    // This will run `kubectl apply -f RESOURCE_CONFIG` and fail the test if there are any errors
    k8s.KubectlApply(t, options, kubeResourcePath)

    // This will wait up to 10 seconds for the service to become available, to ensure that we can access it.
    k8s.WaitUntilServiceAvailable(t, options, "nginx-service", 10, 20*time.Second)
    // Now we verify that the service will successfully boot and start serving requests
    service := k8s.GetService(t, options, "nginx-service")
    endpoint := k8s.GetServiceEndpoint(t, options, service, 80)

    // Setup a TLS configuration to submit with the helper, a blank struct is acceptable
    tlsConfig := tls.Config{}

    // Test the endpoint for up to 5 minutes. This will only fail if we timeout waiting for the service to return a 200
    // response.
    http_helper.HttpGetWithRetryWithCustomValidation(
        t,
        fmt.Sprintf("http://%s", endpoint),
        &tlsConfig,
        30,
        10*time.Second,
        func(statusCode int, body string) bool {
            return statusCode == 200
        },
    )
}

However I get an error:

--- FAIL: TestK8sExample (777.42s)
=== RUN   TestKubernetesBasicExample
TestKubernetesBasicExample 2021-10-15T08:53:08+03:00 client.go:42: Configuring Kubernetes client using config file /home/esu/.kube/config with context
    namespace.go:15:
                Error Trace:    namespace.go:15
                                                        k8s_example_test.go:85
                Error:          Received unexpected error:
                                no Auth Provider found for name "azure"
                Test:           TestKubernetesBasicExample
--- FAIL: TestKubernetesBasicExample (0.00s)
FAIL
FAIL    command-line-arguments  777.433s
FAIL

Any help is appreciated

edvinas31 commented 3 years ago

After my module generates kubeconfig file and I try to run "kubectl get nodes" it asks me to authenticate using browser:

 $ kubectl get nodes
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXX to authenticate.

After I login I can see everything clearly. Maybe there is some option not to ask this prompt?

tkent commented 2 years ago

:wave: See the answer to #976. It's the same situation.