Bring in aarch64 support in apex
[apex.git] / apex / tests / test_apex_common_builder.py
index fe69ca2..3ff95bb 100644 (file)
@@ -24,6 +24,8 @@ DOCKER_YAML = {
     }
 }
 
+a_mock_open = mock_open(read_data=None)
+
 
 class TestCommonBuilder(unittest.TestCase):
     @classmethod
@@ -51,10 +53,52 @@ class TestCommonBuilder(unittest.TestCase):
         path = '/usr/lib/python2.7/site-packages/'
         self.assertEquals(c_builder.project_to_path(project), path)
 
+    def test_is_patch_promoted(self):
+        dummy_change = {'submitted': '2017-06-05 20:23:09.000000000',
+                        'status': 'MERGED'}
+        self.assertTrue(c_builder.is_patch_promoted(dummy_change,
+                                                    'master',
+                                                    con.DOCKERHUB_OOO))
+
+    def test_is_patch_promoted_docker(self):
+        dummy_change = {'submitted': '2017-06-05 20:23:09.000000000',
+                        'status': 'MERGED'}
+        dummy_image = 'centos-binary-opendaylight'
+        self.assertTrue(c_builder.is_patch_promoted(dummy_change,
+                                                    'master',
+                                                    con.DOCKERHUB_OOO,
+                                                    docker_image=dummy_image))
+
+    def test_patch_not_promoted(self):
+        dummy_change = {'submitted': '2900-06-05 20:23:09.000000000',
+                        'status': 'MERGED'}
+        self.assertFalse(c_builder.is_patch_promoted(dummy_change,
+                                                     'master',
+                                                     con.DOCKERHUB_OOO))
+
+    def test_patch_not_promoted_docker(self):
+        dummy_change = {'submitted': '2900-06-05 20:23:09.000000000',
+                        'status': 'MERGED'}
+        dummy_image = 'centos-binary-opendaylight'
+        self.assertFalse(c_builder.is_patch_promoted(dummy_change,
+                                                     'master',
+                                                     con.DOCKERHUB_OOO,
+                                                     docker_image=dummy_image))
+
+    def test_patch_not_promoted_and_not_merged(self):
+        dummy_change = {'submitted': '2900-06-05 20:23:09.000000000',
+                        'status': 'BLAH'}
+        self.assertFalse(c_builder.is_patch_promoted(dummy_change,
+                                                     'master',
+                                                     con.DOCKERHUB_OOO))
+
     @patch('builtins.open', mock_open())
+    @patch('apex.builders.common_builder.is_patch_promoted')
+    @patch('apex.build_utils.get_change')
     @patch('apex.build_utils.get_patch')
     @patch('apex.virtual.utils.virt_customize')
-    def test_add_upstream_patches(self, mock_customize, mock_get_patch):
+    def test_add_upstream_patches(self, mock_customize, mock_get_patch,
+                                  mock_get_change, mock_is_patch_promoted):
         mock_get_patch.return_value = None
         change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
         patches = [{
@@ -73,14 +117,18 @@ class TestCommonBuilder(unittest.TestCase):
             {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
                 project_path, patch_file)}]
         mock_get_patch.return_value = 'some random diff'
+        mock_is_patch_promoted.return_value = False
         c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/')
         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
 
     @patch('builtins.open', mock_open())
+    @patch('apex.builders.common_builder.is_patch_promoted')
+    @patch('apex.build_utils.get_change')
     @patch('apex.build_utils.get_patch')
     @patch('apex.virtual.utils.virt_customize')
     def test_add_upstream_patches_docker_puppet(
-            self, mock_customize, mock_get_patch):
+            self, mock_customize, mock_get_patch, mock_get_change,
+            mock_is_patch_promoted):
         change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
         patches = [{
             'change-id': change_id,
@@ -96,19 +144,22 @@ class TestCommonBuilder(unittest.TestCase):
             {con.VIRT_RUN_CMD: "cd {} && patch -p1 < {}".format(
                 project_path, patch_file)}]
         mock_get_patch.return_value = 'some random diff'
+        mock_is_patch_promoted.return_value = False
         c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/',
                                        uc_ip='192.0.2.1',
                                        docker_tag='latest')
         mock_customize.assert_called_once_with(test_virt_ops, 'dummy.qcow2')
 
     @patch('builtins.open', mock_open())
+    @patch('apex.builders.common_builder.is_patch_promoted')
+    @patch('apex.build_utils.get_change')
     @patch('apex.builders.common_builder.project_to_docker_image')
     @patch('apex.builders.overcloud_builder.build_dockerfile')
     @patch('apex.build_utils.get_patch')
     @patch('apex.virtual.utils.virt_customize')
     def test_add_upstream_patches_docker_python(
             self, mock_customize, mock_get_patch, mock_build_docker_file,
-            mock_project2docker):
+            mock_project2docker, ock_get_change, mock_is_patch_promoted):
         mock_project2docker.return_value = ['NovaApi']
         change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
         patches = [{
@@ -116,6 +167,7 @@ class TestCommonBuilder(unittest.TestCase):
             'project': 'openstack/nova'
         }]
         mock_get_patch.return_value = 'some random diff'
