1
"""Tests for charm related classes and helpers."""
7
from jujucharm import (
11
sane_charm_store_api_url,
23
class TestCharm(TestCase):
25
def test_create_default(self):
26
charm = Charm('test', 'a summary')
29
'summary': 'a summary',
30
'series': ('xenial', 'trusty'),
31
'maintainer': 'juju-qa@lists.canonical.com',
32
'description': 'description',
34
self.assertEqual(charm.metadata, expected)
36
def test_default_series_default(self):
37
charm = Charm('test', 'a summary')
38
self.assertEqual(charm.default_series, 'xenial')
40
def test_default_series_unset(self):
41
charm = Charm('test', 'a summary')
42
del charm.metadata['series']
43
self.assertEqual(charm.default_series, 'xenial')
45
def test_default_series_single(self):
46
charm = Charm('test', 'a summary', series='wily')
47
self.assertEqual(charm.default_series, 'wily')
49
def test_default_series_list(self):
50
charm = Charm('test', 'a summary', series=['trusty', 'xenial'])
51
self.assertEqual(charm.default_series, 'trusty')
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)
64
'summary': 'a summary',
65
'description': 'a description',
66
'series': ['xenial', 'trusty'],
68
self.assertEqual(metadata, expected)
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)
79
'summary': 'a summary',
81
'maintainer': Charm.DEFAULT_MAINTAINER,
82
'description': Charm.DEFAULT_DESCRIPTION,
84
self.assertEqual(metadata, expected)
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)
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)
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(
121
'Invalid Juju Charm Name, "BAD_NAME" does not match ".*"\.',
122
Charm, 'BAD_NAME', 'A charm with a checked bad name')
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')
130
class TestLocalCharm(TestCase):
132
def test_make_local_charm_1x(self):
134
path = local_charm_path(charm, '1.25.0')
135
self.assertEqual(path, 'local:mysql')
137
def test_make_local_charm_1x_series(self):
139
path = local_charm_path(charm, '1.25.0', series='trusty')
140
self.assertEqual(path, 'local:trusty/mysql')
142
def test_make_local_charm_2x(self):
144
path = local_charm_path(charm, '2.0.0', repository='/tmp/charms')
145
self.assertEqual(path, '/tmp/charms/mysql')
147
def test_make_local_charm_2x_os_env(self):
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')
153
def test_make_local_charm_2x_win(self):
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')
159
def test_make_local_charm_2x_centos(self):
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')
166
class TestSaneCharmStoreApiUrl(TestCase):
168
def test_returns_default_value(self):
170
sane_charm_store_api_url(None),
171
CharmCommand.default_api_url)
173
def test_replaces_places_api_characters(self):
174
api = 'https://example.com'
175
expected = 'https://api.example.com/charmstore'
177
sane_charm_store_api_url(api),
180
def test_replaces_www_characters(self):
181
api = 'https://www.example.com'
182
expected = 'https://api.example.com/charmstore'
184
sane_charm_store_api_url(api),