forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (129 loc) · 4.26 KB
/
main.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
package main
import (
"context"
"fmt"
"math/rand"
"os"
"os/user"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"github.com/kyma-project/kyma/tests/function-controller/pkg/step"
"github.com/kyma-project/kyma/tests/function-controller/testsuite"
"github.com/kyma-project/kyma/tests/function-controller/testsuite/scenarios"
)
func loadRestConfig(context string) (*rest.Config, error) {
// If the recommended kubeconfig env variable is not specified,
// try the in-cluster config.
kubeconfigPath := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(kubeconfigPath) == 0 {
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if _, ok := os.LookupEnv("HOME"); !ok {
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not get current user: %w", err)
}
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
}
return loadRestConfigWithContext("", loadingRules, context)
}
func loadRestConfigWithContext(apiServerURL string, loader clientcmd.ClientConfigLoader, context string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loader,
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: apiServerURL,
},
CurrentContext: context,
}).ClientConfig()
}
type scenario struct {
displayName string
scenario testScenario
}
var availableScenarios = map[string][]scenario{
"serverless-integration": {
{displayName: "simple", scenario: scenarios.SimpleFunctionTest},
{displayName: "gitops", scenario: scenarios.GitopsSteps},
},
"git-auth-integration": {{displayName: "gitauth", scenario: scenarios.GitAuthTestSteps}},
"simple-tracing": {{displayName: "tracing", scenario: scenarios.SimpleFunctionTracingTest}},
"simple-api-gateway": {{displayName: "api-gateway", scenario: scenarios.SimpleFunctionAPIGatewayTest}},
}
type config struct {
Test testsuite.Config
}
func main() {
logf := logrus.New()
logf.SetFormatter(&logrus.JSONFormatter{})
logf.SetReportCaller(false)
if len(os.Args) < 2 {
logf.Errorf("Scenario not specified. Specify it as the first argument")
os.Exit(2)
}
cfg, err := loadConfig("APP")
failOnError(err, logf)
logf.Printf("loaded config")
scenarioName := os.Args[1]
logf.Printf("Scenario: %s", scenarioName)
os.Args = os.Args[1:]
pickedScenarios, exists := availableScenarios[scenarioName]
if !exists {
logf.Errorf("Scenario %s not exist", scenarioName)
os.Exit(1)
}
restConfig, err := loadRestConfig("")
if err != nil {
logf.Errorf("Unable to get rest config: %s", err.Error())
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
g, _ := errgroup.WithContext(context.Background())
for _, scenario := range pickedScenarios {
// https://eli.thegreenplace.net/2019/go-internals-capturing-loop-variables-in-closures/
scenarioDisplayName := fmt.Sprintf("%s-%s", scenarioName, scenario.displayName)
func(scenario testScenario, name string) {
g.Go(func() error {
return runScenario(scenario, name, logf, cfg, restConfig)
})
}(scenario.scenario, scenarioDisplayName)
}
failOnError(g.Wait(), logf)
}
type testScenario func(*rest.Config, testsuite.Config, *logrus.Entry) (step.Step, error)
func runScenario(scenario testScenario, scenarioName string, logf *logrus.Logger, cfg config, restConfig *rest.Config) error {
scenarioLogger := logf.WithField("scenario", scenarioName)
steps, err := scenario(restConfig, cfg.Test, scenarioLogger)
if err != nil {
logf.Error(err)
return err
}
runner := step.NewRunner(step.WithCleanupDefault(cfg.Test.Cleanup), step.WithLogger(logf))
err = runner.Execute(steps)
if err != nil {
scenarioLogger.Error(err)
return err
}
scenarioLogger.Infof("Scenario succeeded: %s", scenarioName)
return nil
}
func loadConfig(prefix string) (config, error) {
cfg := config{}
err := envconfig.InitWithPrefix(&cfg, prefix)
return cfg, err
}
func failOnError(err error, logf *logrus.Logger) {
if err != nil {
logf.Error(err)
os.Exit(1)
}
}