Skip to content

Commit

Permalink
test-e2e-ansible: Check watchAnnotationsChanges
Browse files Browse the repository at this point in the history
Add annotations to foo-sample and check that
the role is triggered by displaying it
in the logs.

Signed-off-by: Gaël Goinvic <[email protected]>
  • Loading branch information
gaelgatelement committed Mar 24, 2022
1 parent c6d491d commit eb770b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/ansible/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Options struct {
// Add - Creates a new ansible operator controller and adds it to the manager
func Add(mgr manager.Manager, options Options) *controller.Controller {
log.Info("Watching resource", "Options.Group", options.GVK.Group, "Options.Version",
options.GVK.Version, "Options.Kind", options.GVK.Kind, "Options.WatchAnnotationsChanges", options.WatchAnnotationsChanges)
options.GVK.Version, "Options.Kind", options.GVK.Kind)
if options.EventHandlers == nil {
options.EventHandlers = []events.EventHandler{}
}
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/ansible/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ var _ = Describe("Running ansible projects", func() {
}
Eventually(verifyMemcachedPatch, time.Minute, time.Second).Should(Succeed())

By("annotating the CR")
_, err = tc.Kubectl.Command(
"annotate", "foo", "foo-sample", "test-annotation='step2'")
Expect(err).NotTo(HaveOccurred())

Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring(
"Ansible-runner exited successfully"))
Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring(
"test-annotation found : 'step2'"))
Eventually(managerContainerLogs, time.Minute, time.Second).ShouldNot(ContainSubstring("failed=1"))
Eventually(managerContainerLogs, time.Minute, time.Second).ShouldNot(ContainSubstring("[Gathering Facts]"))

By("granting permissions to access the metrics and read the token")
_, err = tc.Kubectl.Command("create", "clusterrolebinding", metricsClusterRoleBindingName,
fmt.Sprintf("--clusterrole=%s-metrics-reader", tc.ProjectName),
Expand Down
30 changes: 30 additions & 0 deletions test/e2e/ansible/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ var _ = BeforeSuite(func() {
"foo: bar")
Expect(err).NotTo(HaveOccurred())

By("adding task to display annotations of Foo")
err = kbutil.ReplaceInFile(filepath.Join(tc.Dir, "roles", "foo", "tasks", "main.yml"),
"# tasks file for Foo", fooDebugAnnotations)
Expect(err).NotTo(HaveOccurred())

By("adding to watches annotations changes")
err = kbutil.ReplaceInFile(filepath.Join(tc.Dir, "watches.yaml"),
"role: foo", fooWatchCustomizations)
Expect(err).NotTo(HaveOccurred())

By("adding RBAC permissions for the Memcached Kind")
err = kbutil.ReplaceInFile(filepath.Join(tc.Dir, "config", "rbac", "role.yaml"),
"#+kubebuilder:scaffold:rules", rolesForBaseOperator)
Expand Down Expand Up @@ -233,3 +243,23 @@ const rolesForBaseOperator = `
- watch
#+kubebuilder:scaffold:rules
`
const fooDebugAnnotations = `
- name: Fetch annotations
k8s_info:
kind: Foo
api_version: cache.example.com/v1alpha1
name: "{{ ansible_operator_meta.name }}"
namespace: "{{ ansible_operator_meta.namespace }}"
register: foo_cr_info
- name: Print annotations
debug:
msg: "test-annotation found : {{ foo_cr_info.resources[0].metadata.annotations['test-annotation'] }}"
when:
- foo_cr_info.resources | length > 0
- "'test-annotation' in foo_cr_info.resources[0].metadata.annotations | default({})"
`

const fooWatchCustomizations = `role: foo
watchAnnotationsChanges: true
`

0 comments on commit eb770b8

Please sign in to comment.