github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/node_test.go (about)

     1  // +build kubeall kubernetes
     2  
     3  // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube
     4  // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with
     5  // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm
     6  // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine.  We
     7  // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together.
     8  
     9  package k8s
    10  
    11  import (
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  // Tests that:
    20  // - kubectl is properly configured to talk to a kubernetes cluster
    21  // - GetNodes will return a list of nodes registered with kubernetes
    22  func TestGetNodes(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	// Assumes local kubernetes (minikube or docker-for-desktop kube), where there is only one node
    26  	options := NewKubectlOptions("", "", "default")
    27  	nodes := GetNodes(t, options)
    28  	require.Equal(t, len(nodes), 1)
    29  
    30  	node := nodes[0]
    31  	// Make sure node name is not blank, indicating an uninitialized Node object
    32  	assert.NotEqual(t, node.Name, "")
    33  }
    34  
    35  // Tests that:
    36  // - kubectl is properly configured to talk to a kubernetes cluster
    37  // - GetReadyNodes will return a list of ready nodes registered with kubernetes
    38  func TestGetReadyNodes(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	// Assumes local kubernetes (minikube or docker-for-desktop kube), where there is only one node
    42  	options := NewKubectlOptions("", "", "default")
    43  	nodes := GetReadyNodes(t, options)
    44  	require.Equal(t, len(nodes), 1)
    45  
    46  	node := nodes[0]
    47  	// Make sure node name is not blank, indicating an uninitialized Node object
    48  	assert.NotEqual(t, node.Name, "")
    49  }
    50  
    51  // Tests that:
    52  // - kubectl is properly configured to talk to a kubernetes cluster
    53  // - WaitUntilAllNodesReady checks if all nodes in the cluster are ready
    54  func TestWaitUntilAllNodesReady(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	options := NewKubectlOptions("", "", "default")
    58  
    59  	WaitUntilAllNodesReady(t, options, 12, 5*time.Second)
    60  
    61  	nodes := GetNodes(t, options)
    62  	nodeNames := map[string]bool{}
    63  	for _, node := range nodes {
    64  		nodeNames[node.Name] = true
    65  	}
    66  
    67  	readyNodes := GetReadyNodes(t, options)
    68  	readyNodeNames := map[string]bool{}
    69  	for _, node := range readyNodes {
    70  		readyNodeNames[node.Name] = true
    71  	}
    72  
    73  	assert.Equal(t, nodeNames, readyNodeNames)
    74  }