Fixes upstream deployments
[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_INSTALL: 'patch'},
62             {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
63                                              project_path)},
64             {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
65                 project_path, patch_file)}]
66         mock_get_patch.return_value = 'some random diff'
67         c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
68         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
69
70     @patch('builtins.open', mock_open())
71     @patch('apex.virtual.utils.virt_customize')
72     def test_add_repo(self, mock_customize):
73         c_builder.add_repo('fake/url', 'dummyrepo', 'dummy.qcow2',
74                            '/dummytmp/')
75         repo_file_path = '/dummytmp/dummyrepo.repo'
76         test_virt_ops = [
77             {con.VIRT_UPLOAD: "{}:/etc/yum.repos.d/".format(repo_file_path)}
78         ]
79         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
80
81     @patch('builtins.open', mock_open())
82     @patch('git.Repo.clone_from')
83     def test_create_git_archive(self, mock_git):
84         mock_git.return_value = MagicMock()
85         self.assertEqual(c_builder.create_git_archive('fake/url', 'dummyrepo',
86                                                       '/dummytmp/'),
87                          '/dummytmp/dummyrepo.tar')