~frankban/charms/precise/juju-gui/guiserver-deployer-initial

« back to all changes in this revision

Viewing changes to tests/test_utils.py

  • Committer: Nicola Larosa
  • Date: 2013-07-30 10:14:55 UTC
  • mfrom: (79.1.6 refactor-gui-startup)
  • Revision ID: nicola.larosa@canonical.com-20130730101455-muso2td9nwy2ybln
Refactor for easier builtin server integration.

Refactor the charm code to ease integration of the builtin server.
Detail of the changes, per file:

hooks/backend.py:

- order the mixins code as they are called, to ease readability;
- merge InstallMixin into GuiMixin;
- rename UpstartMixin to HaproxyApacheMixin;
- reimplement GuiMixin.install using the new functions from the split of
  hooks/utils.py#fetch_gui (see below);
- reimplement GuiMixin.start using the new functions from the split of
  hooks/utils.py#start_gui (see below);
- move here the chain and merge functions from hooks/utils.py, renaming
  them to be less generic;
- further simplify the backend constructor code.

hooks/utils.py:

- rename JUJU_GUI_SITE and JUJU_GUI_PORTS to APACHE_SITE and
  APACHE_PORTS respectively;
- split the start_gui function into compute_build_dir, write_gui_config
  and write_haproxy_config;
- split the fetch_gui function into fetch_gui_from_branch and
  fetch_gui_release;
- move the chain and merge functions to hooks/backend.py.

tests/test_backends.py:

- rename, add mocks and reorder to make tests pass again.

tests/test_utils.py:

- rename TestStartStop to TestStartImprovAgentGui;
- split the start_gui function tests in TestStartImprovAgentGui into
  tests for compute_build_dir, write_gui_config, write_haproxy_config
  and write_apache_config;

Sorry for the big diff. It was even bigger, but I moved some of the
changes to a future branch.

R=bac, gary.poster
CC=
https://codereview.appspot.com/12022044

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    WEB_PORT,
37
37
    _get_by_attr,
38
38
    cmd_log,
 
39
    compute_build_dir,
39
40
    first_path_in_dir,
40
41
    get_api_address,
41
42
    get_release_file_url,
47
48
    render_to_file,
48
49
    save_or_create_certificates,
49
50
    start_agent,
50
 
    start_gui,
51
51
    start_improv,
 
52
    write_apache_config,
 
53
    write_gui_config,
 
54
    write_haproxy_config,
52
55
)
53
56
# Import the whole utils package for monkey patching.
54
57
import utils
553
556
        self.assertTrue(line.endswith(': juju-gui@INFO \nfoo\n'))
554
557
 
555
558
 
556
 
class TestStartStop(unittest.TestCase):
 
559
class TestStartImprovAgentGui(unittest.TestCase):
557
560
 
558
561
    def setUp(self):
559
562
        self.service_names = []
560
563
        self.actions = []
561
564
        self.svc_ctl_call_count = 0
562
565
        self.fake_zk_address = '192.168.5.26'
 
566
        self.build_dir = 'juju-gui/build-'
 
567
        self.charmworld_url = 'http://charmworld.example'
 
568
 
563
569
        # Monkey patches.
564
570
        self.command = charmhelpers.command
565
571
 
630
636
        self.assertEqual(self.service_names, ['juju-api-agent'])
631
637
        self.assertEqual(self.actions, [charmhelpers.START])
632
638
 
633
 
    def test_start_gui(self):
634
 
        ssl_cert_path = '/tmp/certificates/'
635
 
        charmworld_url = 'http://charmworld.example'
636
 
        start_gui(
637
 
            False, 'This is login help.', True, True, ssl_cert_path,
638
 
            charmworld_url, True, haproxy_path='haproxy',
639
 
            config_js_path='config', use_analytics=True)
640
 
        haproxy_conf = self.files['haproxy']
641
 
        self.assertIn('ca-base {0}'.format(ssl_cert_path), haproxy_conf)
642
 
        self.assertIn('crt-base {0}'.format(ssl_cert_path), haproxy_conf)
