~vila/uci-engine/rtfd

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
#!/usr/bin/env python
# Ubuntu CI Engine
# Copyright 2013, 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import urllib2
import unittest
import sys
import yaml


HERE = os.path.abspath(os.path.dirname(__file__))

sys.path.append(os.path.join(HERE, '..', 'ci-utils'))

from ci_utils import data_store


class TestDataStore(unittest.TestCase):

    def setUp(self):
        super(TestDataStore, self).setUp()
        default_config = os.path.join(HERE, '..', 'unit_config')
        if not os.path.exists(default_config):
            self.skipTest('{} is not available'.format(default_config))
        with open(default_config) as fp:
            self.auth_config = yaml.safe_load(fp)

        self.ds = data_store.DataStore("test", auth_config=self.auth_config)
        self.assert_(self.ds, "Failed to create DataStore")
        self.addCleanup(self.ds.delete, recursive=True)

        self.filename = 'test_file'
        self.contents = 'some data'

    def test_create_container_twice(self):
        self.ds.ensure_swift_client()
        # swift is perfectly happy to create the same container twice.
        self.ds._create_container(self.ds.container_id)

    def test_list_files_empty(self):
        """ Test listing files on an empty container. """
        files = self.ds.list_files()
        self.assertEqual([], files)

    def test_add_file(self):
        """ Test adding a file. """

        self.ds.put_file(self.filename, self.contents)
        self.addCleanup(self.ds.delete_file, self.filename)
        files = self.ds.list_files()
        self.assertEqual([self.filename], files)

    def test_add_file_contents(self):
        """ Test adding a file has correct contents. """

        self.ds.put_file(self.filename, self.contents)
        self.addCleanup(self.ds.delete_file, self.filename)
        contents = self.ds.get_file(self.filename)

        self.assertEqual(contents, self.contents)

    def test_public_container(self):
        """ Test accesssing a public container. """

        self.ds.put_file(self.filename, self.contents)
        self.addCleanup(self.ds.delete_file, self.filename)
        self.ds.change_visibility(public=True)

        response = urllib2.urlopen(self.ds.file_path(self.filename))
        self.assertEqual(response.code, 200)

    def test_delete_file(self):
        """ Test delete file removes the file. """

        self.ds.put_file(self.filename, self.contents)
        files = self.ds.list_files()
        self.assertEqual([self.filename], files)

        self.ds.delete_file(self.filename)
        files = self.ds.list_files()
        self.assertEqual([], files)

    def test_put_file_link(self):
        """ Test put file returns a valid link. """

        link = self.ds.put_file(self.filename, self.contents)
        self.addCleanup(self.ds.delete_file, self.filename)
        with self.assertRaises(urllib2.HTTPError) as cm:
            resp = urllib2.urlopen(link)
        self.assertEqual(401, cm.exception.code)
        self.ds.change_visibility(public=True)
        resp = urllib2.urlopen(link)
        self.assertEqual(200, resp.getcode())


class TestDataStoreBadConfigs(unittest.TestCase):

    def setUp(self):
        super(TestDataStoreBadConfigs, self).setUp()

    def test_auth_missing(self):
        """ Test that missing auth details is handled correctly. """

        with self.assertRaises(data_store.DataStoreException):
            ds = data_store.DataStore("test", auth_config={})
            ds.ensure_swift_client()

    def get_config(self):
        default_config = os.path.join(HERE, '..', 'unit_config')
        if not os.path.exists(default_config):
            self.skipTest('{} is not available'.format(default_config))
        with open(default_config) as fp:
            return yaml.safe_load(fp)

    def test_missing_region(self):
        conf = self.get_config()
        del conf['auth_region']
        with self.assertRaises(data_store.DataStoreException):
            ds = data_store.DataStore("test", auth_config=conf)
            ds.ensure_swift_client()

    def test_bad_region(self):
        conf = self.get_config()
        conf['auth_region'] = "I don't exist"
        with self.assertRaises(data_store.DataStoreException):
            ds = data_store.DataStore("test", auth_config=conf)
            ds.ensure_swift_client()


if __name__ == "__main__":
    unittest.main()