Add initial code
[ta/build-tools.git] / tools / script / create_rpm_data_test.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # pylint: disable=invalid-name
16 import os
17 import sys
18 import logging
19 import pytest
20 import mock
21
22 from tools.script.create_rpm_data import RpmDataBuilder
23 from tools.repository import BuildConfigParser
24 from tools.executor import Result
25 import tools.repository
26 from tools.script.create_rpm_data_test_data import \
27     yum_installed_output, \
28     rpm_info_output, \
29     expected_output, \
30     basesystem_combined, \
31     cpp_combined, \
32     centos_logos_combined, \
33     dejavu_fonts_common_combined, yum_available_output, \
34     crypto_rpms_json, boms_output
35
36 from tools.test_data_rpm import basesystem_rpm_info, cpp_rpm_info, centos_logos_rpm_info, \
37     dejavu_fonts_common_rpm_info
38 from tools.test_data_yum import basesystem_yum_info, cpp_yum_info, centos_logos_yum_info, \
39     dejavu_fonts_common_yum_info
40 from tools.yum import YumConfig
41
42
43 @mock.patch.object(YumConfig, 'write')
44 @mock.patch.object(tools.yum, 'run')
45 @mock.patch.object(BuildConfigParser, 'items')
46 def test_complete_parse(mock_config, mock_reporeader, _):
47     logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
48     mock_config.side_effect = [
49         [('base', 'test-url-for-base'),
50          ('none1', 'test-url-for-none1'),
51          ('none2', 'test-url-for-none2')],
52         [('purkki-3rdparty', 'test-url-for-purkki-3rdparty#test-extra-option=test-value')],
53         [('base', 'src-test-url-for-base'),
54          ('none1', 'src-test-url-for-none1'),
55          ('none2', 'src-test-url-for-none2')],
56         [('purkki-3rdparty', 'src-test-url-for-purkki-3rdparty#test-extra-option2=test-value2')]
57     ]
58     mock_reporeader.side_effect = [Result(0, '', ''),  # yum clean all
59                                    Result(0, '', ''),  # rm -rf /var/yum/cache
60                                    Result(0, yum_available_output, '')]
61     os.environ['BUILD_URL'] = 'test-url/'
62     os.environ['WORKSPACE'] = '/foo/path'
63     result = RpmDataBuilder('fake_build_config_path',
64                             yum_installed_output,
65                             rpm_info_output,
66                             crypto_rpms_json,
67                             boms_output).run()
68     assert mock_reporeader.call_count == 3
69     assert result == expected_output
70
71
72 @pytest.mark.parametrize('yum_info, rpm_info, expected_combined', [
73     (basesystem_yum_info, basesystem_rpm_info, basesystem_combined),
74     (cpp_yum_info, cpp_rpm_info, cpp_combined),
75     (centos_logos_yum_info, centos_logos_rpm_info, centos_logos_combined),
76     (dejavu_fonts_common_yum_info, dejavu_fonts_common_rpm_info, dejavu_fonts_common_combined),
77 ])
78 def test_combine_rpm_data(yum_info, rpm_info, expected_combined):
79     result = RpmDataBuilder('fake_build_config_path',
80                             yum_info,
81                             rpm_info,
82                             '[]',
83                             {}).read_installed_rpms()
84     assert result[0] == expected_combined