Skip to content

Commit

Permalink
[etcd] add test, metrics and service checks
Browse files Browse the repository at this point in the history
Copied from the apache/riackcs tests, using the AgentCheckTest.
  • Loading branch information
degemer committed Feb 19, 2015
1 parent c3ae0e6 commit 3f537dc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ env:
- TRAVIS_FLAVOR=ssh
- TRAVIS_FLAVOR=fluentd
- TRAVIS_FLAVOR=rabbitmq
- TRAVIS_FLAVOR=etcd

# Override travis defaults with empty jobs
before_install: echo "OVERRIDING TRAVIS STEPS"
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require './ci/cassandra'
require './ci/couchdb'
require './ci/default'
require './ci/elasticsearch'
require './ci/etcd'
require './ci/fluentd'
require './ci/gearman'
require './ci/haproxy'
Expand Down
63 changes: 63 additions & 0 deletions ci/etcd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require './ci/common'

def etcd_version
ENV['ETCD_VERSION'] || '2.0.3'
end

def etcd_rootdir
"#{ENV['INTEGRATIONS_DIR']}/etcd_#{etcd_version}"
end

namespace :ci do
namespace :etcd do |flavor|
task :before_install => ['ci:common:before_install']

task :install => ['ci:common:install'] do
unless Dir.exist? File.expand_path(etcd_rootdir)
sh %(curl -s -L -o $VOLATILE_DIR/etcd.tar.gz\
https://github.com/coreos/etcd/releases/download/v#{etcd_version}/etcd-v#{etcd_version}-linux-amd64.tar.gz)
sh %(mkdir -p #{etcd_rootdir})
sh %(tar xzvf $VOLATILE_DIR/etcd.tar.gz\
-C #{etcd_rootdir}\
--strip-components=1 >/dev/null)
end
end

task :before_script => ['ci:common:before_script'] do
sh %(cd $VOLATILE_DIR && #{etcd_rootdir}/etcd >/dev/null &)
sleep_for 10
end

task :script => ['ci:common:script'] do
this_provides = [
'etcd'
]
Rake::Task['ci:common:run_tests'].invoke(this_provides)
end

task :cleanup => ['ci:common:cleanup'] do
# This will delete the temp directory of etcd,
# so the etcd process will kill himself quickly after that (<10s)
sh %(rm -rf $VOLATILE_DIR/*etcd*)
end

task :execute do
exception = nil
begin
%w(before_install install before_script script).each do |t|
Rake::Task["#{flavor.scope.path}:#{t}"].invoke
end
rescue => e
exception = e
puts "Failed task: #{e.class} #{e.message}".red
end
if ENV['SKIP_CLEANUP']
puts 'Skipping cleanup, disposable environments are great'.yellow
else
puts 'Cleaning up'
Rake::Task["#{flavor.scope.path}:cleanup"].invoke
end
fail exception if exception
end
end
end
43 changes: 43 additions & 0 deletions tests/test_etcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
from tests.common import AgentCheckTest
from nose.plugins.attrib import attr
from time import sleep
from checks import AgentCheck
from requests.exceptions import Timeout

@attr(requires='etcd')
class EtcdTest(AgentCheckTest):

CHECK_NAME = "etcd"
def __init__(self, *args, **kwargs):
AgentCheckTest.__init__(self, *args, **kwargs)
self.config = {"instances": [{"url": "http://localhost:4001"}]}

def test_metrics(self):
self.run_check(self.config)
sleep(1)
self.run_check(self.config)
tags = ['url:http://localhost:4001', 'etcd_state:leader']
self.assertMetric('etcd.store.gets.success', metric_value=0.0, tags=tags)
self.assertMetric('etcd.store.gets.fail', metric_value=0.0, tags=tags)
self.assertMetric('etcd.self.send.appendrequest.count', metric_value=0.0, tags=tags)


def test_service_checks(self):
self.run_check(self.config)

self.assertEqual(len(self.service_checks), 1, self.service_checks)
sc = self.service_checks[0]
self.assertEquals(sc["check"], self.check.SERVICE_CHECK_NAME, sc["check"])
self.assertEquals(sc["status"], AgentCheck.OK, sc["status"])
self.assertEquals(sc["tags"], ['url:http://localhost:4001', 'etcd_state:leader'], sc["tags"])

def test_bad_config(self):
self.assertRaises(Exception,
lambda: self.run_check({"instances": [{"url": "http://localhost:4001/test"}]}))
service_checks = self.check.get_service_checks()
self.assertEqual(len(service_checks), 1, service_checks)
sc = service_checks[0]
self.assertEquals(sc["check"], self.check.SERVICE_CHECK_NAME, sc["check"])
self.assertEquals(sc["status"], AgentCheck.CRITICAL, sc["status"])
self.assertEquals(sc["tags"], ['url:http://localhost:4001/test/v2/stats/self'], sc["tags"])

0 comments on commit 3f537dc

Please sign in to comment.