github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/gc/gc_helm_test.go (about)

     1  // +build unit
     2  
     3  package gc_test
     4  
     5  import (
     6  	"bytes"
     7  	"sort"
     8  	"testing"
     9  	"text/template"
    10  
    11  	"github.com/olli-ai/jx/v2/pkg/cmd/gc"
    12  
    13  	v1 "k8s.io/api/core/v1"
    14  	"k8s.io/client-go/kubernetes/scheme"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  const (
    20  	cmtemplate = `{
    21      "apiVersion": "v1",
    22      "data": {
    23          "release": "TESTDATA"
    24      },
    25      "kind": "ConfigMap",
    26      "metadata": {
    27          "creationTimestamp": "2018-04-26T22:56:12Z",
    28          "labels": {
    29              "MODIFIED_AT": "1525125621",
    30              "NAME": "{{.Name}}",
    31              "OWNER": "TILLER",
    32              "STATUS": "SUPERSEDED",
    33              "VERSION": "{{.Version}}"
    34          },
    35          "name": "{{.Name}}.v{{.Version}}",
    36          "namespace": "kube-system",
    37          "resourceVersion": "3983355",
    38          "selfLink": "/api/v1/namespaces/kube-system/configmaps/{{.Name}}.v{{.Version}}",
    39          "uid": "0447f9e9-49a5-11e8-95cd-42010a9a0000"
    40      }
    41  }`
    42  	cmlisttemplate = `{
    43      "apiVersion": "v1",
    44      "items": [{{.List}}],
    45      "kind": "ConfigMapList",
    46      "metadata": {
    47          "resourceVersion": "",
    48          "selfLink": ""
    49      }
    50  }
    51  `
    52  	v_jx_staging    = 27
    53  	v_jenkins_x     = 20
    54  	v_jx_production = 3
    55  )
    56  
    57  type CMConfig struct {
    58  	Name    string
    59  	Version int
    60  }
    61  
    62  type CMListConfig struct {
    63  	List string
    64  }
    65  
    66  func TestGCHelmSortVersion(t *testing.T) {
    67  	t.Parallel()
    68  	test_versions := []string{"jx-production.v2", "jx-production.v3", "jx-production.v1"}
    69  	sort.Sort(gc.ByVersion(test_versions))
    70  	assert.Equal(t, "jx-production.v1", test_versions[0])
    71  	assert.Equal(t, "jx-production.v2", test_versions[1])
    72  	assert.Equal(t, "jx-production.v3", test_versions[2])
    73  }
    74  
    75  func TestGCHelmSortVersionComplex(t *testing.T) {
    76  	t.Parallel()
    77  	test_versions := []string{"jx-p.v3.complex.v2", "jx-p.v1.complex.v3", "jx-p.v2.complex.v1"}
    78  	sort.Sort(gc.ByVersion(test_versions))
    79  	assert.Equal(t, "jx-p.v2.complex.v1", test_versions[0])
    80  	assert.Equal(t, "jx-p.v3.complex.v2", test_versions[1])
    81  	assert.Equal(t, "jx-p.v1.complex.v3", test_versions[2])
    82  
    83  }
    84  
    85  func TestGCHelmSortVersionMissing(t *testing.T) {
    86  	t.Parallel()
    87  	test_versions := []string{"aptly-broken3", "aptly-broken2", "aptly-broken1"}
    88  	sort.Sort(gc.ByVersion(test_versions))
    89  	assert.Equal(t, "aptly-broken3", test_versions[0])
    90  	assert.Equal(t, "aptly-broken2", test_versions[1])
    91  	assert.Equal(t, "aptly-broken1", test_versions[2])
    92  }
    93  
    94  func TestGCHelmExtract(t *testing.T) {
    95  	t.Parallel()
    96  	var b bytes.Buffer
    97  	b.WriteString(createConfigMaps(t, "jx-staging", v_jx_staging))
    98  	b.WriteString(",")
    99  	b.WriteString(createConfigMaps(t, "jenkins-x", v_jenkins_x))
   100  	b.WriteString(",")
   101  	b.WriteString(createConfigMaps(t, "jx-production", v_jx_production))
   102  	configmaplist := createConfigMapList(t, b.String())
   103  
   104  	releases := gc.ExtractReleases(configmaplist)
   105  
   106  	assert.Contains(t, releases, "jx-staging")
   107  	assert.Contains(t, releases, "jx-production")
   108  	assert.Contains(t, releases, "jenkins-x")
   109  
   110  	versions := gc.ExtractVersions(configmaplist, "jx-production")
   111  	expected_versions := []string{"jx-production.v1", "jx-production.v2", "jx-production.v3"}
   112  	assert.Equal(t, expected_versions, versions)
   113  
   114  	to_delete := gc.VersionsToDelete(versions, 10)
   115  	assert.Empty(t, to_delete)
   116  
   117  	versions = gc.ExtractVersions(configmaplist, "jx-staging")
   118  	expected_versions = []string{"jx-staging.v1", "jx-staging.v2", "jx-staging.v3", "jx-staging.v4", "jx-staging.v5", "jx-staging.v6", "jx-staging.v7", "jx-staging.v8", "jx-staging.v9", "jx-staging.v10", "jx-staging.v11", "jx-staging.v12", "jx-staging.v13", "jx-staging.v14", "jx-staging.v15", "jx-staging.v16", "jx-staging.v17", "jx-staging.v18", "jx-staging.v19", "jx-staging.v20", "jx-staging.v21", "jx-staging.v22", "jx-staging.v23", "jx-staging.v24", "jx-staging.v25", "jx-staging.v26", "jx-staging.v27"}
   119  	assert.Equal(t, expected_versions, versions)
   120  
   121  	to_delete = gc.VersionsToDelete(versions, 10)
   122  	expected_to_delete := []string{"jx-staging.v1", "jx-staging.v2", "jx-staging.v3", "jx-staging.v4", "jx-staging.v5", "jx-staging.v6", "jx-staging.v7", "jx-staging.v8", "jx-staging.v9", "jx-staging.v10", "jx-staging.v11", "jx-staging.v12", "jx-staging.v13", "jx-staging.v14", "jx-staging.v15", "jx-staging.v16", "jx-staging.v17"}
   123  	assert.Equal(t, expected_to_delete, to_delete)
   124  
   125  	versions = gc.ExtractVersions(configmaplist, "jenkins-x")
   126  	expected_versions = []string{"jenkins-x.v1", "jenkins-x.v2", "jenkins-x.v3", "jenkins-x.v4", "jenkins-x.v5", "jenkins-x.v6", "jenkins-x.v7", "jenkins-x.v8", "jenkins-x.v9", "jenkins-x.v10", "jenkins-x.v11", "jenkins-x.v12", "jenkins-x.v13", "jenkins-x.v14", "jenkins-x.v15", "jenkins-x.v16", "jenkins-x.v17", "jenkins-x.v18", "jenkins-x.v19", "jenkins-x.v20"}
   127  	assert.Equal(t, expected_versions, versions)
   128  
   129  	to_delete = gc.VersionsToDelete(versions, 10)
   130  	expected_to_delete = []string{"jenkins-x.v1", "jenkins-x.v2", "jenkins-x.v3", "jenkins-x.v4", "jenkins-x.v5", "jenkins-x.v6", "jenkins-x.v7", "jenkins-x.v8", "jenkins-x.v9", "jenkins-x.v10"}
   131  	assert.Equal(t, expected_to_delete, to_delete)
   132  
   133  	versions = gc.ExtractVersions(configmaplist, "flaming-flamingo")
   134  	assert.Empty(t, versions)
   135  
   136  	cm, err := gc.ExtractConfigMap(configmaplist, "flaming-flamingo.v1")
   137  	assert.NotNil(t, err)
   138  
   139  	cm, err = gc.ExtractConfigMap(configmaplist, "jenkins-x.v1")
   140  	assert.Nil(t, err)
   141  	assert.NotNil(t, cm)
   142  
   143  }
   144  
   145  func createConfigMaps(t *testing.T, name string, versions int) string {
   146  	var b bytes.Buffer
   147  	cmtmpl := template.New("configmap")
   148  	cmtmpl, err := cmtmpl.Parse(cmtemplate)
   149  	assert.Nil(t, err)
   150  	for i := 1; i <= versions; i++ {
   151  		cmc := CMConfig{name, i}
   152  		err1 := cmtmpl.Execute(&b, cmc)
   153  		assert.Nil(t, err1)
   154  		if i < versions {
   155  			b.WriteString(",")
   156  		}
   157  	}
   158  	return b.String()
   159  }
   160  
   161  func createConfigMapList(t *testing.T, configmaps string) *v1.ConfigMapList {
   162  	var cmlistc CMListConfig
   163  	cmlistc.List = configmaps
   164  	cmlisttmpl := template.New("configmaplist")
   165  	cmlisttmpl, err3 := cmlisttmpl.Parse(cmlisttemplate)
   166  	assert.Nil(t, err3)
   167  	var b1 bytes.Buffer
   168  	err4 := cmlisttmpl.Execute(&b1, cmlistc)
   169  	assert.Nil(t, err4)
   170  
   171  	decode := scheme.Codecs.UniversalDeserializer().Decode
   172  
   173  	obj, _, err2 := decode(b1.Bytes(), nil, nil)
   174  	assert.Nil(t, err2)
   175  	return obj.(*v1.ConfigMapList)
   176  }