643
 
        self.assertIn('ws1 127.0.0.1:{0}'.format(API_PORT), haproxy_conf)
644
 
        self.assertIn('web1 127.0.0.1:{0}'.format(WEB_PORT), haproxy_conf)
645
 
        self.assertIn('ca-file {0}'.format(JUJU_PEM), haproxy_conf)
646
 
        self.assertIn('crt {0}'.format(JUJU_PEM), haproxy_conf)
647
 
        self.assertIn('redirect scheme https', haproxy_conf)
 
639
    def test_compute_build_dir(self):
 
640
        for (in_staging, serve_tests, result) in (
 
641
            (False, False, 'build-prod'),
 
642
            (True, False, 'build-debug'),
 
643
            (False, True, 'build-prod'),
 
644
            (True, True, 'build-prod'),
 
645
        ):
 
646
            build_dir = compute_build_dir(in_staging, serve_tests)
 
647
            self.assertIn(
 
648
                result, build_dir, 'in_staging: {}, serve_tests: {}'.format(
 
649
                    in_staging, serve_tests))
 
650
 
 
651
    def test_write_gui_config(self):
 
652
        write_gui_config(
 
653
            False, 'This is login help.', True, True, self.charmworld_url,
 
654
            self.build_dir, use_analytics=True, config_js_path='config')
648
655
        js_conf = self.files['config']
649
656
        self.assertIn('consoleEnabled: false', js_conf)
650
657
        self.assertIn('user: "admin"', js_conf)
655
662
        self.assertIn('socket_protocol: "wss"', js_conf)
656
663
        self.assertIn('charmworldURL: "http://charmworld.example"', js_conf)
657
664
        self.assertIn('useAnalytics: true', js_conf)
 
665
 
 
666
    def test_write_haproxy_config(self):
 
667
        write_haproxy_config(self.ssl_cert_path, haproxy_path='haproxy')
 
668
        haproxy_conf = self.files['haproxy']
 
669
        self.assertIn('ca-base {0}'.format(self.ssl_cert_path), haproxy_conf)
 
670
        self.assertIn('crt-base {0}'.format(self.ssl_cert_path), haproxy_conf)
 
671
        self.assertIn('ws1 127.0.0.1:{0}'.format(API_PORT), haproxy_conf)
 
672
        self.assertIn('web1 127.0.0.1:{0}'.format(WEB_PORT), haproxy_conf)
 
673
        self.assertIn('ca-file {0}'.format(JUJU_PEM), haproxy_conf)
 
674
        self.assertIn('crt {0}'.format(JUJU_PEM), haproxy_conf)
 
675
        self.assertIn('redirect scheme https', haproxy_conf)
 
676
 
 
677
    def test_write_apache_config(self):
 
678
        write_apache_config(self.build_dir, serve_tests=True)
658
679
        apache_conf = self.files['juju-gui']
659
680
        self.assertIn('juju-gui/build-', apache_conf)
660
681
        self.assertIn('VirtualHost *:{0}'.format(WEB_PORT), apache_conf)
661
682
        self.assertIn(
662
683
            'Alias /test {0}/test/'.format(JUJU_GUI_DIR), apache_conf)
663
684
 
664
 
    def test_start_gui_insecure(self):
665
 
        ssl_cert_path = '/tmp/certificates/'
666
 
        charmworld_url = 'http://charmworld.example'
667
 
        start_gui(
668
 
            False, 'This is login help.', True, True, ssl_cert_path,
669
 
            charmworld_url, True, haproxy_path='haproxy',
670
 
            config_js_path='config', secure=False)
 
685
    def test_write_gui_config_insecure(self):
 
686
        write_gui_config(
 
687
            False, 'This is login help.', True, True, self.charmworld_url,
 
688
            self.build_dir, secure=False, config_js_path='config')
671
689
        js_conf = self.files['config']
672
690
        self.assertIn("socket_url: 'ws://", js_conf)
673
691
        self.assertIn('socket_protocol: "ws"', js_conf)
674
 
        haproxy_conf = self.files['haproxy']
 
692
 
 
693
    def test_write_haproxy_config_insecure(self):
 
