-
Notifications
You must be signed in to change notification settings - Fork 28
/
testmachinery.go
160 lines (132 loc) · 4.32 KB
/
testmachinery.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package testmachinery
import (
"fmt"
"os"
"time"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"github.com/gardener/test-infra/pkg/apis/config"
tmv1beta1 "github.com/gardener/test-infra/pkg/apis/testmachinery/v1beta1"
)
var tmConfig = TmConfiguration{
Configuration: &config.Configuration{},
}
// Setup fetches all configuration values and creates the TmConfiguration.
func Setup(config *config.Configuration) error {
tmConfig = TmConfiguration{
Configuration: config,
}
var err error
tmConfig.GitHubSecrets, err = readSecretsFromFile(config.GitHub.SecretsPath)
if err != nil {
return err
}
// if no endpoint is defined we assume that no cleanup should happen
// this should only happen in local environments
//if tmConfig.TestMachinery.Local && config.S3.Server == nil {
// tmConfig.S3 = nil
//}
if config.ElasticSearch == nil || len(config.ElasticSearch.Endpoint) == 0 {
tmConfig.ElasticSearch = nil
}
if len(config.TestMachinery.RetryTimeout) != 0 {
d, err := time.ParseDuration(config.TestMachinery.RetryTimeout)
if err != nil {
return fmt.Errorf("unable to parse retry timeout: %w", err)
}
config.TestMachinery.RetryTimeoutDuration = &d
}
if config.TestMachinery.RetryTimeoutDuration == nil {
// default timeout location
d := 15 * time.Minute
config.TestMachinery.RetryTimeoutDuration = &d
}
return nil
}
// GetConfig returns the current testmachinery configuration.
func GetConfig() *TmConfiguration {
return &tmConfig
}
// GetNamespace returns the current testmachinery namespace.
func GetNamespace() string {
return tmConfig.TestMachinery.Namespace
}
// CleanWorkflowPods returns whether pod gc is enabled.
func CleanWorkflowPods() bool {
return tmConfig.TestMachinery.CleanWorkflowPods
}
// TestDefPath returns the path to TestDefinition inside repositories (scripts/integration-tests/argo/tm).
func TestDefPath() string {
return tmConfig.TestMachinery.TestDefPath
}
// Locations returns the locations configuration
func Locations() config.Locations {
return tmConfig.TestMachinery.Locations
}
// Prepare Image returns the image of the prepare step.
func PrepareImage() string {
return tmConfig.TestMachinery.PrepareImage
}
// BaseImage returns the default image that is used if no image is specified by a TestDefinition.
func BaseImage() string {
return tmConfig.TestMachinery.BaseImage
}
// GetGitHubSecrets returns all github secrets
func GetGitHubSecrets() []GitHubInstanceConfig {
return tmConfig.GitHubSecrets
}
// GetS3Configuration returns the current s3 configuration
func GetS3Configuration() *config.S3 {
return tmConfig.S3
}
// GetElasticsearchConfiguration returns the current elasticsearch configuration
func GetElasticsearchConfiguration() *config.ElasticSearch {
return tmConfig.ElasticSearch
}
// IsRunLocal returns if the testmachinery is currently running locally
func IsRunLocal() bool {
return tmConfig.TestMachinery.Local
}
// IsRunInsecure returns if the testmachinery is run locally
func IsRunInsecure() bool {
return tmConfig.TestMachinery.Insecure
}
func GetLandscapeMappings() []config.LandscapeMapping {
return tmConfig.TestMachinery.LandscapeMappings
}
// GetRetryTimeout returns the retry
func GetRetryTimeout() *time.Duration {
return tmConfig.TestMachinery.RetryTimeoutDuration
}
// GetWorkflowName returns the workflow name of a testruns
func GetWorkflowName(tr *tmv1beta1.Testrun) string {
return fmt.Sprintf("%s-wf", tr.Name)
}
// GetPauseTaskName returns the name of the pause step to a corresponding step.
func GetPauseTaskName(name string) string {
return fmt.Sprintf("%s-pause", name)
}
func readSecretsFromFile(path string) ([]GitHubInstanceConfig, error) {
if len(path) == 0 {
return make([]GitHubInstanceConfig, 0), nil
}
if _, err := os.Stat(path); err != nil {
return nil, errors.Wrapf(err, "file %s does not exist", path)
}
rawSecrets, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "unable to read file from %s", path)
}
gitSecrets := GitHubSecrets{}
err = yaml.Unmarshal(rawSecrets, &gitSecrets)
if err != nil {
return nil, errors.Wrap(err, "unable to parse git secrets")
}
if len(gitSecrets.Secrets) == 0 {
return nil, errors.New("git secrets are emtpy")
}
return gitSecrets.Secrets, nil
}