~andrewjbeach/juju-ci-tools/make-local-patcher

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python

from __future__ import print_function

import argparse
from collections import namedtuple
from datetime import datetime
import logging
import os
import sys
from tempfile import NamedTemporaryFile
from textwrap import dedent
from uuid import uuid1

import requests

from jujucharm import (
    CharmCommand,
    local_charm_path,
)
from utility import (
    configure_logging,
    JujuAssertionError,
    scoped_environ,
    temp_dir,
)

__metaclass__ = type
log = logging.getLogger("assess_resources_charmstore")

CHARMSTORE_API_VERSION = 'v5'

# Stores credential details for a target charmstore
CharmstoreDetails = namedtuple(
    'CharmstoreDetails',
    ['email', 'username', 'password', 'api_url'])


# Using a run id we can create a unique charm for each test run allowing us to
# test from fresh.
class RunId:
    _run_id = None

    def __call__(self):
        if self._run_id is None:
            self._run_id = str(uuid1()).replace('-', '')
        return self._run_id


get_run_id = RunId()


def get_charmstore_details(credentials_file=None):
    """Returns a CharmstoreDetails from `credentials_file` or env vars.

    Parses the credentials file (if supplied) and environment variables to
    retrieve the charmstore details and credentials.

    Note. any supplied detail via environment variables will overwrite anything
    supplied in the credentials file..

    """

    required_keys = ('api_url', 'password', 'email', 'username')

    details = {}
    if credentials_file is not None:
        details = parse_credentials_file(credentials_file)

    for key in required_keys:
        env_key = 'CS_{}'.format(key.upper())
        value = os.environ.get(env_key, details.get(key))
        # Can't have empty credential details
        if value is not None:
            details[key] = value

    if not set(details.keys()).issuperset(required_keys):
        raise ValueError('Unable to get all details from file.')

    return CharmstoreDetails(**details)


def split_line_details(string):
    safe_string = string.strip()
    return safe_string.split('=', 1)[-1].strip('"')


def parse_credentials_file(credentials_file):
    details = {}
    with open(credentials_file, 'r') as creds:
        for line in creds.readlines():
            if 'STORE_CREDENTIALS' in line:
                creds = split_line_details(line)
                email_address, password = creds.split(':', 1)
                details['email'] = email_address
                details['password'] = password
                raw_username = email_address.split('@', 1)[0]
                details['username'] = raw_username.replace('.', '-')
            elif 'STORE_URL' in line:
                details['api_url'] = split_line_details(line)
    return details


def ensure_can_push_and_list_charm_with_resources(charm_bin, cs_details):
    """Ensure that a charm can be pushed to a charm store with a resource.

    Checks that:
      - A charm can be pushed with a resource populated with a file
      - A charm can be updated (attach) after being pushed
      - A charms resources revision is updated after a push or attach

    """
    charm_command = CharmCommand(charm_bin, cs_details.api_url)
    with charm_command.logged_in_user(cs_details.email, cs_details.password):
        charm_id = 'juju-qa-resources-{id}'.format(id=get_run_id())
        # Only available for juju 2.x
        charm_path = local_charm_path('dummy-resource', '2.x')
        charm_url = 'cs:~{username}/{id}-0'.format(
            username=cs_details.username, id=charm_id)

        # Ensure we can publish a charm with a resource
        with NamedTemporaryFile(suffix='.txt') as temp_foo_resource:
            temp_foo = temp_foo_resource.name
            populate_file_data(temp_foo)
            push_charm_with_resource(
                charm_command,
                temp_foo,
                charm_id,
                charm_path,
                resource_name='foo')

            # Need to grant permissions so we can access details via the http
            # api.
            grant_everyone_access(charm_command, charm_url)

            expected_resource_details = {'foo': 0, 'bar': -1}
            check_resource_uploaded(
                charm_command,
                charm_url,
                'foo',
                temp_foo,
                expected_resource_details)

        # Ensure we can attach a resource independently of pushing a charm.
        with NamedTemporaryFile(suffix='.txt') as temp_bar_resource:
            temp_bar = temp_bar_resource.name
            populate_file_data(temp_bar)
            output = attach_resource_to_charm(
                charm_command, temp_bar, charm_url, resource_name='bar')
            log.info(output)

            expected_resource_details = {'foo': 0, 'bar': 0}
            check_resource_uploaded(
                charm_command,
                charm_url,
                'bar',
                temp_bar,
                expected_resource_details)