694
        write_haproxy_config(
 
695
            self.ssl_cert_path, secure=False, haproxy_path='haproxy')
675
696
        # The insecure approach eliminates the https redirect.
676
 
        self.assertNotIn('redirect scheme https', haproxy_conf)
 
697
        self.assertNotIn('redirect scheme https', self.files['haproxy'])
677
698
 
678
 
    def test_start_gui_sandbox(self):
679
 
        ssl_cert_path = '/tmp/certificates/'
680
 
        charmworld_url = 'http://charmworld.example'
681
 
        start_gui(
682
 
            False, 'This is login help.', False, False, ssl_cert_path,
683
 
            charmworld_url, True, haproxy_path='haproxy',
684
 
            config_js_path='config', sandbox=True)
 
699
    def test_write_gui_config_sandbox(self):
 
700
        write_gui_config(
 
701
            False, 'This is login help.', False, False, self.charmworld_url,
 
702
            self.build_dir, sandbox=True, config_js_path='config')
685
703
        js_conf = self.files['config']
686
704
        self.assertIn('sandbox: true', js_conf)
687
705
        self.assertIn('user: "admin"', js_conf)
688
706
        self.assertIn('password: "admin"', js_conf)
689
707
 
690
 
    def test_start_gui_no_analytics(self):
691
 
        ssl_cert_path = '/tmp/certificates/'
692
 
        charmworld_url = 'http://charmworld.example'
693
 
        start_gui(
694
 
            False, 'This is login help.', False, False, ssl_cert_path,
695
 
            charmworld_url, True, haproxy_path='haproxy',
696
 
            config_js_path='config', use_analytics=False)
697
 
        js_conf = self.files['config']
698
 
        self.assertIn('useAnalytics: false', js_conf)
699
 
 
700
 
    def test_start_gui_fullscreen(self):
701
 
        ssl_cert_path = '/tmp/certificates/'
702
 
        charmworld_url = 'http://charmworld.example'
703
 
        start_gui(
704
 
            False, 'This is login help.', False, False, ssl_cert_path,
705
 
            charmworld_url, True, haproxy_path='haproxy',
706
 
            config_js_path='config', sandbox=True,
707
 
            default_viewmode='fullscreen')
708
 
        js_conf = self.files['config']
709
 
        self.assertIn('defaultViewmode: "fullscreen"', js_conf)
710
 
 
711
 
    def test_start_gui_with_button(self):
712
 
        ssl_cert_path = '/tmp/certificates/'
713
 
        charmworld_url = 'http://charmworld.example'
714
 
        start_gui(
715
 
            False, 'This is login help.', False, False, ssl_cert_path,
716
 
            charmworld_url, True, haproxy_path='haproxy',
717
 
            config_js_path='config', sandbox=True,
718
 
            show_get_juju_button=True)
719
 
        js_conf = self.files['config']
720
 
        self.assertIn('showGetJujuButton: true', js_conf)
 
708
    def test_write_gui_config_no_analytics(self):
 
709
        write_gui_config(
 
710
            False, 'This is login help.', False, False, self.charmworld_url,
 
711
            self.build_dir, use_analytics=False, config_js_path='config')
 
712
        self.assertIn('useAnalytics: false', self.files['config'])
 
713
 
 
714
    def test_write_gui_config_fullscreen(self):
 
715
        write_gui_config(
 
716
            False, 'This is login help.', False, False, self.charmworld_url,
 
717
            self.build_dir, sandbox=True, default_viewmode='fullscreen',
 
718
            config_js_path='config')
 
719
        self.assertIn('defaultViewmode: "fullscreen"', self.files['config'])
 
720
 
 
721
    def test_write_gui_config_with_button(self):
 
722
        write_gui_config(
 
723
            False, 'This is login help.', False, False, self.charmworld_url,
 
724
            self.build_dir, sandbox=True, show_get_juju_button=True,
 
725
            config_js_path='config')
 
726
        self.assertIn('showGetJujuButton: true', self.files['config'])
721
727
 
722
728
 
723
729
class TestNpmCache(unittest.TestCase):