summaryrefslogtreecommitdiff
path: root/test/integration/targets/mongodb_replicaset
diff options
context:
space:
mode:
authorRhys Campbell <rhys.james.campbell@googlemail.com>2019-07-31 21:45:21 +0200
committerMatt Clay <matt@mystile.com>2019-07-31 12:45:21 -0700
commitdacfd72bd6c661a3ee2565ca081bfe75882e6d0c (patch)
tree33b4295782de84fe76c8e4871ffa5ac0ee6aceb8 /test/integration/targets/mongodb_replicaset
parenteb15ee91dfdb28c77060b332bb5461e9c7015d4d (diff)
downloadansible-dacfd72bd6c661a3ee2565ca081bfe75882e6d0c.tar.gz
Integeration tests for mongodb_shard and mongodb_replicaset modules (#53900)
Diffstat (limited to 'test/integration/targets/mongodb_replicaset')
-rw-r--r--test/integration/targets/mongodb_replicaset/aliases6
-rw-r--r--test/integration/targets/mongodb_replicaset/defaults/main.yml24
-rw-r--r--test/integration/targets/mongodb_replicaset/files/js/is_primary.js13
-rw-r--r--test/integration/targets/mongodb_replicaset/meta/main.yml3
-rw-r--r--test/integration/targets/mongodb_replicaset/tasks/main.yml496
-rw-r--r--test/integration/targets/mongodb_replicaset/tasks/mongod_replicaset.yml44
-rw-r--r--test/integration/targets/mongodb_replicaset/tasks/mongod_teardown.yml27
7 files changed, 613 insertions, 0 deletions
diff --git a/test/integration/targets/mongodb_replicaset/aliases b/test/integration/targets/mongodb_replicaset/aliases
new file mode 100644
index 0000000000..e9fc78ee6d
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/aliases
@@ -0,0 +1,6 @@
+destructive
+shippable/posix/group1
+skip/osx
+skip/freebsd
+skip/rhel
+needs/root
diff --git a/test/integration/targets/mongodb_replicaset/defaults/main.yml b/test/integration/targets/mongodb_replicaset/defaults/main.yml
new file mode 100644
index 0000000000..343d48e96a
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/defaults/main.yml
@@ -0,0 +1,24 @@
+---
+# defaults file for test_mongodb_replicaset
+mongodb_replicaset1: rs1
+mongodb_replicaset2: rs2
+mongodb_replicaset3: rs3
+mongodb_replicaset4: rs4
+mongodb_replicaset5: rs5
+mongodb_replicaset6: rs6
+mongodb_replicaset7: rs7
+mongodb_replicaset8: rs8
+test_mongo_auth: yes
+mongodb_admin_user: test_root
+mongodb_admin_password: saE_Rr9!gE6gh#e~R#nZ
+debug: False
+mongodb_nodes:
+ - 3001
+ - 3002
+ - 3003
+mongod_auth: false
+kill_signal: SIGTERM
+# Should be one of
+mongod_storage_engine_opts: "--storageEngine wiredTiger --wiredTigerEngineConfigString='cache_size=200M'"
+#mongod_storage_engine_opts: "--storageEngine mmapv1 --nojournal"
+mongodb_user: mongodb
diff --git a/test/integration/targets/mongodb_replicaset/files/js/is_primary.js b/test/integration/targets/mongodb_replicaset/files/js/is_primary.js
new file mode 100644
index 0000000000..7bb130614f
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/files/js/is_primary.js
@@ -0,0 +1,13 @@
+var done = false;
+var iterations = 0;
+while(rs.status()['myState'] != 1) {
+ if (!done) {
+ //print("State is not yet PRIMARY. Waiting...");
+ done = true
+ }
+ sleep(1000);
+ iterations++;
+ if (iterations == 100) {
+ throw new Error("Exceeded iterations limit.");
+ }
+ }
diff --git a/test/integration/targets/mongodb_replicaset/meta/main.yml b/test/integration/targets/mongodb_replicaset/meta/main.yml
new file mode 100644
index 0000000000..9d941be0bc
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/meta/main.yml
@@ -0,0 +1,3 @@
+dependencies:
+ - setup_mongodb
+ - setup_remote_tmp_dir
diff --git a/test/integration/targets/mongodb_replicaset/tasks/main.yml b/test/integration/targets/mongodb_replicaset/tasks/main.yml
new file mode 100644
index 0000000000..d416bfc84e
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/tasks/main.yml
@@ -0,0 +1,496 @@
+# test code for the mongodb_replicaset module
+# (c) 2019, Rhys Campbell <rhys.james.campbell@googlemail.com>
+
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+# ============================================================
+
+- name: Ensure tests home exists
+ file:
+ path: "{{ remote_tmp_dir }}/tests"
+ state: directory
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset1 }}"
+
+- include_tasks: mongod_replicaset.yml
+
+# test with yaml list
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset1 }}"
+ heartbeat_timeout_secs: 1
+ election_timeout_millis: 1000
+ members:
+ - "localhost:3001"
+ - "localhost:3002"
+ - "localhost:3003"
+
+- name: Ensure is_primary script exists on host
+ copy:
+ src: js/is_primary.js
+ dest: "{{ remote_tmp_dir }}/tests/is_primary.js"
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.status()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset1 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+
+- name: Add mongodb admin user
+ mongodb_user:
+ login_host: localhost
+ login_port: 3001
+ replica_set: "{{ mongodb_replicaset1 }}"
+ database: admin
+ name: "{{ mongodb_admin_user }}"
+ password: "{{ mongodb_admin_password }}"
+ roles: ["root"]
+ state: present
+ register: mongo_admin_user
+ when: test_mongo_auth
+
+- name: Murder all mongod processes
+ shell: pkill -{{ kill_signal }} mongod;
+
+- name: Getting pids for mongod
+ pids:
+ name: mongod
+ register: pids_of_mongod
+
+- name: Wait for all mongod processes to exit
+ wait_for:
+ path: "/proc/{{ item }}/status"
+ state: absent
+ with_items: "{{ pids_of_mongod }}"
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset1 }}"
+
+- set_fact:
+ mongod_auth: true
+
+- name: Execute mongod script to restart with auth enabled
+ include_tasks: mongod_replicaset.yml
+
+- name: Validate replicaset previously created
+ mongodb_replicaset:
+ login_user: "{{ mongodb_admin_user }}"
+ login_password: "{{ mongodb_admin_password }}"
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset1 }}"
+ election_timeout_millis: 1000
+ members:
+ - "localhost:3001"
+ - "localhost:3002"
+ - "localhost:3003"
+ register: mongodb_replicaset
+
+- name: Assert replicaset name has not changed
+ assert:
+ that: mongodb_replicaset.changed == False
+
+- name: Test with bad password
+ mongodb_replicaset:
+ login_user: "{{ mongodb_admin_user }}"
+ login_password: XXXXXXXXXXXXXXXX
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset1 }}"
+ election_timeout_millis: 1000
+ members:
+ - "localhost:3001"
+ - "localhost:3002"
+ - "localhost:3003"
+ register: mongodb_replicaset_bad_pw
+ ignore_errors: True
+
+- name: Assert login failed
+ assert:
+ that:
+ - "mongodb_replicaset_bad_pw.rc == 1"
+ - "'Authentication failed' in mongodb_replicaset_bad_pw.module_stderr"
+
+ #############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset2 }}"
+
+- set_fact:
+ mongod_auth: false
+
+- name: Execute mongod script to restart with auth enabled
+ include_tasks: mongod_replicaset.yml
+
+# Test with python style list
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset2 }}"
+ members: [ "localhost:3001", "localhost:3002", "localhost:3003" ]
+ election_timeout_millis: 1000
+ heartbeat_timeout_secs: 1
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.status()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset2 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+
+#############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset3 }}"
+
+- set_fact:
+ mongod_auth: false
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test with csv string
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset3 }}"
+ members: "localhost:3001,localhost:3002,localhost:3003"
+ election_timeout_millis: 1000
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.status()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset3 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+
+ #############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset4 }}"
+
+- set_fact:
+ mongod_auth: false
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test with arbiter_at_index
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ arbiter_at_index: 2
+ replica_set: "{{ mongodb_replicaset4 }}"
+ members: "localhost:3001,localhost:3002,localhost:3003"
+ election_timeout_millis: 1000
+
+- name: Ensure host reaches primary before proceeding 3001
+ command: mongo admin --port 3001 "{{ remote_tmp_dir }}/tests/is_primary.js"
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.status()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset4 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'ARBITER' in mongo_output.stdout"
+
+#############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset5 }}"
+
+- set_fact:
+ mongod_auth: false
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test with chainingAllowed
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ chaining_allowed: no
+ replica_set: "{{ mongodb_replicaset5 }}"
+ election_timeout_millis: 1000
+ members:
+ - localhost:3001
+ - localhost:3002
+ - localhost:3003
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.conf()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset5 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'chainingAllowed\" : false,' in mongo_output.stdout"
+
+#############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset6 }}"
+
+- set_fact:
+ mongodb_nodes: [ 3001, 3002, 3003, 3004, 3005]
+
+- set_fact:
+ mongod_auth: false
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test with 5 mongod processes
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ replica_set: "{{ mongodb_replicaset6 }}"
+ election_timeout_millis: 1000
+ members:
+ - localhost:3001
+ - localhost:3002
+ - localhost:3003
+ - localhost:3004
+ - localhost:3005
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.conf()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset6 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'localhost:3004' in mongo_output.stdout"
+ - "'localhost:3005' in mongo_output.stdout"
+
+#############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset7 }}"
+
+- set_fact:
+ mongod_auth: false
+
+- set_fact:
+ mongodb_nodes: [ 3001, 3002, 3003 ]
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test withheartbeatTimeoutSecs
+- name: Create replicaset with module
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ election_timeout_millis: 9999
+ replica_set: "{{ mongodb_replicaset7 }}"
+ members:
+ - localhost:3001
+ - localhost:3002
+ - localhost:3003
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.conf()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset7 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'electionTimeoutMillis\" : 9999,' in mongo_output.stdout"
+
+#############################################################
+
+- include_tasks: mongod_teardown.yml
+
+- set_fact:
+ current_replicaset: "{{ mongodb_replicaset8 }}"
+
+- name: Launch mongod processes
+ include_tasks: mongod_replicaset.yml
+
+# Test with heartbeatTimeoutSecs
+- name: Create replicaset with module protocolVersion 0 (Mongodb 3.0)
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ protocol_version: 0
+ heartbeat_timeout_secs: 9
+ replica_set: "{{ mongodb_replicaset8 }}"
+ election_timeout_millis: 1000
+ members:
+ - localhost:3001
+ - localhost:3002
+ - localhost:3003
+ when: mongodb_version.startswith('3') == True
+
+- name: Create replicaset with module protocolVersion 1 (MongoDB 4.0+)
+ mongodb_replicaset:
+ login_user: admin
+ login_password: secret
+ login_host: "localhost"
+ login_port: 3001
+ login_database: "admin"
+ protocol_version: 1
+ election_timeout_millis: 9000
+ replica_set: "{{ mongodb_replicaset8 }}"
+ members:
+ - localhost:3001
+ - localhost:3002
+ - localhost:3003
+ when: mongodb_version.startswith('4') == True
+
+- name: Get replicaset info
+ command: mongo admin --eval "rs.conf()" --port 3001
+ register: mongo_output
+
+- name: Assert replicaset name is in mongo_output MongoDB 3.0+
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset8 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'heartbeatTimeoutSecs\" : 9,' in mongo_output.stdout"
+ when: mongodb_version.startswith('3') == True
+
+- name: Assert replicaset name is in mongo_output MongoDB 4.0+
+ assert:
+ that:
+ - "mongo_output.changed == true"
+ - "'{{ mongodb_replicaset8 }}' in mongo_output.stdout"
+ - "'localhost:3001' in mongo_output.stdout"
+ - "'localhost:3002' in mongo_output.stdout"
+ - "'localhost:3003' in mongo_output.stdout"
+ - "'electionTimeoutMillis\" : 9000,' in mongo_output.stdout"
+ when: mongodb_version.startswith('4') == True
+
+# TODO - Readd this test once we support serverSelectionTimeoutMS / connectTimeoutMS
+#- name: Run test with unknown host
+# mongodb_replicaset:
+# login_user: admin
+# login_password: secret
+# login_host: "idonotexist"
+# login_port: 3001
+# login_database: "admin"
+# protocol_version: 0
+# heartbeat_timeout_secs: 9
+# replica_set: "{{ mongodb_replicaset8 }}"
+# election_timeout_millis: 1000
+# members:
+# - idonotexist:3001
+# - idonotexist:3002
+# - idonotexist:3003
+# ignore_errors: True
+# register: host_does_not_exist
+
+#- name: Assert that "Name or service not known" is in error
+# assert:
+# that:
+# - "host_does_not_exist.rc == 1"
+# - "'Name or service not known' in host_does_not_exist.module_stderr"
+
+# Final clean up to prevent "directory not empty" error
+- include_tasks: mongod_teardown.yml
diff --git a/test/integration/targets/mongodb_replicaset/tasks/mongod_replicaset.yml b/test/integration/targets/mongodb_replicaset/tasks/mongod_replicaset.yml
new file mode 100644
index 0000000000..9ad3bb1496
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/tasks/mongod_replicaset.yml
@@ -0,0 +1,44 @@
+- name: Set mongodb_user user for redhat
+ set_fact:
+ mongodb_user: "mongod"
+ when: ansible_os_family == "RedHat"
+
+- name: Create directories for mongod processes
+ file:
+ path: "{{ remote_tmp_dir }}/mongod{{ item }}"
+ state: directory
+ owner: "{{ mongodb_user }}"
+ group: "{{ mongodb_user }}"
+ mode: 0755
+ recurse: yes
+ with_items: "{{ mongodb_nodes }}"
+
+- name: Create keyfile
+ copy:
+ dest: "{{ remote_tmp_dir }}/my.key"
+ content: |
+ fd2CUrbXBJpB4rt74A6F
+ owner: "{{ mongodb_user }}"
+ group: "{{ mongodb_user }}"
+ mode: 0600
+ when: mongod_auth == True
+
+- name: Spawn mongod process without auth
+ command: mongod --shardsvr --smallfiles {{ mongod_storage_engine_opts }} --dbpath mongod{{ item }} --port {{ item }} --replSet {{ current_replicaset }} --logpath mongod{{ item }}/log.log --fork
+ args:
+ chdir: "{{ remote_tmp_dir }}"
+ with_items: "{{ mongodb_nodes | sort }}"
+ when: mongod_auth == False
+
+- name: Spawn mongod process with auth
+ command: mongod --shardsvr --smallfiles {{ mongod_storage_engine_opts }} --dbpath mongod{{ item }} --port {{ item }} --replSet {{ current_replicaset }} --logpath mongod{{ item }}/log.log --fork --auth --keyFile my.key
+ args:
+ chdir: "{{ remote_tmp_dir }}"
+ with_items: "{{ mongodb_nodes | sort }}"
+ when: mongod_auth == True
+ ignore_errors: yes
+
+- name: Wait for mongod to start responding
+ wait_for:
+ port: "{{ item }}"
+ with_items: "{{ mongodb_nodes }}"
diff --git a/test/integration/targets/mongodb_replicaset/tasks/mongod_teardown.yml b/test/integration/targets/mongodb_replicaset/tasks/mongod_teardown.yml
new file mode 100644
index 0000000000..7bc5d3d0a0
--- /dev/null
+++ b/test/integration/targets/mongodb_replicaset/tasks/mongod_teardown.yml
@@ -0,0 +1,27 @@
+- name: Kill all mongod processes
+ command: pkill -{{ kill_signal }} mongod
+ ignore_errors: true
+
+- name: Getting pids for mongod
+ pids:
+ name: mongod
+ register: pids_of_mongod
+
+- name: Wait for all mongod processes to exit
+ wait_for:
+ path: "/proc/{{ item }}/status"
+ state: absent
+ delay: 1
+ with_items: "{{ pids_of_mongod }}"
+
+- name: Remove all mongod folders
+ file:
+ path: "{{ remote_tmp_dir }}/{{ item }}"
+ state: absent
+ with_items:
+ - mongod3001
+ - mongod3002
+ - mongod3003
+
+- name: Remove all mongod sock files
+ shell: rm -Rf /tmp/mongodb*.sock