blob: 6baf0658b4d0c9078973e690ebef6495405403cb [file] [log] [blame]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for create_pkg_info."""
from __future__ import print_function
import io
import unittest
import create_pkg_info
class CreatePkgInfoTest(unittest.TestCase):
"""All the tests."""
INSTALLED_PKG_INPUT_LINES = [
'app-arch/gzip-1.9',
'dev-libs/popt-1.16-r2',
'app-containers/docker-credential-helpers-0.6.3-r1',
'_not.real-category1+/_not-real_package1-12.34.56.78',
' _not.real-category1+/_not-real_package2-12.34.56.78-r26 ',
' _not.real-category1+/_not-real_package3-12.34.56.78_rc3 ',
' _not.real-category1+/_not-real_package4-12.34.56.78_rc3-r26 ',
' _not.real-category1+/_not-real_package5-12.34.56.78_pre2_rc3-r26 ',
'_not.real-category2+/_not-real_package1-12.34.56.78q',
'_not.real-category2+/_not-real_package2-12.34.56.78q-r26',
'_not.real-category2+/_not-real_package3-12.34.56.78q_rc3',
'_not.real-category2+/_not-real_package4-12.34.56.78q_rc3-r26',
'_not.real-category2+/_not-real_package5-12.34.56.78q_pre2_rc3-r26'
]
BUILDTIME_PKG_INPUT_LINES = [
'virtual/pkgconfig-0-r1',
'dev-go/protobuf-1.3.2-r1',
'app-containers/docker-credential-helpers-0.6.3-r1',
'_not.real-category1+/_not-real_package1-12.34.56.78',
' _not.real-category1+/_not-real_package2-12.34.56.78-r26 ',
' _not.real-category1+/_not-real_package3-12.34.56.78_rc3 ',
' _not.real-category1+/_not-real_package4-12.34.56.78_rc3-r26 ',
' _not.real-category1+/_not-real_package5-12.34.56.78_pre2_rc3-r26 ',
'_not.real-category2+/_not-real_package1-12.34.56.78q',
'_not.real-category2+/_not-real_package2-12.34.56.78q-r26',
'_not.real-category2+/_not-real_package3-12.34.56.78q_rc3',
'_not.real-category2+/_not-real_package4-12.34.56.78q_rc3-r26',
'_not.real-category2+/_not-real_package5-12.34.56.78q_pre2_rc3-r26'
]
ALL_PKG_INPUT_LINES = [
'app-arch/gzip-1.9',
'dev-libs/popt-1.16-r2 overlay:portage-stable',
'virtual/pkgconfig-0-r1',
'dev-go/protobuf-1.3.2-r1 overlay:portage-stable',
'app-containers/docker-credential-helpers-0.6.3-r1 use_flags:+audit,+crypt,+multiarch,+ssp,+static-libs',
'sys-apps/systemd-248.6-r2 overlay:project-lakitu use_flags:+apparmor,+audit,+cgroup-hybrid',
]
INSTALLED_PKG_EXPECTED_LIST = [
{'category': 'app-arch', 'package': 'gzip', 'version_no_rev': '1.9',
'revision': None},
{'category': 'dev-libs', 'package': 'popt', 'version_no_rev': '1.16',
'revision': '2'},
{'category': 'app-containers', 'package': 'docker-credential-helpers',
'version_no_rev': '0.6.3', 'revision': '1'},
{'category': '_not.real-category1+', 'package': '_not-real_package1',
'version_no_rev': '12.34.56.78', 'revision': None},
{'category': '_not.real-category1+', 'package': '_not-real_package2',
'version_no_rev': '12.34.56.78', 'revision': '26'},
{'category': '_not.real-category1+', 'package': '_not-real_package3',
'version_no_rev': '12.34.56.78_rc3', 'revision': None},
{'category': '_not.real-category1+', 'package': '_not-real_package4',
'version_no_rev': '12.34.56.78_rc3', 'revision': '26'},
{'category': '_not.real-category1+', 'package': '_not-real_package5',
'version_no_rev': '12.34.56.78_pre2_rc3', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package1',
'version_no_rev': '12.34.56.78q', 'revision': None},
{'category': '_not.real-category2+', 'package': '_not-real_package2',
'version_no_rev': '12.34.56.78q', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package3',
'version_no_rev': '12.34.56.78q_rc3', 'revision': None},
{'category': '_not.real-category2+', 'package': '_not-real_package4',
'version_no_rev': '12.34.56.78q_rc3', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package5',
'version_no_rev': '12.34.56.78q_pre2_rc3', 'revision': '26'}
]
BUILDTIME_PKG_EXPECTED_LIST = [
{'category': 'virtual', 'package': 'pkgconfig', 'version_no_rev': '0',
'revision': '1'},
{'category': 'dev-go', 'package': 'protobuf', 'version_no_rev': '1.3.2',
'revision': '1'},
{'category': 'app-containers', 'package': 'docker-credential-helpers',
'version_no_rev': '0.6.3', 'revision': '1'},
{'category': '_not.real-category1+', 'package': '_not-real_package1',
'version_no_rev': '12.34.56.78', 'revision': None},
{'category': '_not.real-category1+', 'package': '_not-real_package2',
'version_no_rev': '12.34.56.78', 'revision': '26'},
{'category': '_not.real-category1+', 'package': '_not-real_package3',
'version_no_rev': '12.34.56.78_rc3', 'revision': None},
{'category': '_not.real-category1+', 'package': '_not-real_package4',
'version_no_rev': '12.34.56.78_rc3', 'revision': '26'},
{'category': '_not.real-category1+', 'package': '_not-real_package5',
'version_no_rev': '12.34.56.78_pre2_rc3', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package1',
'version_no_rev': '12.34.56.78q', 'revision': None},
{'category': '_not.real-category2+', 'package': '_not-real_package2',
'version_no_rev': '12.34.56.78q', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package3',
'version_no_rev': '12.34.56.78q_rc3', 'revision': None},
{'category': '_not.real-category2+', 'package': '_not-real_package4',
'version_no_rev': '12.34.56.78q_rc3', 'revision': '26'},
{'category': '_not.real-category2+', 'package': '_not-real_package5',
'version_no_rev': '12.34.56.78q_pre2_rc3', 'revision': '26'}
]
ALL_PKG_EXPECTED_LIST = [
{'category': 'app-arch', 'package': 'gzip', 'version_no_rev': '1.9',
'revision': None, 'overlay': None, 'use_flags': None},
{'category': 'dev-libs', 'package': 'popt', 'version_no_rev': '1.16',
'revision': '2', 'overlay': 'overlay:portage-stable', 'use_flags': None},
{'category': 'virtual', 'package': 'pkgconfig', 'version_no_rev': '0',
'revision': '1', 'overlay': None, 'use_flags': None},
{'category': 'dev-go', 'package': 'protobuf', 'version_no_rev': '1.3.2',
'revision': '1', 'overlay': 'overlay:portage-stable', 'use_flags': None},
{'category': 'app-containers', 'package': 'docker-credential-helpers',
'version_no_rev': '0.6.3', 'revision': '1', 'overlay': None,
'use_flags': 'use_flags:+audit,+crypt,+multiarch,+ssp,+static-libs'},
{'category': 'sys-apps', 'package': 'systemd', 'version_no_rev': '248.6',
'revision': '2', 'overlay': 'overlay:project-lakitu',
'use_flags': 'use_flags:+apparmor,+audit,+cgroup-hybrid'}
]
EXPECTED_JSON = """{
"installedPackages": [
{
"category": "app-arch",
"name": "gzip",
"version": "123.0.1",
"ebuild_version": "1.9"
},
{
"category": "dev-libs",
"name": "popt",
"version": "123.0.1",
"ebuild_version": "1.16-r2"
},
{
"category": "app-containers",
"name": "docker-credential-helpers",
"version": "123.0.1",
"ebuild_version": "0.6.3-r1"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package1",
"version": "123.0.1",
"ebuild_version": "12.34.56.78"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package2",
"version": "123.0.1",
"ebuild_version": "12.34.56.78-r26"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package3",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_rc3"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package4",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_rc3-r26"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package5",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_pre2_rc3-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package1",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package2",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package3",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_rc3"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package4",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_rc3-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package5",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_pre2_rc3-r26"
}
],
"buildTimePackages": [
{
"category": "virtual",
"name": "pkgconfig",
"version": "123.0.1",
"ebuild_version": "0-r1"
},
{
"category": "dev-go",
"name": "protobuf",
"version": "123.0.1",
"ebuild_version": "1.3.2-r1"
},
{
"category": "app-containers",
"name": "docker-credential-helpers",
"version": "123.0.1",
"ebuild_version": "0.6.3-r1"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package1",
"version": "123.0.1",
"ebuild_version": "12.34.56.78"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package2",
"version": "123.0.1",
"ebuild_version": "12.34.56.78-r26"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package3",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_rc3"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package4",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_rc3-r26"
},
{
"category": "_not.real-category1+",
"name": "_not-real_package5",
"version": "123.0.1",
"ebuild_version": "12.34.56.78_pre2_rc3-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package1",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package2",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package3",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_rc3"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package4",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_rc3-r26"
},
{
"category": "_not.real-category2+",
"name": "_not-real_package5",
"version": "123.0.1",
"ebuild_version": "12.34.56.78q_pre2_rc3-r26"
}
]
}"""
DEBUG_EXPECTED_JSON = """{
"allPackages": [
{
"category": "app-arch",
"name": "gzip",
"version": "123.0.1",
"ebuild_version": "1.9"
},
{
"category": "dev-libs",
"name": "popt",
"version": "123.0.1",
"ebuild_version": "1.16-r2",
"overlay": "portage-stable"
},
{
"category": "virtual",
"name": "pkgconfig",
"version": "123.0.1",
"ebuild_version": "0-r1"
},
{
"category": "dev-go",
"name": "protobuf",
"version": "123.0.1",
"ebuild_version": "1.3.2-r1",
"overlay": "portage-stable"
},
{
"category": "app-containers",
"name": "docker-credential-helpers",
"version": "123.0.1",
"ebuild_version": "0.6.3-r1",
"use_flags": "+audit,+crypt,+multiarch,+ssp,+static-libs"
},
{
"category": "sys-apps",
"name": "systemd",
"version": "123.0.1",
"ebuild_version": "248.6-r2",
"overlay": "project-lakitu",
"use_flags": "+apparmor,+audit,+cgroup-hybrid"
}
]
}"""
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self.maxDiff = None
def testCreateList(self):
self.assertEqual(
create_pkg_info.CreateList(self.INSTALLED_PKG_INPUT_LINES, False),
self.INSTALLED_PKG_EXPECTED_LIST)
self.assertEqual(
create_pkg_info.CreateList(self.BUILDTIME_PKG_INPUT_LINES, False),
self.BUILDTIME_PKG_EXPECTED_LIST)
def testCreateDebugList(self):
self.assertEqual(
create_pkg_info.CreateList(self.ALL_PKG_INPUT_LINES, True),
self.ALL_PKG_EXPECTED_LIST)
def testWriteJson(self):
mock_file = io.StringIO()
pkg_info = {
'installedPackages': self.INSTALLED_PKG_EXPECTED_LIST,
'buildTimePackages': self.BUILDTIME_PKG_EXPECTED_LIST,
}
create_pkg_info.WriteJson(pkg_info, '123.0.1', mock_file, False)
print(mock_file.getvalue())
self.assertEqual(mock_file.getvalue(), self.EXPECTED_JSON)
mock_file.close()
def testWriteDebugJson(self):
mock_file = io.StringIO()
pkg_info = {
'allPackages': self.ALL_PKG_EXPECTED_LIST,
}
create_pkg_info.WriteJson(pkg_info, '123.0.1', mock_file, True)
print(mock_file.getvalue())
self.assertEqual(mock_file.getvalue(), self.DEBUG_EXPECTED_JSON)
mock_file.close()
if __name__ == '__main__':
unittest.main()