Skip to content

Commit

Permalink
[postgresql] Revamp the test suite
Browse files Browse the repository at this point in the history
* Use the `AgentCheckTest` class which makes the code much more readable
and enjoyable for edge cases.
* Factorize the SQL commands into SQL files that are input in psql
commands
* Minor logging facelift of the common test class
  • Loading branch information
LeoCavaille committed Mar 20, 2015
1 parent 6bb6867 commit 89d1b81
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 137 deletions.
21 changes: 4 additions & 17 deletions ci/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,13 @@ def pg_rootdir
sleep_for 5
sh %(#{pg_rootdir}/bin/psql\
-p 15432 -U $USER\
-c "CREATE USER datadog WITH PASSWORD 'datadog'"\
postgres)
postgres < $TRAVIS_BUILD_DIR/ci/resources/postgres/postgres.sql)
sh %(#{pg_rootdir}/bin/psql\
-p 15432 -U $USER\
-c "GRANT SELECT ON pg_stat_database TO datadog"\
postgres)
sh %(#{pg_rootdir}/bin/psql\
-p 15432 -U $USER\
-c "CREATE DATABASE datadog_test"\
postgres)
sh %(#{pg_rootdir}/bin/psql\
-p 15432 -U $USER\
-c "GRANT ALL PRIVILEGES ON DATABASE datadog_test TO datadog"\
postgres)
-p 15432 -U datadog\
datadog_test < $TRAVIS_BUILD_DIR/ci/resources/postgres/datadog_test.sql)
sh %(#{pg_rootdir}/bin/psql\
-p 15432 -U datadog\
-c "CREATE TABLE persons (personid INT, lastname VARCHAR(255), firstname VARCHAR(255), address VARCHAR(255), city VARCHAR(255))"\
datadog_test)
# For pg_stat_user_table to return stuff
sleep_for 5
dogs < $TRAVIS_BUILD_DIR/ci/resources/postgres/dogs.sql)
end

task :script => ['ci:common:script'] do
Expand Down
5 changes: 5 additions & 0 deletions ci/resources/postgres/datadog_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE persons (personid SERIAL, lastname VARCHAR(255), firstname VARCHAR(255), address VARCHAR(255), city VARCHAR(255));
INSERT INTO persons (lastname, firstname, address, city) VALUES ('Cavaille', 'Leo', 'Midtown', 'New York'), ('Someveryveryveryveryveryveryveryveryveryverylongname', 'something', 'Avenue des Champs Elysees', 'Beautiful city of lights');
SELECT * FROM persons;
SELECT * FROM persons;
SELECT * FROM persons;
7 changes: 7 additions & 0 deletions ci/resources/postgres/dogs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE breed (id SERIAL, name VARCHAR(255));
CREATE TABLE kennel (id SERIAL, address VARCHAR(255));
INSERT INTO kennel (address) VALUES ('Midtown, New York'), ('Boston');
SELECT * FROM kennel;
CREATE INDEX breed_names ON breed(name);
INSERT INTO breed (name) VALUES ('Labrador Retriver'), ('German Shepherd'), ('Yorkshire Terrier'), ('Golden Retriever'), ('Bulldog');
SELECT * FROM breed WHERE name = 'Labrador';
5 changes: 5 additions & 0 deletions ci/resources/postgres/postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE USER datadog WITH PASSWORD 'datadog';
GRANT SELECT ON pg_stat_database TO datadog;
CREATE DATABASE datadog_test;
GRANT ALL PRIVILEGES ON DATABASE datadog_test TO datadog;
CREATE DATABASE dogs;
8 changes: 5 additions & 3 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def assertMetric(self, metric_name, value=None, tags=None, count=None, at_least=
log.debug(" * tagged with {0}".format(tags))
if count is not None:
log.debug(" * should have exactly {0} data points".format(count))
if at_least is not None:
elif at_least is not None:
log.debug(" * should have at least {0} data points".format(at_least))

candidates = []
Expand All @@ -270,7 +270,7 @@ def assertMetricTagPrefix(self, metric_name, tag_prefix, count=None, at_least=1)
log.debug("Looking for a tag starting with `{0}:` on metric {1}".format(tag_prefix, metric_name))
if count is not None:
log.debug(" * should have exactly {0} data points".format(count))
if at_least is not None:
elif at_least is not None:
log.debug(" * should have at least {0} data points".format(at_least))

candidates = []
Expand All @@ -292,7 +292,7 @@ def assertMetricTag(self, metric_name, tag, count=None, at_least=1):
log.debug("Looking for tag {0} on metric {1}".format(tag, metric_name))
if count is not None:
log.debug(" * should have exactly {0} data points".format(count))
if at_least is not None:
elif at_least is not None:
log.debug(" * should have at least {0} data points".format(at_least))

candidates = []
Expand All @@ -318,6 +318,8 @@ def assertServiceCheck(self, service_check_name, status=None, tags=None, count=N
log.debug(" * tagged with {0}".format(tags))
if count is not None:
log.debug(" * should have exactly {0} statuses".format(count))
elif at_least is not None:
log.debug(" * should have at least {0} statuses".format(count))
candidates = []
for sc in self.service_checks:
if sc['check'] == service_check_name:
Expand Down

0 comments on commit 89d1b81

Please sign in to comment.