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 overcloud_builder as oc_builder
13 from apex.common import constants as con
14 from mock import patch, mock_open
16 a_mock_open = mock_open(read_data=None)
19 class TestOvercloudBuilder(unittest.TestCase):
22 """This method is run once for each class before any tests are run"""
25 def teardown_class(cls):
26 """This method is run once for each class _after_ all tests are run"""
29 """This method is run once before _each_ test method is executed"""
32 """This method is run once after _each_ test method is executed"""
34 @patch('apex.builders.common_builder.create_git_archive')
35 @patch('apex.builders.common_builder.add_repo')
36 @patch('apex.virtual.utils.virt_customize')
37 def test_inject_opendaylight(self, mock_customize, mock_add_repo,
39 mock_git_archive.return_value = '/dummytmp/puppet-opendaylight.tar'
40 archive = '/dummytmp/puppet-opendaylight.tar'
42 {con.VIRT_UPLOAD: "{}:/etc/puppet/modules/".format(archive)},
43 {con.VIRT_RUN_CMD: 'rm -rf /etc/puppet/modules/opendaylight'},
44 {con.VIRT_RUN_CMD: "cd /etc/puppet/modules/ && tar xvf "
45 "puppet-opendaylight.tar"},
46 {con.VIRT_INSTALL: 'opendaylight'}
48 oc_builder.inject_opendaylight(con.DEFAULT_ODL_VERSION, 'dummy.qcow2',
49 '/dummytmp/', uc_ip='192.0.2.2',
50 os_version=con.DEFAULT_OS_VERSION)
51 assert mock_git_archive.called
52 assert mock_add_repo.called
53 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
55 @patch('apex.builders.overcloud_builder.build_dockerfile')
56 @patch('apex.builders.common_builder.create_git_archive')
57 @patch('apex.builders.common_builder.add_repo')
58 @patch('apex.virtual.utils.virt_customize')
59 def test_inject_opendaylight_docker(self, mock_customize, mock_add_repo,
60 mock_git_archive, mock_build_docker):
61 mock_git_archive.return_value = '/dummytmp/puppet-opendaylight.tar'
62 archive = '/dummytmp/puppet-opendaylight.tar'
64 {con.VIRT_UPLOAD: "{}:/etc/puppet/modules/".format(archive)},
65 {con.VIRT_RUN_CMD: 'rm -rf /etc/puppet/modules/opendaylight'},
66 {con.VIRT_RUN_CMD: "cd /etc/puppet/modules/ && tar xvf "
67 "puppet-opendaylight.tar"},
69 oc_builder.inject_opendaylight('oxygen', 'dummy.qcow2',
70 '/dummytmp/', uc_ip='192.0.2.2',
71 os_version=con.DEFAULT_OS_VERSION,
73 odl_url = "https://nexus.opendaylight.org/content/repositories" \
74 "/opendaylight-oxygen-epel-7-x86_64-devel/"
76 "RUN yum remove opendaylight -y",
77 "RUN echo $'[opendaylight]\\n\\",
78 "baseurl={}\\n\\".format(odl_url),
80 "enabled=1' > /etc/yum.repos.d/opendaylight.repo",
81 "RUN yum -y install opendaylight"
83 src_img_uri = "192.0.2.1:8787/nova-api/centos-binary-master:latest"
84 assert mock_git_archive.called
85 assert mock_add_repo.called
86 assert mock_build_docker.called_once_with(
87 'opendaylight', '/dummytmp', docker_cmds, src_img_uri
89 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
91 @patch('builtins.open', a_mock_open)
93 @patch('os.path.isfile')
94 @patch('os.path.isdir')
95 def test_build_dockerfile(self, mock_isdir, mock_isfile, mock_makedirs):
96 src_img_uri = "192.0.2.1:8787/nova-api/centos-binary-master:latest"
97 oc_builder.build_dockerfile('nova-api', '/tmpdummy/', ['RUN dummy'],
99 a_mock_open.assert_called_with(
100 '/tmpdummy/containers/nova-api/Dockerfile', 'a+')
101 a_mock_open().write.assert_called_once_with('RUN dummy')
103 @patch('tarfile.open')
104 @patch('os.path.isdir')
105 def test_archive_docker_patches(self, mock_isdir, mock_tarfile):
106 oc_builder.archive_docker_patches('/tmpdummy/')
107 assert mock_tarfile.assert_called