github.com/cloudposse/helm@v2.2.3+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  	"testing"
    21  
    22  	"github.com/ghodss/yaml"
    23  
    24  	"k8s.io/helm/pkg/chartutil"
    25  	"k8s.io/helm/pkg/proto/hapi/release"
    26  	util "k8s.io/helm/pkg/releaseutil"
    27  )
    28  
    29  func TestSortManifests(t *testing.T) {
    30  
    31  	data := []struct {
    32  		name     string
    33  		path     string
    34  		kind     string
    35  		hooks    []release.Hook_Event
    36  		manifest string
    37  	}{
    38  		{
    39  			name:  "first",
    40  			path:  "one",
    41  			kind:  "Job",
    42  			hooks: []release.Hook_Event{release.Hook_PRE_INSTALL},
    43  			manifest: `apiVersion: v1
    44  kind: Job
    45  metadata:
    46    name: first
    47    labels:
    48      doesnot: matter
    49    annotations:
    50      "helm.sh/hook": pre-install
    51  `,
    52  		},
    53  		{
    54  			name:  "second",
    55  			path:  "two",
    56  			kind:  "ReplicaSet",
    57  			hooks: []release.Hook_Event{release.Hook_POST_INSTALL},
    58  			manifest: `kind: ReplicaSet
    59  apiVersion: v1beta1
    60  metadata:
    61    name: second
    62    annotations:
    63      "helm.sh/hook": post-install
    64  `,
    65  		}, {
    66  			name:  "third",
    67  			path:  "three",
    68  			kind:  "ReplicaSet",
    69  			hooks: []release.Hook_Event{},
    70  			manifest: `kind: ReplicaSet
    71  apiVersion: v1beta1
    72  metadata:
    73    name: third
    74    annotations:
    75      "helm.sh/hook": no-such-hook
    76  `,
    77  		}, {
    78  			name:  "fourth",
    79  			path:  "four",
    80  			kind:  "Pod",
    81  			hooks: []release.Hook_Event{},
    82  			manifest: `kind: Pod
    83  apiVersion: v1
    84  metadata:
    85    name: fourth
    86    annotations:
    87      nothing: here
    88  `,
    89  		}, {
    90  			name:  "fifth",
    91  			path:  "five",
    92  			kind:  "ReplicaSet",
    93  			hooks: []release.Hook_Event{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:     "sixth",
   104  			path:     "six/_six",
   105  			kind:     "ReplicaSet",
   106  			hooks:    []release.Hook_Event{},
   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:     "seventh",
   111  			path:     "seven",
   112  			kind:     "ReplicaSet",
   113  			hooks:    []release.Hook_Event{},
   114  			manifest: "",
   115  		},
   116  	}
   117  
   118  	manifests := make(map[string]string, len(data))
   119  	for _, o := range data {
   120  		manifests[o.path] = o.manifest
   121  	}
   122  
   123  	hs, generic, err := sortManifests(manifests, chartutil.NewVersionSet("v1", "v1beta1"), InstallOrder)
   124  	if err != nil {
   125  		t.Fatalf("Unexpected error: %s", err)
   126  	}
   127  
   128  	// This test will fail if 'six' or 'seven' was added.
   129  	if len(generic) != 1 {
   130  		t.Errorf("Expected 1 generic manifest, got %d", len(generic))
   131  	}
   132  
   133  	if len(hs) != 3 {
   134  		t.Errorf("Expected 3 hooks, got %d", len(hs))
   135  	}
   136  
   137  	for _, out := range hs {
   138  		found := false
   139  		for _, expect := range data {
   140  			if out.Path == expect.path {
   141  				found = true
   142  				if out.Path != expect.path {
   143  					t.Errorf("Expected path %s, got %s", expect.path, out.Path)
   144  				}
   145  				if out.Name != expect.name {
   146  					t.Errorf("Expected name %s, got %s", expect.name, out.Name)
   147  				}
   148  				if out.Kind != expect.kind {
   149  					t.Errorf("Expected kind %s, got %s", expect.kind, out.Kind)
   150  				}
   151  				for i := 0; i < len(out.Events); i++ {
   152  					if out.Events[i] != expect.hooks[i] {
   153  						t.Errorf("Expected event %d, got %d", expect.hooks[i], out.Events[i])
   154  					}
   155  				}
   156  			}
   157  		}
   158  		if !found {
   159  			t.Errorf("Result not found: %v", out)
   160  		}
   161  	}
   162  
   163  	// Verify the sort order
   164  	sorted := make([]manifest, len(data))
   165  	for i, s := range data {
   166  		var sh util.SimpleHead
   167  		err := yaml.Unmarshal([]byte(s.manifest), &sh)
   168  		if err != nil {
   169  			// This is expected for manifests that are corrupt or empty.
   170  			t.Log(err)
   171  		}
   172  		sorted[i] = manifest{
   173  			content: s.manifest,
   174  			name:    s.name,
   175  			head:    &sh,
   176  		}
   177  	}
   178  	sorted = sortByKind(sorted, InstallOrder)
   179  	for i, m := range generic {
   180  		if m.content != sorted[i].content {
   181  			t.Errorf("Expected %q, got %q", m.content, sorted[i].content)
   182  		}
   183  	}
   184  
   185  }
   186  
   187  func TestVersionSet(t *testing.T) {
   188  	vs := chartutil.NewVersionSet("v1", "v1beta1", "extensions/alpha5", "batch/v1")
   189  
   190  	if l := len(vs); l != 4 {
   191  		t.Errorf("Expected 4, got %d", l)
   192  	}
   193  
   194  	if !vs.Has("extensions/alpha5") {
   195  		t.Error("No match for alpha5")
   196  	}
   197  
   198  	if vs.Has("nosuch/extension") {
   199  		t.Error("Found nonexistent extension")
   200  	}
   201  }