~rharding/charms/precise/juju-gui/update-nagios

« back to all changes in this revision

Viewing changes to tests/test_utils.py

  • Committer: Rick Harding
  • Date: 2014-01-15 14:50:46 UTC
  • mfrom: (147.1.8 remove-pyjuju)
  • Revision ID: rick.harding@canonical.com-20140115145046-lyh9879k9x8hwb8i
Remove support for PyJuju and rapi from charm.

- Removes the support for running rapi/pyjuju.
- Removes the agent used to communicate with zookeeper.
- Removes anything pertaining to zookeeper.
- Attempts to clean up docs and such to make sense with pure juju-core.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from subprocess import CalledProcessError
24
24
import tempfile
25
25
import unittest
 
26
import yaml
26
27
 
27
28
import charmhelpers
28
29
import mock
29
30
from shelltoolbox import environ
30
31
import tempita
31
 
import yaml
32
32
 
33
33
from utils import (
34
 
    API_PORT,
35
34
    JUJU_GUI_DIR,
36
35
    JUJU_PEM,
37
36
    WEB_PORT,
45
44
    get_launchpad_release,
46
45
    get_npm_cache_archive_url,
47
46
    get_release_file_path,
48
 
    get_zookeeper_address,
49
47
    install_builtin_server,
50
48
    install_missing_packages,
51
 
    legacy_juju,
52
49
    log_hook,
53
50
    parse_source,
54
51
    remove_apache_setup,
57
54
    save_or_create_certificates,
58
55
    setup_apache_config,
59
56
    setup_haproxy_config,
60
 
    start_agent,
61
57
    start_builtin_server,
62
58
    start_haproxy_apache,
63
 
    start_improv,
64
 
    stop_agent,
65
59
    stop_builtin_server,
66
60
    stop_haproxy_apache,
67
 
    stop_improv,
68
61
    write_builtin_server_startup,
69
62
    write_gui_config,
70
63
)
410
403
        self.assertIsNone(path)
411
404
 
412
405
 
413
 
class TestLegacyJuju(unittest.TestCase):
414
 
 
415
 
    def setUp(self):
416
 
        self.base_dir = tempfile.mkdtemp()
417
 
        self.addCleanup(shutil.rmtree, self.base_dir)
418
 
        # Monkey patch utils.CURRENT_DIR.
419
 
        self.original_current_dir = utils.CURRENT_DIR
420
 
        utils.CURRENT_DIR = tempfile.mkdtemp(dir=self.base_dir)
421
 
 
422
 
    def tearDown(self):
423
 
        # Restore the original utils.CURRENT_DIR.
424
 
        utils.CURRENT_DIR = self.original_current_dir
425
 
 
426
 
    def test_jujucore(self):
427
 
        # If the agent file is found this is a juju-core environment.
428
 
        agent_path = os.path.join(self.base_dir, 'agent.conf')
429
 
        open(agent_path, 'w').close()
430
 
        self.assertFalse(legacy_juju())
431
 
 
432
 
    def test_pyjuju(self):
433
 
        # If the agent file does not exist this is a PyJuju environment.
434
 
        self.assertTrue(legacy_juju())
