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

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

A new test playbook can be found at:

    tests/location/test_location_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-location.md                            |  1 +
 tests/location/test_location.yml              | 10 ++++-
 .../location/test_location_client_context.yml | 37 +++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 tests/location/test_location_client_context.yml

diff --git a/README-location.md b/README-location.md
index 3e9b7ef1..203ba254 100644
--- a/README-location.md
+++ b/README-location.md
@@ -81,6 +81,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` \| `idnsname` | The list of location name strings. | yes
 `description` | The IPA location string | false
 `state` | The state to ensure. It can be one of `present`, `absent`, default: `present`. | no
diff --git a/tests/location/test_location.yml b/tests/location/test_location.yml
index 10aed32f..3cda4115 100644
--- a/tests/location/test_location.yml
+++ b/tests/location/test_location.yml
@@ -1,6 +1,6 @@
 ---
 - name: Test location
-  hosts: ipaserver
+  hosts: "{{ ipa_test_host | default('ipaserver') }}"
   become: true
 
   tasks:
@@ -10,6 +10,7 @@
   - name: Ensure location my_location1 is absent
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       state: absent
 
@@ -20,6 +21,7 @@
   - name: Ensure location my_location1 is present
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
     register: result
     failed_when: not result.changed or result.failed
@@ -27,6 +29,7 @@
   - name: Ensure location my_location1 is present again
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
     register: result
     failed_when: result.changed or result.failed
@@ -34,6 +37,7 @@
   - name: Ensure location my_location1 is present with description
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       description: My Location 1
     register: result
@@ -42,6 +46,7 @@
   - name: Ensure location my_location1 is present again with description
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       description: My Location 1
     register: result
@@ -50,6 +55,7 @@
   - name: Ensure location my_location1 is absent
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       state: absent
     register: result
@@ -58,6 +64,7 @@
   - name: Ensure location my_location1 is absent again
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       state: absent
     register: result
@@ -68,5 +75,6 @@
   - name: Ensure location my_location1 is absent
     ipalocation:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: my_location1
       state: absent
diff --git a/tests/location/test_location_client_context.yml b/tests/location/test_location_client_context.yml
new file mode 100644
index 00000000..00d5749f
--- /dev/null
+++ b/tests/location/test_location_client_context.yml
@@ -0,0 +1,37 @@
+---
+- name: Test location
+  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.
+    ipalocation:
+      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 location using client context, in client host.
+  import_playbook: test_location.yml
+  when: groups['ipaclients']
+  vars:
+    ipa_test_host: ipaclients
+
+- name: Test location using client context, in server host.
+  import_playbook: test_location.yml
+  when: groups['ipaclients'] is not defined or not groups['ipaclients']
-- 
GitLab