@@ -14,6 +14,7 @@ import (
14
14
"time"
15
15
16
16
g "github.com/onsi/ginkgo"
17
+ o "github.com/onsi/gomega"
17
18
18
19
authorizationapiv1 "k8s.io/api/authorization/v1"
19
20
kapiv1 "k8s.io/api/core/v1"
@@ -44,33 +45,35 @@ import (
44
45
// CLI provides function to call the OpenShift CLI and Kubernetes and OpenShift
45
46
// clients.
46
47
type CLI struct {
47
- execPath string
48
- verb string
49
- configPath string
50
- adminConfigPath string
51
- username string
52
- outputDir string
53
- globalArgs []string
54
- commandArgs []string
55
- finalArgs []string
56
- stdin * bytes.Buffer
57
- stdout io.Writer
58
- stderr io.Writer
59
- verbose bool
60
- withoutNamespace bool
61
- kubeFramework * e2e.Framework
48
+ execPath string
49
+ verb string
50
+ configPath string
51
+ adminConfigPath string
52
+ username string
53
+ outputDir string
54
+ globalArgs []string
55
+ commandArgs []string
56
+ finalArgs []string
57
+ namespacesToDelete []string
58
+ stdin * bytes.Buffer
59
+ stdout io.Writer
60
+ stderr io.Writer
61
+ verbose bool
62
+ withoutNamespace bool
63
+ kubeFramework * e2e.Framework
62
64
}
63
65
64
66
// NewCLI initialize the upstream E2E framework and set the namespace to match
65
67
// with the project name. Note that this function does not initialize the project
66
68
// role bindings for the namespace.
67
69
func NewCLI (project , adminConfigPath string ) * CLI {
68
- // Avoid every caller needing to provide a unique project name
69
- // SetupProject already treats this as a baseName
70
- uniqueProject := names .SimpleNameGenerator .GenerateName (fmt .Sprintf ("%s-" , project ))
71
-
72
70
client := & CLI {}
73
- client .kubeFramework = e2e .NewDefaultFramework (uniqueProject )
71
+
72
+ // has to run before the default framework nukes the client
73
+ g .AfterEach (client .TeardownProject )
74
+
75
+ client .kubeFramework = e2e .NewDefaultFramework (project )
76
+ client .kubeFramework .SkipNamespaceCreation = true
74
77
client .outputDir = os .TempDir ()
75
78
client .username = "admin"
76
79
client .execPath = "oc"
@@ -79,8 +82,7 @@ func NewCLI(project, adminConfigPath string) *CLI {
79
82
}
80
83
client .adminConfigPath = adminConfigPath
81
84
82
- // Register custom ns setup func
83
- setCreateTestingNSFunc (uniqueProject , client .SetupProject )
85
+ g .BeforeEach (client .SetupProject )
84
86
85
87
return client
86
88
}
@@ -154,21 +156,20 @@ func (c *CLI) SetOutputDir(dir string) *CLI {
154
156
}
155
157
156
158
// SetupProject creates a new project and assign a random user to the project.
157
- // All resources will be then created within this project and Kubernetes E2E
158
- // suite will destroy the project after test case finish.
159
- func (c * CLI ) SetupProject (name string , kubeClient kclientset.Interface , _ map [string ]string ) (* kapiv1.Namespace , error ) {
160
- newNamespace := names .SimpleNameGenerator .GenerateName (fmt .Sprintf ("extended-test-%s-" , name ))
159
+ // All resources will be then created within this project.
160
+ func (c * CLI ) SetupProject () {
161
+ newNamespace := names .SimpleNameGenerator .GenerateName (fmt .Sprintf ("e2e-test-%s-" , c .kubeFramework .BaseName ))
161
162
c .SetNamespace (newNamespace ).ChangeUser (fmt .Sprintf ("%s-user" , c .Namespace ()))
162
163
e2e .Logf ("The user is now %q" , c .Username ())
163
164
164
165
e2e .Logf ("Creating project %q" , c .Namespace ())
165
166
_ , err := c .ProjectClient ().Project ().ProjectRequests ().Create (& projectapi.ProjectRequest {
166
167
ObjectMeta : metav1.ObjectMeta {Name : c .Namespace ()},
167
168
})
168
- if err != nil {
169
- e2e . Logf ( "Failed to create a project and namespace %q: %v" , c . Namespace (), err )
170
- return nil , err
171
- }
169
+ o . Expect ( err ). NotTo ( o . HaveOccurred ())
170
+
171
+ // TODO: remove when https://github.com/kubernetes/kubernetes/pull/62606 merges and is in origin
172
+ c . namespacesToDelete = append ( c . namespacesToDelete , c . Namespace ())
172
173
173
174
e2e .Logf ("Waiting on permissions in project %q ..." , c .Namespace ())
174
175
err = WaitForSelfSAR (1 * time .Second , 60 * time .Second , c .KubeClient (), authorizationapiv1.SelfSubjectAccessReviewSpec {
@@ -179,14 +180,14 @@ func (c *CLI) SetupProject(name string, kubeClient kclientset.Interface, _ map[s
179
180
Resource : "pods" ,
180
181
},
181
182
})
182
- if err != nil {
183
- return nil , err
184
- }
185
-
186
- // TODO: Possibly check other resources like Builds depending on a different service account.
187
- // (Builds now check for this in every test.)
183
+ o .Expect (err ).NotTo (o .HaveOccurred ())
184
+ }
188
185
189
- return & kapiv1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : c .Namespace ()}}, nil
186
+ // TeardownProject removes projects created by this test.
187
+ func (c * CLI ) TeardownProject () {
188
+ if len (c .namespacesToDelete ) > 0 {
189
+ e2e .DeleteNamespaces (c .kubeFramework .ClientSet , c .namespacesToDelete , nil )
190
+ }
190
191
}
191
192
192
193
// Verbose turns on printing verbose messages when executing OpenShift commands
0 commit comments