Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

cockroachdb_info: new module #18

Merged
merged 21 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d06cfd5
cockroachdb_info: new module
Andersson007 Nov 9, 2021
7659807
Get server version
Andersson007 Nov 9, 2021
7231602
CI: Remove redundant check
Andersson007 Nov 10, 2021
b872159
Fix CI
Andersson007 Nov 10, 2021
1b44d16
Remove unnecessary try-except
Andersson007 Nov 10, 2021
53e848f
Add comments to functions
Andersson007 Nov 10, 2021
5ee014c
Add unit tests for extract_server_ver function
Andersson007 Nov 10, 2021
601638d
Fix formatting sanity issues
Andersson007 Nov 10, 2021
e513542
Add unit tests for exec_query function
Andersson007 Nov 10, 2021
b480408
Unit tests: replace dummy Module class with monkeypatch
Andersson007 Nov 10, 2021
e48dcf0
Unit tests: replace dummy Cursor classes with monkeypatch
Andersson007 Nov 10, 2021
a031b79
Add get_databases function + its unit and CI tests
Andersson007 Nov 11, 2021
508c1a8
cockroachdb_info unit tests: replace mock funcs with lambdas
Andersson007 Nov 11, 2021
e3bf23d
Switch to DictCursor. Add fetching more fields to get_databases.
Andersson007 Nov 11, 2021
2b6aa54
Add get_users function
Andersson007 Nov 12, 2021
fdac33b
Replace get_databases and get_users func with more general get_info
Andersson007 Nov 15, 2021
db22c71
Add fetching settings
Andersson007 Nov 16, 2021
1e24db2
Add success unit test for get_server_version func
Andersson007 Nov 16, 2021
832425c
Add fail unit test for get_server_version func
Andersson007 Nov 16, 2021
da39a90
Add fetching cluster regions info
Andersson007 Nov 16, 2021
acf8bb7
Fix sanity
Andersson007 Nov 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add get_databases function + its unit and CI tests
  • Loading branch information
Andersson007 committed Nov 11, 2021
commit a031b79916eeecb19bf83d4f6f2322e5f8f94a1b
24 changes: 24 additions & 0 deletions plugins/modules/cockroachdb_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ def get_server_version(module, cursor):
return v_info


def get_databases(module, cursor):
"""Get database info from a server.

Return a dictionary containing database info.
"""
res = exec_query(module, cursor, 'SHOW DATABASES WITH COMMENT')

if not res:
return {}

db_info = {}
for tup in res:
dbname = tup[0]
comment = tup[1]
db_info[dbname] = {}

if comment:
db_info[dbname]['comment'] = comment

return db_info


def main():
# Set up arguments
argument_spec = common_argument_spec()
Expand All @@ -168,11 +190,13 @@ def main():
# We will return it to users at the end
server_info = {
'version': {},
'databases': {},
}

# Collect info
# TODO: implement via loop with filtering
server_info['version'] = get_server_version(module, cursor)
server_info['databases'] = get_databases(module, cursor)

# Close cursor and conn
cursor.close()
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/targets/cockroachdb_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
- result.version.release > 0
- result.version.release < 5
- result.version.patch >= 0
- result.databases.defaultdb.comment == 'root'
- result.databases.postgres.comment == 'root'
- result.databases.system.comment == 'node'


- name: Fetch server info in check_mode
Expand All @@ -49,3 +52,6 @@
- result.version.release > 0
- result.version.release < 5
- result.version.patch >= 0
- result.databases.defaultdb.comment == 'root'
- result.databases.postgres.comment == 'root'
- result.databases.system.comment == 'node'
26 changes: 24 additions & 2 deletions tests/unit/plugins/modules/test_cockroachdb_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ansible_collections.community.cockroachdb.plugins.modules.cockroachdb_info import (
exec_query,
extract_server_ver,
get_databases,
get_server_version,
)

Expand Down Expand Up @@ -68,6 +69,7 @@ def mock__init__(self):

monkeypatch.setattr(Cursor, 'fetchall', mock_fetchall)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)

module = AnsibleModule()
cursor = Cursor()
query = 'SELECT VERSION()'
Expand All @@ -92,8 +94,8 @@ def mock_execute(self, query):
monkeypatch.setattr(Cursor, 'execute', mock_execute)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)
monkeypatch.setattr(AnsibleModule, 'fail_json', mock_fail_json)
module = AnsibleModule()

module = AnsibleModule()
cursor = Cursor()
query = 'SELECT VERSION()'

Expand All @@ -110,12 +112,32 @@ def mock_fetchall(self):
monkeypatch.setattr(Cursor, 'fetchall', mock_fetchall)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)
monkeypatch.setattr(AnsibleModule, 'fail_json', mock_fail_json)
module = AnsibleModule()

module = AnsibleModule()
cursor = Cursor()
query = 'SELECT VERSION()'

exec_query(module, cursor, query)

assert module.fail_msg == ('Failed to fetch rows for query "SELECT VERSION()" '
'from cursor: Fake cursor.fetchall() failing.')


@pytest.mark.parametrize('fetchall_out,expected', [
([['postgres', None]], {'postgres': {}}),
([['postgres', 'comment1']], {'postgres': {'comment': 'comment1'}}),
(
[['postgres', 'comment1'], ['test', 'comment2']],
{'postgres': {'comment': 'comment1'}, 'test': {'comment': 'comment2'}},
)]
)
def test_get_databases(monkeypatch, fetchall_out, expected):
monkeypatch.setattr(Cursor, 'execute', lambda self, x: None)
monkeypatch.setattr(Cursor, 'fetchall', lambda self: fetchall_out)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)
monkeypatch.setattr(AnsibleModule, 'fail_json', mock_fail_json)

module = AnsibleModule()
cursor = Cursor()

assert get_databases(module, cursor) == expected