Skip to content

Commit aa0c623

Browse files
test-e2e/ansible : using samples (#4142)
**Description of the change:** Use Memcached sample instead of re-generate the project. **Motivation for the change:** operator-framework/enhancements#47
1 parent 1c32637 commit aa0c623

File tree

6 files changed

+43
-140
lines changed

6 files changed

+43
-140
lines changed

hack/generate/samples/internal/ansible/constants.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const roleFragment = `
2323
metadata:
2424
name: '{{ ansible_operator_meta.name }}-memcached'
2525
namespace: '{{ ansible_operator_meta.namespace }}'
26+
labels:
27+
app: memcached
2628
spec:
2729
replicas: "{{size}}"
2830
selector:
@@ -44,6 +46,11 @@ const roleFragment = `
4446
image: "docker.io/memcached:1.4.36-alpine"
4547
ports:
4648
- containerPort: 11211
49+
readinessProbe:
50+
tcpSocket:
51+
port: 11211
52+
initialDelaySeconds: 3
53+
periodSeconds: 3
4754
`
4855

4956
const defaultsFragment = `size: 1`
@@ -296,43 +303,6 @@ const molecuTaskToCheckConfigMap = `
296303
`
297304

298305
const memcachedWithBlackListTask = `
299-
- name: start memcached
300-
community.kubernetes.k8s:
301-
definition:
302-
kind: Deployment
303-
apiVersion: apps/v1
304-
metadata:
305-
name: '{{ ansible_operator_meta.name }}-memcached'
306-
namespace: '{{ ansible_operator_meta.namespace }}'
307-
labels:
308-
app: memcached
309-
spec:
310-
replicas: "{{size}}"
311-
selector:
312-
matchLabels:
313-
app: memcached
314-
template:
315-
metadata:
316-
labels:
317-
app: memcached
318-
spec:
319-
containers:
320-
- name: memcached
321-
command:
322-
- memcached
323-
- -m=64
324-
- -o
325-
- modern
326-
- -v
327-
image: "docker.io/memcached:1.4.36-alpine"
328-
ports:
329-
- containerPort: 11211
330-
readinessProbe:
331-
tcpSocket:
332-
port: 11211
333-
initialDelaySeconds: 3
334-
periodSeconds: 3
335-
336306
- operator_sdk.util.k8s_status:
337307
api_version: cache.example.com/v1alpha1
338308
kind: Memcached

hack/generate/samples/internal/ansible/molecule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (ma *MoleculeAnsible) Run() {
8585
pkg.CheckError("replacing in role.yml", err)
8686

8787
log.Infof("adding Memcached mock task to the role with black list")
88-
err = testutils.ReplaceInFile(filepath.Join(ma.ctx.Dir, "roles", strings.ToLower(ma.ctx.Kind), "tasks", "main.yml"),
88+
err = kbtestutils.InsertCode(filepath.Join(ma.ctx.Dir, "roles", strings.ToLower(ma.ctx.Kind), "tasks", "main.yml"),
8989
roleFragment, memcachedWithBlackListTask)
9090
pkg.CheckError("replacing in tasks/main.yml", err)
9191

test/e2e-ansible/e2e_ansible_cluster_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ var _ = Describe("Running ansible projects", func() {
3434
var fooSampleFile string
3535
var memfinSampleFile string
3636
var memcachedDeployment string
37+
var metricsClusterRoleBindingName string
3738

3839
Context("built with operator-sdk", func() {
3940
BeforeEach(func() {
41+
metricsClusterRoleBindingName = fmt.Sprintf("%s-metrics-reader", tc.ProjectName)
42+
4043
By("checking samples")
4144
memcachedSampleFile = filepath.Join(tc.Dir, "config", "samples",
4245
fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind)))
@@ -45,11 +48,6 @@ var _ = Describe("Running ansible projects", func() {
4548
memfinSampleFile = filepath.Join(tc.Dir, "config", "samples",
4649
fmt.Sprintf("%s_%s_memfin.yaml", tc.Group, tc.Version))
4750

48-
By("enabling Prometheus via the kustomization.yaml")
49-
Expect(kbtestutils.UncommentCode(
50-
filepath.Join(tc.Dir, "config", "default", "kustomization.yaml"),
51-
"#- ../prometheus", "#")).To(Succeed())
52-
5351
By("deploying project on the cluster")
5452
err := tc.Make("deploy", "IMG="+tc.ImageName)
5553
Expect(err).NotTo(HaveOccurred())
@@ -65,7 +63,7 @@ var _ = Describe("Running ansible projects", func() {
6563

6664
By("cleaning up permissions")
6765
_, _ = tc.Kubectl.Command("delete", "clusterrolebinding",
68-
fmt.Sprintf("metrics-%s", tc.TestSuffix))
66+
metricsClusterRoleBindingName)
6967

7068
By("undeploy project")
7169
_ = tc.Make("undeploy")
@@ -122,14 +120,14 @@ var _ = Describe("Running ansible projects", func() {
122120
_, err := tc.Kubectl.Get(
123121
true,
124122
"ServiceMonitor",
125-
fmt.Sprintf("e2e-%s-controller-manager-metrics-monitor", tc.TestSuffix))
123+
fmt.Sprintf("%s-controller-manager-metrics-monitor", tc.ProjectName))
126124
Expect(err).NotTo(HaveOccurred())
127125

128126
By("ensuring the created metrics Service for the manager")
129127
_, err = tc.Kubectl.Get(
130128
true,
131129
"Service",
132-
fmt.Sprintf("e2e-%s-controller-manager-metrics-service", tc.TestSuffix))
130+
fmt.Sprintf("%s-controller-manager-metrics-service", tc.ProjectName))
133131
Expect(err).NotTo(HaveOccurred())
134132

135133
By("create custom resource (Memcached CR)")
@@ -263,9 +261,8 @@ var _ = Describe("Running ansible projects", func() {
263261
By("granting permissions to access the metrics and read the token")
264262
_, err = tc.Kubectl.Command(
265263
"create",
266-
"clusterrolebinding",
267-
fmt.Sprintf("metrics-%s", tc.TestSuffix),
268-
fmt.Sprintf("--clusterrole=e2e-%s-metrics-reader", tc.TestSuffix),
264+
"clusterrolebinding", metricsClusterRoleBindingName,
265+
fmt.Sprintf("--clusterrole=%s-metrics-reader", tc.ProjectName),
269266
fmt.Sprintf("--serviceaccount=%s:default", tc.Kubectl.Namespace))
270267
Expect(err).NotTo(HaveOccurred())
271268

@@ -286,8 +283,8 @@ var _ = Describe("Running ansible projects", func() {
286283
cmdOpts := []string{
287284
"run", "--generator=run-pod/v1", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--",
288285
"curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token),
289-
fmt.Sprintf("https://e2e-%v-controller-manager-metrics-service.e2e-%v-system.svc:8443/metrics",
290-
tc.TestSuffix, tc.TestSuffix),
286+
fmt.Sprintf("https://%s-controller-manager-metrics-service.%s.svc:8443/metrics",
287+
tc.ProjectName, tc.Kubectl.Namespace),
291288
}
292289
_, err = tc.Kubectl.CommandInNamespace(cmdOpts...)
293290
Expect(err).NotTo(HaveOccurred())

test/e2e-ansible/e2e_ansible_olm_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ var _ = Describe("Integrating ansible Projects with OLM", func() {
2626
const operatorVersion = "0.0.1"
2727

2828
It("should generate and run a valid OLM bundle and packagemanifests", func() {
29-
By("turning off interactive prompts for all generation tasks.")
30-
err := tc.DisableOLMBundleInteractiveMode()
31-
Expect(err).NotTo(HaveOccurred())
32-
3329
By("building the bundle")
34-
err = tc.Make("bundle", "IMG="+tc.ImageName)
30+
err := tc.Make("bundle", "IMG="+tc.ImageName)
3531
Expect(err).NotTo(HaveOccurred())
3632

3733
By("building the operator bundle image")

test/e2e-ansible/e2e_ansible_suite_test.go

Lines changed: 17 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ package e2e_ansible_test
1616

1717
import (
1818
"fmt"
19+
"os/exec"
1920
"path/filepath"
2021
"strings"
2122
"testing"
2223

24+
kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils"
25+
2326
. "github.com/onsi/ginkgo"
2427
. "github.com/onsi/gomega"
2528

@@ -47,8 +50,15 @@ var _ = BeforeSuite(func() {
4750
tc, err = testutils.NewTestContext(testutils.BinaryName, "GO111MODULE=on")
4851
Expect(err).NotTo(HaveOccurred())
4952

50-
By("creating the repository")
51-
Expect(tc.Prepare()).To(Succeed())
53+
tc.Domain = "example.com"
54+
tc.Version = "v1alpha1"
55+
tc.Group = "cache"
56+
tc.Kind = "Memcached"
57+
tc.ProjectName = "memcached-operator"
58+
tc.Kubectl.Namespace = fmt.Sprintf("%s-system", tc.ProjectName)
59+
60+
By("copying sample to a temporary e2e directory")
61+
Expect(exec.Command("cp", "-r", "../../testdata/ansible/memcached-operator", tc.Dir).Run()).To(Succeed())
5262

5363
By("fetching the current-context")
5464
tc.Kubectx, err = tc.Kubectl.Command("config", "current-context")
@@ -57,50 +67,17 @@ var _ = BeforeSuite(func() {
5767
By("preparing the prerequisites on cluster")
5868
tc.InstallPrerequisites()
5969

60-
By("setting domain and GVK")
61-
tc.Domain = "example.com"
62-
tc.Version = "v1alpha1"
63-
tc.Group = "ansible"
64-
tc.Kind = "Memcached"
65-
66-
By("initializing a ansible project")
67-
err = tc.Init(
68-
"--plugins", "ansible",
69-
"--project-version", "3-alpha",
70-
"--domain", tc.Domain)
71-
Expect(err).NotTo(HaveOccurred())
72-
7370
By("using dev image for scorecard-test")
7471
err = tc.ReplaceScorecardImagesForDev()
7572
Expect(err).NotTo(HaveOccurred())
7673

77-
By("creating the Memcached API")
78-
err = tc.CreateAPI(
79-
"--group", tc.Group,
80-
"--version", tc.Version,
81-
"--kind", tc.Kind,
82-
"--generate-playbook",
83-
"--generate-role")
84-
Expect(err).NotTo(HaveOccurred())
85-
8674
By("replacing project Dockerfile to use ansible base image with the dev tag")
8775
err = testutils.ReplaceRegexInFile(filepath.Join(tc.Dir, "Dockerfile"), "quay.io/operator-framework/ansible-operator:.*", "quay.io/operator-framework/ansible-operator:dev")
8876
Expect(err).Should(Succeed())
8977

9078
By("adding Memcached mock task to the role")
91-
err = testutils.ReplaceInFile(filepath.Join(tc.Dir, "roles", strings.ToLower(tc.Kind), "tasks", "main.yml"),
92-
fmt.Sprintf("# tasks file for %s", tc.Kind), memcachedWithBlackListTask)
93-
Expect(err).NotTo(HaveOccurred())
94-
95-
By("setting defaults to Memcached")
96-
err = testutils.ReplaceInFile(filepath.Join(tc.Dir, "roles", strings.ToLower(tc.Kind), "defaults", "main.yml"),
97-
fmt.Sprintf("# defaults file for %s", tc.Kind), "size: 1")
98-
Expect(err).NotTo(HaveOccurred())
99-
100-
By("updating Memcached sample")
101-
memcachedSampleFile := filepath.Join(tc.Dir, "config", "samples",
102-
fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind)))
103-
err = testutils.ReplaceInFile(memcachedSampleFile, "foo: bar", "size: 1")
79+
err = kbtestutils.InsertCode(filepath.Join(tc.Dir, "roles", strings.ToLower(tc.Kind), "tasks", "main.yml"),
80+
"periodSeconds: 3", memcachedWithBlackListTask)
10481
Expect(err).NotTo(HaveOccurred())
10582

10683
By("creating an API definition to add a task to delete the config map")
@@ -134,15 +111,6 @@ var _ = BeforeSuite(func() {
134111
"# +kubebuilder:scaffold:rules", rolesForBaseOperator)
135112
Expect(err).NotTo(HaveOccurred())
136113

137-
By("turning off interactive prompts for all generation tasks.")
138-
replace := "operator-sdk generate kustomize manifests"
139-
err = testutils.ReplaceInFile(filepath.Join(tc.Dir, "Makefile"), replace, replace+" --interactive=false")
140-
Expect(err).NotTo(HaveOccurred())
141-
142-
By("checking the kustomize setup")
143-
err = tc.Make("kustomize")
144-
Expect(err).NotTo(HaveOccurred())
145-
146114
By("building the project image")
147115
err = tc.Make("docker-build", "IMG="+tc.ImageName)
148116
Expect(err).NotTo(HaveOccurred())
@@ -168,45 +136,10 @@ var _ = AfterSuite(func() {
168136
tc.Destroy()
169137
})
170138

171-
const memcachedWithBlackListTask = `- name: start memcached
172-
community.kubernetes.k8s:
173-
definition:
174-
kind: Deployment
175-
apiVersion: apps/v1
176-
metadata:
177-
name: '{{ ansible_operator_meta.name }}-memcached'
178-
namespace: '{{ ansible_operator_meta.namespace }}'
179-
labels:
180-
app: memcached
181-
spec:
182-
replicas: "{{size}}"
183-
selector:
184-
matchLabels:
185-
app: memcached
186-
template:
187-
metadata:
188-
labels:
189-
app: memcached
190-
spec:
191-
containers:
192-
- name: memcached
193-
command:
194-
- memcached
195-
- -m=64
196-
- -o
197-
- modern
198-
- -v
199-
image: "docker.io/memcached:1.4.36-alpine"
200-
ports:
201-
- containerPort: 11211
202-
readinessProbe:
203-
tcpSocket:
204-
port: 11211
205-
initialDelaySeconds: 3
206-
periodSeconds: 3
139+
const memcachedWithBlackListTask = `
207140
208141
- operator_sdk.util.k8s_status:
209-
api_version: ansible.example.com/v1alpha1
142+
api_version: cache.example.com/v1alpha1
210143
kind: Memcached
211144
name: "{{ ansible_operator_meta.name }}"
212145
namespace: "{{ ansible_operator_meta.namespace }}"
@@ -257,7 +190,7 @@ const taskToDeleteConfigMap = `- name: delete configmap for test
257190

258191
const memcachedWatchCustomizations = `playbook: playbooks/memcached.yml
259192
finalizer:
260-
name: finalizer.ansible.example.com
193+
name: finalizer.cache.example.com
261194
role: memfin
262195
blacklist:
263196
- group: ""

testdata/ansible/memcached-operator/roles/memcached/tasks/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
metadata:
99
name: '{{ ansible_operator_meta.name }}-memcached'
1010
namespace: '{{ ansible_operator_meta.namespace }}'
11+
labels:
12+
app: memcached
1113
spec:
1214
replicas: "{{size}}"
1315
selector:
@@ -29,4 +31,9 @@
2931
image: "docker.io/memcached:1.4.36-alpine"
3032
ports:
3133
- containerPort: 11211
34+
readinessProbe:
35+
tcpSocket:
36+
port: 11211
37+
initialDelaySeconds: 3
38+
periodSeconds: 3
3239

0 commit comments

Comments
 (0)