Enables containerized overcloud 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.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
18
19 DOCKER_YAML = {
20     'resource_registry': {
21         'OS::TripleO::Services::NovaApi': '../docker/services/nova-api.yaml',
22         'OS::TripleO::Services::NovaConductor':
23             '../docker/services/nova-conductor.yaml'
24     }
25 }
26
27
28 class TestCommonBuilder(unittest.TestCase):
29     @classmethod
30     def setup_class(cls):
31         """This method is run once for each class before any tests are run"""
32
33     @classmethod
34     def teardown_class(cls):
35         """This method is run once for each class _after_ all tests are run"""
36
37     def setup(self):
38         """This method is run once before _each_ test method is executed"""
39
40     def teardown(self):
41         """This method is run once after _each_ test method is executed"""
42
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/nova'
52         self.assertEquals(c_builder.project_to_path(project), path)
53
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'
60         patches = [{
61             'change-id': change_id,
62             'project': 'openstack/tripleo-heat-templates'
63         }]
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)
69         test_virt_ops = [
70             {con.VIRT_INSTALL: 'patch'},
71             {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
72                                              project_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')
78
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'
85         patches = [{
86             'change-id': change_id,
87             'project': 'openstack/puppet-tripleo'
88         }]
89         project_path = '/etc/puppet/modules/tripleo'
90         patch_file = "{}.patch".format(change_id)
91         patch_file_path = "/dummytmp/{}".format(patch_file)
92         test_virt_ops = [
93             {con.VIRT_INSTALL: 'patch'},
94             {con.VIRT_UPLOAD: "{}:{}".format(patch_file_path,
95                                              project_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/',
100                                        uc_ip='192.0.2.1',
101                                        docker_tag='latest')
102         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
103
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'
114         patches = [{
115             'change-id': change_id,
116             'project': 'openstack/nova'
117         }]
118         mock_get_patch.return_value = 'some random diff'
119         services = c_builder.add_upstream_patches(patches, 'dummy.qcow2',
120                                                   '/dummytmp/',
121                                                   uc_ip='192.0.2.1',
122                                                   docker_tag='latest')
123         assert mock_customize.not_called
124         assert mock_build_docker_file.called
125         self.assertSetEqual(services, {'NovaApi'})
126
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',
131                            '/dummytmp/')
132         repo_file_path = '/dummytmp/dummyrepo.repo'
133         test_virt_ops = [
134             {con.VIRT_UPLOAD: "{}:/etc/yum.repos.d/".format(repo_file_path)}
135         ]
136         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
137
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',
143                                                       '/dummytmp/'),
144                          '/dummytmp/dummyrepo.tar')
145
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
149
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,
156                           'nova')