mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
829 lines
23 KiB
829 lines
23 KiB
/*
|
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package tiller
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ghodss/yaml"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
"k8s.io/kubernetes/pkg/apis/core"
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
|
|
|
"k8s.io/helm/pkg/hapi"
|
|
"k8s.io/helm/pkg/hapi/chart"
|
|
"k8s.io/helm/pkg/hapi/release"
|
|
"k8s.io/helm/pkg/hooks"
|
|
"k8s.io/helm/pkg/kube"
|
|
"k8s.io/helm/pkg/tiller/environment"
|
|
)
|
|
|
|
const notesText = "my notes here"
|
|
|
|
var manifestWithHook = `kind: ConfigMap
|
|
metadata:
|
|
name: test-cm
|
|
annotations:
|
|
"helm.sh/hook": post-install,pre-delete
|
|
data:
|
|
name: value`
|
|
|
|
var manifestWithTestHook = `kind: Pod
|
|
metadata:
|
|
name: finding-nemo,
|
|
annotations:
|
|
"helm.sh/hook": test-success
|
|
spec:
|
|
containers:
|
|
- name: nemo-test
|
|
image: fake-image
|
|
cmd: fake-command
|
|
`
|
|
|
|
var manifestWithKeep = `kind: ConfigMap
|
|
metadata:
|
|
name: test-cm-keep
|
|
annotations:
|
|
"helm.sh/resource-policy": keep
|
|
data:
|
|
name: value
|
|
`
|
|
|
|
var manifestWithUpgradeHooks = `kind: ConfigMap
|
|
metadata:
|
|
name: test-cm
|
|
annotations:
|
|
"helm.sh/hook": post-upgrade,pre-upgrade
|
|
data:
|
|
name: value`
|
|
|
|
var manifestWithRollbackHooks = `kind: ConfigMap
|
|
metadata:
|
|
name: test-cm
|
|
annotations:
|
|
"helm.sh/hook": post-rollback,pre-rollback
|
|
data:
|
|
name: value
|
|
`
|
|
|
|
func rsFixture() *ReleaseServer {
|
|
dc := fake.NewSimpleClientset().Discovery()
|
|
return &ReleaseServer{
|
|
env: MockEnvironment(),
|
|
discovery: dc,
|
|
Log: func(_ string, _ ...interface{}) {},
|
|
}
|
|
}
|
|
|
|
type chartOptions struct {
|
|
*chart.Chart
|
|
}
|
|
|
|
type chartOption func(*chartOptions)
|
|
|
|
func buildChart(opts ...chartOption) *chart.Chart {
|
|
c := &chartOptions{
|
|
Chart: &chart.Chart{
|
|
// TODO: This should be more complete.
|
|
Metadata: &chart.Metadata{
|
|
Name: "hello",
|
|
},
|
|
// This adds a basic template and hooks.
|
|
Templates: []*chart.File{
|
|
{Name: "templates/hello", Data: []byte("hello: world")},
|
|
{Name: "templates/hooks", Data: []byte(manifestWithHook)},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(c)
|
|
}
|
|
|
|
return c.Chart
|
|
}
|
|
|
|
func withKube(version string) chartOption {
|
|
return func(opts *chartOptions) {
|
|
opts.Metadata.KubeVersion = version
|
|
}
|
|
}
|
|
|
|
func withDependency(dependencyOpts ...chartOption) chartOption {
|
|
return func(opts *chartOptions) {
|
|
opts.Dependencies = append(opts.Dependencies, buildChart(dependencyOpts...))
|
|
}
|
|
}
|
|
|
|
func withNotes(notes string) chartOption {
|
|
return func(opts *chartOptions) {
|
|
opts.Templates = append(opts.Templates, &chart.File{
|
|
Name: "templates/NOTES.txt",
|
|
Data: []byte(notes),
|
|
})
|
|
}
|
|
}
|
|
|
|
func withSampleTemplates() chartOption {
|
|
return func(opts *chartOptions) {
|
|
sampleTemplates := []*chart.File{
|
|
// This adds basic templates and partials.
|
|
{Name: "templates/goodbye", Data: []byte("goodbye: world")},
|
|
{Name: "templates/empty", Data: []byte("")},
|
|
{Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)},
|
|
{Name: "templates/partials/_planet", Data: []byte(`{{define "_planet"}}Earth{{end}}`)},
|
|
}
|
|
opts.Templates = append(opts.Templates, sampleTemplates...)
|
|
}
|
|
}
|
|
|
|
type installOptions struct {
|
|
*hapi.InstallReleaseRequest
|
|
}
|
|
|
|
type installOption func(*installOptions)
|
|
|
|
func withName(name string) installOption {
|
|
return func(opts *installOptions) {
|
|
opts.Name = name
|
|
}
|
|
}
|
|
|
|
func withDryRun() installOption {
|
|
return func(opts *installOptions) {
|
|
opts.DryRun = true
|
|
}
|
|
}
|
|
|
|
func withDisabledHooks() installOption {
|
|
return func(opts *installOptions) {
|
|
opts.DisableHooks = true
|
|
}
|
|
}
|
|
|
|
func withReuseName() installOption {
|
|
return func(opts *installOptions) {
|
|
opts.ReuseName = true
|
|
}
|
|
}
|
|
|
|
func withChart(chartOpts ...chartOption) installOption {
|
|
return func(opts *installOptions) {
|
|
opts.Chart = buildChart(chartOpts...)
|
|
}
|
|
}
|
|
|
|
func installRequest(opts ...installOption) *hapi.InstallReleaseRequest {
|
|
reqOpts := &installOptions{
|
|
&hapi.InstallReleaseRequest{
|
|
Namespace: "spaced",
|
|
Chart: buildChart(),
|
|
},
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(reqOpts)
|
|
}
|
|
|
|
return reqOpts.InstallReleaseRequest
|
|
}
|
|
|
|
// chartStub creates a fully stubbed out chart.
|
|
func chartStub() *chart.Chart {
|
|
return buildChart(withSampleTemplates())
|
|
}
|
|
|
|
// releaseStub creates a release stub, complete with the chartStub as its chart.
|
|
func releaseStub() *release.Release {
|
|
return namedReleaseStub("angry-panda", release.Status_DEPLOYED)
|
|
}
|
|
|
|
func namedReleaseStub(name string, status release.StatusCode) *release.Release {
|
|
return &release.Release{
|
|
Name: name,
|
|
Info: &release.Info{
|
|
FirstDeployed: time.Now(),
|
|
LastDeployed: time.Now(),
|
|
Status: &release.Status{Code: status},
|
|
Description: "Named Release Stub",
|
|
},
|
|
Chart: chartStub(),
|
|
Config: []byte(`name: value`),
|
|
Version: 1,
|
|
Hooks: []*release.Hook{
|
|
{
|
|
Name: "test-cm",
|
|
Kind: "ConfigMap",
|
|
Path: "test-cm",
|
|
Manifest: manifestWithHook,
|
|
Events: []release.HookEvent{
|
|
release.Hook_POST_INSTALL,
|
|
release.Hook_PRE_DELETE,
|
|
},
|
|
},
|
|
{
|
|
Name: "finding-nemo",
|
|
Kind: "Pod",
|
|
Path: "finding-nemo",
|
|
Manifest: manifestWithTestHook,
|
|
Events: []release.HookEvent{
|
|
release.Hook_RELEASE_TEST_SUCCESS,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func upgradeReleaseVersion(rel *release.Release) *release.Release {
|
|
rel.Info.Status.Code = release.Status_SUPERSEDED
|
|
return &release.Release{
|
|
Name: rel.Name,
|
|
Info: &release.Info{
|
|
FirstDeployed: rel.Info.FirstDeployed,
|
|
LastDeployed: time.Now(),
|
|
Status: &release.Status{Code: release.Status_DEPLOYED},
|
|
},
|
|
Chart: rel.Chart,
|
|
Config: rel.Config,
|
|
Version: rel.Version + 1,
|
|
}
|
|
}
|
|
|
|
func TestValidName(t *testing.T) {
|
|
for name, valid := range map[string]error{
|
|
"nina pinta santa-maria": errInvalidName,
|
|
"nina-pinta-santa-maria": nil,
|
|
"-nina": errInvalidName,
|
|
"pinta-": errInvalidName,
|
|
"santa-maria": nil,
|
|
"niña": errInvalidName,
|
|
"...": errInvalidName,
|
|
"pinta...": errInvalidName,
|
|
"santa...maria": nil,
|
|
"": errMissingRelease,
|
|
" ": errInvalidName,
|
|
".nina.": errInvalidName,
|
|
"nina.pinta": nil,
|
|
"abcdefghi-abcdefghi-abcdefghi-abcdefghi-abcdefghi-abcd": errInvalidName,
|
|
} {
|
|
if valid != validateReleaseName(name) {
|
|
t.Errorf("Expected %q to be %s", name, valid)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetVersionSet(t *testing.T) {
|
|
rs := rsFixture()
|
|
vs, err := GetVersionSet(rs.discovery)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !vs.Has("v1") {
|
|
t.Errorf("Expected supported versions to at least include v1.")
|
|
}
|
|
if vs.Has("nosuchversion/v1") {
|
|
t.Error("Non-existent version is reported found.")
|
|
}
|
|
}
|
|
|
|
func TestUniqName(t *testing.T) {
|
|
rs := rsFixture()
|
|
|
|
rel1 := releaseStub()
|
|
rel2 := releaseStub()
|
|
rel2.Name = "happy-panda"
|
|
rel2.Info.Status.Code = release.Status_DELETED
|
|
|
|
rs.env.Releases.Create(rel1)
|
|
rs.env.Releases.Create(rel2)
|
|
|
|
tests := []struct {
|
|
name string
|
|
expect string
|
|
reuse bool
|
|
err bool
|
|
}{
|
|
{"first", "first", false, false},
|
|
{"", "[a-z]+-[a-z]+", false, false},
|
|
{"angry-panda", "", false, true},
|
|
{"happy-panda", "", false, true},
|
|
{"happy-panda", "happy-panda", true, false},
|
|
{"hungry-hungry-hungry-hungry-hungry-hungry-hungry-hungry-hippos", "", true, true}, // Exceeds max name length
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
u, err := rs.uniqName(tt.name, tt.reuse)
|
|
if err != nil {
|
|
if tt.err {
|
|
continue
|
|
}
|
|
t.Fatal(err)
|
|
}
|
|
if tt.err {
|
|
t.Errorf("Expected an error for %q", tt.name)
|
|
}
|
|
if match, err := regexp.MatchString(tt.expect, u); err != nil {
|
|
t.Fatal(err)
|
|
} else if !match {
|
|
t.Errorf("Expected %q to match %q", u, tt.expect)
|
|
}
|
|
}
|
|
}
|
|
|
|
func releaseWithKeepStub(rlsName string) *release.Release {
|
|
ch := &chart.Chart{
|
|
Metadata: &chart.Metadata{
|
|
Name: "bunnychart",
|
|
},
|
|
Templates: []*chart.File{
|
|
{Name: "templates/configmap", Data: []byte(manifestWithKeep)},
|
|
},
|
|
}
|
|
|
|
return &release.Release{
|
|
Name: rlsName,
|
|
Info: &release.Info{
|
|
FirstDeployed: time.Now(),
|
|
LastDeployed: time.Now(),
|
|
Status: &release.Status{Code: release.Status_DEPLOYED},
|
|
},
|
|
Chart: ch,
|
|
Config: []byte(`name: value`),
|
|
Version: 1,
|
|
Manifest: manifestWithKeep,
|
|
}
|
|
}
|
|
|
|
func MockEnvironment() *environment.Environment {
|
|
e := environment.New()
|
|
e.KubeClient = &environment.PrintingKubeClient{Out: ioutil.Discard}
|
|
return e
|
|
}
|
|
|
|
func newUpdateFailingKubeClient() *updateFailingKubeClient {
|
|
return &updateFailingKubeClient{
|
|
PrintingKubeClient: environment.PrintingKubeClient{Out: os.Stdout},
|
|
}
|
|
|
|
}
|
|
|
|
type updateFailingKubeClient struct {
|
|
environment.PrintingKubeClient
|
|
}
|
|
|
|
func (u *updateFailingKubeClient) Update(namespace string, originalReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error {
|
|
return errors.New("Failed update in kube client")
|
|
}
|
|
|
|
func newHookFailingKubeClient() *hookFailingKubeClient {
|
|
return &hookFailingKubeClient{
|
|
PrintingKubeClient: environment.PrintingKubeClient{Out: ioutil.Discard},
|
|
}
|
|
}
|
|
|
|
type hookFailingKubeClient struct {
|
|
environment.PrintingKubeClient
|
|
}
|
|
|
|
func (h *hookFailingKubeClient) WatchUntilReady(ns string, r io.Reader, timeout int64, shouldWait bool) error {
|
|
return errors.New("Failed watch")
|
|
}
|
|
|
|
type mockHooksManifest struct {
|
|
Metadata struct {
|
|
Name string
|
|
Annotations map[string]string
|
|
}
|
|
}
|
|
type mockHooksKubeClient struct {
|
|
Resources map[string]*mockHooksManifest
|
|
}
|
|
|
|
var errResourceExists = errors.New("resource already exists")
|
|
|
|
func (kc *mockHooksKubeClient) makeManifest(r io.Reader) (*mockHooksManifest, error) {
|
|
b, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
manifest := &mockHooksManifest{}
|
|
if err = yaml.Unmarshal(b, manifest); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return manifest, nil
|
|
}
|
|
func (kc *mockHooksKubeClient) Create(ns string, r io.Reader, timeout int64, shouldWait bool) error {
|
|
manifest, err := kc.makeManifest(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, hasKey := kc.Resources[manifest.Metadata.Name]; hasKey {
|
|
return errResourceExists
|
|
}
|
|
|
|
kc.Resources[manifest.Metadata.Name] = manifest
|
|
|
|
return nil
|
|
}
|
|
func (kc *mockHooksKubeClient) Get(ns string, r io.Reader) (string, error) {
|
|
return "", nil
|
|
}
|
|
func (kc *mockHooksKubeClient) Delete(ns string, r io.Reader) error {
|
|
manifest, err := kc.makeManifest(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
delete(kc.Resources, manifest.Metadata.Name)
|
|
|
|
return nil
|
|
}
|
|
func (kc *mockHooksKubeClient) WatchUntilReady(ns string, r io.Reader, timeout int64, shouldWait bool) error {
|
|
paramManifest, err := kc.makeManifest(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
manifest, hasManifest := kc.Resources[paramManifest.Metadata.Name]
|
|
if !hasManifest {
|
|
return fmt.Errorf("mockHooksKubeClient.WatchUntilReady: no such resource %s found", paramManifest.Metadata.Name)
|
|
}
|
|
|
|
if manifest.Metadata.Annotations["mockHooksKubeClient/Emulate"] == "hook-failed" {
|
|
return fmt.Errorf("mockHooksKubeClient.WatchUntilReady: hook-failed")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
func (kc *mockHooksKubeClient) Update(ns string, currentReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error {
|
|
return nil
|
|
}
|
|
func (kc *mockHooksKubeClient) Build(ns string, reader io.Reader) (kube.Result, error) {
|
|
return []*resource.Info{}, nil
|
|
}
|
|
func (kc *mockHooksKubeClient) BuildUnstructured(ns string, reader io.Reader) (kube.Result, error) {
|
|
return []*resource.Info{}, nil
|
|
}
|
|
func (kc *mockHooksKubeClient) WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, timeout time.Duration) (core.PodPhase, error) {
|
|
return core.PodUnknown, nil
|
|
}
|
|
|
|
func deletePolicyStub(kubeClient *mockHooksKubeClient) *ReleaseServer {
|
|
e := environment.New()
|
|
e.KubeClient = kubeClient
|
|
|
|
dc := fake.NewSimpleClientset().Discovery()
|
|
return &ReleaseServer{
|
|
env: e,
|
|
discovery: dc,
|
|
Log: func(_ string, _ ...interface{}) {},
|
|
}
|
|
}
|
|
|
|
func deletePolicyHookStub(hookName string, extraAnnotations map[string]string, DeletePolicies []release.HookDeletePolicy) *release.Hook {
|
|
extraAnnotationsStr := ""
|
|
for k, v := range extraAnnotations {
|
|
extraAnnotationsStr += fmt.Sprintf(" \"%s\": \"%s\"\n", k, v)
|
|
}
|
|
|
|
return &release.Hook{
|
|
Name: hookName,
|
|
Kind: "Job",
|
|
Path: hookName,
|
|
Manifest: fmt.Sprintf(`kind: Job
|
|
metadata:
|
|
name: %s
|
|
annotations:
|
|
"helm.sh/hook": pre-install,pre-upgrade
|
|
%sdata:
|
|
name: value`, hookName, extraAnnotationsStr),
|
|
Events: []release.HookEvent{
|
|
release.Hook_PRE_INSTALL,
|
|
release.Hook_PRE_UPGRADE,
|
|
},
|
|
DeletePolicies: DeletePolicies,
|
|
}
|
|
}
|
|
|
|
func execHookShouldSucceed(rs *ReleaseServer, hook *release.Hook, releaseName string, namespace string, hookType string) error {
|
|
if err := rs.execHook([]*release.Hook{hook}, releaseName, namespace, hookType, 600); err != nil {
|
|
return fmt.Errorf("expected hook %s to be successful: %s", hook.Name, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func execHookShouldFail(rs *ReleaseServer, hook *release.Hook, releaseName string, namespace string, hookType string) error {
|
|
if err := rs.execHook([]*release.Hook{hook}, releaseName, namespace, hookType, 600); err == nil {
|
|
return fmt.Errorf("expected hook %s to be failed", hook.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func execHookShouldFailWithError(rs *ReleaseServer, hook *release.Hook, releaseName string, namespace string, hookType string, expectedError error) error {
|
|
if err := rs.execHook([]*release.Hook{hook}, releaseName, namespace, hookType, 600); err != expectedError {
|
|
return fmt.Errorf("expected hook %s to fail with error %v, got %v", hook.Name, expectedError, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type deletePolicyContext struct {
|
|
ReleaseServer *ReleaseServer
|
|
ReleaseName string
|
|
Namespace string
|
|
HookName string
|
|
KubeClient *mockHooksKubeClient
|
|
}
|
|
|
|
func newDeletePolicyContext() *deletePolicyContext {
|
|
kubeClient := &mockHooksKubeClient{
|
|
Resources: make(map[string]*mockHooksManifest),
|
|
}
|
|
|
|
return &deletePolicyContext{
|
|
KubeClient: kubeClient,
|
|
ReleaseServer: deletePolicyStub(kubeClient),
|
|
ReleaseName: "flying-carp",
|
|
Namespace: "river",
|
|
HookName: "migration-job",
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulHookWithoutDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
hook := deletePolicyHookStub(ctx.HookName, nil, nil)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be created by kube client", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedHookWithoutDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{"mockHooksKubeClient/Emulate": "hook-failed"},
|
|
nil,
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be created by kube client", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulHookWithSucceededDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{"helm.sh/hook-delete-policy": "hook-succeeded"},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook succeeded", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulHookWithFailedDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{"helm.sh/hook-delete-policy": "hook-failed"},
|
|
[]release.HookDeletePolicy{release.Hook_FAILED},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook succeeded", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedHookWithSucceededDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"mockHooksKubeClient/Emulate": "hook-failed",
|
|
"helm.sh/hook-delete-policy": "hook-succeeded",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED},
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook failed", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedHookWithFailedDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"mockHooksKubeClient/Emulate": "hook-failed",
|
|
"helm.sh/hook-delete-policy": "hook-failed",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_FAILED},
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook failed", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulHookWithSuccededOrFailedDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,hook-failed",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_FAILED},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook succeeded", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedHookWithSuccededOrFailedDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"mockHooksKubeClient/Emulate": "hook-failed",
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,hook-failed",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_FAILED},
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook failed", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestHookAlreadyExists(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName, nil, nil)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook succeeded", hook.Name)
|
|
}
|
|
if err := execHookShouldFailWithError(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreUpgrade, errResourceExists); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after already exists error", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestHookDeletingWithBeforeHookCreationDeletePolicy(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{"helm.sh/hook-delete-policy": "before-hook-creation"},
|
|
[]release.HookDeletePolicy{release.Hook_BEFORE_HOOK_CREATION},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook succeeded", hook.Name)
|
|
}
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreUpgrade); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook succeeded", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulHookWithMixedDeletePolicies(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_BEFORE_HOOK_CREATION},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook succeeded", hook.Name)
|
|
}
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreUpgrade); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook succeeded", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedHookWithMixedDeletePolicies(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"mockHooksKubeClient/Emulate": "hook-failed",
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_BEFORE_HOOK_CREATION},
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook failed", hook.Name)
|
|
}
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreUpgrade); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook failed", hook.Name)
|
|
}
|
|
}
|
|
|
|
func TestFailedThenSuccessfulHookWithMixedDeletePolicies(t *testing.T) {
|
|
ctx := newDeletePolicyContext()
|
|
|
|
hook := deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"mockHooksKubeClient/Emulate": "hook-failed",
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_BEFORE_HOOK_CREATION},
|
|
)
|
|
|
|
if err := execHookShouldFail(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreInstall); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; !hasResource {
|
|
t.Errorf("expected resource %s to be existing after hook failed", hook.Name)
|
|
}
|
|
|
|
hook = deletePolicyHookStub(ctx.HookName,
|
|
map[string]string{
|
|
"helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation",
|
|
},
|
|
[]release.HookDeletePolicy{release.Hook_SUCCEEDED, release.Hook_BEFORE_HOOK_CREATION},
|
|
)
|
|
|
|
if err := execHookShouldSucceed(ctx.ReleaseServer, hook, ctx.ReleaseName, ctx.Namespace, hooks.PreUpgrade); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, hasResource := ctx.KubeClient.Resources[hook.Name]; hasResource {
|
|
t.Errorf("expected resource %s to be unexisting after hook succeeded", hook.Name)
|
|
}
|
|
}
|