d042d2ba4690fe427658c0c4478537a6f63784e6
[apex.git] / apex / tests / test_apex_common_builder.py
1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (Red Hat)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import unittest
11
12 from apex.builders import common_builder as c_builder
13 from apex.common import constants as con
14 from mock import patch
15 from mock import mock_open
16 from mock import MagicMock
17
18
19 class TestCommonBuilder(unittest.TestCase):
20     @classmethod
21     def setup_class(cls):
22         """This method is run once for each class before any tests are run"""
23
24     @classmethod
25     def teardown_class(cls):
26         """This method is run once for each class _after_ all tests are run"""
27
28     def setup(self):
29         """This method is run once before _each_ test method is executed"""
30
31     def teardown(self):
32         """This method is run once after _each_ test method is executed"""
33
34     def test_project_to_path(self):
35         project = 'openstack/tripleo-heat-templates'
36         path = '/usr/share/openstack-tripleo-heat-templates'
37         self.assertEquals(c_builder.project_to_path(project), path)
38         project = 'openstack/puppet-tripleo'
39         path = '/etc/puppet/modules/tripleo'
40         self.assertEquals(c_builder.project_to_path(project), path)
41         project = 'openstack/nova'
42         path = '/usr/lib/python2.7/site-packages/nova'
43         self.assertEquals(c_builder.project_to_path(project), path)
44
45     @patch('builtins.open', mock_open())
46     @patch('apex.build_utils.get_patch')
47     @patch('apex.virtual.utils.virt_customize')
48     def test_add_upstream_patches(self, mock_customize, mock_get_patch):
49         mock_get_patch.return_value = None
50         change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
51         patches = [{
52             'change-id': change_id,
53             'project': 'openstack/tripleo-heat-templates'
54         }]
55         c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
56         assert mock_customize.not_called
57         project_path = '/usr/share/openstack-tripleo-heat-templates'
58         patch_file = "{}.patch".format(change_id)
59         patch_file_path = "/dummytmp/{}".format(patch_file)
60         test_virt_ops = [
61             {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
62                                              project_path)},
63             {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
64                 project_path, patch_file)}]
65         mock_get_patch.return_value = 'some random diff'
66         c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
67         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
68
69     @patch('builtins.open', mock_open())
70     @patch('apex.virtual.utils.virt_customize')
71     def test_add_repo(self, mock_customize):
72         c_builder.add_repo('fake/url', 'dummyrepo', 'dummy.qcow2',
73                            '/dummytmp/')
74         repo_file_path = '/dummytmp/dummyrepo.repo'
75         test_virt_ops = [
76             {con.VIRT_UPLOAD: "{}:/etc/yum.repos.d/".format(repo_file_path)}
77         ]
78         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
79
80     @patch('builtins.open', mock_open())
81     @patch('git.Repo.clone_from')
82     def test_create_git_archive(self, mock_git):
83         mock_git.return_value = MagicMock()
84         self.assertEqual(c_builder.create_git_archive('fake/url', 'dummyrepo',
85                                                       '/dummytmp/'),
86                          '/dummytmp/dummyrepo.tar')