Skip to content

Commit ea8d50a

Browse files
committed
Support customization in test-e2e-ansible
Signed-off-by: arkadeepsen <[email protected]>
1 parent 550c648 commit ea8d50a

File tree

7 files changed

+132
-72
lines changed

7 files changed

+132
-72
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ test-e2e-teardown: $(KIND)
160160
$(e2e_targets):: test-e2e-setup
161161
test-e2e:: $(e2e_tests) ## Run e2e tests
162162

163-
test-e2e-ansible:: image/ansible-operator ## Run Ansible e2e tests
163+
test-e2e-ansible:: image/ansible-operator test-e2e-ansible-run ## Run Ansible e2e tests
164+
165+
.PHONY: test-e2e-ansible-run
166+
test-e2e-ansible-run:
164167
go test ./test/e2e/ansible -v -ginkgo.v
168+
165169
test-e2e-ansible-molecule:: install dev-install image/ansible-operator ## Run molecule-based Ansible e2e tests
166170
go run ./hack/generate/samples/molecule/generate.go
167171
./hack/tests/e2e-ansible-molecule.sh

hack/generate/samples/ansible/generate.go

+30-18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ var memcachedGVK = schema.GroupVersionKind{
4242
Kind: "Memcached",
4343
}
4444

45+
var skipSecretGeneration = ""
46+
47+
func init() {
48+
skipSecretGeneration = os.Getenv("SKIP_SECRET_GENERATION")
49+
}
50+
4551
func getCli() *cli.CLI {
4652
ansibleBundle, _ := plugin.NewBundleWithOptions(
4753
plugin.WithName(golang.DefaultNameQualifier),
@@ -128,21 +134,25 @@ func GenerateMoleculeSample(samplesPath string) []sample.Sample {
128134
)
129135
pkg.CheckError("attempting to create sample cli", err)
130136

131-
addIgnore, err := samplecli.NewCliSample(
132-
samplecli.WithCLI(getCli()),
133-
samplecli.WithCommandContext(ansibleMoleculeMemcached.CommandContext()),
134-
samplecli.WithGvk(
135-
schema.GroupVersionKind{
136-
Group: "ignore",
137-
Version: "v1",
138-
Kind: "Secret",
139-
},
140-
),
141-
samplecli.WithPlugins("ansible"),
142-
samplecli.WithExtraApiOptions("--generate-role"),
143-
samplecli.WithName(ansibleMoleculeMemcached.Name()),
144-
)
145-
pkg.CheckError("creating ignore samples", err)
137+
var addIgnore sample.Sample
138+
139+
if skipSecretGeneration == "" {
140+
addIgnore, err = samplecli.NewCliSample(
141+
samplecli.WithCLI(getCli()),
142+
samplecli.WithCommandContext(ansibleMoleculeMemcached.CommandContext()),
143+
samplecli.WithGvk(
144+
schema.GroupVersionKind{
145+
Group: "ignore",
146+
Version: "v1",
147+
Kind: "Secret",
148+
},
149+
),
150+
samplecli.WithPlugins("ansible"),
151+
samplecli.WithExtraApiOptions("--generate-role"),
152+
samplecli.WithName(ansibleMoleculeMemcached.Name()),
153+
)
154+
pkg.CheckError("creating ignore samples", err)
155+
}
146156

147157
// remove sample directory if it already exists
148158
err = os.RemoveAll(ansibleMoleculeMemcached.Dir())
@@ -159,9 +169,11 @@ func GenerateMoleculeSample(samplesPath string) []sample.Sample {
159169
err = e2e.AllowProjectBeMultiGroup(ansibleMoleculeMemcached)
160170
pkg.CheckError("updating PROJECT file", err)
161171

162-
ignoreGen := sample.NewGenerator(sample.WithNoInit(), sample.WithNoWebhook())
163-
err = ignoreGen.GenerateSamples(addIgnore)
164-
pkg.CheckError("generating ansible molecule sample - ignore", err)
172+
if skipSecretGeneration == "" {
173+
ignoreGen := sample.NewGenerator(sample.WithNoInit(), sample.WithNoWebhook())
174+
err = ignoreGen.GenerateSamples(addIgnore)
175+
pkg.CheckError("generating ansible molecule sample - ignore", err)
176+
}
165177

166178
ImplementMemcached(ansibleMoleculeMemcached, bundleImage)
167179

hack/generate/samples/ansible/memcached_molecule.go

+38-34
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func ImplementMemcachedMolecule(sample sample.Sample, image string) {
4040
err := kbutil.InsertCode(moleculeTaskPath, targetMoleculeCheckDeployment, molecuTaskToCheckConfigMap)
4141
pkg.CheckError("replacing memcached task to add config map check", err)
4242

43-
log.Info("insert molecule task to ensure to check secret")
44-
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testSecretMoleculeCheck)
45-
pkg.CheckError("replacing memcached task to add secret check", err)
43+
if skipSecretGeneration == "" {
44+
log.Info("insert molecule task to ensure to check secret")
45+
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testSecretMoleculeCheck)
46+
pkg.CheckError("replacing memcached task to add secret check", err)
47+
}
4648

4749
log.Info("insert molecule task to ensure to foo ")
48-
err = kbutil.InsertCode(moleculeTaskPath, testSecretMoleculeCheck, testFooMoleculeCheck)
50+
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testFooMoleculeCheck)
4951
pkg.CheckError("replacing memcached task to add foo check", err)
5052

5153
log.Info("insert molecule task to check custom metrics")
@@ -98,35 +100,37 @@ func ImplementMemcachedMolecule(sample sample.Sample, image string) {
98100
"playbook: playbooks/memcached.yml", memcachedWatchCustomizations)
99101
pkg.CheckError("replacing in watches", err)
100102

101-
log.Info("removing ignore group for the secret from watches as an workaround to work with core types")
102-
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
103-
"ignore.example.com", "\"\"")
104-
pkg.CheckError("replacing the watches file", err)
105-
106-
log.Info("removing molecule test for the Secret since it is a core type")
107-
cmd := exec.Command("rm", "-rf", filepath.Join(sample.Dir(), "molecule", "default", "tasks", "secret_test.yml"))
108-
_, err = sample.CommandContext().Run(cmd)
109-
pkg.CheckError("removing secret test file", err)
110-
111-
log.Info("adding Secret task to the role")
112-
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "roles", "secret", "tasks", "main.yml"),
113-
originalTaskSecret, taskForSecret)
114-
pkg.CheckError("replacing in secret/tasks/main.yml file", err)
115-
116-
log.Info("adding ManageStatus == false for role secret")
117-
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
118-
"role: secret", manageStatusFalseForRoleSecret)
119-
pkg.CheckError("replacing in watches.yaml", err)
120-
121-
// prevent high load of controller caused by watching all the secrets in the cluster
122-
watchNamespacePatchFileName := "watch_namespace_patch.yaml"
123-
log.Info("adding WATCH_NAMESPACE env patch to watch own namespace")
124-
err = os.WriteFile(filepath.Join(sample.Dir(), "config", "testing", watchNamespacePatchFileName), []byte(watchNamespacePatch), 0644)
125-
pkg.CheckError("adding watch_namespace_patch.yaml", err)
126-
127-
log.Info("adding WATCH_NAMESPACE env patch to patch list to be applied")
128-
err = kbutil.InsertCode(filepath.Join(sample.Dir(), "config", "testing", "kustomization.yaml"), "patches:",
129-
fmt.Sprintf("\n- path: %s", watchNamespacePatchFileName))
130-
pkg.CheckError("inserting in kustomization.yaml", err)
103+
if skipSecretGeneration == "" {
104+
log.Info("removing ignore group for the secret from watches as an workaround to work with core types")
105+
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
106+
"ignore.example.com", "\"\"")
107+
pkg.CheckError("replacing the watches file", err)
108+
109+
log.Info("removing molecule test for the Secret since it is a core type")
110+
cmd := exec.Command("rm", "-rf", filepath.Join(sample.Dir(), "molecule", "default", "tasks", "secret_test.yml"))
111+
_, err = sample.CommandContext().Run(cmd)
112+
pkg.CheckError("removing secret test file", err)
113+
114+
log.Info("adding Secret task to the role")
115+
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "roles", "secret", "tasks", "main.yml"),
116+
originalTaskSecret, taskForSecret)
117+
pkg.CheckError("replacing in secret/tasks/main.yml file", err)
118+
119+
log.Info("adding ManageStatus == false for role secret")
120+
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
121+
"role: secret", manageStatusFalseForRoleSecret)
122+
pkg.CheckError("replacing in watches.yaml", err)
123+
124+
// prevent high load of controller caused by watching all the secrets in the cluster
125+
watchNamespacePatchFileName := "watch_namespace_patch.yaml"
126+
log.Info("adding WATCH_NAMESPACE env patch to watch own namespace")
127+
err = os.WriteFile(filepath.Join(sample.Dir(), "config", "testing", watchNamespacePatchFileName), []byte(watchNamespacePatch), 0644)
128+
pkg.CheckError("adding watch_namespace_patch.yaml", err)
129+
130+
log.Info("adding WATCH_NAMESPACE env patch to patch list to be applied")
131+
err = kbutil.InsertCode(filepath.Join(sample.Dir(), "config", "testing", "kustomization.yaml"), "patches:",
132+
fmt.Sprintf("\n- path: %s", watchNamespacePatchFileName))
133+
pkg.CheckError("inserting in kustomization.yaml", err)
134+
}
131135

