Skip to content

Commit d32e1b1

Browse files
committed
Support customization in test-e2e-ansible
Signed-off-by: arkadeepsen <arsen@redhat.com>
1 parent ee3d340 commit d32e1b1

File tree

8 files changed

+133
-73
lines changed

8 files changed

+133
-73
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ var memcachedGVK = schema.GroupVersionKind{
4141
Kind: "Memcached",
4242
}
4343

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

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

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

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

165177
ImplementMemcached(ansibleMoleculeMemcached, bundleImage)
166178

hack/generate/samples/ansible/memcached_molecule.go

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

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)
44+
if skipSecretGeneration == "" {
45+
log.Info("insert molecule task to ensure to check secret")
46+
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testSecretMoleculeCheck)
47+
pkg.CheckError("replacing memcached task to add secret check", err)
48+
}
4749

4850
log.Info("insert molecule task to ensure to foo ")
49-
err = kbutil.InsertCode(moleculeTaskPath, testSecretMoleculeCheck, testFooMoleculeCheck)
51+
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testFooMoleculeCheck)
5052
pkg.CheckError("replacing memcached task to add foo check", err)
5153

5254
log.Info("insert molecule task to check custom metrics")
@@ -100,34 +102,36 @@ func ImplementMemcachedMolecule(sample sample.Sample, image string) {
100102
"playbook: playbooks/memcached.yml", memcachedWatchCustomizations)
101103
pkg.CheckError("replacing in watches", err)
102104

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

pkg/testutils/e2e/metrics/helpers.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,25 @@ func GetMetrics(sample sample.Sample, kubectl kubernetes.Kubectl, metricsCluster
4848
gomega.Expect(len(token)).To(gomega.BeNumerically(">", 0))
4949

5050
ginkgo.By("creating a curl pod")
51+
securityContext := `
52+
{
53+
"apiVersion": "v1",
54+
"spec": {
55+
"securityContext": {
56+
"runAsNonRoot": true,
57+
"runAsUser": 1000,
58+
"runAsGroup": 1000,
59+
"fsGroup": 1000,
60+
"seccompProfile": {
61+
"type": "RuntimeDefault"
62+
}
63+
}
64+
}
65+
}
66+
`
67+
overrides := fmt.Sprintf("--overrides=%s", securityContext)
5168
cmdOpts := []string{
52-
"run", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--",
69+
"run", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", overrides, "--",
5370
"curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token),
5471
fmt.Sprintf("https://%s-controller-manager-metrics-service.%s.svc:8443/metrics", sample.Name(), kubectl.Namespace()),
5572
}

test/e2e/ansible/cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var _ = Describe("Running ansible projects", func() {
6060
Expect(metrics.CleanUpMetrics(kctl, metricsClusterRoleBindingName)).To(Succeed())
6161

6262
By("cleaning up created API objects during test process")
63-
Expect(operator.UndeployOperator(ansibleSample)).To(Succeed())
63+
testutils.WrapWarnOutput("", operator.UndeployOperator(ansibleSample))
6464

6565
By("ensuring that the namespace was deleted")
6666
testutils.WrapWarnOutput(kctl.Wait(false, "namespace", "foo", "--for", "delete", "--timeout", "2m"))

test/e2e/ansible/local_test.go

Lines changed: 18 additions & 6 deletions
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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ var (
5757
image = "e2e-test-ansible:temp"
5858
)
5959

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

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

113122
onKind, err := kind.IsRunningOnKind(kctl)
114123
Expect(err).NotTo(HaveOccurred())
@@ -129,9 +138,11 @@ var _ = AfterSuite(func() {
129138
}
130139

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

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

Lines changed: 6 additions & 6 deletions
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)