github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/releaseutil/kind_sorter_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  )
    23  
    24  func TestKindSorter(t *testing.T) {
    25  	manifests := []Manifest{
    26  		{
    27  			Name: "i",
    28  			Head: &SimpleHead{Kind: "ClusterRole"},
    29  		},
    30  		{
    31  			Name: "j",
    32  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
    33  		},
    34  		{
    35  			Name: "e",
    36  			Head: &SimpleHead{Kind: "ConfigMap"},
    37  		},
    38  		{
    39  			Name: "u",
    40  			Head: &SimpleHead{Kind: "CronJob"},
    41  		},
    42  		{
    43  			Name: "2",
    44  			Head: &SimpleHead{Kind: "CustomResourceDefinition"},
    45  		},
    46  		{
    47  			Name: "n",
    48  			Head: &SimpleHead{Kind: "DaemonSet"},
    49  		},
    50  		{
    51  			Name: "r",
    52  			Head: &SimpleHead{Kind: "Deployment"},
    53  		},
    54  		{
    55  			Name: "!",
    56  			Head: &SimpleHead{Kind: "HonkyTonkSet"},
    57  		},
    58  		{
    59  			Name: "v",
    60  			Head: &SimpleHead{Kind: "Ingress"},
    61  		},
    62  		{
    63  			Name: "t",
    64  			Head: &SimpleHead{Kind: "Job"},
    65  		},
    66  		{
    67  			Name: "c",
    68  			Head: &SimpleHead{Kind: "LimitRange"},
    69  		},
    70  		{
    71  			Name: "a",
    72  			Head: &SimpleHead{Kind: "Namespace"},
    73  		},
    74  		{
    75  			Name: "f",
    76  			Head: &SimpleHead{Kind: "PersistentVolume"},
    77  		},
    78  		{
    79  			Name: "g",
    80  			Head: &SimpleHead{Kind: "PersistentVolumeClaim"},
    81  		},
    82  		{
    83  			Name: "o",
    84  			Head: &SimpleHead{Kind: "Pod"},
    85  		},
    86  		{
    87  			Name: "q",
    88  			Head: &SimpleHead{Kind: "ReplicaSet"},
    89  		},
    90  		{
    91  			Name: "p",
    92  			Head: &SimpleHead{Kind: "ReplicationController"},
    93  		},
    94  		{
    95  			Name: "b",
    96  			Head: &SimpleHead{Kind: "ResourceQuota"},
    97  		},
    98  		{
    99  			Name: "k",
   100  			Head: &SimpleHead{Kind: "Role"},
   101  		},
   102  		{
   103  			Name: "l",
   104  			Head: &SimpleHead{Kind: "RoleBinding"},
   105  		},
   106  		{
   107  			Name: "d",
   108  			Head: &SimpleHead{Kind: "Secret"},
   109  		},
   110  		{
   111  			Name: "m",
   112  			Head: &SimpleHead{Kind: "Service"},
   113  		},
   114  		{
   115  			Name: "h",
   116  			Head: &SimpleHead{Kind: "ServiceAccount"},
   117  		},
   118  		{
   119  			Name: "s",
   120  			Head: &SimpleHead{Kind: "StatefulSet"},
   121  		},
   122  		{
   123  			Name: "1",
   124  			Head: &SimpleHead{Kind: "StorageClass"},
   125  		},
   126  		{
   127  			Name: "w",
   128  			Head: &SimpleHead{Kind: "APIService"},
   129  		},
   130  	}
   131  
   132  	for _, test := range []struct {
   133  		description string
   134  		order       KindSortOrder
   135  		expected    string
   136  	}{
   137  		{"install", InstallOrder, "abcde1fgh2ijklmnopqrstuvw!"},
   138  		{"uninstall", UninstallOrder, "wvmutsrqponlkji2hgf1edcba!"},
   139  	} {
   140  		var buf bytes.Buffer
   141  		t.Run(test.description, func(t *testing.T) {
   142  			if got, want := len(test.expected), len(manifests); got != want {
   143  				t.Fatalf("Expected %d names in order, got %d", want, got)
   144  			}
   145  			defer buf.Reset()
   146  			for _, r := range sortByKind(manifests, test.order) {
   147  				buf.WriteString(r.Name)
   148  			}
   149  			if got := buf.String(); got != test.expected {
   150  				t.Errorf("Expected %q, got %q", test.expected, got)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  // TestKindSorterSubSort verifies manifests of same kind are also sorted alphanumeric
   157  func TestKindSorterSubSort(t *testing.T) {
   158  	manifests := []Manifest{
   159  		{
   160  			Name: "a",
   161  			Head: &SimpleHead{Kind: "ClusterRole"},
   162  		},
   163  		{
   164  			Name: "A",
   165  			Head: &SimpleHead{Kind: "ClusterRole"},
   166  		},
   167  		{
   168  			Name: "0",
   169  			Head: &SimpleHead{Kind: "ConfigMap"},
   170  		},
   171  		{
   172  			Name: "1",
   173  			Head: &SimpleHead{Kind: "ConfigMap"},
   174  		},
   175  		{
   176  			Name: "z",
   177  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   178  		},
   179  		{
   180  			Name: "!",
   181  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   182  		},
   183  		{
   184  			Name: "u2",
   185  			Head: &SimpleHead{Kind: "Unknown"},
   186  		},
   187  		{
   188  			Name: "u1",
   189  			Head: &SimpleHead{Kind: "Unknown"},
   190  		},
   191  		{
   192  			Name: "t3",
   193  			Head: &SimpleHead{Kind: "Unknown2"},
   194  		},
   195  	}
   196  	for _, test := range []struct {
   197  		description string
   198  		order       KindSortOrder
   199  		expected    string
   200  	}{
   201  		// expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
   202  		{"cm,clusterRole,clusterRoleBinding,Unknown,Unknown2", InstallOrder, "01Aa!zu1u2t3"},
   203  	} {
   204  		var buf bytes.Buffer
   205  		t.Run(test.description, func(t *testing.T) {
   206  			defer buf.Reset()
   207  			for _, r := range sortByKind(manifests, test.order) {
   208  				buf.WriteString(r.Name)
   209  			}
   210  			if got := buf.String(); got != test.expected {
   211  				t.Errorf("Expected %q, got %q", test.expected, got)
   212  			}
   213  		})
   214  	}
   215  }