435
 
 
436
 
 
437
406
def make_collection(attr, values):
438
407
    """Create a collection of objects having an attribute named *attr*.
439
408
 
632
601
        self.assertEqual('0.1.0.xz', name)
633
602
 
634
603
 
635
 
class TestGetZookeeperAddress(unittest.TestCase):
636
 
 
637
 
    def setUp(self):
638
 
        self.zookeeper_address = 'example.com:2000'
639
 
        contents = 'env JUJU_ZOOKEEPER="{}"\n'.format(self.zookeeper_address)
640
 
        with tempfile.NamedTemporaryFile(delete=False) as agent_file:
641
 
            agent_file.write(contents)
642
 
            self.agent_file_path = agent_file.name
643
 
        self.addCleanup(os.remove, self.agent_file_path)
644
 
 
645
 
    def test_get_zookeeper_address(self):
646
 
        # Ensure the Zookeeper address is correctly retreived.
647
 
        address = get_zookeeper_address(self.agent_file_path)
648
 
        self.assertEqual(self.zookeeper_address, address)
649
 
 
650
 
 
651
604
class TestLogHook(unittest.TestCase):
652
605
 
653
606
    def setUp(self):
853
806
        def su(user):
854
807
            yield None
855
808
 
856
 
        def get_zookeeper_address_mock(fp):
857
 
            return self.fake_zk_address
858
 
 
859
809
        self.files = {}
860
810
        orig_rtf = utils.render_to_file
861
811
 
872
822
            run=(utils.run, run),
873
823
            unit_get=(utils.unit_get, noop),
874
824
            render_to_file=(utils.render_to_file, render_to_file),
875
 
            get_zookeeper_address=(
876
 
                utils.get_zookeeper_address, get_zookeeper_address_mock),
877
825
            get_api_address=(utils.get_api_address, noop),
878
826
            APACHE_PORTS=(utils.APACHE_PORTS, 'PORTS_NOT_THERE'),
879
827
            APACHE_SITE=(utils.APACHE_SITE, 'SITE_NOT_THERE'),
891
839
            setattr(utils, fn, fcns[0])
892
840
        shutil.copy = self.shutil_copy
893
841
 
894
 
    def test_start_improv(self):
895
 
        staging_env = 'large'
896
 
        start_improv(staging_env, self.ssl_cert_path,)
897
 
        conf = self.files['juju-api-improv.conf']
898
 
        self.assertTrue('--port %s' % API_PORT in conf)
899
 
        self.assertTrue(staging_env + '.json' in conf)
900
 
        self.assertTrue(self.ssl_cert_path in conf)
901
 
        self.assertEqual(self.svc_ctl_call_count, 1)
902
 
        self.assertEqual(self.service_names, ['juju-api-improv'])
903
 
        self.assertEqual(self.actions, [charmhelpers.START])
904
 
 
905
 
    def test_stop_improv(self):
906
 
        stop_improv()
907
 
        self.assertEqual(self.svc_ctl_call_count, 1)
908
 
        self.assertEqual(self.service_names, ['juju-api-improv'])
909
 
        self.assertEqual(self.actions, [charmhelpers.STOP])
910
 
 
911
 
    def test_start_agent(self):
912
 
        start_agent(self.ssl_cert_path, 'config')
913
 
        conf = self.files['juju-api-agent.conf']
914
 
        self.assertTrue('--port %s' % API_PORT in conf)
915
 
        self.assertTrue('JUJU_ZOOKEEPER=%s' % self.fake_zk_address in conf)
916
 
        self.assertTrue(self.ssl_cert_path in conf)
917
 
        self.assertEqual(self.svc_ctl_call_count, 1)
918
 
        self.assertEqual(self.service_names, ['juju-api-agent'])
919
 
        self.assertEqual(self.actions, [charmhelpers.START])
920
 
 
921
 
    def test_stop_agent(self):
922
 
        stop_agent()
923
 
        self.assertEqual(self.svc_ctl_call_count, 1)
924
 
        self.assertEqual(self.service_names, ['juju-api-agent'])
925
 
        self.assertEqual(self.actions, [charmhelpers.STOP])
926
 
 
927
842
    def test_compute_build_dir(self):
928
843
        for (juju_gui_debug, serve_tests, result) in (
929
844
            (False, False, 'build-prod'),
941
856
        haproxy_conf = self.files['haproxy.cfg']
942
857
        self.assertIn('ca-base {}'.format(self.ssl_cert_path), haproxy_conf)
943
858
        self.assertIn('crt-base {}'.format(self.ssl_cert_path), haproxy_conf)
944
 
        self.assertIn('ws1 127.0.0.1:{}'.format(API_PORT), haproxy_conf)
945
859
        self.assertIn('web1 127.0.0.1:{}'.format(WEB_PORT), haproxy_conf)
946
 
        self.assertIn('ca-file {}'.format(JUJU_PEM), haproxy_conf)
947
860
        self.assertIn('crt {}'.format(JUJU_PEM), haproxy_conf)
948
861
        self.assertIn('redirect scheme https', haproxy_conf)
949
862
 
988
901
        guiserver_conf = self.files['guiserver.conf']
989
902
        self.assertIn('description "GUIServer"', guiserver_conf)
990
903
        self.assertIn('--logging="info"', guiserver_conf)
991
 
        self.assertIn('--apiurl="wss://127.0.0.1:8080/ws"', guiserver_conf)
992
 
        self.assertIn('--apiversion="python"', guiserver_conf)
 
904
        # The get_api_address is noop'd in these tests so the addr is None.
 
905
        self.assertIn('--apiurl="wss://None"', guiserver_conf)
 
906
        self.assertIn('--apiversion="go"', guiserver_conf)
993
907
        self.assertIn(
994
908
            '--testsroot="{}/test/"'.format(JUJU_GUI_DIR), guiserver_conf)
995
909
        self.assertIn('--insecure', guiserver_conf)
1029
943
 
1030
944
    def test_write_gui_config(self):
1031
945
        write_gui_config(
1032
 
            False, 'This is login help.', True, True, self.charmworld_url,
 
946
            False, 'This is login help.', True, self.charmworld_url,
1033
947
            self.build_dir, config_js_path='config',
1034
948
            ga_key='UA-123456')
1035
949
        js_conf = self.files['config']
1036
950
        self.assertIn('consoleEnabled: false', js_conf)
1037
 
        self.assertIn('user: "admin"', js_conf)
1038
 
        self.assertIn('password: "admin"', js_conf)
 
951
        self.assertIn('user: "user-admin"', js_conf)
 
952
        self.assertIn('password: null', js_conf)
1039
953
        self.assertIn('login_help: "This is login help."', js_conf)
1040
954
        self.assertIn('readOnly: true', js_conf)
1041
955
        self.assertIn("socket_url: 'wss://", js_conf)
1046
960
 
1047
961
    def test_write_gui_config_insecure(self):
1048
962
        write_gui_config(
1049
 
            False, 'This is login help.', True, True, self.charmworld_url,
 
963
            False, 'This is login help.', True, self.charmworld_url,
1050
964
            self.build_dir, secure=False, config_js_path='config')
1051
965
        js_conf = self.files['config']
1052
966
        self.assertIn("socket_url: 'ws://", js_conf)
1053
967
        self.assertIn('socket_protocol: "ws"', js_conf)
1054
968
 
1055
 
    @mock.patch('utils.legacy_juju')
1056
 
    def test_write_gui_config_default_python_password(self, mock_legacy_juju):
1057
 
        mock_legacy_juju.return_value = True
1058
 
        write_gui_config(
1059
 
            False, 'This is login help.', True, True, self.charmworld_url,
1060
 
            self.build_dir, config_js_path='config',
1061
 
            password='kumquat')
1062
 
        js_conf = self.files['config']
1063
 
        self.assertIn('user: "admin"', js_conf)
1064
 
        self.assertIn('password: "kumquat"', js_conf)
1065
 
 
1066
 
    @mock.patch('utils.legacy_juju')
1067
 
    def test_write_gui_config_default_sandbox_backend(self, mock_legacy_juju):
1068
 
        mock_legacy_juju.return_value = True
1069
 
        write_gui_config(
1070
 
            False, 'This is login help.', True, True, self.charmworld_url,
 
969
    def test_write_gui_config_default_sandbox_backend(self):
 
970
        write_gui_config(
 
971
            False, 'This is login help.', True, self.charmworld_url,
1071
972
            self.build_dir, config_js_path='config',
1072
973
            password='kumquat', sandbox=True)
1073
974
        js_conf = self.files['config']
1074
 
        # Because this is sandbox, the apiBackend is always go, even though it
1075
 
        # is legacy_juju.
 
975
        # Because this is sandbox, the apiBackend is always go.
1076
976
        self.assertIn('apiBackend: "go"', js_conf)
1077
977
 
1078
 
    @mock.patch('utils.legacy_juju')
1079
 
    def test_write_gui_config_default_go_password(self, mock_legacy_juju):
1080
 
        mock_legacy_juju.return_value = False
 
978
    def test_write_gui_config_default_go_password(self):
1081
979
        write_gui_config(
1082
980
            False, 'This is login help.', True, True, self.charmworld_url,
1083
981
            self.build_dir, config_js_path='config',
1097
995
            self.build_dir, sandbox=True, config_js_path='config')
1098
996
        js_conf = self.files['config']
1099
997
        self.assertIn('sandbox: true', js_conf)
1100
 
        self.assertIn('user: "admin"', js_conf)
 
998
        self.assertIn('user: "user-admin"', js_conf)
1101
999
        self.assertIn('password: "admin"', js_conf)
1102
1000
 
1103
1001
    def test_write_gui_config_with_button(self):