From 40f544bab440b27952ab6576f4d23ebf8eae4384 Mon Sep 17 00:00:00 2001
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
Date: Fri, 3 Sep 2021 13:28:30 -0300
Subject: [PATCH] ipahostgroup: Allow execution of plugin in client host.

Update hostgroup README file and add tests for executing plugin with
`ipaapi_context` set to `client`.

A new test playbook can be found at:

    tests/hostgroup/test_hostgroup_client_context.yml

The new test file can be executed in a FreeIPA client host that is
not a server. In this case, it should be defined in the `ipaclients`
group, in the inventory file.
---
 README-hostgroup.md                           |  1 +
 tests/hostgroup/test_hostgroup.yml            | 20 +++++++++-
 .../test_hostgroup_client_context.yml         | 37 +++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 tests/hostgroup/test_hostgroup_client_context.yml

diff --git a/README-hostgroup.md b/README-hostgroup.md
index cedca7e2..e97136ca 100644
--- a/README-hostgroup.md
+++ b/README-hostgroup.md
@@ -150,6 +150,7 @@ Variable | Description | Required
 -------- | ----------- | --------
 `ipaadmin_principal` | The admin principal is a string and defaults to `admin` | no
 `ipaadmin_password` | The admin password is a string and is required if there is no admin ticket available on the node | no
+`ipaapi_context` | The context in which the module will execute. Executing in a server context is preferred. If not provided context will be determined by the execution environment. Valid values are `server` and `client`. | no
 `name` \| `cn` | The list of hostgroup name strings. | no
 `description` | The hostgroup description string. | no
 `nomembers` | Suppress processing of membership attributes. (bool) | no
diff --git a/tests/hostgroup/test_hostgroup.yml b/tests/hostgroup/test_hostgroup.yml
index 1b1ba7a6..076f76d1 100644
--- a/tests/hostgroup/test_hostgroup.yml
+++ b/tests/hostgroup/test_hostgroup.yml
@@ -1,6 +1,6 @@
 ---
 - name: Test hostgroup
-  hosts: ipaserver
+  hosts: "{{ ipa_test_host | default('ipaserver') }}"
   become: true
   gather_facts: true
 
@@ -13,6 +13,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -22,6 +23,7 @@
   - name: Test hosts db1 and db2 absent
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - "{{ 'db1.' + ipaserver_domain }}"
       - "{{ 'db2.' + ipaserver_domain }}"
@@ -30,6 +32,7 @@
   - name: Host "{{ 'db1.' + ipaserver_domain }}" present
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: "{{ 'db1.' + ipaserver_domain }}"
       force: yes
     register: result
@@ -38,6 +41,7 @@
   - name: Host "{{ 'db2.' + ipaserver_domain }}" present
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: "{{ 'db2.' + ipaserver_domain }}"
       force: yes
     register: result
@@ -46,6 +50,7 @@
   - name: Ensure host-group mysql-server is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: mysql-server
       state: present
     register: result
@@ -54,6 +59,7 @@
   - name: Ensure host-group mysql-server is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: mysql-server
       state: present
     register: result
@@ -62,6 +68,7 @@
   - name: Ensure host-group oracle-server is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: oracle-server
       state: present
     register: result
@@ -70,6 +77,7 @@
   - name: Ensure host-group oracle-server is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: oracle-server
       state: present
     register: result
@@ -78,6 +86,7 @@
   - name: Ensure host-group databases is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -90,6 +99,7 @@
   - name: Ensure host-group databases is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -102,6 +112,7 @@
   - name: Ensure host db2 is member of host-group databases
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -113,6 +124,7 @@
   - name: Ensure host db2 is member of host-group databases again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -124,6 +136,7 @@
   - name: Ensure host-group mysql-server is member of host-group databases
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -135,6 +148,7 @@
   - name: Ensure host-group mysql-server is member of host-group databases again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -146,6 +160,7 @@
   - name: Ensure host-group oracle-server is member of host-group databases (again)
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -157,6 +172,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -168,6 +184,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -179,6 +196,7 @@
   - name: Test hosts db1 and db2 absent
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - "{{ 'db1.' + ipaserver_domain }}"
       - "{{ 'db2.' + ipaserver_domain }}"
diff --git a/tests/hostgroup/test_hostgroup_client_context.yml b/tests/hostgroup/test_hostgroup_client_context.yml
new file mode 100644
index 00000000..a234da90
--- /dev/null
+++ b/tests/hostgroup/test_hostgroup_client_context.yml
@@ -0,0 +1,37 @@
+---
+- name: Test hostgroup
+  hosts: ipaclients, ipaserver
+  become: no
+  gather_facts: no
+
+  tasks:
+  - name: Include FreeIPA facts.
+    include_tasks: ../env_freeipa_facts.yml
+
+  # Test will only be executed if host is not a server.
+  - name: Execute with server context in the client.
+    ipahostgroup:
+      ipaadmin_password: SomeADMINpassword
+      ipaapi_context: server
+      name: ThisShouldNotWork
+    register: result
+    failed_when: not (result.failed and result.msg is regex("No module named '*ipaserver'*"))
+    when: ipa_host_is_client
+
+# Import basic module tests, and execute with ipa_context set to 'client'.
+# If ipaclients is set, it will be executed using the client, if not,
+# ipaserver will be used.
+#
+# With this setup, tests can be executed against an IPA client, against
+# an IPA server using "client" context, and ensure that tests are executed
+# in upstream CI.
+
+- name: Test hostgroup using client context, in client host.
+  import_playbook: test_hostgroup.yml
+  when: groups['ipaclients']
+  vars:
+    ipa_test_host: ipaclients
+
+- name: Test hostgroup using client context, in server host.
+  import_playbook: test_hostgroup.yml
+  when: groups['ipaclients'] is not defined or not groups['ipaclients']
-- 
GitLab