From f8ed9178309a2a038a0a0d0b1bb19471c076668c Mon Sep 17 00:00:00 2001 From: Matthew Fisher Date: Wed, 13 Mar 2019 10:16:07 -0700 Subject: [PATCH] test(cmd): refactor release_testing_test.go Signed-off-by: Matthew Fisher --- cmd/helm/release_testing_test.go | 103 +++++++++++++++++++++ cmd/helm/testdata/output/test-failure.txt | 11 +++ cmd/helm/testdata/output/test-no-tests.txt | 1 + cmd/helm/testdata/output/test-running.txt | 11 +++ cmd/helm/testdata/output/test-success.txt | 11 +++ cmd/helm/testdata/output/test-unknown.txt | 11 +++ pkg/release/mock.go | 35 ++++--- 7 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 cmd/helm/release_testing_test.go create mode 100644 cmd/helm/testdata/output/test-failure.txt create mode 100644 cmd/helm/testdata/output/test-no-tests.txt create mode 100644 cmd/helm/testdata/output/test-running.txt create mode 100644 cmd/helm/testdata/output/test-success.txt create mode 100644 cmd/helm/testdata/output/test-unknown.txt diff --git a/cmd/helm/release_testing_test.go b/cmd/helm/release_testing_test.go new file mode 100644 index 000000000..b693dd619 --- /dev/null +++ b/cmd/helm/release_testing_test.go @@ -0,0 +1,103 @@ +/* +Copyright The Helm Authors. + +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 main + +import ( + "testing" + "time" + + "k8s.io/helm/pkg/release" +) + +const mockTestSuccessTemplate = `apiVersion: v1 +kind: Pod +metadata: + annotations: + "helm.sh/hook": test-success +` + +func TestReleaseTesting(t *testing.T) { + timestamp := time.Unix(1452902400, 0).UTC() + + tests := []cmdTestCase{{ + name: "successful test", + cmd: "status test-success", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{ + Name: "test-success", + TestSuiteResults: []*release.TestRun{ + { + Name: "test-success", + Status: release.TestRunSuccess, + StartedAt: timestamp, + CompletedAt: timestamp, + }, + }, + })}, + golden: "output/test-success.txt", + }, { + name: "test failure", + cmd: "status test-failure", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{ + Name: "test-failure", + TestSuiteResults: []*release.TestRun{ + { + Name: "test-failure", + Status: release.TestRunFailure, + StartedAt: timestamp, + CompletedAt: timestamp, + }, + }, + })}, + golden: "output/test-failure.txt", + }, { + name: "test unknown", + cmd: "status test-unknown", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{ + Name: "test-unknown", + TestSuiteResults: []*release.TestRun{ + { + Name: "test-unknown", + Status: release.TestRunUnknown, + StartedAt: timestamp, + CompletedAt: timestamp, + }, + }, + })}, + golden: "output/test-unknown.txt", + }, { + name: "test running", + cmd: "status test-running", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{ + Name: "test-running", + TestSuiteResults: []*release.TestRun{ + { + Name: "test-running", + Status: release.TestRunRunning, + StartedAt: timestamp, + CompletedAt: timestamp, + }, + }, + })}, + golden: "output/test-running.txt", + }, { + name: "test with no tests", + cmd: "test no-tests", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "no-tests"})}, + golden: "output/test-no-tests.txt", + }} + runTestCmd(t, tests) +} diff --git a/cmd/helm/testdata/output/test-failure.txt b/cmd/helm/testdata/output/test-failure.txt new file mode 100644 index 000000000..93ae567be --- /dev/null +++ b/cmd/helm/testdata/output/test-failure.txt @@ -0,0 +1,11 @@ +NAME: test-failure +LAST DEPLOYED: 1977-09-02 22:04:05 +0000 UTC +NAMESPACE: default +STATUS: deployed + +TEST SUITE: +Last Started: 1977-09-02 22:04:05 +0000 UTC +Last Completed: 1977-09-02 22:04:05 +0000 UTC + +TEST STATUS INFO STARTED COMPLETED +test-failure failure 2016-01-16 00:00:00 +0000 UTC 2016-01-16 00:00:00 +0000 UTC diff --git a/cmd/helm/testdata/output/test-no-tests.txt b/cmd/helm/testdata/output/test-no-tests.txt new file mode 100644 index 000000000..fe5e07c3c --- /dev/null +++ b/cmd/helm/testdata/output/test-no-tests.txt @@ -0,0 +1 @@ +No Tests Found diff --git a/cmd/helm/testdata/output/test-running.txt b/cmd/helm/testdata/output/test-running.txt new file mode 100644 index 000000000..0061033df --- /dev/null +++ b/cmd/helm/testdata/output/test-running.txt @@ -0,0 +1,11 @@ +NAME: test-running +LAST DEPLOYED: 1977-09-02 22:04:05 +0000 UTC +NAMESPACE: default +STATUS: deployed + +TEST SUITE: +Last Started: 1977-09-02 22:04:05 +0000 UTC +Last Completed: 1977-09-02 22:04:05 +0000 UTC + +TEST STATUS INFO STARTED COMPLETED +test-running running 2016-01-16 00:00:00 +0000 UTC 2016-01-16 00:00:00 +0000 UTC diff --git a/cmd/helm/testdata/output/test-success.txt b/cmd/helm/testdata/output/test-success.txt new file mode 100644 index 000000000..99b6afdc5 --- /dev/null +++ b/cmd/helm/testdata/output/test-success.txt @@ -0,0 +1,11 @@ +NAME: test-success +LAST DEPLOYED: 1977-09-02 22:04:05 +0000 UTC +NAMESPACE: default +STATUS: deployed + +TEST SUITE: +Last Started: 1977-09-02 22:04:05 +0000 UTC +Last Completed: 1977-09-02 22:04:05 +0000 UTC + +TEST STATUS INFO STARTED COMPLETED +test-success success 2016-01-16 00:00:00 +0000 UTC 2016-01-16 00:00:00 +0000 UTC diff --git a/cmd/helm/testdata/output/test-unknown.txt b/cmd/helm/testdata/output/test-unknown.txt new file mode 100644 index 000000000..4b0cde7eb --- /dev/null +++ b/cmd/helm/testdata/output/test-unknown.txt @@ -0,0 +1,11 @@ +NAME: test-unknown +LAST DEPLOYED: 1977-09-02 22:04:05 +0000 UTC +NAMESPACE: default +STATUS: deployed + +TEST SUITE: +Last Started: 1977-09-02 22:04:05 +0000 UTC +Last Completed: 1977-09-02 22:04:05 +0000 UTC + +TEST STATUS INFO STARTED COMPLETED +test-unknown unknown 2016-01-16 00:00:00 +0000 UTC 2016-01-16 00:00:00 +0000 UTC diff --git a/pkg/release/mock.go b/pkg/release/mock.go index ee227c04b..0b5e8df35 100644 --- a/pkg/release/mock.go +++ b/pkg/release/mock.go @@ -40,11 +40,12 @@ metadata: // MockReleaseOptions allows for user-configurable options on mock release objects. type MockReleaseOptions struct { - Name string - Version int - Chart *chart.Chart - Status Status - Namespace string + Name string + Version int + Chart *chart.Chart + Status Status + Namespace string + TestSuiteResults []*TestRun } // Mock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing. @@ -84,14 +85,24 @@ func Mock(opts *MockReleaseOptions) *Release { scode = opts.Status } + info := &Info{ + FirstDeployed: date, + LastDeployed: date, + Status: scode, + Description: "Release mock", + } + + if len(opts.TestSuiteResults) > 0 { + info.LastTestSuiteRun = &TestSuite{ + StartedAt: date, + CompletedAt: date, + Results: opts.TestSuiteResults, + } + } + return &Release{ - Name: name, - Info: &Info{ - FirstDeployed: date, - LastDeployed: date, - Status: scode, - Description: "Release mock", - }, + Name: name, + Info: info, Chart: ch, Config: map[string]interface{}{"name": "value"}, Version: version,