github.com/jenkins-x/jx/v2@v2.1.155/pkg/cluster/eks/clusterclient_test.go (about)

     1  // +build unit
     2  
     3  package eks
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	eksctltest "github.com/jenkins-x/jx/v2/pkg/cloud/amazon/eksctl/mocks"
    11  
    12  	"github.com/jenkins-x/jx/v2/pkg/cluster"
    13  
    14  	"github.com/petergtz/pegomock"
    15  
    16  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/testutils"
    17  )
    18  
    19  func TestAWSClusterClient_List(t *testing.T) {
    20  	p := testutils.NewMockProvider("", "")
    21  	pegomock.When(p.EKS().ListClusters()).ThenReturn([]*cluster.Cluster{
    22  		{
    23  			Name:   "cluster-1",
    24  			Status: "ACTIVE",
    25  			Labels: map[string]string{
    26  				"tag1": "value1",
    27  			},
    28  			Location: "this.is.an/endpoint",
    29  		},
    30  	}, nil)
    31  	c := awsClusterClient{
    32  		Provider: p,
    33  	}
    34  
    35  	clusters, err := c.List()
    36  	assert.NoError(t, err)
    37  
    38  	assert.Len(t, clusters, 1, "there must be at least one returned cluster from List")
    39  	if len(clusters) > 0 {
    40  		cluster := clusters[0]
    41  		assert.Equal(t, "cluster-1", cluster.Name)
    42  		assert.Equal(t, "ACTIVE", cluster.Status)
    43  		assert.Equal(t, "this.is.an/endpoint", cluster.Location)
    44  		assert.Equal(t, map[string]string{
    45  			"tag1": "value1",
    46  		}, cluster.Labels)
    47  	}
    48  }
    49  
    50  func TestAWSClusterClient_Get(t *testing.T) {
    51  	p := testutils.NewMockProvider("", "")
    52  	pegomock.When(p.EKS().DescribeCluster(pegomock.EqString("cluster-1"))).ThenReturn(
    53  		&cluster.Cluster{
    54  			Name: "cluster-1",
    55  			Labels: map[string]string{
    56  				"tag1": "value1",
    57  			},
    58  			Status:   "ACTIVE",
    59  			Location: "this.is.an/endpoint",
    60  		}, "", nil,
    61  	)
    62  
    63  	c := awsClusterClient{
    64  		Provider: p,
    65  	}
    66  
    67  	obtainedCluster, err := c.Get("cluster-1")
    68  	assert.NoError(t, err)
    69  
    70  	assert.Equal(t, "cluster-1", obtainedCluster.Name)
    71  	assert.Equal(t, "ACTIVE", obtainedCluster.Status)
    72  	assert.Equal(t, "this.is.an/endpoint", obtainedCluster.Location)
    73  	assert.Equal(t, map[string]string{
    74  		"tag1": "value1",
    75  	}, obtainedCluster.Labels)
    76  }
    77  
    78  func TestAWSClusterClient_ListFilter(t *testing.T) {
    79  	p := testutils.NewMockProvider("", "")
    80  	pegomock.When(p.EKS().ListClusters()).ThenReturn([]*cluster.Cluster{
    81  		{
    82  			Name: "cluster-1",
    83  			Labels: map[string]string{
    84  				"tag1": "value1",
    85  			},
    86  			Status:   "ACTIVE",
    87  			Location: "this.is.an/endpoint",
    88  		},
    89  		{
    90  			Name: "cluster-2",
    91  			Labels: map[string]string{
    92  				"IWANT": "THIS_CLUSTER",
    93  			},
    94  			Status:   "ACTIVE",
    95  			Location: "this.is.an/endpoint",
    96  		},
    97  		{
    98  			Name:     "cluster-3",
    99  			Labels:   map[string]string{},
   100  			Status:   "ACTIVE",
   101  			Location: "this.is.an/endpoint",
   102  		},
   103  	}, nil)
   104  
   105  	c := awsClusterClient{
   106  		Provider: p,
   107  	}
   108  
   109  	clusters, err := c.ListFilter(map[string]string{
   110  		"IWANT": "THIS_CLUSTER",
   111  	})
   112  	assert.NoError(t, err)
   113  
   114  	assert.Len(t, clusters, 1, "it must return only one cluster matching tags")
   115  	if len(clusters) > 0 {
   116  		obtainedCluster := clusters[0]
   117  		assert.Equal(t, "cluster-2", obtainedCluster.Name)
   118  	}
   119  }
   120  
   121  func TestAWSClusterClient_Delete(t *testing.T) {
   122  	pegomock.RegisterMockTestingT(t)
   123  	p := testutils.NewMockProvider("", "")
   124  
   125  	c := awsClusterClient{
   126  		Provider: p,
   127  	}
   128  	cl := &cluster.Cluster{
   129  		Name: "cluster1",
   130  	}
   131  	err := c.Delete(cl)
   132  	assert.NoError(t, err)
   133  	p.EKSCtl().(*eksctltest.MockEKSCtl).VerifyWasCalledOnce().DeleteCluster(cl)
   134  }