Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 committed Feb 9, 2024
1 parent 071ef7e commit b71c6d1
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 31 deletions.
44 changes: 13 additions & 31 deletions plugins/modules/clickhouse_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@
type: str
choices: ['present', 'absent']
default: 'present'
required: true
name:
description:
- Database name to add or remove.
type: str
required: true
comment:
description:
- Comment on the database.
type: str
engine:
description:
- Database engine.
Expand All @@ -64,7 +59,6 @@
login_password: my_password
name: test_db
engine: Memory
comment: Temporary test database
'''

RETURN = r'''
Expand Down Expand Up @@ -151,32 +145,28 @@ def __init__(self, module, client, name):
self.module = module
self.client = client
self.name = name
self.exists, self.engine, self.comment = self.__populate_info()
self.exists, self.engine = self.__populate_info()

def __populate_info(self):
query = "SELECT engine, comment FROM system.databases WHERE name = %(name)s"
query = "SELECT engine FROM system.databases WHERE name = %(name)s"
# Will move this function to the lib later and reuse
exec_kwargs = {'params': {'name': self.name}}
result = execute_query(self.module, self.client, query, exec_kwargs)

# Assume the DB does not exist by default
exists = False
engine = None
comment = None
if result:
# If exists
exists = True
engine = result[0][0]
comment = result[0][1]

return exists, engine, comment
return exists, engine

def create(self, engine, comment):
def create(self, engine):
query = "CREATE DATABASE %s" % self.name
if engine:
query += " ENGINE = %s" % engine
if comment:
query += " COMMENT '%s'" % comment

executed_statements.append(query)

Expand All @@ -185,24 +175,17 @@ def create(self, engine, comment):

return True

def update(self, engine, comment):
# There's no way to change neither a comment
# nor the engine now, so just inform the users
# they have to recreate the DB in order to change them
def update(self, engine):
# There's no way to change the engine
# so just inform the users they have to recreate
# the DB in order to change them
if engine and engine != self.engine:
msg = ("The provided engine '%s' is different from "
"the current one '%s'. It is NOT possible to "
"change it. The recreation of the database is required "
"in order to change it." % (engine, self.engine))
self.module.warn(msg)

if comment and comment != self.comment:
msg = ("The provided comment '%s' is different from "
"the current one '%s'. It is NOT possible to "
"change it. The recreation of the database is required "
"in order to change it." % (comment, self.comment))
self.module.warn(msg)

return False

def drop(self):
Expand All @@ -222,10 +205,9 @@ def main():
# and invoke here to return a dict with those arguments
argument_spec = client_common_argument_spec()
argument_spec.update(
state=dict(type='str', choices=['present', 'absent'], required=True),
state=dict(type='str', choices=['present', 'absent'], default='present'),
name=dict(type='str', required=True),
engine=dict(type='str', default=None),
comment=dict(type='str', default=None),
)

# Instantiate an object of module class
Expand All @@ -244,7 +226,6 @@ def main():
state = module.params['state']
name = module.params['name']
engine = module.params['engine']
comment = module.params['comment']

# Will fail if no driver informing the user
check_driver(module, HAS_DB_DRIVER)
Expand All @@ -258,14 +239,15 @@ def main():

if state == 'present':
if not database.exists:
changed = database.create(engine, comment)
changed = database.create(engine)
else:
# If database exists
changed = database.update(engine, comment)
changed = database.update(engine)

else:
# If state is absent
changed = database.drop()
if database.exists:
changed = database.drop()

# Close connection
client.disconnect_connection()
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/clickhouse_db/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_clickhouse
224 changes: 224 additions & 0 deletions tests/integration/targets/clickhouse_db/tasks/initial.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################

# Test
- name: Create database in check mode
register: result
check_mode: true
community.clickhouse.clickhouse_db:
state: present
name: test_db

- name: Check ret values
ansible.builtin.assert:
that:
- result is changed
- result.executed_statements == ['CREATE DATABASE test_db']

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT name FROM system.databases WHERE name = 'test_db'"

- name: Check the DB is not there
ansible.builtin.assert:
that:
- result.result == []

# Test
- name: Create database in check mode with engine
register: result
check_mode: true
community.clickhouse.clickhouse_db:
state: present
name: test_db
engine: Memory

- name: Check ret values
ansible.builtin.assert:
that:
- result is changed
- result.executed_statements == ["CREATE DATABASE test_db ENGINE = Memory"]

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT name FROM system.databases WHERE name = 'test_db'"

- name: Check the DB is not there
ansible.builtin.assert:
that:
- result.result == []

# Test
- name: Create database with engine in real mode
register: result
community.clickhouse.clickhouse_db:
name: test_db
engine: Memory

- name: Check ret values
ansible.builtin.assert:
that:
- result is changed
- result.executed_statements == ["CREATE DATABASE test_db ENGINE = Memory"]

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT engine FROM system.databases WHERE name = 'test_db'"

- name: Check the DB is there
ansible.builtin.assert:
that:
- result.result == [["Memory"]]

# Test
- name: Create database that already exists
register: result
community.clickhouse.clickhouse_db:
name: test_db
engine: Memory

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []

# Test
- name: Create database that already exists in check mode
register: result
check_mode: true
community.clickhouse.clickhouse_db:
name: test_db
engine: Memory

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []

# Test
- name: Create database that already exists with a different engine in check mode
register: result
check_mode: true
community.clickhouse.clickhouse_db:
name: test_db
engine: Atomic

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT engine FROM system.databases WHERE name = 'test_db'"

- name: Check the attributes are the same
ansible.builtin.assert:
that:
- result.result == [["Memory"]]

# Test
- name: Create database that already exists with a different engine in real mode
register: result
community.clickhouse.clickhouse_db:
name: test_db
engine: Atomic

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT engine FROM system.databases WHERE name = 'test_db'"

- name: Check the attributes are the same
ansible.builtin.assert:
that:
- result.result == [["Memory"]]

# Test
- name: Drop database in check mode
register: result
check_mode: true
community.clickhouse.clickhouse_db:
name: test_db
state: absent

- name: Check ret values
ansible.builtin.assert:
that:
- result is changed
- result.executed_statements == ["DROP DATABASE test_db"]

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT engine FROM system.databases WHERE name = 'test_db'"

- name: Check the DB is there
ansible.builtin.assert:
that:
- result.result == [["Memory"]]

# Test
- name: Drop database
register: result
community.clickhouse.clickhouse_db:
name: test_db
state: absent

- name: Check ret values
ansible.builtin.assert:
that:
- result is changed
- result.executed_statements == ["DROP DATABASE test_db"]

- name: Check the actual state
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT engine FROM system.databases WHERE name = 'test_db'"

- name: Check the DB is there
ansible.builtin.assert:
that:
- result.result == []

# Test
- name: Drop non-existing database
register: result
community.clickhouse.clickhouse_db:
name: test_db
state: absent

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []

# Test
- name: Drop non-existing database in check mode
register: result
check_mode: true
community.clickhouse.clickhouse_db:
name: test_db
state: absent

- name: Check ret values
ansible.builtin.assert:
that:
- result is not changed
- result.executed_statements == []
7 changes: 7 additions & 0 deletions tests/integration/targets/clickhouse_db/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################

# Initial CI tests of clickhouse_db module
- import_tasks: initial.yml

0 comments on commit b71c6d1

Please sign in to comment.