Skip to content

Commit

Permalink
Update override code added additional tests, update version and chang…
Browse files Browse the repository at this point in the history
…elog
  • Loading branch information
quintush committed Oct 1, 2024
1 parent 7177016 commit 71a84e1
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 49 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
0.6.2 / 2024-09-30
0.6.2 / 2024-10-02
==================
- Fix assertion does not match when using camelcase function (resolves #359)
- Fix templating multi document Helm file with trims break YAML parsing (resolves #403)
- Fix lookup of resources defined in kubernetesProvider without any objects (resolves #380)
- Improvement support for linux x390x
- Fix chart object cannot be overwritten in the test job (credits @ivankatliarchuk, resolves #412)
- Fix capabilities.apiVersions cannot be unset (resolves #413)
- Improvement support for linux x390x (credits @dandotimujahid)
- Update packages to latest patch versions
- Update documentation

Expand Down
6 changes: 5 additions & 1 deletion DOCUMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ tests:

- **set**: *object of any, optional*. Set the values directly in suite file. The key is the value path with the format just like `--set` option of `helm install`, for example `image.pullPolicy`. The value is anything you want to set to the path specified by the key, which can be even an array or an object. This set will override values which are already set in the values file.

- **template**: *string, optional*. **templates**: *array of string, optional*. The template file(s) which render the manifest to be tested, default to the list of template file defined in `templates` of suite file, unless template is defined in the assertion(s) (check [Assertion](#assertion)).
- **template**: *string, optional*. <br/>**templates**: *array of string, optional*. The template file(s) which render the manifest to be tested, default to the list of template file defined in `templates` of suite file, unless template is defined in the assertion(s) (check [Assertion](#assertion)).

- **documentIndex**: *int, optional*. The index of rendered documents (divided by `---`) to be tested, default to -1, which results in asserting all documents (see Assertion). Generally you can ignored this field if the template file render only one document.

Expand All @@ -151,6 +151,10 @@ tests:
- **version**: *string, optional*. The semantic version of the chart, default to the version set in the Chart.
- **appVersion**: *string, optional*. The app-version of the chart, default to the app-version set in the Chart.

- **kubernetesProvider**: *object, optional*. Define Kubernetes resources to fake.
- **scheme**: *object, optional*. Define the Kubernetes schema to fake
- **objects**: *array of objects*. Define the Kubernetes objects to fake

- **asserts**: *array of assertion, required*. The assertions to validate the rendered chart, check [Assertion](#assertion).

## Assertion
Expand Down
2 changes: 1 addition & 1 deletion pkg/unittest/.snapshots/TestV3RunnerOkWithFakeK8sClient
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Charts: 1 passed, 1 total
Test Suites: 4 passed, 4 total
Tests: 4 passed, 4 total
Tests: 5 passed, 5 total
Snapshot: 4 passed, 4 total
Time: XX.XXXms

Expand Down
14 changes: 7 additions & 7 deletions pkg/unittest/test_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ func (t *TestJob) releaseV3Option() *v3util.ReleaseOptions {
func (t *TestJob) capabilitiesV3() *v3util.Capabilities {
capabilities := v3util.DefaultCapabilities

// Override the version, when set.
if t.Capabilities.MajorVersion != "" || t.Capabilities.MinorVersion != "" {
capabilities.KubeVersion = v3util.KubeVersion{
Version: fmt.Sprintf("v%s.%s.0", t.Capabilities.MajorVersion, t.Capabilities.MinorVersion),
Major: cmp.Or(t.Capabilities.MajorVersion, capabilities.KubeVersion.Major),
Minor: cmp.Or(t.Capabilities.MinorVersion, capabilities.KubeVersion.Minor),
}
majorVersion := cmp.Or(t.Capabilities.MajorVersion, capabilities.KubeVersion.Major)
minorVersion := cmp.Or(t.Capabilities.MinorVersion, capabilities.KubeVersion.Minor)

capabilities.KubeVersion = v3util.KubeVersion{
Version: fmt.Sprintf("v%s.%s.0", majorVersion, minorVersion),
Major: majorVersion,
Minor: minorVersion,
}

capabilities.APIVersions = v3util.VersionSet(t.Capabilities.APIVersions)
Expand Down
43 changes: 12 additions & 31 deletions pkg/unittest/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,12 @@ func (s *TestSuite) polishTestJobsPathInfo() {

// override release settings in testjobs when defined in testsuite
func (s *TestSuite) polishReleaseSettings(test *TestJob) {
if s.Release.Name != "" {
if test.Release.Name == "" {
test.Release.Name = s.Release.Name
}
}

if s.Release.Namespace != "" {
if test.Release.Namespace == "" {
test.Release.Namespace = s.Release.Namespace
}
}

if s.Release.Revision > 0 {
if test.Release.Revision == 0 {
test.Release.Revision = s.Release.Revision
}
}

if s.Release.IsUpgrade {
if !test.Release.IsUpgrade {
test.Release.IsUpgrade = s.Release.IsUpgrade
}
}
test.Release.Name = cmp.Or(test.Release.Name, s.Release.Name)
test.Release.Namespace = cmp.Or(test.Release.Namespace, s.Release.Namespace)
test.Release.Revision = cmp.Or(test.Release.Revision, s.Release.Revision)
test.Release.IsUpgrade = cmp.Or(test.Release.IsUpgrade, s.Release.IsUpgrade)
log.WithField(common.LOG_TEST_SUITE, "polish-release-settings").Debug("test.release '", test.Release)
}

// override capabilities settings in testjobs when defined in testsuite
Expand All @@ -310,9 +293,9 @@ func (s *TestSuite) polishCapabilitiesSettings(test *TestJob) {
}

func (s *TestSuite) polishKubernetesProviderSettings(test *TestJob) {
if len(s.KubernetesProvider.Objects) > 0 {
test.KubernetesProvider.Objects = append(test.KubernetesProvider.Objects, s.KubernetesProvider.Objects...)
}

test.KubernetesProvider.Objects = append(test.KubernetesProvider.Objects, s.KubernetesProvider.Objects...)

if len(s.KubernetesProvider.Scheme) > 0 {
if test.KubernetesProvider.Scheme == nil {
test.KubernetesProvider.Scheme = map[string]KubernetesFakeKindProps{}
Expand All @@ -325,12 +308,10 @@ func (s *TestSuite) polishKubernetesProviderSettings(test *TestJob) {

// override chart settings in testjobs when defined in testsuite
func (s *TestSuite) polishChartSettings(test *TestJob) {
if test.Chart.Version == "" && s.Chart.Version != "" {
test.Chart.Version = s.Chart.Version
}
if test.Chart.AppVersion == "" && s.Chart.AppVersion != "" {
test.Chart.AppVersion = s.Chart.AppVersion
}

test.Chart.Version = cmp.Or(test.Chart.Version, s.Chart.Version)
test.Chart.AppVersion = cmp.Or(test.Chart.AppVersion, s.Chart.AppVersion)
log.WithField(common.LOG_TEST_SUITE, "polish-chart-settings").Debug("test.chart '", test.Chart)
}

func (s *TestSuite) runV3TestJobs(
Expand Down
2 changes: 1 addition & 1 deletion plugin-dbg.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "unittest"
version: "0.6.1"
version: "0.6.2"
usage: "unittest for helm charts"
description: "Unit test for helm chart in YAML with ease to keep your chart functional and robust."
ignoreFlags: false
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "unittest"
version: "0.6.1"
version: "0.6.2"
usage: "unittest for helm charts"
description: "Unit test for helm chart in YAML with ease to keep your chart functional and robust."
ignoreFlags: false
Expand Down
11 changes: 8 additions & 3 deletions schema/helm-testsuite.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
"chart": {
"$ref": "#/definitions/chart"
},
"kubernetesProvider": {
"$ref": "#/definitions/kubernetesProvider"
},
"asserts": {
"type": "array",
"description": "The assertions to validate the rendered chart.",
Expand Down Expand Up @@ -1021,7 +1024,10 @@
"markdownDescription": "**minorVersion** (integer) _optional_\n\nThe kubernetes minor version, default to the minor version which is set by helm."
},
"apiVersions": {
"type": ["array", "null"],
"type": [
"array",
"null"
],
"description": "A set of versions, default to the versionset used by the defined kubernetes version.",
"markdownDescription": "**apiVersions** (array<string>) _optional_\n\nA set of versions, default to the versionset used by the defined kubernetes version.",
"items": {
Expand Down Expand Up @@ -1113,7 +1119,6 @@
}
},
"required": [
"scheme",
"objects"
],
"additionalProperties": false
Expand Down Expand Up @@ -1169,4 +1174,4 @@
}
}
}
}
}
10 changes: 10 additions & 0 deletions test/data/v3/basic/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ tests:
limits:
memory: "128Mi"
cpu: "1500m"
chart:
appVersion: 0.1.0
template: templates/deployment.yaml
documentIndex: 0
asserts:
- greaterOrEqual:
path: metadata.labels.appVersion
value: 2.0.0
- greaterOrEqual:
path: spec.template.spec.securityContext.runAsUser
value: 1286
Expand All @@ -107,9 +112,14 @@ tests:
limits:
memory: "128Mi"
cpu: "1500m"
chart:
appVersion: 10.0.0
template: templates/deployment.yaml
documentIndex: 0
asserts:
- lessOrEqual:
path: metadata.labels.appVersion
value: 10.0.0
- lessOrEqual:
path: spec.template.spec.securityContext.runAsUser
value: 1281
Expand Down
3 changes: 1 addition & 2 deletions test/data/v3/basic/tests/servicemonitor_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ capabilities:
apiVersions:
- monitoring.coreos.com/v1
tests:

- it: with monitoring disabled, ServiceMonitor not created
capabilities:
apiVersions:
Expand All @@ -15,6 +14,7 @@ tests:

- it: with monitoring, ServiceMonitor is created
capabilities:
apiVersions: []
minorVersion: 20
asserts:
- hasDocuments:
Expand All @@ -24,4 +24,3 @@ tests:
- equal:
path: metadata.labels["kubeVersion.minor"]
value: "20"

23 changes: 23 additions & 0 deletions test/data/v3/with-k8s-fake-client/tests/lookup_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,26 @@ tests:
- lengthEqual:
path: namespaces.items
count: 2
- it: manifest should validate pod_not_exists due to additional object add in testjob
kubernetesProvider:
objects:
- kind: Pod
apiVersion: v1
metadata:
name: not-exists
namespace: default
asserts:
- isNotNullOrEmpty:
path: pod_not_exists
- equal:
path: pod_not_exists.kind
value: Pod
- equal:
path: pod_not_exists.apiVersion
value: v1
- equal:
path: pod_not_exists.metadata.name
value: not-exists
- equal:
path: pod_not_exists.metadata.namespace
value: default

0 comments on commit 71a84e1

Please sign in to comment.