-
Notifications
You must be signed in to change notification settings - Fork 430
/
framework.go
188 lines (150 loc) · 4.58 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package framework
import (
"fmt"
"os"
"strings"
"time"
attachnetclientset "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/test/e2e/framework"
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
kubeovncs "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
)
const (
IPv4 = "ipv4"
IPv6 = "ipv6"
Dual = "dual"
)
const (
// poll is how often to Poll resources.
poll = 2 * time.Second
timeout = 2 * time.Minute
)
type Framework struct {
KubeContext string
*framework.Framework
KubeOVNClientSet kubeovncs.Interface
AttachNetClient attachnetclientset.Interface
// master/release-1.10/...
ClusterVersion string
// 999.999 for master
ClusterVersionMajor uint
ClusterVersionMinor uint
// ipv4/ipv6/dual
ClusterIpFamily string
// overlay/underlay/underlay-hairpin
ClusterNetworkMode string
}
func NewDefaultFramework(baseName string) *Framework {
f := &Framework{
Framework: framework.NewDefaultFramework(baseName),
}
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
f.ClusterIpFamily = os.Getenv("E2E_IP_FAMILY")
f.ClusterVersion = os.Getenv("E2E_BRANCH")
f.ClusterNetworkMode = os.Getenv("E2E_NETWORK_MODE")
if strings.HasPrefix(f.ClusterVersion, "release-") {
n, err := fmt.Sscanf(f.ClusterVersion, "release-%d.%d", &f.ClusterVersionMajor, &f.ClusterVersionMinor)
ExpectNoError(err)
ExpectEqual(n, 2)
} else {
f.ClusterVersionMajor, f.ClusterVersionMinor = 999, 999
}
ginkgo.BeforeEach(f.BeforeEach)
return f
}
func (f *Framework) useContext() error {
if f.KubeContext == "" {
return nil
}
pathOptions := clientcmd.NewDefaultPathOptions()
pathOptions.GlobalFile = framework.TestContext.KubeConfig
pathOptions.EnvVar = ""
config, err := pathOptions.GetStartingConfig()
if err != nil {
return err
}
if config.CurrentContext != f.KubeContext {
Logf("Switching context to " + f.KubeContext)
config.CurrentContext = f.KubeContext
if err = clientcmd.ModifyConfig(pathOptions, *config, true); err != nil {
return err
}
}
return nil
}
func NewFrameworkWithContext(baseName, kubeContext string) *Framework {
f := &Framework{KubeContext: kubeContext}
ginkgo.BeforeEach(f.BeforeEach)
f.Framework = framework.NewDefaultFramework(baseName)
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
f.ClusterIpFamily = os.Getenv("E2E_IP_FAMILY")
f.ClusterVersion = os.Getenv("E2E_BRANCH")
f.ClusterNetworkMode = os.Getenv("E2E_NETWORK_MODE")
ginkgo.BeforeEach(func() {
framework.TestContext.Host = ""
})
return f
}
func (f *Framework) IsIPv4() bool {
return f.ClusterIpFamily == IPv4
}
func (f *Framework) IsIPv6() bool {
return f.ClusterIpFamily == IPv6
}
func (f *Framework) IsDual() bool {
return f.ClusterIpFamily == Dual
}
func (f *Framework) HasIPv4() bool {
return !f.IsIPv6()
}
func (f *Framework) HasIPv6() bool {
return !f.IsIPv4()
}
// BeforeEach gets a kube-ovn client
func (f *Framework) BeforeEach() {
ginkgo.By("Setting kubernetes context")
ExpectNoError(f.useContext())
if f.KubeOVNClientSet == nil {
ginkgo.By("Creating a Kube-OVN client")
config, err := framework.LoadConfig()
ExpectNoError(err)
config.QPS = f.Options.ClientQPS
config.Burst = f.Options.ClientBurst
f.KubeOVNClientSet, err = kubeovncs.NewForConfig(config)
ExpectNoError(err)
}
if f.AttachNetClient == nil {
ginkgo.By("Creating a nad client")
config, err := framework.LoadConfig()
ExpectNoError(err)
config.QPS = f.Options.ClientQPS
config.Burst = f.Options.ClientBurst
f.AttachNetClient, err = attachnetclientset.NewForConfig(config)
ExpectNoError(err)
}
framework.TestContext.Host = ""
}
func (f *Framework) VersionPriorTo(major, minor uint) bool {
return f.ClusterVersionMajor < major || (f.ClusterVersionMajor == major && f.ClusterVersionMinor < minor)
}
func (f *Framework) SkipVersionPriorTo(major, minor uint, reason string) {
if f.VersionPriorTo(major, minor) {
ginkgo.Skip(reason)
}
}
func Describe(text string, body func()) bool {
return ginkgo.Describe("[CNI:Kube-OVN] "+text, body)
}
func SerialDescribe(text string, body func()) bool {
return ginkgo.Describe("[CNI:Kube-OVN] "+text, ginkgo.Serial, body)
}
func OrderedDescribe(text string, body func()) bool {
return ginkgo.Describe("[CNI:Kube-OVN] "+text, ginkgo.Ordered, body)
}
// ConformanceIt is wrapper function for ginkgo It.
// Adds "[Conformance]" tag and makes static analysis easier.
func ConformanceIt(text string, body interface{}) bool {
return ginkgo.It(text+" [Conformance]", ginkgo.Offset(1), body)
}