/
acceptance_test.go
149 lines (125 loc) · 4.59 KB
/
acceptance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright The Enterprise Contract Contributors
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package acceptance
import (
"context"
"flag"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/cucumber/godog"
"github.com/gkampitakis/go-snaps/snaps"
"github.com/enterprise-contract/ec-cli/acceptance/cli"
"github.com/enterprise-contract/ec-cli/acceptance/conftest"
"github.com/enterprise-contract/ec-cli/acceptance/crypto"
"github.com/enterprise-contract/ec-cli/acceptance/git"
"github.com/enterprise-contract/ec-cli/acceptance/image"
"github.com/enterprise-contract/ec-cli/acceptance/kubernetes"
"github.com/enterprise-contract/ec-cli/acceptance/log"
"github.com/enterprise-contract/ec-cli/acceptance/pipeline"
"github.com/enterprise-contract/ec-cli/acceptance/registry"
"github.com/enterprise-contract/ec-cli/acceptance/rekor"
"github.com/enterprise-contract/ec-cli/acceptance/tekton"
"github.com/enterprise-contract/ec-cli/acceptance/testenv"
"github.com/enterprise-contract/ec-cli/acceptance/tuf"
"github.com/enterprise-contract/ec-cli/acceptance/wiremock"
)
// NOTE: flags need to be initialized with the package in order to be recognized
// a flag that can be set by running the test with "-args -persist" command line options
var persist = flag.Bool("persist", false, "persist the stubbed environment to facilitate debugging")
// run acceptance tests with the persisted environment
var restore = flag.Bool("restore", false, "restore last persisted environment")
var noColors = flag.Bool("no-colors", false, "disable colored output")
// specify a subset of scenarios to run filtering by given tags
var tags = flag.String("tags", "", "select scenarios to run based on tags")
// initializeScenario adds all steps and registers all hooks to the
// provided godog.ScenarioContext
func initializeScenario(sc *godog.ScenarioContext) {
cli.AddStepsTo(sc)
crypto.AddStepsTo(sc)
git.AddStepsTo(sc)
image.AddStepsTo(sc)
kubernetes.AddStepsTo(sc)
registry.AddStepsTo(sc)
rekor.AddStepsTo(sc)
tekton.AddStepsTo(sc)
wiremock.AddStepsTo(sc)
pipeline.AddStepsTo(sc)
conftest.AddStepsTo(sc)
tuf.AddStepsTo(sc)
sc.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
logger, ctx := log.LoggerFor(ctx)
logger.Name(sc.Name)
return context.WithValue(ctx, testenv.Scenario, sc), nil
})
sc.After(func(ctx context.Context, scenario *godog.Scenario, scenarioErr error) (context.Context, error) {
_, err := testenv.Persist(ctx)
return ctx, err
})
}
func initializeSuite(ctx context.Context) func(*godog.TestSuiteContext) {
return func(tsc *godog.TestSuiteContext) {
kubernetes.InitializeSuite(ctx, tsc)
}
}
// setupContext creates a Context prepopulated with the *testing.T and *persist
// values
func setupContext(t *testing.T) context.Context {
ctx := context.WithValue(context.Background(), testenv.TestingT, t)
ctx = context.WithValue(ctx, testenv.PersistStubEnvironment, *persist)
ctx = context.WithValue(ctx, testenv.RestoreStubEnvironment, *restore)
ctx = context.WithValue(ctx, testenv.NoColors, *noColors)
return ctx
}
// TestFeatures launches all acceptance test scenarios running them
// in random order in parallel threads equal to the number of available
// cores
func TestFeatures(t *testing.T) {
// change the directory to repository root, makes for easier paths
if err := os.Chdir(".."); err != nil {
t.Error(err)
}
featuresDir, err := filepath.Abs("features")
if err != nil {
t.Error(err)
}
ctx := setupContext(t)
opts := godog.Options{
Format: "pretty",
Paths: []string{featuresDir},
Randomize: -1,
Concurrency: runtime.NumCPU(),
TestingT: t,
DefaultContext: ctx,
Tags: *tags,
NoColors: *noColors,
}
suite := godog.TestSuite{
ScenarioInitializer: initializeScenario,
TestSuiteInitializer: initializeSuite(ctx),
Options: &opts,
}
if suite.Run() != 0 {
t.Fatal("failure in acceptance tests")
}
}
func TestMain(t *testing.M) {
v := t.Run()
// After all tests have run `go-snaps` can check for not used snapshots
snaps.Clean(t)
os.Exit(v)
}