Skip to content

Commit 94a8031

Browse files
authored
Merge pull request kubernetes-sigs#1697 from camilamacedo86/e2e-test-nit-cleanup
🌱 centralize helper utils for e2e in utils
2 parents 3274dcb + cdd1a81 commit 94a8031

File tree

3 files changed

+63
-120
lines changed

3 files changed

+63
-120
lines changed

test/e2e/utils/util.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package utils
1919
import (
2020
"bytes"
2121
"crypto/rand"
22+
"fmt"
2223
"io/ioutil"
2324
"math/big"
2425
"strings"
@@ -107,3 +108,63 @@ func UncommentCode(filename, target, prefix string) error {
107108
// nolint:gosec
108109
return ioutil.WriteFile(filename, out.Bytes(), 0644)
109110
}
111+
112+
// ImplementWebhooks will mock an webhook data
113+
func ImplementWebhooks(filename string) error {
114+
bs, err := ioutil.ReadFile(filename)
115+
if err != nil {
116+
return err
117+
}
118+
str := string(bs)
119+
120+
str, err = EnsureExistAndReplace(
121+
str,
122+
"import (",
123+
`import (
124+
"errors"`)
125+
if err != nil {
126+
return err
127+
}
128+
129+
// implement defaulting webhook logic
130+
str, err = EnsureExistAndReplace(
131+
str,
132+
"// TODO(user): fill in your defaulting logic.",
133+
`if r.Spec.Count == 0 {
134+
r.Spec.Count = 5
135+
}`)
136+
if err != nil {
137+
return err
138+
}
139+
140+
// implement validation webhook logic
141+
str, err = EnsureExistAndReplace(
142+
str,
143+
"// TODO(user): fill in your validation logic upon object creation.",
144+
`if r.Spec.Count < 0 {
145+
return errors.New(".spec.count must >= 0")
146+
}`)
147+
if err != nil {
148+
return err
149+
}
150+
str, err = EnsureExistAndReplace(
151+
str,
152+
"// TODO(user): fill in your validation logic upon object update.",
153+
`if r.Spec.Count < 0 {
154+
return errors.New(".spec.count must >= 0")
155+
}`)
156+
if err != nil {
157+
return err
158+
}
159+
// false positive
160+
// nolint:gosec
161+
return ioutil.WriteFile(filename, []byte(str), 0644)
162+
}
163+
164+
// EnsureExistAndReplace check if the content exists and then do the replace
165+
func EnsureExistAndReplace(input, match, replace string) (string, error) {
166+
if !strings.Contains(input, match) {
167+
return "", fmt.Errorf("can't find %q", match)
168+
}
169+
return strings.Replace(input, match, replace, -1), nil
170+
}

test/e2e/v2/e2e_suite.go

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package v2
1919
import (
2020
"encoding/base64"
2121
"fmt"
22-
"io/ioutil"
2322
"path/filepath"
2423
"strconv"
2524
"strings"
@@ -100,7 +99,7 @@ var _ = Describe("kubebuilder", func() {
10099
Expect(err).Should(Succeed())
101100

102101
By("implementing the mutating and validating webhooks")
103-
err = implementWebhooks(filepath.Join(
102+
err = utils.ImplementWebhooks(filepath.Join(
104103
kbc.Dir, "api", kbc.Version,
105104
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
106105
Expect(err).Should(Succeed())
@@ -330,61 +329,3 @@ var _ = Describe("kubebuilder", func() {
330329
})
331330
})
332331
})
333-
334-
func implementWebhooks(filename string) error {
335-
bs, err := ioutil.ReadFile(filename)
336-
if err != nil {
337-
return err
338-
}
339-
str := string(bs)
340-
341-
str, err = ensureExistAndReplace(
342-
str,
343-
"import (",
344-
`import (
345-
"errors"`)
346-
if err != nil {
347-
return err
348-
}
349-
350-
// implement defaulting webhook logic
351-
str, err = ensureExistAndReplace(
352-
str,
353-
"// TODO(user): fill in your defaulting logic.",
354-
`if r.Spec.Count == 0 {
355-
r.Spec.Count = 5
356-
}`)
357-
if err != nil {
358-
return err
359-
}
360-
361-
// implement validation webhook logic
362-
str, err = ensureExistAndReplace(
363-
str,
364-
"// TODO(user): fill in your validation logic upon object creation.",
365-
`if r.Spec.Count < 0 {
366-
return errors.New(".spec.count must >= 0")
367-
}`)
368-
if err != nil {
369-
return err
370-
}
371-
str, err = ensureExistAndReplace(
372-
str,
373-
"// TODO(user): fill in your validation logic upon object update.",
374-
`if r.Spec.Count < 0 {
375-
return errors.New(".spec.count must >= 0")
376-
}`)
377-
if err != nil {
378-
return err
379-
}
380-
// false positive
381-
// nolint:gosec
382-
return ioutil.WriteFile(filename, []byte(str), 0644)
383-
}
384-
385-
func ensureExistAndReplace(input, match, replace string) (string, error) {
386-
if !strings.Contains(input, match) {
387-
return "", fmt.Errorf("can't find %q", match)
388-
}
389-
return strings.Replace(input, match, replace, -1), nil
390-
}

test/e2e/v3/e2e_suite.go

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package v3
1919
import (
2020
"encoding/base64"
2121
"fmt"
22-
"io/ioutil"
2322
"path/filepath"
2423
"strconv"
2524
"strings"
@@ -100,7 +99,7 @@ var _ = Describe("kubebuilder", func() {
10099
Expect(err).Should(Succeed())
101100

102101
By("implementing the mutating and validating webhooks")
103-
err = implementWebhooks(filepath.Join(
102+
err = utils.ImplementWebhooks(filepath.Join(
104103
kbc.Dir, "api", kbc.Version,
105104
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
106105
Expect(err).Should(Succeed())
@@ -330,61 +329,3 @@ var _ = Describe("kubebuilder", func() {
330329
})
331330
})
332331
})
333-
334-
func implementWebhooks(filename string) error {
335-
bs, err := ioutil.ReadFile(filename)
336-
if err != nil {
337-
return err
338-
}
339-
str := string(bs)
340-
341-
str, err = ensureExistAndReplace(
342-
str,
343-
"import (",
344-
`import (
345-
"errors"`)
346-
if err != nil {
347-
return err
348-
}
349-
350-
// implement defaulting webhook logic
351-
str, err = ensureExistAndReplace(
352-
str,
353-
"// TODO(user): fill in your defaulting logic.",
354-
`if r.Spec.Count == 0 {
355-
r.Spec.Count = 5
356-
}`)
357-
if err != nil {
358-
return err
359-
}
360-
361-
// implement validation webhook logic
362-
str, err = ensureExistAndReplace(
363-
str,
364-
"// TODO(user): fill in your validation logic upon object creation.",
365-
`if r.Spec.Count < 0 {
366-
return errors.New(".spec.count must >= 0")
367-
}`)
368-
if err != nil {
369-
return err
370-
}
371-
str, err = ensureExistAndReplace(
372-
str,
373-
"// TODO(user): fill in your validation logic upon object update.",
374-
`if r.Spec.Count < 0 {
375-
return errors.New(".spec.count must >= 0")
376-
}`)
377-
if err != nil {
378-
return err
379-
}
380-
// false positive
381-
// nolint:gosec
382-
return ioutil.WriteFile(filename, []byte(str), 0644)
383-
}
384-
385-
func ensureExistAndReplace(input, match, replace string) (string, error) {
386-
if !strings.Contains(input, match) {
387-
return "", fmt.Errorf("can't find %q", match)
388-
}
389-
return strings.Replace(input, match, replace, -1), nil
390-
}

0 commit comments

Comments
 (0)