/
testsuite.go
188 lines (161 loc) · 5.49 KB
/
testsuite.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package upgrade
import (
"context"
_ "embed"
"encoding/base64"
"fmt"
"github.com/kyma-project/api-gateway/tests/integration/pkg/auth"
"log"
"os"
"path"
"time"
"github.com/kyma-project/api-gateway/tests/integration/pkg/hooks"
"github.com/cucumber/godog"
"github.com/kyma-project/api-gateway/tests/integration/pkg/helpers"
"github.com/kyma-project/api-gateway/tests/integration/pkg/manifestprocessor"
"github.com/kyma-project/api-gateway/tests/integration/pkg/resource"
"github.com/kyma-project/api-gateway/tests/integration/pkg/testcontext"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
const manifestsDirectory = "testsuites/upgrade/manifests/"
const upgradeImageEnv = "TEST_UPGRADE_IMG"
type tokenFrom struct {
From string
Prefix string
AsHeader bool
}
func (t *testsuite) createScenario(templateFileName string, scenarioName string) *scenario {
ns := t.namespace
testId := helpers.GenerateRandomTestId()
template := make(map[string]string)
template["Namespace"] = ns
template["NamePrefix"] = scenarioName
template["TestID"] = testId
template["Domain"] = t.config.Domain
template["GatewayName"] = t.config.GatewayName
template["GatewayNamespace"] = t.config.GatewayNamespace
template["IssuerUrl"] = t.config.IssuerUrl
template["EncodedCredentials"] = base64.RawStdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", t.config.ClientID, t.config.ClientSecret)))
return &scenario{
Namespace: ns,
TestID: testId,
Domain: t.config.Domain,
ManifestTemplate: template,
ApiResourceManifestPath: templateFileName,
ApiResourceDirectory: path.Dir(manifestsDirectory),
APIGatewayImageVersion: getUpgradeImageVersion(),
k8sClient: t.K8sClient(),
oauth2Cfg: t.oauth2Cfg,
httpClient: t.httpClient,
resourceManager: t.ResourceManager(),
config: t.config,
}
}
func getUpgradeImageVersion() string {
imageVersion, ok := os.LookupEnv(upgradeImageEnv)
if !ok {
panic(fmt.Sprintf("could not get %s", upgradeImageEnv))
}
return imageVersion
}
type testsuite struct {
name string
namespace string
httpClient *helpers.RetryableHttpClient
k8sClient dynamic.Interface
resourceManager *resource.Manager
config testcontext.Config
oauth2Cfg *clientcredentials.Config
}
func (t *testsuite) ResourceManager() *resource.Manager {
return t.resourceManager
}
func (t *testsuite) K8sClient() dynamic.Interface {
return t.k8sClient
}
func (t *testsuite) Setup() error {
namespace := fmt.Sprintf("%s-%s", t.name, helpers.GenerateRandomString(6))
log.Printf("Using namespace: %s\n", namespace)
// create common resources for all scenarios
globalCommonResources, err := manifestprocessor.ParseFromFileWithTemplate("global-commons.yaml", manifestsDirectory, struct {
Namespace string
}{
Namespace: namespace,
})
if err != nil {
return err
}
// delete test namespace if the previous test namespace persists
nsResourceSchema, ns, name := t.resourceManager.GetResourceSchemaAndNamespace(globalCommonResources[0])
log.Printf("Delete test namespace, if exists: %s\n", name)
err = t.resourceManager.DeleteResource(t.k8sClient, nsResourceSchema, ns, name)
if err != nil {
return err
}
time.Sleep(time.Duration(t.config.ReqDelay) * time.Second)
log.Printf("Creating common tests resources")
_, err = t.resourceManager.CreateResources(t.k8sClient, globalCommonResources...)
if err != nil {
return err
}
var tokenURL string
if t.config.OIDCConfigUrl == "empty" {
issuerUrl, err := auth.ApplyOAuth2MockServer(t.resourceManager, t.k8sClient, namespace, t.config.Domain)
if err != nil {
return err
}
t.config.IssuerUrl = fmt.Sprintf("http://mock-oauth2-server.%s.svc.cluster.local", namespace)
tokenURL = fmt.Sprintf("%s/oauth2/token", issuerUrl)
} else {
oidcConfiguration, err := helpers.GetOIDCConfiguration(t.config.OIDCConfigUrl)
if err != nil {
return err
}
t.config.IssuerUrl = oidcConfiguration.Issuer
tokenURL = oidcConfiguration.TokenEndpoint
}
oauth2Cfg := &clientcredentials.Config{
ClientID: t.config.ClientID,
ClientSecret: t.config.ClientSecret,
TokenURL: tokenURL,
AuthStyle: oauth2.AuthStyleInHeader,
}
t.oauth2Cfg = oauth2Cfg
t.namespace = namespace
return nil
}
func (t *testsuite) TearDown() {
res := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}
err := t.k8sClient.Resource(res).Delete(context.Background(), t.namespace, v1.DeleteOptions{})
if err != nil {
log.Print(err.Error())
}
}
func (t *testsuite) FeaturePath() []string {
return []string{"testsuites/upgrade/features/"}
}
func (t *testsuite) Name() string {
return t.name
}
func (t *testsuite) InitScenarios(ctx *godog.ScenarioContext) {
initCommon(ctx, t)
}
func (t *testsuite) BeforeSuiteHooks() []func() error {
return []func() error{hooks.ApplyAndVerifyApiGatewayCrSuiteHook}
}
func (t *testsuite) AfterSuiteHooks() []func() error {
return []func() error{hooks.DeleteBlockingResourcesSuiteHook, hooks.ApiGatewayCrTearDownSuiteHook}
}
func NewTestsuite(httpClient *helpers.RetryableHttpClient, k8sClient dynamic.Interface, rm *resource.Manager, config testcontext.Config) testcontext.Testsuite {
return &testsuite{
name: "upgrade",
httpClient: httpClient,
k8sClient: k8sClient,
resourceManager: rm,
config: config,
}
}