~andrewjbeach/juju-ci-tools/get-juju-dict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Tests for charm related classes and helpers."""

import os

import yaml

from jujucharm import (
    Charm,
    CharmCommand,
    local_charm_path,
    sane_charm_store_api_url,
    )
from tests import (
    temp_os_env,
    TestCase,
    )
from utility import (
    JujuAssertionError,
    temp_dir,
    )


class TestCharm(TestCase):

    def test_create_default(self):
        charm = Charm('test', 'a summary')
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': ('xenial', 'trusty'),
            'maintainer': 'juju-qa@lists.canonical.com',
            'description': 'description',
        }
        self.assertEqual(charm.metadata, expected)

    def test_default_series_default(self):
        charm = Charm('test', 'a summary')
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_unset(self):
        charm = Charm('test', 'a summary')
        del charm.metadata['series']
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_single(self):
        charm = Charm('test', 'a summary', series='wily')
        self.assertEqual(charm.default_series, 'wily')

    def test_default_series_list(self):
        charm = Charm('test', 'a summary', series=['trusty', 'xenial'])
        self.assertEqual(charm.default_series, 'trusty')

    def test_to_dir(self):
        charm = Charm('test', 'a summary')
        charm.metadata['description'] = 'a description'
        del charm.metadata['maintainer']
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            metafile = os.path.join(charm_dir, 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'description': 'a description',
            'series': ['xenial', 'trusty'],
        }
        self.assertEqual(metadata, expected)

    def test_to_repo_dir(self):
        charm = Charm('test', 'a summary', series='wily')
        with temp_dir() as repo_dir:
            charm.to_repo_dir(repo_dir)
            metafile = os.path.join(repo_dir, 'wily', 'test', 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': 'wily',
            'maintainer': Charm.DEFAULT_MAINTAINER,
            'description': Charm.DEFAULT_DESCRIPTION,
        }
        self.assertEqual(metadata, expected)

    def test_add_hook_script(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        charm.add_hook_script('config-changed', config_changed)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            hookfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(hookfile, os.X_OK))
            with open(hookfile) as f:
                self.assertEqual(f.read(), config_changed)

    def test_add_hook_multiple(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        upgrade_charm = '#!/bin/sh\necho upgraded'
        charm.add_hook_script('config-changed', config_changed)
        charm.add_hook_script('upgrade-charm', upgrade_charm)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            changedfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(changedfile, os.X_OK))
            with open(changedfile) as f:
                self.assertEqual(f.read(), config_changed)
            upgradedfile = os.path.join(charm_dir, 'hooks', 'upgrade-charm')
            self.assertTrue(os.access(upgradedfile, os.X_OK))
            with open(upgradedfile) as f:
                self.assertEqual(f.read(), upgrade_charm)

    def test_ensure_valid_name(self):
        Charm('good-name', 'A charm with a valid name')
        charm = Charm('BAD_NAME', 'A charm with a bad name',
                      ensure_valid_name=False)
        self.assertIsNone(Charm.NAME_REGEX.match(charm.metadata['name']))
        self.assertRaisesRegexp(
            JujuAssertionError,
            'Invalid Juju Charm Name, "BAD_NAME" does not match ".*"\.',
            Charm, 'BAD_NAME', 'A charm with a checked bad name')


class TestLocalCharm(TestCase):

    def test_make_local_charm_1x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0')
        self.assertEqual(path, 'local:mysql')

    def test_make_local_charm_1x_series(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0', series='trusty')
        self.assertEqual(path, 'local:trusty/mysql')

    def test_make_local_charm_2x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '2.0.0', repository='/tmp/charms')
        self.assertEqual(path, '/tmp/charms/mysql')

    def test_make_local_charm_2x_os_env(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0')
        self.assertEqual(path, '/home/foo/repository/charms/mysql')

    def test_make_local_charm_2x_win(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='win')
        self.assertEqual(path, '/home/foo/repository/charms-win/mysql')

    def test_make_local_charm_2x_centos(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='centos')
        self.assertEqual(path, '/home/foo/repository/charms-centos/mysql')


class TestSaneCharmStoreApiUrl(TestCase):

    def test_returns_default_value(self):
        self.assertEqual(
            sane_charm_store_api_url(None),
            CharmCommand.default_api_url)

    def test_replaces_places_api_characters(self):
        api = 'https://example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)

    def test_replaces_www_characters(self):
        api = 'https://www.example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)