Skip to content

Commit e5060da

Browse files
committed
UPSTREAM: <carry>: Initialize testContext before generating ginkgo tests
SELinux tests require testContext to be initialized before ginkgo discovers their closures (i.e. before kubeTestsExtension.AddSuite() is called). Split the testContext initialization in two parts: initializeCommonTestFramework() files the fields that are either static or can be discovered easily and are needed to generate the tests by ginkgo. updateTestFrameworkForTests() files the rest, mostly from the cloud provider env. var. and adds fields necessary to actually run the tests.
1 parent 10466b2 commit e5060da

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

openshift-hack/cmd/k8s-tests-ext/k8s-tests.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func main() {
3939
framework.RegisterCommonFlags(flag.CommandLine)
4040
framework.RegisterClusterFlags(flag.CommandLine)
4141

42+
if err := initializeCommonTestFramework(); err != nil {
43+
panic(err)
44+
}
45+
4246
// Get version info from kube
4347
kubeVersion := version.Get()
4448
v.GitTreeState = kubeVersion.GitTreeState
@@ -85,7 +89,7 @@ func main() {
8589

8690
// Initialization for kube ginkgo test framework needs to run before all tests execute
8791
specs.AddBeforeAll(func() {
88-
if err := initializeTestFramework(os.Getenv("TEST_PROVIDER")); err != nil {
92+
if err := updateTestFrameworkForTests(os.Getenv("TEST_PROVIDER")); err != nil {
8993
panic(err)
9094
}
9195
})

openshift-hack/cmd/k8s-tests-ext/provider.go

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,11 @@ import (
3030
_ "k8s.io/kubernetes/test/e2e/lifecycle"
3131
)
3232

33-
// copied directly from github.com/openshift/origin/cmd/openshift-tests/provider.go
34-
// and github.com/openshift/origin/test/extended/util/test.go
35-
func initializeTestFramework(provider string) error {
36-
providerInfo := &ClusterConfiguration{}
37-
if err := json.Unmarshal([]byte(provider), &providerInfo); err != nil {
38-
return fmt.Errorf("provider must be a JSON object with the 'type' key at a minimum: %v", err)
39-
}
40-
if len(providerInfo.ProviderName) == 0 {
41-
return fmt.Errorf("provider must be a JSON object with the 'type' key")
42-
}
43-
config := &ClusterConfiguration{}
44-
if err := json.Unmarshal([]byte(provider), config); err != nil {
45-
return fmt.Errorf("provider must decode into the ClusterConfig object: %v", err)
46-
}
47-
33+
// Initialize a good enough test context for generating e2e tests,
34+
// so they can be listed and filtered.
35+
func initializeCommonTestFramework() error {
4836
// update testContext with loaded config
4937
testContext := &framework.TestContext
50-
testContext.Provider = config.ProviderName
51-
testContext.CloudConfig = framework.CloudConfig{
52-
ProjectID: config.ProjectID,
53-
Region: config.Region,
54-
Zone: config.Zone,
55-
Zones: config.Zones,
56-
NumNodes: config.NumNodes,
57-
MultiMaster: config.MultiMaster,
58-
MultiZone: config.MultiZone,
59-
ConfigFile: config.ConfigFile,
60-
}
6138
testContext.AllowedNotReadyNodes = -1
6239
testContext.MinStartupPods = -1
6340
testContext.MaxNodesToGather = 0
@@ -91,6 +68,45 @@ func initializeTestFramework(provider string) error {
9168
testContext.NodeOSDistro = "custom"
9269
testContext.MasterOSDistro = "custom"
9370

71+
return nil
72+
}
73+
74+
// Finish test context initialization. This is called before a real test is going to run.
75+
// It parses the cloud provider and file other parameters that are needed for running
76+
// already generated tests.
77+
func updateTestFrameworkForTests(provider string) error {
78+
providerInfo := &ClusterConfiguration{}
79+
if err := json.Unmarshal([]byte(provider), &providerInfo); err != nil {
80+
return fmt.Errorf("provider must be a JSON object with the 'type' key at a minimum: %v", err)
81+
}
82+
if len(providerInfo.ProviderName) == 0 {
83+
return fmt.Errorf("provider must be a JSON object with the 'type' key")
84+
}
85+
config := &ClusterConfiguration{}
86+
if err := json.Unmarshal([]byte(provider), config); err != nil {
87+
return fmt.Errorf("provider must decode into the ClusterConfig object: %v", err)
88+
}
89+
90+
// update testContext with loaded config
91+
testContext := &framework.TestContext
92+
testContext.Provider = config.ProviderName
93+
testContext.CloudConfig = framework.CloudConfig{
94+
ProjectID: config.ProjectID,
95+
Region: config.Region,
96+
Zone: config.Zone,
97+
Zones: config.Zones,
98+
NumNodes: config.NumNodes,
99+
MultiMaster: config.MultiMaster,
100+
MultiZone: config.MultiZone,
101+
ConfigFile: config.ConfigFile,
102+
}
103+
104+
// these constants are taken from kube e2e and used by tests
105+
testContext.IPFamily = "ipv4"
106+
if config.HasIPv6 && !config.HasIPv4 {
107+
testContext.IPFamily = "ipv6"
108+
}
109+
94110
// load and set the host variable for kubectl
95111
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{ExplicitPath: testContext.KubeConfig}, &clientcmd.ConfigOverrides{})
96112
cfg, err := clientConfig.ClientConfig()
@@ -108,13 +124,6 @@ func initializeTestFramework(provider string) error {
108124

109125
framework.AfterReadingAllFlags(testContext)
110126
testContext.DumpLogsOnFailure = true
111-
112-
// these constants are taken from kube e2e and used by tests
113-
testContext.IPFamily = "ipv4"
114-
if config.HasIPv6 && !config.HasIPv4 {
115-
testContext.IPFamily = "ipv6"
116-
}
117-
118127
testContext.ReportDir = os.Getenv("TEST_JUNIT_DIR")
119128

120129
return nil

0 commit comments

Comments
 (0)