github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/tests/test_jujucharm.py (about) 1 """Tests for charm related classes and helpers.""" 2 3 import os 4 5 import yaml 6 7 from jujucharm import ( 8 Charm, 9 CharmCommand, 10 local_charm_path, 11 sane_charm_store_api_url, 12 ) 13 from tests import ( 14 temp_os_env, 15 TestCase, 16 ) 17 from utility import ( 18 JujuAssertionError, 19 temp_dir, 20 ) 21 22 23 class TestCharm(TestCase): 24 25 def test_create_default(self): 26 charm = Charm('test', 'a summary') 27 expected = { 28 'name': 'test', 29 'summary': 'a summary', 30 'series': ('bionic', 'xenial', 'trusty'), 31 'maintainer': 'juju-qa@lists.canonical.com', 32 'description': 'description', 33 } 34 self.assertEqual(charm.metadata, expected) 35 36 def test_default_series_default(self): 37 charm = Charm('test', 'a summary') 38 self.assertEqual(charm.default_series, 'bionic') 39 40 def test_default_series_unset(self): 41 charm = Charm('test', 'a summary') 42 del charm.metadata['series'] 43 self.assertEqual(charm.default_series, 'bionic') 44 45 def test_default_series_single(self): 46 charm = Charm('test', 'a summary', series='wily') 47 self.assertEqual(charm.default_series, 'wily') 48 49 def test_default_series_list(self): 50 charm = Charm('test', 'a summary', series=['trusty', 'xenial', 'bionic']) 51 self.assertEqual(charm.default_series, 'trusty') 52 53 def test_to_dir(self): 54 charm = Charm('test', 'a summary') 55 charm.metadata['description'] = 'a description' 56 del charm.metadata['maintainer'] 57 with temp_dir() as charm_dir: 58 charm.to_dir(charm_dir) 59 metafile = os.path.join(charm_dir, 'metadata.yaml') 60 with open(metafile) as f: 61 metadata = yaml.load(f) 62 expected = { 63 'name': 'test', 64 'summary': 'a summary', 65 'description': 'a description', 66 'series': ['bionic', 'xenial', 'trusty'], 67 } 68 self.assertEqual(metadata, expected) 69 70 def test_to_repo_dir(self): 71 charm = Charm('test', 'a summary', series='wily') 72 with temp_dir() as repo_dir: 73 charm.to_repo_dir(repo_dir) 74 metafile = os.path.join(repo_dir, 'wily', 'test', 'metadata.yaml') 75 with open(metafile) as f: 76 metadata = yaml.load(f) 77 expected = { 78 'name': 'test', 79 'summary': 'a summary', 80 'series': 'wily', 81 'maintainer': Charm.DEFAULT_MAINTAINER, 82 'description': Charm.DEFAULT_DESCRIPTION, 83 } 84 self.assertEqual(metadata, expected) 85 86 def test_add_hook_script(self): 87 charm = Charm('test', 'a summary') 88 config_changed = '#!/bin/sh\necho changed' 89 charm.add_hook_script('config-changed', config_changed) 90 with temp_dir() as charm_dir: 91 charm.to_dir(charm_dir) 92 hookfile = os.path.join(charm_dir, 'hooks', 'config-changed') 93 self.assertTrue(os.access(hookfile, os.X_OK)) 94 with open(hookfile) as f: 95 self.assertEqual(f.read(), config_changed) 96 97 def test_add_hook_multiple(self): 98 charm = Charm('test', 'a summary') 99 config_changed = '#!/bin/sh\necho changed' 100 upgrade_charm = '#!/bin/sh\necho upgraded' 101 charm.add_hook_script('config-changed', config_changed) 102 charm.add_hook_script('upgrade-charm', upgrade_charm) 103 with temp_dir() as charm_dir: 104 charm.to_dir(charm_dir) 105 changedfile = os.path.join(charm_dir, 'hooks', 'config-changed') 106 self.assertTrue(os.access(changedfile, os.X_OK)) 107 with open(changedfile) as f: 108 self.assertEqual(f.read(), config_changed) 109 upgradedfile = os.path.join(charm_dir, 'hooks', 'upgrade-charm') 110 self.assertTrue(os.access(upgradedfile, os.X_OK)) 111 with open(upgradedfile) as f: 112 self.assertEqual(f.read(), upgrade_charm) 113 114 def test_ensure_valid_name(self): 115 Charm('good-name', 'A charm with a valid name') 116 charm = Charm('BAD_NAME', 'A charm with a bad name', 117 ensure_valid_name=False) 118 self.assertIsNone(Charm.NAME_REGEX.match(charm.metadata['name'])) 119 self.assertRaisesRegexp( 120 JujuAssertionError, 121 'Invalid Juju Charm Name, "BAD_NAME" does not match ".*"\.', 122 Charm, 'BAD_NAME', 'A charm with a checked bad name') 123 124 def test_ensure_valid_name_anchoring(self): 125 for name in ['~bad-name', 'bad-name-!']: 126 self.assertRaises(JujuAssertionError, Charm, name, 127 'A charm with a partially correct name') 128 129 130 class TestLocalCharm(TestCase): 131 132 def test_make_local_charm_1x(self): 133 charm = 'mysql' 134 path = local_charm_path(charm, '1.25.0') 135 self.assertEqual(path, 'local:mysql') 136 137 def test_make_local_charm_1x_series(self): 138 charm = 'mysql' 139 path = local_charm_path(charm, '1.25.0', series='trusty') 140 self.assertEqual(path, 'local:trusty/mysql') 141 142 def test_make_local_charm_2x(self): 143 charm = 'mysql' 144 path = local_charm_path(charm, '2.0.0', repository='/tmp/charms') 145 self.assertEqual(path, '/tmp/charms/mysql') 146 147 def test_make_local_charm_2x_os_env(self): 148 charm = 'mysql' 149 with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'): 150 path = local_charm_path(charm, '2.0.0') 151 self.assertEqual(path, '/home/foo/repository/charms/mysql') 152 153 def test_make_local_charm_2x_win(self): 154 charm = 'mysql' 155 with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'): 156 path = local_charm_path(charm, '2.0.0', platform='win') 157 self.assertEqual(path, '/home/foo/repository/charms-win/mysql') 158 159 def test_make_local_charm_2x_centos(self): 160 charm = 'mysql' 161 with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'): 162 path = local_charm_path(charm, '2.0.0', platform='centos') 163 self.assertEqual(path, '/home/foo/repository/charms-centos/mysql') 164 165 166 class TestSaneCharmStoreApiUrl(TestCase): 167 168 def test_returns_default_value(self): 169 self.assertEqual( 170 sane_charm_store_api_url(None), 171 CharmCommand.default_api_url) 172 173 def test_appends_charmstore(self): 174 api = 'https://example.com' 175 expected = 'https://example.com/charmstore' 176 self.assertEqual( 177 sane_charm_store_api_url(api), 178 expected)