/
commonframework.go
150 lines (124 loc) · 4.64 KB
/
commonframework.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
150
// Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 framework
import (
"flag"
"os"
"path/filepath"
"github.com/go-logr/logr"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/gardener/gardener/pkg/logger"
)
var commonCfg *CommonConfig
// CommonConfig is the configuration for a common framework
type CommonConfig struct {
LogLevel string
DisableStateDump bool
ResourceDir string
ChartDir string
}
// CommonFramework represents the common gardener test framework that consolidates all
// shared features of the specific test frameworks (system, garderner, shoot)
type CommonFramework struct {
Config *CommonConfig
Logger logr.Logger
DisableStateDump bool
// ResourcesDir is the absolute path to the resources directory
ResourcesDir string
// TemplatesDir is the absolute path to the templates directory
TemplatesDir string
// Chart is the absolute path to the helm chart directory
ChartDir string
}
// NewCommonFramework creates a new common framework and registers its ginkgo BeforeEach setup
func NewCommonFramework(cfg *CommonConfig) *CommonFramework {
f := newCommonFrameworkFromConfig(cfg)
ginkgo.BeforeEach(f.BeforeEach)
return f
}
// newCommonFrameworkFromConfig creates a new common framework and without registering its ginkgo BeforeEach setup
func newCommonFrameworkFromConfig(cfg *CommonConfig) *CommonFramework {
f := &CommonFramework{
Config: cfg,
}
return f
}
// BeforeEach should be called in ginkgo's BeforeEach.
// It sets up the common framework.
func (f *CommonFramework) BeforeEach() {
f.Config = mergeCommonConfigs(f.Config, commonCfg)
f.DisableStateDump = f.Config.DisableStateDump
logf.SetLogger(logger.MustNewZapLogger(f.Config.LogLevel, logger.FormatJSON, zap.WriteTo(ginkgo.GinkgoWriter)))
f.Logger = logf.Log.WithName("test")
if f.ResourcesDir == "" {
var err error
if f.Config.ResourceDir != "" {
f.ResourcesDir, err = filepath.Abs(f.Config.ResourceDir)
} else {
// This is the default location if the framework is running in one of the gardener/shoot suites.
// Otherwise the resource dir has to be adjusted
f.ResourcesDir, err = filepath.Abs(filepath.Join("..", "..", "..", "framework", "resources"))
}
ExpectNoError(err)
}
FileExists(f.ResourcesDir)
f.TemplatesDir = filepath.Join(f.ResourcesDir, "templates")
f.ChartDir = filepath.Join(f.ResourcesDir, "charts")
if f.Config.ChartDir != "" {
f.ChartDir = f.Config.ChartDir
}
}
// CommonAfterSuite performs necessary common steps after all tests of a suite a run
func CommonAfterSuite() {
// run all registered cleanup functions
RunCleanupActions()
resourcesDir, err := filepath.Abs(filepath.Join("..", "..", "..", "framework", "resources"))
ExpectNoError(err)
err = os.RemoveAll(filepath.Join(resourcesDir, "charts"))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = os.RemoveAll(filepath.Join(resourcesDir, "repository", "cache"))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
func mergeCommonConfigs(base, overwrite *CommonConfig) *CommonConfig {
if base == nil {
return overwrite
}
if overwrite == nil {
return base
}
if StringSet(overwrite.LogLevel) {
base.LogLevel = overwrite.LogLevel
}
if StringSet(overwrite.ResourceDir) {
base.ResourceDir = overwrite.ResourceDir
}
if StringSet(overwrite.ChartDir) {
base.ChartDir = overwrite.ChartDir
}
if overwrite.DisableStateDump {
base.DisableStateDump = overwrite.DisableStateDump
}
return base
}
// RegisterCommonFrameworkFlags adds all flags that are needed to configure a common framework to the provided flagset.
func RegisterCommonFrameworkFlags() *CommonConfig {
newCfg := &CommonConfig{}
flag.StringVar(&newCfg.LogLevel, "verbose", logger.InfoLevel, "verbosity level (defaults to info)")
flag.BoolVar(&newCfg.DisableStateDump, "disable-dump", false, "Disable the state dump if a test fails")
commonCfg = newCfg
return commonCfg
}