-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (88 loc) · 2.33 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
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"os"
"strings"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
messages "github.com/cucumber/messages-go/v16"
"github.com/matt-simons/gkube"
"github.com/matt-simons/kubedog/kubernetes"
"github.com/matt-simons/trackedclient"
"github.com/onsi/gomega"
"github.com/spf13/pflag"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
}
func main() {
godog.BindCommandLineFlags("", &opts)
name := pflag.String("name", "kubedog", "name")
pflag.Parse()
opts.Paths = pflag.Args()
testSuite := godog.TestSuite{
Name: *name,
Options: &opts,
}
testSuite.ScenarioInitializer = func(sc *godog.ScenarioContext) {
sc.BeforeScenario(func(s *godog.Scenario) {
if isKubernetesScenario(s.Tags) {
replaceVariables(s)
setupKubernetesScenarioRun(sc)
}
})
}
os.Exit(testSuite.Run())
}
func isKubernetesScenario(tags []*messages.PickleTag) bool {
for t := range tags {
if tags[t].Name == "@kubernetes" {
return true
}
}
return false
}
func replaceVariables(s *godog.Scenario) {
uniqueNamespace := "test-" + uuidOrDie()
for i := range s.Steps {
step := s.Steps[i]
step.Text = strings.ReplaceAll(step.Text, "$NAMESPACE", uniqueNamespace)
if step.Argument != nil {
step.Argument.DocString.Content = strings.ReplaceAll(step.Argument.DocString.Content, "$NAMESPACE", uniqueNamespace)
}
}
}
func setupKubernetesScenarioRun(sc *godog.ScenarioContext) {
gomega.RegisterFailHandler(func(message string, _ ...int) {
panic(message)
})
// Setup client
cfg := config.GetConfigOrDie()
opts := client.Options{
Scheme: clientgoscheme.Scheme,
}
objTrackingClient, err := trackedclient.New(cfg, opts)
if err != nil {
panic(err)
}
kubernetes.NewKubernetesScenario(sc, gkube.NewKubernetesHelper(gkube.WithClient(objTrackingClient)))
sc.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
objTrackingClient.DeleteAllTracked(ctx)
return ctx, err
})
}
func uuidOrDie() string {
u := make([]byte, 16)
_, err := rand.Read(u)
if err != nil {
panic(err)
}
u[8] = (u[8] | 0x80) & 0xBF
u[6] = (u[6] | 0x40) & 0x4F
return hex.EncodeToString(u)
}