~longsleep/snapcraft/snapcraft-debs-plugin

« back to all changes in this revision

Viewing changes to snapcraft/tests/test_base_plugin.py

  • Committer: Snappy Tarmac
  • Author(s): Leo Arias
  • Date: 2015-08-04 21:41:11 UTC
  • mfrom: (116.1.9 base_plugin_tests)
  • Revision ID: snappy_tarmac-20150804214111-pye903my941iu1io
Added unit tests for the base plugin.
Added python3-testscenarios and wget as a dependency. by elopio approved by mterry

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
 
17
import http.server
 
18
import fixtures
 
19
import logging
 
20
import os
 
21
import threading
 
22
 
17
23
import snapcraft
18
 
from snapcraft.tests import TestCase
19
 
 
20
 
 
21
 
class TestBasePlugin(TestCase):
 
24
from snapcraft import tests
 
25
 
 
26
 
 
27
class FakeTarballHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
 
28
 
 
29
    def do_GET(self):
 
30
        data = 'Test fake tarball file'
 
31
        self.send_response(200)
 
32
        self.send_header('Content-Length', len(data))
 
33
        self.send_header('Content-type', 'text/html')
 
34
        self.end_headers()
 
35
        self.wfile.write(data.encode())
 
36
 
 
37
    def log_message(self, *args):
 
38
        # Overwritten so the test does not write to stderr.
 
39
        pass
 
40
 
 
41
 
 
42
class TestBasePlugin(tests.TestCase):
22
43
 
23
44
    def test_isurl(self):
24
45
        plugin = snapcraft.BasePlugin('mock', {})
27
48
        self.assertFalse(plugin.isurl('./'))
28
49
        self.assertFalse(plugin.isurl('/foo'))
29
50
        self.assertFalse(plugin.isurl('/fo:o'))
 
51
 
 
52
    def test_pull_git_with_tag_and_branch_must_raise_error(self):
 
53
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
 
54
        self.useFixture(fake_logger)
 
55
 
 
56
        plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
 
57
        with self.assertRaises(SystemExit) as raised:
 
58
            plugin.pull_git(
 
59
                'dummy_source', source_tag='test_tag',
 
60
                source_branch='test_branch')
 
61
 
 
62
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
 
63
        expected = (
 
64
            "You can't specify both source-tag and source-branch for a git "
 
65
            "source (part 'test_plugin').\n")
 
66
        self.assertEqual(expected, fake_logger.output)
 
67
 
 
68
    def test_pull_tarball_must_download_to_sourcedir(self):
 
69
        server = http.server.HTTPServer(('', 0), FakeTarballHTTPRequestHandler)
 
70
        server_thread = threading.Thread(target=server.serve_forever)
 
71
        self.addCleanup(server_thread.join)
 
72
        self.addCleanup(server.server_close)
 
73
        self.addCleanup(server.shutdown)
 
74
        server_thread.start()
 
75
 
 
76
        plugin_name = 'test_plugin'
 
77
        dest_dir = os.path.join('parts', plugin_name, 'src')
 
78
        os.makedirs(dest_dir)
 
79
        tar_file_name = 'test.tar'
 
80
        source = 'http://{}:{}/{file_name}'.format(
 
81
            *server.server_address, file_name=tar_file_name)
 
82
        plugin = snapcraft.BasePlugin(plugin_name, 'dummy_options')
 
83
        plugin.pull_tarball(source)
 
84
 
 
85
        with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file:
 
86
            self.assertEqual('Test fake tarball file', tar_file.read())
 
87
 
 
88
    def test_get_source_with_unrecognized_source_must_raise_error(self):
 
89
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
 
90
        self.useFixture(fake_logger)
 
91
 
 
92
        plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
 
93
        with self.assertRaises(SystemExit) as raised:
 
94
            plugin.get_source('unrecognized://test_source')
 
95
 
 
96
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
 
97
        expected = (
 
98
            "Unrecognized source 'unrecognized://test_source' for part "
 
99
            "'test_plugin'.\n")
 
100
        self.assertEqual(expected, fake_logger.output)
 
101
 
 
102
    def test_makedirs_with_existing_dir(self):
 
103
        plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
 
104
        plugin.makedirs(self.path)
 
105
        self.assertTrue(os.path.exists(self.path))
 
106
 
 
107
    def test_makedirs_with_unexisting_dir(self):
 
108
        path = os.path.join(self.path, 'unexisting')
 
109
        plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
 
110
        plugin.makedirs(path)
 
111
        self.assertTrue(os.path.exists(path))
 
112
 
 
113
 
 
114
class GetSourceTestCase(tests.TestCase):
 
115
 
 
116
    scenarios = [
 
117
        ('bzr with source branch', {
 
118
            'source_type': 'bzr',
 
119
            'source_branch': 'test_branch',
 
120
            'source_tag': None,
 
121
            'error': 'source-branch'}),
 
122
        ('tar with source branch', {
 
123
            'source_type': 'tar',
 
124
            'source_branch': 'test_branch',
 
125
            'source_tag': None,
 
126
            'error': 'source-branch'}),
 
127
        ('tar with source tag', {
 
128
            'source_type': 'tar',
 
129
            'source_branch': None,
 
130
            'source_tag': 'test_tag',
 
131
            'error': 'source-tag'})
 
132
    ]
 
133
 
 
134
    def test_get_source_with_branch_must_raise_error(self):
 
135
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
 
136
        self.useFixture(fake_logger)
 
137
 
 
138
        plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
 
139
        with self.assertRaises(SystemExit) as raised:
 
140
            plugin.get_source(
 
141
                'dummy_source', source_type=self.source_type,
 
142
                source_branch=self.source_branch, source_tag=self.source_tag)
 
143
 
 
144
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
 
145
        expected = (
 
146
            "You can't specify {} for a {} source "
 
147
            "(part 'test_plugin').\n".format(self.error, self.source_type))
 
148
        self.assertEqual(expected, fake_logger.output)