Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test case for issue #657

Merged
merged 19 commits into from
Jul 9, 2024
Merged
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
Loading