-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathaks_kubeconfig_test.go
More file actions
77 lines (61 loc) · 1.86 KB
/
aks_kubeconfig_test.go
File metadata and controls
77 lines (61 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package test
import (
"fmt"
"io/ioutil"
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func TestAKSKubeConfig(t *testing.T) {
t.Parallel()
terraformDependenciesOptions := &terraform.Options{
TerraformDir: "./dependencies",
VarFiles: []string{"testing.tfvars"},
}
defer terraform.Destroy(t, terraformDependenciesOptions)
terraform.InitAndApply(t, terraformDependenciesOptions)
terraformFixtureOptions := &terraform.Options{
TerraformDir: "./fixture",
VarFiles: []string{"testing.tfvars"},
}
defer terraform.Destroy(t, terraformFixtureOptions)
terraform.InitAndApply(t, terraformFixtureOptions)
aksKubeConfig := terraform.Output(t, terraformFixtureOptions, "aks_kube_config")
err := testKubeConfig(aksKubeConfig)
if err != nil {
t.Fatalf("AKS KubeConfig test has failed: %e", err)
}
}
func testKubeConfig(aksKubeConfig string) error {
// get a bytes array with the AKS kube config
kubeconfigBytes := []byte(aksKubeConfig)
// write a temporary file with kube config
err := ioutil.WriteFile("/tmp/kubeconfig", kubeconfigBytes, 0644)
if err != nil {
return err
}
// create config from file
config, err := clientcmd.BuildConfigFromFlags("", "/tmp/kubeconfig")
if err != nil {
return err
}
// create a new Kubernetes client using the config
client, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
// retrieve the list of nodes
nodesList, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return err
}
// check there are two nodes
expectedNodesCount := 2
actualNodesCount := len(nodesList.Items)
if actualNodesCount != expectedNodesCount {
return fmt.Errorf("Expected nodes count = %d, Actual = %d", expectedNodesCount, actualNodesCount)
}
return nil
}