aodh/ceilometer/tests/test_bin.py
Zhongyue Luo f3e03442a7 Utilizes assertIsNone and assertIsNotNone
Using assertEqual/assertNotEqual to test if an element
is or is not None is too python2.4.
Our unit testing framework supports assertIsNone and assertIsNotNone
which were created for these types of tests.

Change-Id: Ieba2d4ce5602885dc8a33a938a40f3086879b2c3
2014-01-03 13:04:52 +08:00

136 lines
4.9 KiB
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright © 2012 eNovance <licensing@enovance.com>
#
# Author: Julien Danjou <julien@danjou.info>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import os
import random
import socket
import subprocess
import time
import httplib2
from ceilometer.openstack.common import fileutils
from ceilometer.tests import base
class BinTestCase(base.BaseTestCase):
def setUp(self):
super(BinTestCase, self).setUp()
content = "[database]\n"\
"connection=log://localhost\n"
self.tempfile = fileutils.write_to_tempfile(content=content,
prefix='ceilometer',
suffix='.conf')
def tearDown(self):
super(BinTestCase, self).tearDown()
os.remove(self.tempfile)
def test_dbsync_run(self):
subp = subprocess.Popen(['ceilometer-dbsync',
"--config-file=%s" % self.tempfile])
self.assertEqual(subp.wait(), 0)
def test_run_expirer(self):
subp = subprocess.Popen(['ceilometer-expirer',
"--config-file=%s" % self.tempfile])
self.assertEqual(subp.wait(), 0)
class BinSendCounterTestCase(base.BaseTestCase):
def setUp(self):
super(BinSendCounterTestCase, self).setUp()
pipeline_cfg_file = self.path_get('etc/ceilometer/pipeline.yaml')
content = "[DEFAULT]\n"\
"rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\
"pipeline_cfg_file={0}\n".format(pipeline_cfg_file)
self.tempfile = fileutils.write_to_tempfile(content=content,
prefix='ceilometer',
suffix='.conf')
def tearDown(self):
super(BinSendCounterTestCase, self).tearDown()
os.remove(self.tempfile)
def test_send_counter_run(self):
subp = subprocess.Popen([self.path_get('bin/ceilometer-send-counter'),
"--config-file=%s" % self.tempfile,
"--counter-resource=someuuid",
"--counter-name=mycounter"])
self.assertEqual(subp.wait(), 0)
class BinApiTestCase(base.BaseTestCase):
def setUp(self):
super(BinApiTestCase, self).setUp()
self.api_port = random.randint(10000, 11000)
self.http = httplib2.Http()
pipeline_cfg_file = self.path_get('etc/ceilometer/pipeline.yaml')
policy_file = self.path_get('etc/ceilometer/policy.json')
content = "[DEFAULT]\n"\
"rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\
"auth_strategy=noauth\n"\
"debug=true\n"\
"pipeline_cfg_file={0}\n"\
"policy_file={1}\n"\
"[api]\n"\
"port={2}\n"\
"[database]\n"\
"connection=log://localhost\n".format(pipeline_cfg_file,
policy_file,
self.api_port)
self.tempfile = fileutils.write_to_tempfile(content=content,
prefix='ceilometer',
suffix='.conf')
self.subp = subprocess.Popen(['ceilometer-api',
"--config-file=%s" % self.tempfile])
def tearDown(self):
super(BinApiTestCase, self).tearDown()
self.subp.kill()
self.subp.wait()
os.remove(self.tempfile)
def get_response(self, path):
url = 'http://%s:%d/%s' % ('127.0.0.1', self.api_port, path)
for x in range(10):
try:
r, c = self.http.request(url, 'GET')
except socket.error:
time.sleep(.5)
self.assertIsNone(self.subp.poll())
else:
return r, c
return (None, None)
def test_v1(self):
response, content = self.get_response('v1/meters')
self.assertEqual(response.status, 200)
self.assertEqual(json.loads(content), {'meters': []})
def test_v2(self):
response, content = self.get_response('v2/meters')
self.assertEqual(response.status, 200)
self.assertEqual(json.loads(content), [])