Skip to content

Commit

Permalink
Adds test case for issue (#657)
Browse files Browse the repository at this point in the history
* Adds test case for issue

* Move files to correct location

* Fix variable name

* Remove unneeded params

* Run on arbiter

* Add retry code

* Longer delay

* Try different command

* Add new error code

* Remove param

* Add debug step

* Add debug code

* Use module.debug

* Runs listDatabases cmd

* Add Arbiter handling to auth function

* Adds further test cases

* Fix param indent

* dummy

* Add asserts to tests
  • Loading branch information
rhysmeister authored Jul 9, 2024
1 parent 27d350a commit 21ded18
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plugins/module_utils/mongodb_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@ def is_auth_enabled(module):
connection_params = rename_ssl_option_for_pymongo4(connection_params)
try:
myclient = MongoClient(**connection_params)
myclient['admin'].command('listDatabases', 1.0)
auth_is_enabled = False
hello_response = myclient.admin.command('hello')
if 'arbiterOnly' in hello_response and hello_response['arbiterOnly']:
auth_is_enabled = False # Arbiters cannot login with a user
else:
myclient['admin'].command('listDatabases', 1.0)
auth_is_enabled = False
except Exception as excep:
if hasattr(excep, 'code') and excep.code in [13]:
auth_is_enabled = True
Expand Down
65 changes: 65 additions & 0 deletions tests/integration/targets/mongodb_status/tasks/651_auth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Tests for issue 651
- vars:
mongo_parameters: &mongo_parameters
login_host: localhost
login_port: 3003 # Run on Arbiter
login_user: "{{ mongodb_admin_user }}"
login_password: "{{ mongodb_admin_password }}"
login_database: "admin"

block:

- set_fact:
mongodb_nodes:
- 3001
- 3002
- 3003

- set_fact:
mongod_auth: true

- set_fact:
current_replicaset: "issue_651"

- include_tasks: mongod_replicaset.yml

- name: Create replicaset with module
community.mongodb.mongodb_replicaset:
login_host: localhost
login_port: 3001
replica_set: "{{ current_replicaset }}"
members:
- localhost:3001
- localhost:3002
- localhost:3003
arbiter_at_index: 2

- name: Add mongodb admin user - rs0
community.mongodb.mongodb_user:
login_host: localhost
login_port: 3001
replica_set: '{{ current_replicaset }}'
database: admin
name: '{{ mongodb_admin_user }}'
password: '{{ mongodb_admin_password }}'
roles:
- root
state: present
run_once: true

- name: Get replica_set status
community.mongodb.mongodb_status:
<<: *mongo_parameters
replica_set: "{{ current_replicaset }}"
#auth_mechanism: "SCRAM-SHA-256" # Why is this here for no auth?
poll: 6
interval: 10
register: replset_status

- name: Validate expected number of PRIMARY, SECONDARY, ARBITER hosts
assert:
that:
- "{{ replset_status['replicaset'].values() | list | length == 3 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'PRIMARY') | list | length == 1 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'SECONDARY') | list | length == 1 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'ARBITER') | list | length == 1 }}"
50 changes: 50 additions & 0 deletions tests/integration/targets/mongodb_status/tasks/651_noauth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Tests for issue 651
- vars:
mongo_parameters: &mongo_parameters
login_host: localhost
login_port: 3003 # Run on Arbiter
login_database: "admin"

block:

- set_fact:
mongodb_nodes:
- 3001
- 3002
- 3003

- set_fact:
mongod_auth: false

- set_fact:
current_replicaset: "issue_651"

- include_tasks: mongod_replicaset.yml

- name: Create replicaset with module
community.mongodb.mongodb_replicaset:
login_host: localhost
login_port: 3001
replica_set: "{{ current_replicaset }}"
members:
- localhost:3001
- localhost:3002
- localhost:3003
arbiter_at_index: 2

- name: Get replica_set status
community.mongodb.mongodb_status:
<<: *mongo_parameters
replica_set: "{{ current_replicaset }}"
#auth_mechanism: "SCRAM-SHA-256" # Why is this here for no auth?
poll: 6
interval: 10
register: replset_status

- name: Validate expected number of PRIMARY, SECONDARY, ARBITER hosts
assert:
that:
- "{{ replset_status['replicaset'].values() | list | length == 3 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'PRIMARY') | list | length == 1 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'SECONDARY') | list | length == 1 }}"
- "{{ replset_status['replicaset'].values() | list | select('match', 'ARBITER') | list | length == 1 }}"
8 changes: 8 additions & 0 deletions tests/integration/targets/mongodb_status/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,11 @@
- include_tasks: mongod_teardown.yml

- include_tasks: 302.yml

- include_tasks: mongod_teardown.yml

- include_tasks: 651_noauth.yml

- include_tasks: mongod_teardown.yml

- include_tasks: 651_auth.yml

0 comments on commit 21ded18

Please sign in to comment.