Merge #293 `Add tests for generating discinfo and media.repo files`

This commit is contained in:
Dennis Gilmore 2016-05-27 01:23:22 +00:00
commit 0e0e333982
3 changed files with 177 additions and 22 deletions

View File

@ -45,23 +45,22 @@ def write_discinfo(file_path, description, arch, disc_numbers=None, timestamp=No
raise TypeError("Invalid type: disc_numbers type is %s; expected: <list>" % type(disc_numbers))
if not timestamp:
timestamp = "%f" % time.time()
f = open(file_path, "w")
f.write("%s\n" % timestamp)
f.write("%s\n" % description)
f.write("%s\n" % arch)
if disc_numbers:
f.write("%s\n" % ",".join([str(i) for i in disc_numbers]))
f.close()
with open(file_path, "w") as f:
f.write("%s\n" % timestamp)
f.write("%s\n" % description)
f.write("%s\n" % arch)
if disc_numbers:
f.write("%s\n" % ",".join([str(i) for i in disc_numbers]))
return timestamp
def read_discinfo(file_path):
result = {}
f = open(file_path, "r")
result["timestamp"] = f.readline().strip()
result["description"] = f.readline().strip()
result["arch"] = f.readline().strip()
disc_numbers = f.readline().strip()
with open(file_path, "r") as f:
result["timestamp"] = f.readline().strip()
result["description"] = f.readline().strip()
result["arch"] = f.readline().strip()
disc_numbers = f.readline().strip()
if not disc_numbers:
result["disc_numbers"] = None
elif disc_numbers == "ALL":
@ -71,16 +70,11 @@ def read_discinfo(file_path):
return result
def write_media_repo(file_path, description, timestamp=None):
def write_media_repo(file_path, description, timestamp):
"""
Write media.repo file for the disc to be used on installed system.
PackageKit uses this.
"""
if not timestamp:
raise
timestamp = "%f" % time.time()
data = [
"[InstallMedia]",
"name=%s" % description,
@ -91,7 +85,6 @@ def write_media_repo(file_path, description, timestamp=None):
"",
]
repo_file = open(file_path, "w")
repo_file.write("\n".join(data))
repo_file.close()
with open(file_path, "w") as repo_file:
repo_file.write("\n".join(data))
return timestamp

View File

@ -36,7 +36,7 @@ def get_description(compose, variant, arch):
else:
result = "%s %s" % (compose.conf["release_name"], compose.conf["release_version"])
if compose.conf.get("release_is_layered", False):
result += "for %s %s" % (compose.conf["base_product_name"], compose.conf["base_product_version"])
result += " for %s %s" % (compose.conf["base_product_name"], compose.conf["base_product_version"])
result = result % {"variant_name": variant.name, "arch": arch}
return result

162
tests/test_metadata.py Normal file
View File

@ -0,0 +1,162 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import mock
import unittest
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from tests import helpers
from pungi import metadata
from pungi.compose_metadata import discinfo
def mock_time():
return 101010101.01
class DiscInfoTestCase(helpers.PungiTestCase):
def setUp(self):
super(DiscInfoTestCase, self).setUp()
self.path = os.path.join(self.topdir, 'compose/Server/x86_64/os/.discinfo')
@mock.patch('time.time', new=mock_time)
def test_write_discinfo_variant(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
})
metadata.write_discinfo(compose, 'x86_64', compose.variants['Server'])
with open(self.path) as f:
self.assertEqual(f.read().strip().split('\n'),
['101010101.010000',
'Test 1.0',
'x86_64',
'ALL'])
self.assertEqual(discinfo.read_discinfo(self.path),
{'timestamp': '101010101.010000',
'description': 'Test 1.0',
'disc_numbers': ['ALL'],
'arch': 'x86_64'})
@mock.patch('time.time', new=mock_time)
def test_write_discinfo_custom_description(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
'release_discinfo_description': 'Fuzzy %(variant_name)s.%(arch)s',
})
compose.variants['Server'].name = 'Server'
metadata.write_discinfo(compose, 'x86_64', compose.variants['Server'])
with open(self.path) as f:
self.assertEqual(f.read().strip().split('\n'),
['101010101.010000',
'Fuzzy Server.x86_64',
'x86_64',
'ALL'])
@mock.patch('time.time', new=mock_time)
def test_write_discinfo_layered_product(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
'release_is_layered': True,
'base_product_name': 'Base',
'base_product_version': 42,
})
metadata.write_discinfo(compose, 'x86_64', compose.variants['Server'])
with open(self.path) as f:
self.assertEqual(f.read().strip().split('\n'),
['101010101.010000',
'Test 1.0 for Base 42',
'x86_64',
'ALL'])
@mock.patch('time.time', new=mock_time)
def test_write_discinfo_integrated_layered_product(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
})
compose.variants['ILP'] = mock.Mock(uid='Server', arches=['x86_64'],
type='layered-product', is_empty=False,
release_name='Integrated',
release_version='2.1',
parent=compose.variants['Server'])
metadata.write_discinfo(compose, 'x86_64', compose.variants['ILP'])
with open(self.path) as f:
self.assertEqual(f.read().strip().split('\n'),
['101010101.010000',
'Integrated 2.1 for Test 1',
'x86_64',
'ALL'])
@mock.patch('time.time', new=mock_time)
def test_addons_dont_have_discinfo(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
})
compose.variants['ILP'] = mock.Mock(uid='Server', arches=['x86_64'],
type='addon', is_empty=False,
parent=compose.variants['Server'])
metadata.write_discinfo(compose, 'x86_64', compose.variants['ILP'])
self.assertFalse(os.path.isfile(self.path))
class MediaRepoTestCase(helpers.PungiTestCase):
def setUp(self):
super(MediaRepoTestCase, self).setUp()
self.path = os.path.join(self.topdir, 'compose/Server/x86_64/os/media.repo')
def test_write_media_repo(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
})
metadata.write_media_repo(compose, 'x86_64', compose.variants['Server'],
timestamp=123456)
with open(self.path) as f:
lines = f.read().strip().split('\n')
self.assertEqual(lines[0], '[InstallMedia]')
self.assertItemsEqual(lines[1:],
['name=Test 1.0',
'mediaid=123456',
'metadata_expire=-1',
'gpgcheck=0',
'cost=500'])
def test_addons_dont_have_media_repo(self):
compose = helpers.DummyCompose(self.topdir, {
'release_name': 'Test',
'release_version': '1.0',
})
compose.variants['ILP'] = mock.Mock(uid='Server', arches=['x86_64'],
type='addon', is_empty=False,
parent=compose.variants['Server'])
metadata.write_discinfo(compose, 'x86_64', compose.variants['ILP'])
self.assertFalse(os.path.isfile(self.path))
if __name__ == "__main__":
unittest.main()