summaryrefslogtreecommitdiff
path: root/morphlib/remoteartifactcache_tests.py
blob: f6d19ae7281c5ad059c0f652712c16bb8f68d36a (plain)
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
# Copyright (C) 2012-2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import BaseHTTPServer
import contextlib
import os
import socket
import SocketServer
import threading
import unittest
import urlparse

import morphlib
import morphlib.testutils


def artifact_files_for_chunk_source(source):
    '''Determine what filenames would exist for all artifacts of one source.'''

    existing_files = set(['%s.meta' % source.cache_key])
    for a in source.artifacts.itervalues():
        existing_files.add(a.basename())
        existing_files.add(a.metadata_basename('meta'))
    return existing_files



@contextlib.contextmanager
def http_server(request_handler):
    server = SocketServer.TCPServer(('', 0), request_handler)
    thread = threading.Thread(target=server.serve_forever)
    thread.setDaemon(True)
    thread.start()

    url = 'http://%s:%s' % (socket.gethostname(), server.server_address[1])
    yield url

    server.shutdown()


def artifact_cache_server(valid_filenames):
    '''Run a basic artifact cache server.

    It implements the 'artifacts' request only. Dummy content will be returned
    for any file listed in 'valid_filenames'.  Anything else will get a 404
    error.

    '''

    class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
        dummy_content = 'I am an artifact.\n'

        def log_message(self, *args, **kwargs):
            # This is overridden to avoid dumping logs on stdout.
            pass

        def artifacts(self, query, send_body=True):
            '''Return a cached artifact, or 404.'''
            filename = urlparse.parse_qs(query)['filename'][0]

            if filename in valid_filenames:
                self.send_response(200)
                self.end_headers()
                if send_body:
                    self.wfile.write(self.dummy_content)
            else:
                self.send_response(404)

        def do_GET(self, send_body=True):
            parts = urlparse.urlsplit(self.path)
            if parts.path == '/1.0/artifacts':
                self.artifacts(parts.query, send_body=send_body)
            else:
                self.send_response(404)

        def do_HEAD(self):
            return self.do_GET(send_body=False)

    return http_server(Handler)


class RemoteArtifactCacheTests(unittest.TestCase):
    def fake_status_cb(*args, **kwargs):
        pass

    def test_artifacts_are_cached(self):
        '''Exercise fetching of artifacts that are cached.'''

        chunk = morphlib.testutils.example_chunk()
        valid_files = artifact_files_for_chunk_source(chunk)

        with artifact_cache_server(valid_files) as url:
            rac = morphlib.remoteartifactcache.RemoteArtifactCache(url)
            an_artifact = chunk.artifacts.values()[0]

            self.assertTrue(
                rac.has(an_artifact))

            self.assertTrue(
                rac.has_artifact_metadata(an_artifact, 'meta'))

            self.assertTrue(
                rac.has_source_metadata(chunk, chunk.cache_key, 'meta'))

            lac = morphlib.testutils.FakeLocalArtifactCache()
            self.assertFalse(lac.has(an_artifact))

            rac.get_artifacts(
                chunk.artifacts.values(), lac, self.fake_status_cb)
            self.assertTrue(lac.has(an_artifact))

    def test_artifacts_are_not_cached(self):
        '''Exercise trying to fetch artifacts that the cache doesn't have.'''

        chunk = morphlib.testutils.example_chunk()
        valid_files = []

        with artifact_cache_server(valid_files) as url:
            rac = morphlib.remoteartifactcache.RemoteArtifactCache(url)
            an_artifact = chunk.artifacts.values()[0]

            self.assertFalse(
                rac.has(an_artifact))

            self.assertFalse(
                rac.has_artifact_metadata(an_artifact, 'non-existent-meta'))

            self.assertFalse(
                rac.has_source_metadata(chunk, chunk.cache_key, 'meta'))

            lac = morphlib.testutils.FakeLocalArtifactCache()
            self.assertRaises(
                morphlib.remoteartifactcache.GetError, rac.get_artifacts,
                chunk.artifacts.values(), lac, self.fake_status_cb)

    def test_escapes_pluses_in_request_urls(self):
        rac = morphlib.remoteartifactcache.RemoteArtifactCache(
            'http://example.com')

        returned_url = rac._request_url('gtk+')
        correct_url = 'http://example.com/1.0/artifacts?filename=gtk%2B'
        self.assertEqual(returned_url, correct_url)