Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess the default product name from the os-release files #3155

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion anaconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,15 @@ def setup_environment():
anaconda_logging.logger.setupVirtio(opts.virtiolog)

# Load the remaining configuration after a logging is set up.
conf.set_from_product(opts.product_name, opts.variant_name)
from pyanaconda import product
conf.set_from_product(
requested_product=opts.product_name,
requested_variant=opts.variant_name,
buildstamp_product=product.productName,
buildstamp_variant=product.productVariant,
default_product=util.get_os_release_value("NAME")
)

conf.set_from_files()
conf.set_from_opts(opts)

Expand Down
22 changes: 13 additions & 9 deletions pyanaconda/core/configuration/anaconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from pyanaconda.core.configuration.product import ProductLoader
from pyanaconda.core.configuration.ui import UserInterfaceSection
from pyanaconda.core.constants import ANACONDA_CONFIG_TMP, ANACONDA_CONFIG_DIR
from pyanaconda.product import productName, productVariant


__all__ = ["conf", "AnacondaConfiguration"]
Expand Down Expand Up @@ -179,7 +178,9 @@ def set_from_defaults(self):
self.read(path)
self.validate()

def set_from_product(self, requested_product="", requested_variant=""):
def set_from_product(self, requested_product="", requested_variant="",
buildstamp_product="", buildstamp_variant="",
default_product=""):
"""Set the configuration from the product configuration files.

We will use configuration files of a product requested by the user
Expand All @@ -190,6 +191,9 @@ def set_from_product(self, requested_product="", requested_variant=""):

:param str requested_product: a name of the requested product
:param str requested_variant: a name of the requested variant
:param str buildstamp_product: a name of the product from .buildstamp
:param str buildstamp_variant: a name of the variant from .buildstamp
:param str default_product: a name of the default product
"""
loader = ProductLoader()
loader.load_products(os.path.join(ANACONDA_CONFIG_DIR, "product.d"))
Expand All @@ -208,18 +212,18 @@ def set_from_product(self, requested_product="", requested_variant=""):
variant_name = requested_variant

# Or use the product name and the variant name from .buildstamp.
elif loader.check_product(productName, productVariant):
product_name = productName
variant_name = productVariant
elif loader.check_product(buildstamp_product, buildstamp_variant):
product_name = buildstamp_product
variant_name = buildstamp_variant

# Or the product name from .buildstamp.
elif loader.check_product(productName):
product_name = productName
elif loader.check_product(buildstamp_product):
product_name = buildstamp_product
variant_name = ""

# Or use the default product name.
elif loader.check_product("Fedora"):
product_name = "Fedora"
elif loader.check_product(default_product):
product_name = default_product
variant_name = ""

# Or fail.
Expand Down
109 changes: 109 additions & 0 deletions tests/nosetests/pyanaconda_tests/core/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tempfile
import unittest
from textwrap import dedent
from unittest.mock import patch

from blivet.size import Size

Expand All @@ -30,9 +31,13 @@
Configuration
from pyanaconda.core.configuration.storage import StorageSection
from pyanaconda.core.configuration.ui import UserInterfaceSection
from pyanaconda.core.util import get_os_release_value
from pyanaconda.modules.common.constants import services
from pyanaconda.core.constants import SOURCE_TYPE_CLOSEST_MIRROR

# Path to the configuration directory of the repo.
CONFIG_DIR = os.environ.get("ANACONDA_DATA")


class ConfigurationTestCase(unittest.TestCase):
"""Test the configuration support."""
Expand Down Expand Up @@ -298,6 +303,110 @@ def set_from_files_test(self):
["a.conf", "conf.d/b.conf", "conf.d/c.conf"]
)

def _check_configuration_sources(self, conf, file_names):
"""Check the loaded configuration sources."""
file_paths = [os.path.join(CONFIG_DIR, path) for path in file_names]
self.assertEqual(file_paths, conf.get_sources())

@patch("pyanaconda.core.configuration.anaconda.ANACONDA_CONFIG_DIR", CONFIG_DIR)
def set_from_no_product_test(self):
conf = AnacondaConfiguration.from_defaults()

with self.assertRaises(ConfigurationError) as cm:
conf.set_from_product()

expected = "Unable to find any suitable configuration files " \
"for this product."

self.assertEqual(str(cm.exception), expected)

@patch("pyanaconda.core.configuration.anaconda.ANACONDA_CONFIG_DIR", CONFIG_DIR)
def set_from_requested_product_test(self):
conf = AnacondaConfiguration.from_defaults()

# Test an unknown requested product.
with self.assertRaises(ConfigurationError) as cm:
conf.set_from_product(
requested_product="Unknown product",
requested_variant="Unknown variant",
)

expected = "Unable to find any suitable configuration files " \
"for the product name 'Unknown product' and the " \
"variant name 'Unknown variant'."

self.assertEqual(str(cm.exception), expected)

# Test a known requested product.
conf.set_from_product(
requested_product="Fedora",
requested_variant="Workstation",
)

self._check_configuration_sources(conf, [
"anaconda.conf",
"product.d/fedora.conf",
"product.d/fedora-workstation.conf"
])

@patch("pyanaconda.core.configuration.anaconda.ANACONDA_CONFIG_DIR", CONFIG_DIR)
def set_from_buildstamp_product_test(self):
conf = AnacondaConfiguration.from_defaults()

# Test an unknown .buildstamp product.
with self.assertRaises(ConfigurationError) as cm:
conf.set_from_product(
buildstamp_product="Unknown product",
buildstamp_variant="Unknown variant",
)

expected = "Unable to find any suitable configuration files " \
"for this product."

self.assertEqual(str(cm.exception), expected)

# Test a known .buildstamp product.
conf.set_from_product(
buildstamp_product="Fedora",
buildstamp_variant="Workstation",
)

self._check_configuration_sources(conf, [
"anaconda.conf",
"product.d/fedora.conf",
"product.d/fedora-workstation.conf"
])

@patch("pyanaconda.core.configuration.anaconda.ANACONDA_CONFIG_DIR", CONFIG_DIR)
def set_from_default_product_test(self):
conf = AnacondaConfiguration.from_defaults()

# Test an unknown default product.
with self.assertRaises(ConfigurationError) as cm:
conf.set_from_product(
default_product="Unknown product",
)

expected = "Unable to find any suitable configuration files " \
"for this product."

self.assertEqual(str(cm.exception), expected)

# Test a known default product.
conf.set_from_product(
default_product="Fedora"
)

self._check_configuration_sources(conf, [
"anaconda.conf",
"product.d/fedora.conf"
])

@patch("pyanaconda.core.configuration.anaconda.ANACONDA_CONFIG_DIR", CONFIG_DIR)
def set_from_detected_product_test(self):
conf = AnacondaConfiguration.from_defaults()
conf.set_from_product(get_os_release_value("NAME"))

def kickstart_modules_test(self):
conf = AnacondaConfiguration.from_defaults()

Expand Down