-
Notifications
You must be signed in to change notification settings - Fork 28
/
framework.go
112 lines (92 loc) · 3.2 KB
/
framework.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package framework
import (
"context"
"flag"
"os"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/test-infra/pkg/apis/config"
"github.com/gardener/test-infra/pkg/testmachinery"
"github.com/gardener/test-infra/pkg/util"
kutil "github.com/gardener/test-infra/pkg/util/kubernetes"
)
// New creates a new test operation with a logger and a framework configuration.
func New(log logr.Logger, config *Config) (*Operation, error) {
flag.Parse()
ctx := context.Background()
defer ctx.Done()
if err := ValidateConfig(config); err != nil {
return nil, err
}
tmClient, err := kutil.NewClientFromFile(config.TmKubeconfigPath, client.Options{
Scheme: testmachinery.TestMachineryScheme,
})
if err != nil {
return nil, errors.Wrapf(err, "unable to create client from %s", config.TmKubeconfigPath)
}
operation := &Operation{
testConfig: config,
log: log,
tmClient: tmClient,
}
if err := operation.setTestMachineryConfig(ctx); err != nil {
return nil, err
}
if err := operation.EnsureTestNamespace(ctx); err != nil {
return nil, err
}
return operation, nil
}
func (o *Operation) setTestMachineryConfig(ctx context.Context) error {
var (
data []byte
err error
decoder = serializer.NewCodecFactory(testmachinery.ConfigScheme).UniversalDecoder()
)
if len(o.testConfig.TMConfigPath) != 0 {
data, err = os.ReadFile(o.testConfig.TMConfigPath)
if err != nil {
return err
}
} else {
secret := &corev1.Secret{}
if err := o.Client().Get(ctx, client.ObjectKey{Name: "tm-configuration", Namespace: o.TestMachineryNamespace()}, secret); err != nil {
return err
}
data = secret.Data["config.yaml"]
}
cfg := &config.Configuration{}
if _, _, err := decoder.Decode(data, nil, cfg); err != nil {
return err
}
o.tmConfig = cfg
return nil
}
// RegisterFlags adds all framework operation specific flags to the provided flagset.
func RegisterFlags(flagset *flag.FlagSet) *Config {
if flagset == nil {
flagset = flag.CommandLine
}
cfg := Config{}
flagset.StringVar(&cfg.CommitSha, "git-commit-sha", util.Getenv("GIT_COMMIT_SHA", "master"),
"Commit hash of the current test-infra git")
flagset.StringVar(&cfg.Namespace, "namespace", util.Getenv("NAMESPACE", ""),
"testing namespace")
flagset.StringVar(&cfg.TmKubeconfigPath, "kubecfg", util.Getenv("TM_KUBECONFIG_PATH", "default"),
"Kubeconfig path to the testmachinery")
flagset.StringVar(&cfg.TmNamespace, "tm-namespace", util.Getenv("TM_NAMESPACE", "default"),
"namespace where the testmachinery is running")
flagset.StringVar(&cfg.S3Endpoint, "s3-endpoint", os.Getenv("S3_ENDPOINT"),
"s3 endpoint of the s3 storage used by the workflows")
flagset.BoolVar(&cfg.Local, "local", false,
"test runs locally which means that prerequisites like readyness of controller and minio is not checked")
flagset.StringVar(&cfg.TMConfigPath, "tm-config", "",
"path to the testmachinery kubeconfig. It will be read from the cluster if not specified")
return &cfg
}