github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+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  			Name: "x",
   132  			Head: &SimpleHead{Kind: "HorizontalPodAutoscaler"},
   133  		},
   134  	}
   135  
   136  	for _, test := range []struct {
   137  		description string
   138  		order       KindSortOrder
   139  		expected    string
   140  	}{
   141  		{"install", InstallOrder, "abcde1fgh2ijklmnopqrxstuvw!"},
   142  		{"uninstall", UninstallOrder, "wvmutsxrqponlkji2hgf1edcba!"},
   143  	} {
   144  		var buf bytes.Buffer
   145  		t.Run(test.description, func(t *testing.T) {
   146  			if got, want := len(test.expected), len(manifests); got != want {
   147  				t.Fatalf("Expected %d names in order, got %d", want, got)
   148  			}
   149  			defer buf.Reset()
   150  			for _, r := range sortByKind(manifests, test.order) {
   151  				buf.WriteString(r.Name)
   152  			}
   153  			if got := buf.String(); got != test.expected {
   154  				t.Errorf("Expected %q, got %q", test.expected, got)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  // TestKindSorterSubSort verifies manifests of same kind are also sorted alphanumeric
   161  func TestKindSorterSubSort(t *testing.T) {
   162  	manifests := []Manifest{
   163  		{
   164  			Name: "a",
   165  			Head: &SimpleHead{Kind: "ClusterRole"},
   166  		},
   167  		{
   168  			Name: "A",
   169  			Head: &SimpleHead{Kind: "ClusterRole"},
   170  		},
   171  		{
   172  			Name: "0",
   173  			Head: &SimpleHead{Kind: "ConfigMap"},
   174  		},
   175  		{
   176  			Name: "1",
   177  			Head: &SimpleHead{Kind: "ConfigMap"},
   178  		},
   179  		{
   180  			Name: "z",
   181  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   182  		},
   183  		{
   184  			Name: "!",
   185  			Head: &SimpleHead{Kind: "ClusterRoleBinding"},
   186  		},
   187  		{
   188  			Name: "u2",
   189  			Head: &SimpleHead{Kind: "Unknown"},
   190  		},
   191  		{
   192  			Name: "u1",
   193  			Head: &SimpleHead{Kind: "Unknown"},
   194  		},
   195  		{
   196  			Name: "t3",
   197  			Head: &SimpleHead{Kind: "Unknown2"},
   198  		},
   199  	}
   200  	for _, test := range []struct {
   201  		description string
   202  		order       KindSortOrder
   203  		expected    string
   204  	}{
   205  		// expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
   206  		{"cm,clusterRole,clusterRoleBinding,Unknown,Unknown2", InstallOrder, "01Aa!zu1u2t3"},
   207  	} {
   208  		var buf bytes.Buffer
   209  		t.Run(test.description, func(t *testing.T) {
   210  			defer buf.Reset()
   211  			for _, r := range sortByKind(manifests, test.order) {
   212  				buf.WriteString(r.Name)
   213  			}
   214  			if got := buf.String(); got != test.expected {
   215  				t.Errorf("Expected %q, got %q", test.expected, got)
   216  			}
   217  		})
   218  	}
   219  }