Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
include:
- stage: test
script:
- make test
- make check
- stage: test-doc
script:
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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

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
8 changes: 4 additions & 4 deletions library/grafana_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
Expand All @@ -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']

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ansible==2.6
pycodestyle==2.3.1
pytest==3.4.2
12 changes: 6 additions & 6 deletions test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down
77 changes: 77 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -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