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