def populate_file_data(filepath):
    """Write unique data to file at `filepath`."""
    datestamp = datetime.utcnow().isoformat()
    with open(filepath, 'w') as f:
        f.write('{datestamp}:{uuid}'.format(
            datestamp=datestamp,
            uuid=get_run_id()))


def push_charm_with_resource(
        charm_command, temp_file, charm_id, charm_path, resource_name):

    output = charm_command.run(
        'push',
        charm_path,
        charm_id,
        '--resource', '{}={}'.format(resource_name, temp_file))
    log.info('Pushing charm "{id}": {output}'.format(
        id=charm_id, output=output))


def attach_resource_to_charm(
        charm_command, temp_file, charm_id, resource_name):

    return charm_command.run('attach', charm_id, '{}={}'.format(
        resource_name, temp_file))


def check_resource_uploaded(
        charm_command, charm_url, resource_name, src_file, resource_details):
    for check_name, check_revno in resource_details.items():
        check_resource_uploaded_revno(
            charm_command, charm_url, check_name, check_revno)
    check_resource_uploaded_contents(
        charm_command, charm_url, resource_name, src_file)


def check_resource_uploaded_revno(
        charm_command, charm_url, resource_name, revno):
    """Parse list-resources and ensure resource revno is equal to `revno`.

    :raises JujuAssertionError: If the resources revision is not equal to
      `revno`

    """
    revno = int(revno)
    output = charm_command.run('list-resources', charm_url)

    for line in output.split('\n'):
        if line.startswith(resource_name):
            rev = int(line.split(None, 1)[-1])
            if rev != revno:
                raise JujuAssertionError(
                    'Failed to upload resource and increment revision number.')
            return
    raise JujuAssertionError(
        'Failed to find named resource "{}" in output'.format(resource_name))


def grant_everyone_access(charm_command, charm_url):
    output = charm_command.run('grant', charm_url, 'everyone')
    log.info('Setting permissions on charm: {}'.format(output))


def check_resource_uploaded_contents(
        charm_command, charm_url, resource_name, src_file):
    charmname = charm_url.replace('cs:', '')
    api_url = '{apiurl}/{api_version}/{charmname}/resource/{name}'.format(
        apiurl=charm_command.api_url,
        api_version=CHARMSTORE_API_VERSION,
        charmname=charmname,
        name=resource_name,
    )
    log.info('Using api url: {}'.format(api_url))

    res = requests.get(api_url)

    if not res.ok:
        raise JujuAssertionError('Failed to retrieve details: {}'.format(
            res.content))

    with open(src_file, 'r') as f:
        file_contents = f.read()
    resource_contents = res.content

    raise_if_contents_differ(resource_contents, file_contents)


def raise_if_contents_differ(resource_contents, file_contents):
    if resource_contents != file_contents:
        raise JujuAssertionError(
            dedent("""\
            Resource contents mismatch.
            Expected:
            {}
            Got:
            {}""".format(
                file_contents,
                resource_contents)))


def assess_charmstore_resources(charm_bin, credentials_file):
    with temp_dir() as fake_home:
        temp_env = os.environ.copy()
        temp_env['HOME'] = fake_home
        with scoped_environ(temp_env):
            cs_details = get_charmstore_details(credentials_file)
            ensure_can_push_and_list_charm_with_resources(
                charm_bin, cs_details)


def parse_args(argv):
    """Parse all arguments."""
    parser = argparse.ArgumentParser(description="Assess resources charmstore")
    parser.add_argument('charm_bin', help='Full path to charn binary')
    parser.add_argument(
        'credentials_file',
        help='Path to the file containing the charm store credentials and url')
    parser.add_argument(
        '--verbose', action='store_const',
        default=logging.INFO, const=logging.DEBUG,
        help='Verbose test harness output.')
    return parser.parse_args(argv)


def check_resources():
    if os.environ.get('JUJU_REPOSITORY') is None:
        raise AssertionError('JUJU_REPOSITORY required')


def main(argv=None):
    args = parse_args(argv)
    configure_logging(args.verbose)

    check_resources()

    assess_charmstore_resources(args.charm_bin, args.credentials_file)
    return 0


if __name__ == '__main__':
    sys.exit(main())