diff --git a/.travis.yml b/.travis.yml index af7ead0..743014f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ jobs: include: - stage: test script: + - make test - make check - stage: test-doc script: diff --git a/Makefile b/Makefile index 9d46649..f8e4405 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +PYTHONPATH := $(shell pip show ansible | awk -F' ' '/Location/ {print $$2}') +BASEDIR := $(shell git rev-parse --show-toplevel) syntax: pycodestyle --ignore=E265 library/grafana_annotations.py @@ -5,5 +7,8 @@ syntax: container: tools/start_grafana.sh -check: container +check: container GRAFANA_API_TOKEN='$(shell python tools/get_or_create_token.py)' ansible-playbook test.yml + +test: + PYTHONPATH=$(PYTHONPATH) BASEDIR=$(BASEDIR) pytest diff --git a/library/grafana_annotations.py b/library/grafana_annotations.py index 95d8120..9c62b88 100644 --- a/library/grafana_annotations.py +++ b/library/grafana_annotations.py @@ -215,8 +215,8 @@ def main(): base_arg_spec = url_argument_spec() base_arg_spec.update( token=dict(required=False, default=None, no_log=True), - tstamp=dict(required=False, default=None), - end_tstamp=dict(required=False, default=None, type='int'), + time=dict(required=False, default=None), + timeEnd=dict(required=False, default=None, type='int'), tags=dict(required=False, default=[], type='list'), text=dict(required=True, type='str'), ) @@ -230,8 +230,8 @@ def main(): url_username = module.params['url_username'] url_password = module.params['url_password'] token = module.params['token'] - tstamp = module.params['tstamp'] - end_tstamp = module.params['end_tstamp'] + tstamp = module.params['time'] + end_tstamp = module.params['timeEnd'] tags = ["ansible"] + module.params['tags'] text = module.params['text'] diff --git a/requirements.txt b/requirements.txt index 54ece6a..2d03daf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ ansible==2.6 pycodestyle==2.3.1 +pytest==3.4.2 diff --git a/test.yml b/test.yml index 0f0bf99..3fc2548 100644 --- a/test.yml +++ b/test.yml @@ -41,7 +41,7 @@ url: "{{ grafana_url }}" url_username: "{{ grafana_login }}" url_password: "{{ grafana_password }}" - tstamp: "{{ curdate }}" + time: "{{ curdate }}" text: "This is an annotation explicitly set at {{ curdate }}" tags: - test-tag @@ -59,7 +59,7 @@ url: "{{ grafana_url }}" url_username: "{{ grafana_login }}" url_password: "{{ grafana_password }}" - tstamp: "{{ curdate }}" + time: "{{ curdate }}" text: "This is an annotation explicitly set at {{ curdate }}" tags: - test-tag @@ -83,8 +83,8 @@ url: "{{ grafana_url }}" url_username: "{{ grafana_login }}" url_password: "{{ grafana_password }}" - tstamp: "{{ start_date }}" - end_tstamp: "{{ end_date }}" + time: "{{ start_date }}" + timeEnd: "{{ end_date }}" text: "This is a global region annotation" register: anno tags: @@ -100,8 +100,8 @@ url: "{{ grafana_url }}" url_username: "{{ grafana_login }}" url_password: "{{ grafana_password }}" - tstamp: "{{ start_date }}" - end_tstamp: "{{ end_date }}" + time: "{{ start_date }}" + timeEnd: "{{ end_date }}" text: "This is a global region annotation" register: anno tags: diff --git a/tests/test_base.py b/tests/test_base.py new file mode 100644 index 0000000..bd62715 --- /dev/null +++ b/tests/test_base.py @@ -0,0 +1,77 @@ +import pytest +import os +import subprocess +import json +import time + + +class TestClass(object): + + # {{{ call module + def _run_module(self, json_args): + with open("/tmp/test.json", "w+") as jsonfile: + json.dump(json_args, jsonfile) + args = ["python", "%s/library/grafana_annotations.py" % os.getenv("BASEDIR"), "/tmp/test.json"] + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p.wait() + return p.returncode, p.stdout.read(), p.stderr.read() + # }}} + + def test_basic(self): + module_args = { + "ANSIBLE_MODULE_ARGS": { + "text": "This is pytest basic test without time value", + "url": "http://127.0.0.1:3000/api/annotations", + "url_password": "admin", + "url_username": "admin", + "_ansible_remote_tmp": "/tmp", + "_ansible_keep_remote_files": "false" + } + } + rc, stdout, stderr = self._run_module(module_args) + assert rc == 0, stderr + ouput_json = json.loads(stdout) + assert ouput_json.get("changed") is True + + def test_explicit_time(self): + current_time = int(time.time()) + module_args = { + "ANSIBLE_MODULE_ARGS": { + "text": "This is pytest basic test with explicit time value", + "url": "http://127.0.0.1:3000/api/annotations", + "url_password": "admin", + "url_username": "admin", + "time": str(current_time), + "_ansible_remote_tmp": "/tmp", + "_ansible_keep_remote_files": "false" + } + } + rc, stdout, stderr = self._run_module(module_args) + assert rc == 0, stderr + ouput_json = json.loads(stdout) + assert ouput_json.get("changed") is True, ouput_json + + def test_idempotency(self): + current_time = int(time.time()) + module_args = { + "ANSIBLE_MODULE_ARGS": { + "text": "This is pytest basic test for idempotency", + "url": "http://127.0.0.1:3000/api/annotations", + "url_password": "admin", + "url_username": "admin", + "time": str(current_time), + "_ansible_remote_tmp": "/tmp", + "_ansible_keep_remote_files": "false" + } + } + # {{{ send the annotation once + rc, stdout, stderr = self._run_module(module_args) + assert rc == 0, stderr + ouput_json = json.loads(stdout) + assert ouput_json.get("changed") is True, ouput_json + # }}} + # send the annotation again + rc, stdout, stderr = self._run_module(module_args) + assert rc == 0, stderr + ouput_json = json.loads(stdout) + assert ouput_json.get("changed") is False, ouput_json