forked from knative/eventing
/
e2e_flags.go
92 lines (73 loc) · 2.85 KB
/
e2e_flags.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
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This file contains logic to encapsulate flags which are needed to specify
// what cluster, etc. to use for e2e tests.
package test
import (
"flag"
"fmt"
"log"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/test/common"
pkgTest "knative.dev/pkg/test"
testLogging "knative.dev/pkg/test/logging"
)
// EventingFlags holds the command line flags specific to knative/eventing.
var EventingFlags *EventingEnvironmentFlags
// Channels holds the Channels we want to run test against.
type Channels []metav1.TypeMeta
func (channels *Channels) String() string {
return fmt.Sprint(*channels)
}
// Set converts the input string to Channels.
// The default Channel we will test against is InMemoryChannel.
func (channels *Channels) Set(value string) error {
for _, channel := range strings.Split(value, ",") {
channel := strings.TrimSpace(channel)
split := strings.Split(channel, ":")
if len(split) != 2 {
log.Fatalf("The given Channel name %q is invalid, it needs to be in the form \"apiVersion:Kind\".", channel)
}
tm := metav1.TypeMeta{
APIVersion: split[0],
Kind: split[1],
}
if !isValid(tm.Kind) {
log.Fatalf("The given channel name %q is invalid, tests cannot be run.\n", channel)
}
*channels = append(*channels, tm)
}
return nil
}
// Check if the channel name is valid.
func isValid(channel string) bool {
return strings.HasSuffix(channel, "Channel")
}
// EventingEnvironmentFlags holds the e2e flags needed only by the eventing repo.
type EventingEnvironmentFlags struct {
Channels
}
// InitializeEventingFlags registers flags used by e2e tests, calling flag.Parse() here would fail in
// go1.13+, see https://github.com/knative/test-infra/issues/1329 for details
func InitializeEventingFlags() {
f := EventingEnvironmentFlags{}
flag.Var(&f.Channels, "channels", "The names of the channel type metas, separated by comma. Example: \"messaging.knative.dev/v1alpha1:InMemoryChannel,messaging.cloud.google.com/v1alpha1:Channel,messaging.knative.dev/v1alpha1:KafkaChannel\".")
flag.Parse()
// If no channel is passed through the flag, initialize it as the DefaultChannel.
if f.Channels == nil || len(f.Channels) == 0 {
f.Channels = []metav1.TypeMeta{common.DefaultChannel}
}
testLogging.InitializeLogger(pkgTest.Flags.LogVerbose)
EventingFlags = &f
}