github.com/migueleliasweb/helm@v2.6.1+incompatible/pkg/tiller/kind_sorter_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 tiller
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	util "k8s.io/helm/pkg/releaseutil"
    24  )
    25  
    26  func TestKindSorter(t *testing.T) {
    27  	manifests := []manifest{
    28  		{
    29  			name: "i",
    30  			head: &util.SimpleHead{Kind: "ClusterRole"},
    31  		},
    32  		{
    33  			name: "j",
    34  			head: &util.SimpleHead{Kind: "ClusterRoleBinding"},
    35  		},
    36  		{
    37  			name: "e",
    38  			head: &util.SimpleHead{Kind: "ConfigMap"},
    39  		},
    40  		{
    41  			name: "u",
    42  			head: &util.SimpleHead{Kind: "CronJob"},
    43  		},
    44  		{
    45  			name: "n",
    46  			head: &util.SimpleHead{Kind: "DaemonSet"},
    47  		},
    48  		{
    49  			name: "r",
    50  			head: &util.SimpleHead{Kind: "Deployment"},
    51  		},
    52  		{
    53  			name: "!",
    54  			head: &util.SimpleHead{Kind: "HonkyTonkSet"},
    55  		},
    56  		{
    57  			name: "v",
    58  			head: &util.SimpleHead{Kind: "Ingress"},
    59  		},
    60  		{
    61  			name: "t",
    62  			head: &util.SimpleHead{Kind: "Job"},
    63  		},
    64  		{
    65  			name: "c",
    66  			head: &util.SimpleHead{Kind: "LimitRange"},
    67  		},
    68  		{
    69  			name: "a",
    70  			head: &util.SimpleHead{Kind: "Namespace"},
    71  		},
    72  		{
    73  			name: "f",
    74  			head: &util.SimpleHead{Kind: "PersistentVolume"},
    75  		},
    76  		{
    77  			name: "g",
    78  			head: &util.SimpleHead{Kind: "PersistentVolumeClaim"},
    79  		},
    80  		{
    81  			name: "o",
    82  			head: &util.SimpleHead{Kind: "Pod"},
    83  		},
    84  		{
    85  			name: "q",
    86  			head: &util.SimpleHead{Kind: "ReplicaSet"},
    87  		},
    88  		{
    89  			name: "p",
    90  			head: &util.SimpleHead{Kind: "ReplicationController"},
    91  		},
    92  		{
    93  			name: "b",
    94  			head: &util.SimpleHead{Kind: "ResourceQuota"},
    95  		},
    96  		{
    97  			name: "k",
    98  			head: &util.SimpleHead{Kind: "Role"},
    99  		},
   100  		{
   101  			name: "l",
   102  			head: &util.SimpleHead{Kind: "RoleBinding"},
   103  		},
   104  		{
   105  			name: "d",
   106  			head: &util.SimpleHead{Kind: "Secret"},
   107  		},
   108  		{
   109  			name: "m",
   110  			head: &util.SimpleHead{Kind: "Service"},
   111  		},
   112  		{
   113  			name: "h",
   114  			head: &util.SimpleHead{Kind: "ServiceAccount"},
   115  		},
   116  		{
   117  			name: "s",
   118  			head: &util.SimpleHead{Kind: "StatefulSet"},
   119  		},
   120  		{
   121  			name:    "w",
   122  			content: "",
   123  			head:    &util.SimpleHead{Kind: "APIService"},
   124  		},
   125  	}
   126  
   127  	for _, test := range []struct {
   128  		description string
   129  		order       SortOrder
   130  		expected    string
   131  	}{
   132  		{"install", InstallOrder, "abcdefghijklmnopqrstuvw!"},
   133  		{"uninstall", UninstallOrder, "wvmutsrqponlkjihgfedcba!"},
   134  	} {
   135  		var buf bytes.Buffer
   136  		t.Run(test.description, func(t *testing.T) {
   137  			if got, want := len(test.expected), len(manifests); got != want {
   138  				t.Fatalf("Expected %d names in order, got %d", want, got)
   139  			}
   140  			defer buf.Reset()
   141  			for _, r := range sortByKind(manifests, test.order) {
   142  				buf.WriteString(r.name)
   143  			}
   144  			if got := buf.String(); got != test.expected {
   145  				t.Errorf("Expected %q, got %q", test.expected, got)
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  // TestKindSorterSubSort verifies manifests of same kind are also sorted alphanumeric
   152  func TestKindSorterSubSort(t *testing.T) {
   153  	manifests := []manifest{
   154  		{
   155  			name: "a",
   156  			head: &util.SimpleHead{Kind: "ClusterRole"},
   157  		},
   158  		{
   159  			name: "A",
   160  			head: &util.SimpleHead{Kind: "ClusterRole"},
   161  		},
   162  		{
   163  			name: "0",
   164  			head: &util.SimpleHead{Kind: "ConfigMap"},
   165  		},
   166  		{
   167  			name: "1",
   168  			head: &util.SimpleHead{Kind: "ConfigMap"},
   169  		},
   170  		{
   171  			name: "z",
   172  			head: &util.SimpleHead{Kind: "ClusterRoleBinding"},
   173  		},
   174  		{
   175  			name: "!",
   176  			head: &util.SimpleHead{Kind: "ClusterRoleBinding"},
   177  		},
   178  		{
   179  			name: "u3",
   180  			head: &util.SimpleHead{Kind: "Unknown"},
   181  		},
   182  		{
   183  			name: "u1",
   184  			head: &util.SimpleHead{Kind: "Unknown"},
   185  		},
   186  		{
   187  			name: "u2",
   188  			head: &util.SimpleHead{Kind: "Unknown"},
   189  		},
   190  	}
   191  	for _, test := range []struct {
   192  		description string
   193  		order       SortOrder
   194  		expected    string
   195  	}{
   196  		// expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
   197  		{"cm,clusterRole,clusterRoleBinding,Unknown", InstallOrder, "01Aa!zu1u2u3"},
   198  	} {
   199  		var buf bytes.Buffer
   200  		t.Run(test.description, func(t *testing.T) {
   201  			defer buf.Reset()
   202  			for _, r := range sortByKind(manifests, test.order) {
   203  				buf.WriteString(r.name)
   204  			}
   205  			if got := buf.String(); got != test.expected {
   206  				t.Errorf("Expected %q, got %q", test.expected, got)
   207  			}
   208  		})
   209  	}
   210  }