Skip to content
This repository
Browse code

Add tests for svc_adapter

  • Loading branch information...
commit c7a31ffe030f156d4fa0405fb5d41daf0cb6f187 1 parent ea329f0
Patrick Armstrong oldpatricka authored
4 src/README
@@ -10,6 +10,10 @@ and run something like:
10 10
11 11 $ nosetests --with-coverage --cover-package=epumgmt.main.em_core_status
12 12
  13 +You may also want to avoid slow tests, to do this, use:
  14 +
  15 +$ nosetests -a '!slow'
  16 +
13 17 During (test driven) development this can be conveniently combined with:
14 18
15 19 $ ./sbin/cat-latest-logfile.sh
7 src/python/tests/mocks/common.py
... ... @@ -1,3 +1,5 @@
  1 +import sys
  2 +import traceback
1 3
2 4 class FakeLog():
3 5
@@ -16,6 +18,11 @@ def warn(self, msg):
16 18 def error(self, msg):
17 19 self.transcript.append(("ERROR", msg))
18 20
  21 + def exception(self, msg):
  22 + exc = sys.exc_info()
  23 + msg += "".join(traceback.format_exception(exc[0], exc[1], exc[2]))
  24 + self.transcript.append(("ERROR", msg))
  25 +
19 26 class FakeCommon():
20 27 """FakeCommon fakes the common object so we can check what
21 28 gets logged by the stuff that calls it
142 src/python/tests/test_epumgmt_defaults_svc_adapter.py
@@ -3,8 +3,10 @@
3 3 import types
4 4 import shutil
5 5 import tempfile
  6 +import datetime
6 7 import nose.tools
7 8 import ConfigParser
  9 +from nose.plugins.attrib import attr
8 10
9 11 import epumgmt.defaults.svc_adapter
10 12 import epumgmt.main.em_args as em_args
@@ -14,8 +16,10 @@
14 16 from epumgmt.api import RunVM
15 17 from epumgmt.defaults import DefaultParameters
16 18 from epumgmt.defaults import DefaultRunlogs
  19 +from epumgmt.defaults import DefaultRemoteSvcAdapter
17 20 from mocks.common import FakeCommon
18 21 from mocks.modules import FakeModules
  22 +from mocks.event import Event
19 23
20 24
21 25 @nose.tools.raises(ProgrammingError)
@@ -41,7 +45,6 @@ def test_check_init():
41 45 class TestDefaultRemoteSvcAdapter:
42 46
43 47 def setup(self):
44   - from epumgmt.defaults.svc_adapter import DefaultRemoteSvcAdapter
45 48
46 49 self.run_name = "TESTRUN"
47 50
@@ -218,6 +221,143 @@ def test_intake_query_result_from_file(self):
218 221 assert instance.heartbeat_state == heartbeat_state
219 222 assert instance.heartbeat_time == heartbeat_time
220 223
  224 + def test_intake_query_result(self):
  225 +
  226 + provisioner = RunVM()
  227 + provisioner.runlogdir = tempfile.mkdtemp()
  228 +
  229 + local_filename = "log.json"
  230 + remote_filename = "something"
  231 + local_abs_filepath = os.path.join(provisioner.runlogdir, local_filename)
  232 +
  233 + controller_name = "test-controller"
  234 + de_state = "STABLE_DE"
  235 + de_conf_report = "balala"
  236 + last_queuelen_size = 5
  237 + last_queuelen_time = 1234
  238 + instance_0 = "fashfjsahfjksa"
  239 + instance_0_state = "600-RUNNING"
  240 + iaas_state_time = 12312142
  241 + heartbeat_state = "SOMETHING"
  242 + heartbeat_time = 5
  243 +
  244 +
  245 + test_json = """
  246 + {"%s": {"de_state":"%s", "de_conf_report":"%s", "last_queuelen_size":%s,
  247 + "last_queuelen_time": %s,
  248 + "instances": {
  249 + "%s": {"iaas_state":"%s", "iaas_state_time": %s,
  250 + "heartbeat_state":"%s", "heartbeat_time": %s}}}}
  251 + """ % (controller_name, de_state, de_conf_report, last_queuelen_size,
  252 + last_queuelen_time, instance_0, instance_0_state, iaas_state_time,
  253 + heartbeat_state, heartbeat_time)
  254 +
  255 + with open(local_abs_filepath, "w") as j_file:
  256 + j_file.write(test_json)
  257 +
  258 + self.svc_adapter.initialize(self.m, self.run_name, self.cloudinitd)
  259 +
  260 + # Test detection of non-existant files
  261 + try:
  262 + fake_file = "not_a_file"
  263 + self.svc_adapter._intake_query_result(provisioner, fake_file, remote_filename)
  264 + raised_unexpected_error = False
  265 + except UnexpectedError:
  266 + raised_unexpected_error = True
  267 +
  268 + assert raised_unexpected_error
  269 +
  270 + # Test reading a file actually works
  271 + # all of the other values should be tested by test_intake_query_result_from_file
  272 + map = self.svc_adapter._intake_query_result(provisioner, local_filename, remote_filename)
  273 + assert map.has_key(controller_name)
  274 +
  275 +
  276 + def test_run_one_command(self):
  277 +
  278 + svc_adapter = DefaultRemoteSvcAdapter(self.p, self.c)
  279 +
  280 + command_to_succeed = "true"
  281 + succeeded = svc_adapter._run_one_cmd(command_to_succeed)
  282 + assert succeeded
  283 +
  284 +
  285 + non_existant_command = "a4f6d2f32r22c3c34c423c2f2g34"
  286 + succeeded = svc_adapter._run_one_cmd(non_existant_command)
  287 + assert not succeeded
  288 +
  289 +
  290 + command_to_fail = "cat fsdbjkfsdy89fsdy89fsfsdfsdfsd"
  291 + succeeded = svc_adapter._run_one_cmd(command_to_fail)
  292 + assert not succeeded
  293 +
  294 +
  295 + @attr("slow")
  296 + def test_run_one_command_timeout(self):
  297 + """
  298 + This tests _run_one_command's timeout feature, so you may want
  299 + to skip it when running these tests frequently.
  300 + """
  301 +
  302 + command_that_will_timeout = "sleep 60"
  303 + svc_adapter = DefaultRemoteSvcAdapter(self.p, self.c)
  304 + succeeded = svc_adapter._run_one_cmd(command_that_will_timeout)
  305 + assert not succeeded
  306 +
  307 +
  308 + def test_get_pathconfs(self):
  309 +
  310 + source = "something"
  311 + username = "fordprefect"
  312 +
  313 + self.svc_adapter.initialize(self.m, self.run_name, self.cloudinitd)
  314 +
  315 + abs_homedir, abs_envfile = self.svc_adapter._get_pathconfs(source, username)
  316 + print abs_homedir, abs_envfile
  317 + assert abs_homedir == "/home/%s/app" % username
  318 + assert abs_envfile == "/home/%s/app-venv/bin/activate" % username
  319 +
  320 + def test_get_provisioner(self):
  321 +
  322 + self.svc_adapter.initialize(self.m, self.run_name, self.cloudinitd)
  323 + provisioner = self.svc_adapter._get_provisioner()
  324 +
  325 + assert provisioner.name == "provisioner"
  326 +
  327 +
  328 + def test_latest_iaas_status(self):
  329 +
  330 + self.svc_adapter.initialize(self.m, self.run_name, self.cloudinitd)
  331 +
  332 + vm = RunVM()
  333 + vm.instanceid = "i-dfs3f32"
  334 +
  335 + try:
  336 + status = self.svc_adapter.latest_iaas_status(None)
  337 + raised_programming_error = False
  338 + except ProgrammingError:
  339 + raised_programming_error = True
  340 + assert raised_programming_error
  341 +
  342 +
  343 + # Test response when there's no status in VM object
  344 + status = self.svc_adapter.latest_iaas_status(vm)
  345 + assert not status
  346 +
  347 +
  348 + vm.events = []
  349 + vm_iaas_state_0 = "RUNNING"
  350 + vm_timestamp_0 = datetime.datetime(2000, 10, 3)
  351 + vm_iaas_state_1 = "SOMETHINGELSE"
  352 + vm_timestamp_1 = datetime.datetime(2000, 10, 5)
  353 + vm.events.append(Event(name="iaas_state", state=vm_iaas_state_0, timestamp=vm_timestamp_0))
  354 + vm.events.append(Event(name="iaas_state", state=vm_iaas_state_1, timestamp=vm_timestamp_1))
  355 +
  356 + status = self.svc_adapter.latest_iaas_status(vm)
  357 +
  358 + assert status == vm_iaas_state_1
  359 +
  360 +
221 361 def make_fake_run_one_cmd(target, real_run_one_cmd):
222 362 def fake_run_one_cmd(target, cmd):
223 363 cmd = "echo %s" % cmd

0 comments on commit c7a31ff

Please sign in to comment.
Something went wrong with that request. Please try again.