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: "java-1.8.0-openjdk"},
47 {con.VIRT_INSTALL: 'opendaylight'}
49 oc_builder.inject_opendaylight(con.DEFAULT_ODL_VERSION, 'dummy.qcow2',
50 '/dummytmp/', uc_ip='192.0.2.2',
51 os_version=con.DEFAULT_OS_VERSION)
52 assert mock_git_archive.called
53 assert mock_add_repo.called
54 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
56 @patch('apex.builders.overcloud_builder.build_dockerfile')
57 @patch('apex.builders.common_builder.create_git_archive')
58 @patch('apex.builders.common_builder.add_repo')
59 @patch('apex.virtual.utils.virt_customize')
60 def test_inject_opendaylight_docker(self, mock_customize, mock_add_repo,
61 mock_git_archive, mock_build_docker):
62 mock_git_archive.return_value = '/dummytmp/puppet-opendaylight.tar'
63 archive = '/dummytmp/puppet-opendaylight.tar'
65 {con.VIRT_UPLOAD: "{}:/etc/puppet/modules/".format(archive)},
66 {con.VIRT_RUN_CMD: 'rm -rf /etc/puppet/modules/opendaylight'},
67 {con.VIRT_RUN_CMD: "cd /etc/puppet/modules/ && tar xvf "
68 "puppet-opendaylight.tar"},
69 {con.VIRT_INSTALL: "java-1.8.0-openjdk"},
71 oc_builder.inject_opendaylight('oxygen', 'dummy.qcow2',
72 '/dummytmp/', uc_ip='192.0.2.2',
73 os_version=con.DEFAULT_OS_VERSION,
75 odl_url = "https://nexus.opendaylight.org/content/repositories" \
76 "/opendaylight-oxygen-epel-7-x86_64-devel/"
78 "RUN yum remove opendaylight -y",
79 "RUN echo $'[opendaylight]\\n\\",
80 "baseurl={}\\n\\".format(odl_url),
82 "enabled=1' > /etc/yum.repos.d/opendaylight.repo",
83 "RUN yum -y install opendaylight"
85 src_img_uri = "192.0.2.1:8787/nova-api/centos-binary-master:latest"
86 assert mock_git_archive.called
87 assert mock_add_repo.called
88 assert mock_build_docker.called_once_with(
89 'opendaylight', '/dummytmp', docker_cmds, src_img_uri
91 mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
93 @patch('builtins.open', a_mock_open)
95 @patch('os.path.isfile')
96 @patch('os.path.isdir')
97 def test_build_dockerfile(self, mock_isdir, mock_isfile, mock_makedirs):
98 src_img_uri = "192.0.2.1:8787/nova-api/centos-binary-master:latest"
99 oc_builder.build_dockerfile('nova-api', '/tmpdummy/', ['RUN dummy'],
101 a_mock_open.assert_called_with(
102 '/tmpdummy/containers/nova-api/Dockerfile', 'a+')
103 a_mock_open().write.assert_called_once_with('RUN dummy')
105 @patch('tarfile.open')
106 @patch('os.path.isdir')
107 def test_archive_docker_patches(self, mock_isdir, mock_tarfile):
108 oc_builder.archive_docker_patches('/tmpdummy/')
109 assert mock_tarfile.assert_called