github.com/x-helm/helm@v3.0.0-beta.3+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: "I",
    32  			Head: &SimpleHead{Kind: "ClusterRoleList"},
    33  		},
    34  		{
    35  			Name: "j",
    36  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
    37  		},
    38  		{
    39  			Name: "J",
    40  			Head: &SimpleHead{Kind: "ClusterRoleBindingList"},
    41  		},
    42  		{
    43  			Name: "e",
    44  			Head: &SimpleHead{Kind: "ConfigMap"},
    45  		},
    46  		{
    47  			Name: "u",
    48  			Head: &SimpleHead{Kind: "CronJob"},
    49  		},
    50  		{
    51  			Name: "2",
    52  			Head: &SimpleHead{Kind: "CustomResourceDefinition"},
    53  		},
    54  		{
    55  			Name: "n",
    56  			Head: &SimpleHead{Kind: "DaemonSet"},
    57  		},
    58  		{
    59  			Name: "r",
    60  			Head: &SimpleHead{Kind: "Deployment"},
    61  		},
    62  		{
    63  			Name: "!",
    64  			Head: &SimpleHead{Kind: "HonkyTonkSet"},
    65  		},
    66  		{
    67  			Name: "v",
    68  			Head: &SimpleHead{Kind: "Ingress"},
    69  		},
    70  		{
    71  			Name: "t",
    72  			Head: &SimpleHead{Kind: "Job"},
    73  		},
    74  		{
    75  			Name: "c",
    76  			Head: &SimpleHead{Kind: "LimitRange"},
    77  		},
    78  		{
    79  			Name: "a",
    80  			Head: &SimpleHead{Kind: "Namespace"},
    81  		},
    82  		{
    83  			Name: "f",
    84  			Head: &SimpleHead{Kind: "PersistentVolume"},
    85  		},
    86  		{
    87  			Name: "g",
    88  			Head: &SimpleHead{Kind: "PersistentVolumeClaim"},
    89  		},
    90  		{
    91  			Name: "o",
    92  			Head: &SimpleHead{Kind: "Pod"},
    93  		},
    94  		{
    95  			Name: "q",
    96  			Head: &SimpleHead{Kind: "ReplicaSet"},
    97  		},
    98  		{
    99  			Name: "p",
   100  			Head: &SimpleHead{Kind: "ReplicationController"},
   101  		},
   102  		{
   103  			Name: "b",
   104  			Head: &SimpleHead{Kind: "ResourceQuota"},
   105  		},
   106  		{
   107  			Name: "k",
   108  			Head: &SimpleHead{Kind: "Role"},
   109  		},
   110  		{
   111  			Name: "K",
   112  			Head: &SimpleHead{Kind: "RoleList"},
   113  		},
   114  		{
   115  			Name: "l",
   116  			Head: &SimpleHead{Kind: "RoleBinding"},
   117  		},
   118  		{
   119  			Name: "L",
   120  			Head: &SimpleHead{Kind: "RoleBindingList"},
   121  		},
   122  		{
   123  			Name: "d",
   124  			Head: &SimpleHead{Kind: "Secret"},
   125  		},
   126  		{
   127  			Name: "m",
   128  			Head: &SimpleHead{Kind: "Service"},
   129  		},
   130  		{
   131  			Name: "h",
   132  			Head: &SimpleHead{Kind: "ServiceAccount"},
   133  		},
   134  		{
   135  			Name: "s",
   136  			Head: &SimpleHead{Kind: "StatefulSet"},
   137  		},
   138  		{
   139  			Name: "1",
   140  			Head: &SimpleHead{Kind: "StorageClass"},
   141  		},
   142  		{
   143  			Name: "w",
   144  			Head: &SimpleHead{Kind: "APIService"},
   145  		},
   146  		{
   147  			Name: "x",
   148  			Head: &SimpleHead{Kind: "HorizontalPodAutoscaler"},
   149  		},
   150  	}
   151  
   152  	for _, test := range []struct {
   153  		description string
   154  		order       KindSortOrder
   155  		expected    string
   156  	}{
   157  		{"install", InstallOrder, "abcde1fgh2iIjJkKlLmnopqrxstuvw!"},
   158  		{"uninstall", UninstallOrder, "wvmutsxrqponLlKkJjIi2hgf1edcba!"},
   159  	} {
   160  		var buf bytes.Buffer
   161  		t.Run(test.description, func(t *testing.T) {
   162  			if got, want := len(test.expected), len(manifests); got != want {
   163  				t.Fatalf("Expected %d names in order, got %d", want, got)
   164  			}
   165  			defer buf.Reset()
   166  			for _, r := range sortByKind(manifests, test.order) {
   167  				buf.WriteString(r.Name)
   168  			}
   169  			if got := buf.String(); got != test.expected {
   170  				t.Errorf("Expected %q, got %q", test.expected, got)
   171  			}
   172  		})
   173  	}
   174  }
   175  
   176  // TestKindSorterSubSort verifies manifests of same kind are also sorted alphanumeric
   177  func TestKindSorterSubSort(t *testing.T) {
   178  	manifests := []Manifest{
   179  		{
   180  			Name: "a",
   181  			Head: &SimpleHead{Kind: "ClusterRole"},
   182  		},
   183  		{
   184  			Name: "A",
   185  			Head: &SimpleHead{Kind: "ClusterRole"},
   186  		},
   187  		{
   188  			Name: "0",
   189  			Head: &SimpleHead{Kind: "ConfigMap"},
   190  		},
   191  		{
   192  			Name: "1",
   193  			Head: &SimpleHead{Kind: "ConfigMap"},
   194  		},
   195  		{
   196  			Name: "z",
   197  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   198  		},
   199  		{
   200  			Name: "!",
   201  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   202  		},
   203  		{
   204  			Name: "u2",
   205  			Head: &SimpleHead{Kind: "Unknown"},
   206  		},
   207  		{
   208  			Name: "u1",
   209  			Head: &SimpleHead{Kind: "Unknown"},
   210  		},
   211  		{
   212  			Name: "t3",
   213  			Head: &SimpleHead{Kind: "Unknown2"},
   214  		},
   215  	}
   216  	for _, test := range []struct {
   217  		description string
   218  		order       KindSortOrder
   219  		expected    string
   220  	}{
   221  		// expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
   222  		{"cm,clusterRole,clusterRoleBinding,Unknown,Unknown2", InstallOrder, "01Aa!zu1u2t3"},
   223  	} {
   224  		var buf bytes.Buffer
   225  		t.Run(test.description, func(t *testing.T) {
   226  			defer buf.Reset()
   227  			for _, r := range sortByKind(manifests, test.order) {
   228  				buf.WriteString(r.Name)
   229  			}
   230  			if got := buf.String(); got != test.expected {
   231  				t.Errorf("Expected %q, got %q", test.expected, got)
   232  			}
   233  		})
   234  	}
   235  }