+        mock_is_patch_promoted.return_value = False
         services = c_builder.add_upstream_patches(patches, 'dummy.qcow2',
                                                   '/dummytmp/',
                                                   uc_ip='192.0.2.1',
@@ -124,6 +176,56 @@ class TestCommonBuilder(unittest.TestCase):
         assert mock_build_docker_file.called
         self.assertSetEqual(services, {'NovaApi'})
 
+    @patch('builtins.open', mock_open())
+    @patch('apex.builders.common_builder.is_patch_promoted')
+    @patch('apex.build_utils.get_change')
+    @patch('apex.builders.common_builder.project_to_docker_image')
+    @patch('apex.builders.overcloud_builder.build_dockerfile')
+    @patch('apex.build_utils.get_patch')
+    @patch('apex.virtual.utils.virt_customize')
+    def test_not_add_upstream_patches_docker_python(
+            self, mock_customize, mock_get_patch, mock_build_docker_file,
+            mock_project2docker, ock_get_change, mock_is_patch_promoted):
+        # Test that the calls are not made when the patch is already merged and
+        # promoted
+        mock_project2docker.return_value = ['NovaApi']
+        change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
+        patches = [{
+            'change-id': change_id,
+            'project': 'openstack/nova'
+        }]
+        mock_get_patch.return_value = 'some random diff'
+        mock_is_patch_promoted.return_value = True
+        services = c_builder.add_upstream_patches(patches, 'dummy.qcow2',
+                                                  '/dummytmp/',
+                                                  uc_ip='192.0.2.1',
+                                                  docker_tag='latest')
+        assert mock_customize.not_called
+        assert mock_build_docker_file.not_called
+        assert len(services) == 0
+
+    @patch('builtins.open', mock_open())
+    @patch('apex.builders.common_builder.is_patch_promoted')
+    @patch('apex.build_utils.get_change')
+    @patch('apex.build_utils.get_patch')
+    @patch('apex.virtual.utils.virt_customize')
+    def test_not_upstream_patches_docker_puppet(
+            self, mock_customize, mock_get_patch, mock_get_change,
+            mock_is_patch_promoted):
+        # Test that the calls are not made when the patch is already merged and
+        # promoted
+        change_id = 'I301370fbf47a71291614dd60e4c64adc7b5ebb42'
+        patches = [{
+            'change-id': change_id,
+            'project': 'openstack/puppet-tripleo'
+        }]
+        mock_get_patch.return_value = 'some random diff'
+        mock_is_patch_promoted.return_value = True
+        c_builder.add_upstream_patches(patches, 'dummy.qcow2', '/dummytmp/',
+                                       uc_ip='192.0.2.1',
+                                       docker_tag='latest')
+        assert mock_customize.not_called
+
     @patch('builtins.open', mock_open())
     @patch('apex.virtual.utils.virt_customize')
     def test_add_repo(self, mock_customize):
@@ -144,7 +246,8 @@ class TestCommonBuilder(unittest.TestCase):
                          '/dummytmp/dummyrepo.tar')
 
     def test_project_to_docker_image(self):
-        found_services = c_builder.project_to_docker_image(project='nova')
+        found_services = c_builder.project_to_docker_image('nova',
+                                                           con.DOCKERHUB_OOO)
         assert 'nova-api' in found_services
 
     @patch('apex.common.utils.open_webpage')
@@ -153,4 +256,55 @@ class TestCommonBuilder(unittest.TestCase):
         mock_open_web.return_value = b'{"blah": "blah"}'
         self.assertRaises(exceptions.ApexCommonBuilderException,
                           c_builder.project_to_docker_image,
-                          'nova')
+                          'nova',
+                          con.DOCKERHUB_OOO)
+
+    def test_get_neutron_driver(self):
+        ds_opts = {'dataplane': 'fdio',
+                   'sdn_controller': 'opendaylight',
+                   'odl_version': 'master',
+                   'vpn': False,
+                   'sriov': False}
+        self.assertEquals(c_builder.get_neutron_driver(ds_opts),
+                          'odl')
+        ds_opts['sdn_controller'] = None
+        ds_opts['vpp'] = True
+        self.assertEquals(c_builder.get_neutron_driver(ds_opts),
+                          'vpp')
+        ds_opts['sdn_controller'] = 'ovn'
+        self.assertEquals(c_builder.get_neutron_driver(ds_opts),
+                          'ovn')
+
+    @patch('apex.builders.common_builder.yaml')
+    @patch('apex.overcloud.deploy.os.path.isfile')
+    @patch('builtins.open', a_mock_open, create=True)
+    def test_prepare_container_images(self, mock_is_file, mock_yaml):
+        mock_yaml.safe_load.return_value = {
+            'parameter_defaults': {
+                'ContainerImagePrepare': [
+                    {'set':
+                        {'namespace': 'blah',
+                         'neutron_driver': 'null',
+                         }
+                     }
+                ]
+            }
+        }
+        expected_output = {
+            'parameter_defaults': {
+                'ContainerImagePrepare': [
+                    {'set':
+                        {'namespace': 'docker.io/tripleoqueens',
+                         'neutron_driver': 'odl',
+                         }
+                     }
+                ]
+            }
+        }
+
+        c_builder.prepare_container_images('dummy.yaml', 'queens',
+                                           'odl')
+        mock_yaml.safe_dump.assert_called_with(
+            expected_output,
+            a_mock_open.return_value,
+            default_flow_style=False)