1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (Red Hat)
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 ##############################################################################
12 from apex.builders import common_builder as c_builder
13 from apex.builders import exceptions
14 from apex.common import constants as con
15 from mock import patch
16 from mock import mock_open
17 from mock import MagicMock
20 'resource_registry': {
21 'OS::TripleO::Services::NovaApi': '../docker/services/nova-api.yaml',
22 'OS::TripleO::Services::NovaConductor':
23 '../docker/services/nova-conductor.yaml'
28 class TestCommonBuilder(unittest.TestCase):
31 """This method is run once for each class before any tests are run"""
34 def teardown_class(cls):
35 """This method is run once for each class _after_ all tests are run"""
38 """This method is run once before _each_ test method is executed"""
41 """This method is run once after _each_ test method is executed"""
43 def test_project_to_path(self):
44 project = 'openstack/tripleo-heat-templates'
45 path = '/usr/share/openstack-tripleo-heat-templates'
46 self.assertEquals(c_builder.project_to_path(project), path)
47 project = 'openstack/puppet-tripleo'
48 path = '/etc/puppet/modules/tripleo'
49 self.assertEquals(c_builder.project_to_path(project), path)
50 project = 'openstack/nova'
51 path = '/usr/lib/python2.7/site-packages/'
52 self.assertEquals(c_builder.project_to_path(project), path)
54 @patch('builtins.open', mock_open())
55 @patch('apex.build_utils.get_patch')
56 @patch('apex.virtual.utils.virt_customize')
57 def test_add_upstream_patches(self, mock_customize, mock_get_patch):
58 mock_get_patch.return_value = None
59 change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
61 'change-id': change_id,
62 'project': 'openstack/tripleo-heat-templates'
64 c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
65 assert mock_customize.not_called
66 project_path = '/usr/share/openstack-tripleo-heat-templates'
67 patch_file = "{}.patch".format(change_id)
68 patch_file_path = "/dummytmp/{}".format(patch_file)
70 {con.VIRT_INSTALL: 'patch'},
71 {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
73 {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
74 project_path, patch_file)}]
75 mock_get_patch.return_value = 'some random diff'
76 c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
77 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
79 @patch('builtins.open', mock_open())
80 @patch('apex.build_utils.get_patch')
81 @patch('apex.virtual.utils.virt_customize')
82 def test_add_upstream_patches_docker_puppet(
83 self, mock_customize, mock_get_patch):
84 change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
86 'change-id': change_id,
87 'project': 'openstack/puppet-tripleo'
89 project_path = '/etc/puppet/modules/tripleo'
90 patch_file = "{}.patch".format(change_id)
91 patch_file_path = "/dummytmp/{}".format(patch_file)
93 {con.VIRT_INSTALL: 'patch'},
94 {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
96 {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
97 project_path, patch_file)}]
98 mock_get_patch.return_value = 'some random diff'
99 c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/',
102 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
104 @patch('builtins.open', mock_open())
105 @patch('apex.builders.common_builder.project_to_docker_image')
106 @patch('apex.builders.overcloud_builder.build_dockerfile')
107 @patch('apex.build_utils.get_patch')
108 @patch('apex.virtual.utils.virt_customize')
109 def test_add_upstream_patches_docker_python(
110 self, mock_customize, mock_get_patch, mock_build_docker_file,
111 mock_project2docker):
112 mock_project2docker.return_value = ['NovaApi']
113 change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
115 'change-id': change_id,
116 'project': 'openstack/nova'
118 mock_get_patch.return_value = 'some random diff'
119 services = c_builder.add_upstream_patches(patches, 'dummy.qcow2',
123 assert mock_customize.not_called
124 assert mock_build_docker_file.called
125 self.assertSetEqual(services, {'NovaApi'})
127 @patch('builtins.open', mock_open())
128 @patch('apex.virtual.utils.virt_customize')
129 def test_add_repo(self, mock_customize):
130 c_builder.add_repo('fake/url', 'dummyrepo', 'dummy.qcow2',
132 repo_file_path = '/dummytmp/dummyrepo.repo'
134 {con.VIRT_UPLOAD: "{}:/etc/yum.repos.d/".format(repo_file_path)}
136 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
138 @patch('builtins.open', mock_open())
139 @patch('git.Repo.clone_from')
140 def test_create_git_archive(self, mock_git):
141 mock_git.return_value = MagicMock()
142 self.assertEqual(c_builder.create_git_archive('fake/url', 'dummyrepo',
144 '/dummytmp/dummyrepo.tar')
146 def test_project_to_docker_image(self):
147 found_services = c_builder.project_to_docker_image(project='nova')
148 assert 'nova-api' in found_services
150 @patch('apex.common.utils.open_webpage')
151 def test_project_to_docker_image_bad_web_content(
152 self, mock_open_web):
153 mock_open_web.return_value = b'{"blah": "blah"}'
154 self.assertRaises(exceptions.ApexCommonBuilderException,
155 c_builder.project_to_docker_image,