github.com/zoumo/helm@v2.5.0+incompatible/pkg/tiller/hooks_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  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/ghodss/yaml"
    24  
    25  	"k8s.io/helm/pkg/chartutil"
    26  	"k8s.io/helm/pkg/proto/hapi/release"
    27  	util "k8s.io/helm/pkg/releaseutil"
    28  )
    29  
    30  func TestSortManifests(t *testing.T) {
    31  
    32  	data := []struct {
    33  		name     []string
    34  		path     string
    35  		kind     []string
    36  		hooks    map[string][]release.Hook_Event
    37  		manifest string
    38  	}{
    39  		{
    40  			name:  []string{"first"},
    41  			path:  "one",
    42  			kind:  []string{"Job"},
    43  			hooks: map[string][]release.Hook_Event{"first": {release.Hook_PRE_INSTALL}},
    44  			manifest: `apiVersion: v1
    45  kind: Job
    46  metadata:
    47    name: first
    48    labels:
    49      doesnot: matter
    50    annotations:
    51      "helm.sh/hook": pre-install
    52  `,
    53  		},
    54  		{
    55  			name:  []string{"second"},
    56  			path:  "two",
    57  			kind:  []string{"ReplicaSet"},
    58  			hooks: map[string][]release.Hook_Event{"second": {release.Hook_POST_INSTALL}},
    59  			manifest: `kind: ReplicaSet
    60  apiVersion: v1beta1
    61  metadata:
    62    name: second
    63    annotations:
    64      "helm.sh/hook": post-install
    65  `,
    66  		}, {
    67  			name:  []string{"third"},
    68  			path:  "three",
    69  			kind:  []string{"ReplicaSet"},
    70  			hooks: map[string][]release.Hook_Event{"third": nil},
    71  			manifest: `kind: ReplicaSet
    72  apiVersion: v1beta1
    73  metadata:
    74    name: third
    75    annotations:
    76      "helm.sh/hook": no-such-hook
    77  `,
    78  		}, {
    79  			name:  []string{"fourth"},
    80  			path:  "four",
    81  			kind:  []string{"Pod"},
    82  			hooks: map[string][]release.Hook_Event{"fourth": nil},
    83  			manifest: `kind: Pod
    84  apiVersion: v1
    85  metadata:
    86    name: fourth
    87    annotations:
    88      nothing: here`,
    89  		}, {
    90  			name:  []string{"fifth"},
    91  			path:  "five",
    92  			kind:  []string{"ReplicaSet"},
    93  			hooks: map[string][]release.Hook_Event{"fifth": {release.Hook_POST_DELETE, release.Hook_POST_INSTALL}},
    94  			manifest: `kind: ReplicaSet
    95  apiVersion: v1beta1
    96  metadata:
    97    name: fifth
    98    annotations:
    99      "helm.sh/hook": post-delete, post-install
   100  `,
   101  		}, {
   102  			// Regression test: files with an underscore in the base name should be skipped.
   103  			name:     []string{"sixth"},
   104  			path:     "six/_six",
   105  			kind:     []string{"ReplicaSet"},
   106  			hooks:    map[string][]release.Hook_Event{"sixth": nil},
   107  			manifest: `invalid manifest`, // This will fail if partial is not skipped.
   108  		}, {
   109  			// Regression test: files with no content should be skipped.
   110  			name:     []string{"seventh"},
   111  			path:     "seven",
   112  			kind:     []string{"ReplicaSet"},
   113  			hooks:    map[string][]release.Hook_Event{"seventh": nil},
   114  			manifest: "",
   115  		},
   116  		{
   117  			name:  []string{"eighth", "example-test"},
   118  			path:  "eight",
   119  			kind:  []string{"ConfigMap", "Pod"},
   120  			hooks: map[string][]release.Hook_Event{"eighth": nil, "example-test": {release.Hook_RELEASE_TEST_SUCCESS}},
   121  			manifest: `kind: ConfigMap
   122  apiVersion: v1
   123  metadata:
   124    name: eighth
   125  data:
   126    name: value
   127  ---
   128  apiVersion: v1
   129  kind: Pod
   130  metadata:
   131    name: example-test
   132    annotations:
   133      "helm.sh/hook": test-success
   134  `,
   135  		},
   136  	}
   137  
   138  	manifests := make(map[string]string, len(data))
   139  	for _, o := range data {
   140  		manifests[o.path] = o.manifest
   141  	}
   142  
   143  	hs, generic, err := sortManifests(manifests, chartutil.NewVersionSet("v1", "v1beta1"), InstallOrder)
   144  	if err != nil {
   145  		t.Fatalf("Unexpected error: %s", err)
   146  	}
   147  
   148  	// This test will fail if 'six' or 'seven' was added.
   149  	if len(generic) != 2 {
   150  		t.Errorf("Expected 2 generic manifests, got %d", len(generic))
   151  	}
   152  
   153  	if len(hs) != 4 {
   154  		t.Errorf("Expected 4 hooks, got %d", len(hs))
   155  	}
   156  
   157  	for _, out := range hs {
   158  		found := false
   159  		for _, expect := range data {
   160  			if out.Path == expect.path {
   161  				found = true
   162  				if out.Path != expect.path {
   163  					t.Errorf("Expected path %s, got %s", expect.path, out.Path)
   164  				}
   165  				nameFound := false
   166  				for _, expectedName := range expect.name {
   167  					if out.Name == expectedName {
   168  						nameFound = true
   169  					}
   170  				}
   171  				if !nameFound {
   172  					t.Errorf("Got unexpected name %s", out.Name)
   173  				}
   174  				kindFound := false
   175  				for _, expectedKind := range expect.kind {
   176  					if out.Kind == expectedKind {
   177  						kindFound = true
   178  					}
   179  				}
   180  				if !kindFound {
   181  					t.Errorf("Got unexpected kind %s", out.Kind)
   182  				}
   183  
   184  				expectedHooks := expect.hooks[out.Name]
   185  				if !reflect.DeepEqual(expectedHooks, out.Events) {
   186  					t.Errorf("expected events: %v but got: %v", expectedHooks, out.Events)
   187  				}
   188  
   189  			}
   190  		}
   191  		if !found {
   192  			t.Errorf("Result not found: %v", out)
   193  		}
   194  	}
   195  
   196  	// Verify the sort order
   197  	sorted := []manifest{}
   198  	for _, s := range data {
   199  		manifests := util.SplitManifests(s.manifest)
   200  		mCount := 0
   201  		for _, m := range manifests {
   202  			name := s.name[mCount]
   203  
   204  			var sh util.SimpleHead
   205  			err := yaml.Unmarshal([]byte(m), &sh)
   206  			if err != nil {
   207  				// This is expected for manifests that are corrupt or empty.
   208  				t.Log(err)
   209  			}
   210  
   211  			//only keep track of non-hook manifests
   212  			if err == nil && s.hooks[name] == nil {
   213  				another := manifest{
   214  					content: m,
   215  					name:    name,
   216  					head:    &sh,
   217  				}
   218  				sorted = append(sorted, another)
   219  			}
   220  
   221  			mCount++
   222  		}
   223  	}
   224  
   225  	sorted = sortByKind(sorted, InstallOrder)
   226  	for i, m := range generic {
   227  		if m.content != sorted[i].content {
   228  			t.Errorf("Expected %q, got %q", m.content, sorted[i].content)
   229  		}
   230  	}
   231  }
   232  
   233  func TestVersionSet(t *testing.T) {
   234  	vs := chartutil.NewVersionSet("v1", "v1beta1", "extensions/alpha5", "batch/v1")
   235  
   236  	if l := len(vs); l != 4 {
   237  		t.Errorf("Expected 4, got %d", l)
   238  	}
   239  
   240  	if !vs.Has("extensions/alpha5") {
   241  		t.Error("No match for alpha5")
   242  	}
   243  
   244  	if vs.Has("nosuch/extension") {
   245  		t.Error("Found nonexistent extension")
   246  	}
   247  }