~hatch/charms/precise/juju-gui/trunk

« back to all changes in this revision

Viewing changes to tests/test_utils.py

  • Committer: Francesco Banconi
  • Date: 2013-02-06 11:56:36 UTC
  • mfrom: (30.1.16 juju-gui)
  • Revision ID: francesco.banconi@canonical.com-20130206115636-eoy4r99x6claq0jo
Add an option to start test server.

Added the "serve-tests" option to the charm, 
defaulting to False. When the option is enabled 
it is possible to run the Juju GUI unit tests 
using https://[sevice url]/test/.

I needed to conditionally add the corresponding 
location to the nginx configuration file. 
I discussed with Gary about possible ways to 
achieve this. Using the standard Python string 
substitution seemed hacky, and the resulting 
template file could be less readable. We decided 
to start using a templating library: ended up with 
tempita because it is lightweight and it is already 
used in maas.

nginx is now configured so that the /test/ location:
- serves unit tests if "serve-tests" is True;
- redirects to / if "serve-tests" is False.
The latter is required because otherwise the test 
location would be added to the browser appcache: 
the index.html file would be served and it references 
the manifest file.

Also fixed dependency management in the install
hook. Now all the Python libraries required by the
charm are installed at the beginning of the process,
before utils, launchpadlib, tempita etc. are imported. 

QA:

- deploy the charm;
- juju set juju-gui serve-tests=true;
- go to "https://[sevice url]/test/";
- you should see tests run (mocha html reporter).

Problems:

test-debug (staging=true) fails to run because 
it attempts to load external (insecure) resources.
Filed bug #1116320.

R=gary.poster, teknico
CC=
https://codereview.appspot.com/7306045

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import shutil
6
6
from simplejson import dumps
7
7
import tempfile
 
8
import tempita
8
9
import unittest
9
10
 
10
11
import charmhelpers
15
16
    first_path_in_dir,
16
17
    get_release_file_url,
17
18
    get_zookeeper_address,
 
19
    JUJU_GUI_DIR,
18
20
    JUJU_PEM,
19
21
    parse_source,
20
22
    render_to_file,
298
300
    def setUp(self):
299
301
        self.destination_file = tempfile.NamedTemporaryFile()
300
302
        self.addCleanup(self.destination_file.close)
301
 
        self.template_contents = '%(foo)s, %(bar)s'
 
303
        self.template = tempita.Template('{{foo}}, {{bar}}')
302
304
        with tempfile.NamedTemporaryFile(delete=False) as template_file:
303
 
            template_file.write(self.template_contents)
 
305
            template_file.write(self.template.content)
304
306
            self.template_path = template_file.name
305
307
        self.addCleanup(os.remove, self.template_path)
306
308
 
308
310
        # Ensure the template is correctly rendered using the given context.
309
311
        context = {'foo': 'spam', 'bar': 'eggs'}
310
312
        render_to_file(self.template_path, context, self.destination_file.name)
311
 
        expected = self.template_contents % context
 
313
        expected = self.template.substitute(context)
312
314
        self.assertEqual(expected, self.destination_file.read())
313
315
 
314
316
 
439
441
        self.addCleanup(nginx_file.close)
440
442
        ssl_cert_path = '/tmp/certificates/'
441
443
        start_gui(
442
 
            False, 'This is login help.', True, True, ssl_cert_path,
 
444
            False, 'This is login help.', True, True, ssl_cert_path, True,
443
445
            haproxy_path=haproxy_file.name, nginx_path=nginx_file.name,
444
446
            config_js_path=config_js_file.name)
445
447
        self.assertEqual(self.svc_ctl_call_count, 2)
459
461
        self.assertIn('login_help: "This is login help."', js_conf)
460
462
        self.assertIn('readOnly: true', js_conf)
461
463
        nginx_conf = nginx_file.read()
462
 
        self.assertIn('juju-gui/build-debug', nginx_conf)
463
 
        self.assertIn('listen 127.0.0.1:{}'.format(WEB_PORT), nginx_conf)
 
464
        self.assertIn('juju-gui/build-', nginx_conf)
 
465
        self.assertIn('listen 127.0.0.1:{0}'.format(WEB_PORT), nginx_conf)
 
466
        self.assertIn('alias {0}/test/;'.format(JUJU_GUI_DIR), nginx_conf)
464
467
 
465
468
    def test_stop_staging(self):
466
469
        stop(True)