Add more testcases for hooks

Signed-off-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
Yusuke Kuoka 2022-09-19 02:19:42 +00:00
parent 793050cc18
commit dc40ccde2e
7 changed files with 304 additions and 1 deletions

View File

@ -261,4 +261,225 @@ releases:
logLevel: "info", logLevel: "info",
}) })
}) })
t.Run("hooks for no-diff release", func(t *testing.T) {
check(t, testcase{
files: map[string]string{
"/path/to/helmfile.yaml": `
releases:
- name: foo
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
# only prepare and cleanup are run
- events: ["prepare", "preapply", "presync", "cleanup"]
command: echo
showlogs: true
args: ["foo"]
`,
},
selectors: []string{"app=test"},
diffs: map[exectest.DiffKey]error{
{Name: "foo", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: nil,
},
error: "",
// as we check for log output, set concurrency to 1 to avoid non-deterministic test result
concurrency: 1,
logLevel: "info",
})
})
t.Run("hooks are run on enabled release", func(t *testing.T) {
check(t, testcase{
files: map[string]string{
"/path/to/helmfile.yaml": `
values:
- bar:
enabled: true
releases:
- name: foo
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["foo"]
- name: bar
condition: bar.enabled
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["bar"]
`,
},
selectors: []string{"app=test"},
upgraded: []exectest.Release{
{Name: "foo"},
{Name: "bar"},
},
diffs: map[exectest.DiffKey]error{
{Name: "foo", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: helmexec.ExitError{Code: 2},
{Name: "bar", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: helmexec.ExitError{Code: 2},
},
error: "",
// as we check for log output, set concurrency to 1 to avoid non-deterministic test result
concurrency: 1,
logLevel: "info",
})
})
t.Run("hooks are not run on disabled release", func(t *testing.T) {
check(t, testcase{
files: map[string]string{
"/path/to/helmfile.yaml": `
values:
- bar:
enabled: false
releases:
- name: foo
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["foo"]
- name: bar
condition: bar.enabled
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["bar"]
`,
},
selectors: []string{"app=test"},
upgraded: []exectest.Release{
{Name: "foo"},
},
diffs: map[exectest.DiffKey]error{
{Name: "foo", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: helmexec.ExitError{Code: 2},
},
error: "",
// as we check for log output, set concurrency to 1 to avoid non-deterministic test result
concurrency: 1,
logLevel: "info",
})
})
t.Run("hooks are run on to-be-uninstalled release", func(t *testing.T) {
check(t, testcase{
files: map[string]string{
"/path/to/helmfile.yaml": `
releases:
- name: foo
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["foo"]
- name: bar
installed: false
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["bar"]
`,
},
selectors: []string{"app=test"},
lists: map[exectest.ListKey]string{
{Filter: "^foo$", Flags: helmV2ListFlags}: `NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
foo 4 Fri Nov 1 08:40:07 2019 DEPLOYED raw-3.1.0 3.1.0 default
`,
{Filter: "^bar$", Flags: helmV2ListFlags}: `NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
bar 4 Fri Nov 1 08:40:07 2019 DEPLOYED raw-3.1.0 3.1.0 default
`,
},
upgraded: []exectest.Release{
{Name: "foo"},
},
diffs: map[exectest.DiffKey]error{
{Name: "foo", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: helmexec.ExitError{Code: 2},
},
error: "",
// as we check for log output, set concurrency to 1 to avoid non-deterministic test result
concurrency: 1,
logLevel: "info",
})
})
t.Run("hooks are not run on alreadyd uninstalled release", func(t *testing.T) {
check(t, testcase{
files: map[string]string{
"/path/to/helmfile.yaml": `
releases:
- name: foo
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["foo"]
- name: bar
installed: false
chart: incubator/raw
namespace: default
labels:
app: test
hooks:
- events: ["prepare", "preapply", "presync"]
command: echo
showlogs: true
args: ["bar"]
`,
},
selectors: []string{"app=test"},
lists: map[exectest.ListKey]string{
{Filter: "^foo$", Flags: helmV2ListFlags}: `NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
foo 4 Fri Nov 1 08:40:07 2019 DEPLOYED raw-3.1.0 3.1.0 default
`,
{Filter: "^bar$", Flags: helmV2ListFlags}: ``,
},
upgraded: []exectest.Release{
{Name: "foo"},
},
diffs: map[exectest.DiffKey]error{
{Name: "foo", Chart: "incubator/raw", Flags: "--kube-contextdefault--namespacedefault--detailed-exitcode"}: helmexec.ExitError{Code: 2},
},
error: "",
// as we check for log output, set concurrency to 1 to avoid non-deterministic test result
concurrency: 1,
logLevel: "info",
})
})
} }

View File

@ -0,0 +1,14 @@
hook[prepare] logs | foo
hook[prepare] logs |
hook[preapply] logs | foo
hook[preapply] logs |
hook[presync] logs | foo
hook[presync] logs |
UPDATED RELEASES:
NAME CHART VERSION
foo incubator/raw 3.1.0

View File

@ -0,0 +1,14 @@
hook[prepare] logs | foo
hook[prepare] logs |
hook[preapply] logs | foo
hook[preapply] logs |
hook[presync] logs | foo
hook[presync] logs |
UPDATED RELEASES:
NAME CHART VERSION
foo incubator/raw

View File

@ -0,0 +1,24 @@
hook[prepare] logs | foo
hook[prepare] logs |
hook[prepare] logs | bar
hook[prepare] logs |
hook[preapply] logs | foo
hook[preapply] logs |
hook[preapply] logs | bar
hook[preapply] logs |
hook[presync] logs | foo
hook[presync] logs |
hook[presync] logs | bar
hook[presync] logs |
UPDATED RELEASES:
NAME CHART VERSION
foo incubator/raw
bar incubator/raw

View File

@ -0,0 +1,24 @@
hook[prepare] logs | foo
hook[prepare] logs |
hook[preapply] logs | bar
hook[preapply] logs |
hook[presync] logs | bar
hook[presync] logs |
hook[preapply] logs | foo
hook[preapply] logs |
hook[presync] logs | foo
hook[presync] logs |
UPDATED RELEASES:
NAME CHART VERSION
foo incubator/raw 3.1.0
DELETED RELEASES:
NAME
bar

View File

@ -0,0 +1,6 @@
hook[prepare] logs | foo
hook[prepare] logs |
hook[cleanup] logs | foo
hook[cleanup] logs |

View File

@ -161,7 +161,7 @@ func (helm *Helm) List(context helmexec.HelmContext, filter string, flags ...str
for k := range helm.Lists { for k := range helm.Lists {
keys = append(keys, k.String()) keys = append(keys, k.String())
} }
return "", fmt.Errorf("unexpected list key: %v in %v", key, strings.Join(keys, ", ")) return "", fmt.Errorf("unexpected list key: %v not found in %v", key, strings.Join(keys, ", "))
} }
return res, nil return res, nil
} }