410
403
self.assertIsNone(path)
413
class TestLegacyJuju(unittest.TestCase):
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)
423
# Restore the original utils.CURRENT_DIR.
424
utils.CURRENT_DIR = self.original_current_dir
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())
432
def test_pyjuju(self):
433
# If the agent file does not exist this is a PyJuju environment.
434
self.assertTrue(legacy_juju())
437
406
def make_collection(attr, values):
438
407
"""Create a collection of objects having an attribute named *attr*.
632
601
self.assertEqual('0.1.0.xz', name)
635
class TestGetZookeeperAddress(unittest.TestCase):
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)
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)
651
604
class TestLogHook(unittest.TestCase):
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
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])
905
def test_stop_improv(self):
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])
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])
921
def test_stop_agent(self):
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])
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)
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)
994
908
'--testsroot="{}/test/"'.format(JUJU_GUI_DIR), guiserver_conf)
995
909
self.assertIn('--insecure', guiserver_conf)
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)
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)
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
1059
False, 'This is login help.', True, True, self.charmworld_url,
1060
self.build_dir, config_js_path='config',
1062
js_conf = self.files['config']
1063
self.assertIn('user: "admin"', js_conf)
1064
self.assertIn('password: "kumquat"', js_conf)
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
1070
False, 'This is login help.', True, True, self.charmworld_url,
969
def test_write_gui_config_default_sandbox_backend(self):
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
975
# Because this is sandbox, the apiBackend is always go.
1076
976
self.assertIn('apiBackend: "go"', js_conf)
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)
1103
1001
def test_write_gui_config_with_button(self):