LEFT | RIGHT |
(no file at all) | |
| 1 from __future__ import print_function |
| 2 |
| 3 import unittest |
| 4 import sys |
| 5 from deploy_charm_for_testing import ( |
| 6 get_branch_url, |
| 7 get_state, |
| 8 main, |
| 9 wait_for_service, |
| 10 ) |
| 11 |
| 12 |
| 13 def noop(*args, **kws): |
| 14 pass |
| 15 |
| 16 |
| 17 class TestArgumentParsing(unittest.TestCase): |
| 18 """The get_branch_url function does simple argument parsing.""" |
| 19 |
| 20 def test_no_branch_given(self): |
| 21 # If there is no branch given, None is returned. |
| 22 branch = get_branch_url(['script-name']) |
| 23 self.assertIs(branch, None) |
| 24 |
| 25 def test_branch_given(self): |
| 26 # If a branch is given, the branch's URL is returned. |
| 27 branch = get_branch_url(['script-name', 'lp:foo']) |
| 28 self.assertEqual(branch, 'lp:foo') |
| 29 |
| 30 |
| 31 class TestWaitingForService(unittest.TestCase): |
| 32 """The wait_for_service function does what it says.""" |
| 33 |
| 34 def test_service_started(self): |
| 35 # If the service has already started, the function returns immediately. |
| 36 def get_service_state(): |
| 37 return 'started' |
| 38 # The fact that no exception is raised shows that no unexpected |
| 39 # sleeping was done. |
| 40 def sleep(seconds): |
| 41 raise RuntimeError('narcolepsy') |
| 42 wait_for_service(get_service_state, sleep) |
| 43 |
| 44 def test_sleep_if_service_not_started(self): |
| 45 # If the service has not started, the sleep function is called. |
| 46 def get_service_state(): |
| 47 return 'not started' |
| 48 class ZZZZ(Exception): |
| 49 pass |
| 50 def sleep(seconds): |
| 51 raise ZZZZ |
| 52 with self.assertRaises(ZZZZ): |
| 53 wait_for_service(get_service_state, sleep) |
| 54 |
| 55 def test_exception_on_error(self): |
| 56 # If the service is in an error state, an exception is raised. |
| 57 def get_service_state(): |
| 58 return 'error: unnecessary zebra' |
| 59 with self.assertRaises(Exception): |
| 60 wait_for_service(get_service_state) |
| 61 |
| 62 def test_returns_when_transitions_to_started(self): |
| 63 # get_service_state and sleep are called repeatedly until the service |
| 64 # is started. |
| 65 statuses = ['installing', 'pending', 'pending', 'started'] |
| 66 def get_service_state(): |
| 67 return statuses[0] |
| 68 def sleep(seconds): |
| 69 statuses.pop(0) |
| 70 wait_for_service(get_service_state, sleep) |
| 71 |
| 72 |
| 73 class TestParsingStatus(unittest.TestCase): |
| 74 """The Juju status is parsed from the output of --format json.""" |
| 75 |
| 76 def test_state_is_extracted(self): |
| 77 # The "get_state" function pulls the juju-gui service state out of the |
| 78 # JSON document produced by "juju status --format json". |
| 79 def get_status(): |
| 80 return ('{"services": {"juju-gui": {"units": {"juju-gui/0":' |
| 81 '{"agent-state": "started"}}}}}') |
| 82 self.assertEqual(get_state(get_status), 'started') |
| 83 |
| 84 |
| 85 class TestScript(unittest.TestCase): |
| 86 """The main() function is the entry point when run as a script.""" |
| 87 |
| 88 def test_status_messages_are_displayed(self): |
| 89 # While running, the script tells the user what is happening. |
| 90 printed = [] |
| 91 main(argv=[], print=printed.append, juju=noop, wait_for_service=noop) |
| 92 self.assertSequenceEqual( |
| 93 printed, |
| 94 ['Bootstrapping...', |
| 95 'Deploying service...', |
| 96 'Waiting for service to start...', |
| 97 'Exposing the service...']) |
| 98 |
| 99 def test_providing_a_branch(self): |
| 100 # If the user provides a branch name on the command line, it will be |
| 101 # passed to the charm. |
| 102 printed = [] |
| 103 juju_commands = [] |
| 104 def juju(s): |
| 105 juju_commands.append(s) |
| 106 main(argv=['', 'lp:foo'], print=printed.append, juju=juju, |
| 107 wait_for_service=noop) |
| 108 self.assertSequenceEqual( |
| 109 printed, |
| 110 ['Bootstrapping...', |
| 111 'Deploying service...', |
| 112 'Setting branch for charm to deploy...', |
| 113 'Waiting for service to start...', |
| 114 'Exposing the service...']) |
| 115 self.assertIn( |
| 116 ('set juju-gui juju-gui-source=lp:foo --environment juju-gui-testing
'), |
| 117 juju_commands) |
| 118 |
| 119 |
| 120 if __name__ == '__main__': |
| 121 unittest.main() |
LEFT | RIGHT |