| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | ############################################################################ |
| 4 | # Copyright 2016 RIFT.io Inc # |
| 5 | # # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); # |
| 7 | # you may not use this file except in compliance with the License. # |
| 8 | # You may obtain a copy of the License at # |
| 9 | # # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 # |
| 11 | # # |
| 12 | # Unless required by applicable law or agreed to in writing, software # |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, # |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # |
| 15 | # See the License for the specific language governing permissions and # |
| 16 | # limitations under the License. # |
| 17 | ############################################################################ |
| 18 | |
| 19 | |
| 20 | import argparse |
| 21 | import asyncio |
| 22 | import logging |
| 23 | from unittest import mock |
| 24 | import os |
| 25 | import sys |
| 26 | import unittest |
| 27 | import xmlrunner |
| 28 | |
| 29 | import rift.mano.utils.juju_api as juju_api |
| 30 | |
| 31 | |
| 32 | class JujuClientTest(unittest.TestCase): |
| 33 | |
| 34 | log = None |
| 35 | |
| 36 | @classmethod |
| 37 | def set_logger(cls, log): |
| 38 | cls.log = log |
| 39 | |
| 40 | @asyncio.coroutine |
| 41 | def juju_client_test(self, mock_jujuclient, loop): |
| 42 | api = juju_api.JujuApi(secret='test', loop=loop, version=1) |
| 43 | |
| 44 | env = yield from api.get_env() |
| 45 | |
| 46 | self.assertTrue(env.login.called, |
| 47 | "Login to Juju not called") |
| 48 | env.login.assert_called_with('test', user='user-admin') |
| 49 | |
| 50 | charm = 'test-charm' |
| 51 | service = 'test-service' |
| 52 | yield from api.deploy_service(charm, service) |
| 53 | # self.assertTrue(env.deploy.called, |
| 54 | # "Deploy failed") |
| 55 | |
| 56 | config = { |
| 57 | 'test_param': 'test_value', |
| 58 | } |
| 59 | yield from api.apply_config(config, env=env) |
| 60 | self.assertTrue(env.set_config.called, |
| 61 | "Config failed") |
| 62 | |
| 63 | try: |
| 64 | yield from api.resolve_error(env=env) |
| 65 | except KeyError as e: |
| 66 | # Since the status does have values, this throws error |
| 67 | pass |
| 68 | # resolved method will not be called due to error above |
| 69 | self.assertFalse(env.resolved.called, |
| 70 | "Resolve error failed") |
| 71 | |
| 72 | action = 'test-action' |
| 73 | params = {} |
| 74 | api.units = ['test-service-0'] |
| 75 | # yield from api.execute_action(action, params, service=service, env=env) |
| 76 | |
| 77 | action_tag = 'test-123434352' |
| 78 | # yield from api.get_action_status(action_tag) |
| 79 | |
| 80 | api.destroy_retries = 2 |
| 81 | api.retry_delay = 0.1 |
| 82 | try: |
| 83 | yield from api.destroy_service() |
| 84 | |
| 85 | except Exception as e: |
| 86 | JujuClientTest.log.debug("Expected exception on destroy service: {}". |
| 87 | format(e)) |
| 88 | |
| 89 | self.assertTrue(env.destroy_service.called, |
| 90 | "Destroy failed") |
| 91 | |
| 92 | @mock.patch('rift.mano.utils.juju_api.Env1', autospec=True) |
| 93 | def test_client(self, mock_jujuclient): |
| 94 | loop = asyncio.get_event_loop() |
| 95 | |
| 96 | loop.run_until_complete(self.juju_client_test(mock_jujuclient, |
| 97 | loop)) |
| 98 | |
| 99 | loop.close() |
| 100 | |
| 101 | def main(argv=sys.argv[1:]): |
| 102 | logging.basicConfig(format='TEST %(message)s') |
| 103 | |
| 104 | runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"]) |
| 105 | parser = argparse.ArgumentParser() |
| 106 | parser.add_argument('-v', '--verbose', action='store_true') |
| 107 | parser.add_argument('-n', '--no-runner', action='store_true') |
| 108 | |
| 109 | args, unknown = parser.parse_known_args(argv) |
| 110 | if args.no_runner: |
| 111 | runner = None |
| 112 | |
| 113 | # Set the global logging level |
| 114 | log = logging.getLogger() |
| 115 | log.setLevel(logging.DEBUG if args.verbose else logging.ERROR) |
| 116 | JujuClientTest.set_logger(log) |
| 117 | |
| 118 | # The unittest framework requires a program name, so use the name of this |
| 119 | # file instead (we do not want to have to pass a fake program name to main |
| 120 | # when this is called from the interpreter). |
| 121 | unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner) |
| 122 | |
| 123 | if __name__ == '__main__': |
| 124 | main() |