Skip to content

Commit 19e9786

Browse files
OpenShift tests had doubled random suffixes
1 parent 47f318c commit 19e9786

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

test/extended/util/cli.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
g "github.com/onsi/ginkgo"
17+
o "github.com/onsi/gomega"
1718

1819
authorizationapiv1 "k8s.io/api/authorization/v1"
1920
kapiv1 "k8s.io/api/core/v1"
@@ -44,33 +45,35 @@ import (
4445
// CLI provides function to call the OpenShift CLI and Kubernetes and OpenShift
4546
// clients.
4647
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
6264
}
6365

6466
// NewCLI initialize the upstream E2E framework and set the namespace to match
6567
// with the project name. Note that this function does not initialize the project
6668
// role bindings for the namespace.
6769
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-
7270
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
7477
client.outputDir = os.TempDir()
7578
client.username = "admin"
7679
client.execPath = "oc"
@@ -79,8 +82,7 @@ func NewCLI(project, adminConfigPath string) *CLI {
7982
}
8083
client.adminConfigPath = adminConfigPath
8184

82-
// Register custom ns setup func
83-
setCreateTestingNSFunc(uniqueProject, client.SetupProject)
85+
g.BeforeEach(client.SetupProject)
8486

8587
return client
8688
}
@@ -154,21 +156,20 @@ func (c *CLI) SetOutputDir(dir string) *CLI {
154156
}
155157

156158
// 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))
161162
c.SetNamespace(newNamespace).ChangeUser(fmt.Sprintf("%s-user", c.Namespace()))
162163
e2e.Logf("The user is now %q", c.Username())
163164

164165
e2e.Logf("Creating project %q", c.Namespace())
165166
_, err := c.ProjectClient().Project().ProjectRequests().Create(&projectapi.ProjectRequest{
166167
ObjectMeta: metav1.ObjectMeta{Name: c.Namespace()},
167168
})
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())
172173

173174
e2e.Logf("Waiting on permissions in project %q ...", c.Namespace())
174175
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
179180
Resource: "pods",
180181
},
181182
})
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+
}
188185

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+
}
190191
}
191192

192193
// Verbose turns on printing verbose messages when executing OpenShift commands

0 commit comments

Comments
 (0)