github.com/mponton/terratest@v0.44.0/modules/k8s/node_test.go (about)

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