132136
}

pkg/testutils/e2e/metrics/helpers.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,25 @@ import (
1414
// GetMetrics creates a pod with the permissions to `curl` metrics. It will then return the output of the `curl` pod
1515
func GetMetrics(sample sample.Sample, kubectl kubernetes.Kubectl, metricsClusterRoleBindingName string) string {
1616
ginkgo.By("creating a curl pod")
17+
securityContext := `
18+
{
19+
"apiVersion": "v1",
20+
"spec": {
21+
"securityContext": {
22+
"runAsNonRoot": true,
23+
"runAsUser": 1000,
24+
"runAsGroup": 1000,
25+
"fsGroup": 1000,
26+
"seccompProfile": {
27+
"type": "RuntimeDefault"
28+
}
29+
}
30+
}
31+
}
32+
`
33+
overrides := fmt.Sprintf("--overrides=%s", securityContext)
1734
cmdOpts := []string{
18-
"run", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--",
35+
"run", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", overrides, "--",
1936
"curl", "-v",
2037
fmt.Sprintf("http://%s-controller-manager-metrics-service.%s.svc:8443/metrics", sample.Name(), kubectl.Namespace()),
2138
}

test/e2e/ansible/local_test.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package e2e_ansible_test
1616

1717
import (
18+
"os"
1819
"os/exec"
1920

2021
. "github.com/onsi/ginkgo/v2"
@@ -24,19 +25,30 @@ import (
2425

2526
var _ = Describe("Running Ansible projects", func() {
2627
Context("built with operator-sdk", func() {
28+
var (
29+
skip_local_test string
30+
)
2731
BeforeEach(func() {
28-
By("Installing CRD's")
29-
err := operator.InstallCRDs(ansibleSample)
30-
Expect(err).NotTo(HaveOccurred())
32+
skip_local_test = os.Getenv(SKIP_LOCAL_TEST)
33+
if skip_local_test == "" {
34+
By("Installing CRD's")
35+
err := operator.InstallCRDs(ansibleSample)
36+
Expect(err).NotTo(HaveOccurred())
37+
}
3138
})
3239

3340
AfterEach(func() {
34-
By("Uninstalling CRD's")
35-
err := operator.UninstallCRDs(ansibleSample)
36-
Expect(err).NotTo(HaveOccurred())
41+
if skip_local_test == "" {
42+
By("Uninstalling CRD's")
43+
err := operator.UninstallCRDs(ansibleSample)
44+
Expect(err).NotTo(HaveOccurred())
45+
}
3746
})
3847

3948
It("Should run correctly when run locally", func() {
49+
if skip_local_test != "" {
50+
Skip("Skipping local test")
51+
}
4052
By("Running the project")
4153
cmd := exec.Command("make", "run")
4254
cmd.Dir = ansibleSample.Dir()

test/e2e/ansible/suite_test.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ var (
5656
image = "e2e-test-ansible:temp"
5757
)
5858

59+
const (
60+
MEMCACHED_MOLECULE_OPERATOR_IMAGE = "MEMCACHED_MOLECULE_OPERATOR_IMAGE"
61+
SKIP_LOCAL_TEST = "SKIP_LOCAL_TEST"
62+
)
63+
5964
// BeforeSuite run before any specs are run to perform the required actions for all e2e ansible tests.
6065
var _ = BeforeSuite(func() {
6166
wd, err := os.Getwd()
@@ -105,9 +110,13 @@ var _ = BeforeSuite(func() {
105110
}, 3*time.Minute, time.Second).Should(Succeed())
106111
}
107112

108-
By("building the project image")
109-
err = operator.BuildOperatorImage(ansibleSample, image)
110-
Expect(err).NotTo(HaveOccurred())
113+
if image_env_var, exists := os.LookupEnv(MEMCACHED_MOLECULE_OPERATOR_IMAGE); exists {
114+
image = image_env_var
115+
} else {
116+
By("building the project image")
117+
err = operator.BuildOperatorImage(ansibleSample, image)
118+
Expect(err).NotTo(HaveOccurred())
119+
}
111120

112121
onKind, err := kind.IsRunningOnKind(kctl)
113122
Expect(err).NotTo(HaveOccurred())
@@ -128,9 +137,11 @@ var _ = AfterSuite(func() {
128137
}
129138

130139
By("destroying container image and work dir")
131-
cmd := exec.Command("docker", "rmi", "-f", image)
132-
if _, err := ansibleSample.CommandContext().Run(cmd); err != nil {
133-
Expect(err).To(BeNil())
140+
if _, exists := os.LookupEnv(MEMCACHED_MOLECULE_OPERATOR_IMAGE); !exists {
141+
cmd := exec.Command("docker", "rmi", "-f", image)
142+
if _, err := ansibleSample.CommandContext().Run(cmd); err != nil {
143+
Expect(err).To(BeNil())
144+
}
134145
}
135146
if err := os.RemoveAll(testdir); err != nil {
136147
Expect(err).To(BeNil())

testdata/memcached-molecule-operator/molecule/default/tasks/memcached_test.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
resource_name=custom_resource.metadata.name
5757
)}}'
5858

59-
# This will verify that the secret role was executed
60-
- name: Verify that test-service was created
61-
assert:
62-
that: lookup('k8s', kind='Service', api_version='v1', namespace=namespace, resource_name='test-service')
63-
64-
6559
- name: Verify that project testing-foo was created
6660
assert:
6761
that: lookup('k8s', kind='Namespace', api_version='v1', resource_name='testing-foo')
@@ -116,6 +110,12 @@
116110

117111

118112

113+
# This will verify that the secret role was executed
114+
- name: Verify that test-service was created
115+
assert:
116+
that: lookup('k8s', kind='Service', api_version='v1', namespace=namespace, resource_name='test-service')
117+
118+
119119
- when: molecule_yml.scenario.name == "test-local"
120120
block:
121121
- name: Restart the operator by killing the pod

0 commit comments

Comments
 (0)