class ProductConfigurationTestCase(unittest.TestCase): """Test the default product configurations.""" def setUp(self): """Set up the default loader.""" self._loader = ProductLoader() self._loader.load_products(PRODUCT_DIR) def _load_product(self, content): """Load a product configuration with the given content.""" with tempfile.NamedTemporaryFile("w") as f: f.write(content) f.flush() self._loader.load_product(f.name) return f.name def _check_product(self, product_name, variant_name, file_paths): """Check a product.""" self.assertTrue(self._loader.check_product(product_name, variant_name)) config_paths = self._loader.collect_configurations(product_name, variant_name) self.assertEqual(file_paths, config_paths) def _check_default_product(self, product_name, variant_name, file_names): """Check a default product.""" self._check_product( product_name, variant_name, [os.path.join(PRODUCT_DIR, path) for path in file_names] ) config = AnacondaConfiguration.from_defaults() paths = self._loader.collect_configurations(product_name, variant_name) for path in paths: config.read(path) config.validate() def _get_config(self, product_name, variant_name=""): """Get parsed config file.""" config = AnacondaConfiguration.from_defaults() paths = self._loader.collect_configurations(product_name, variant_name) for path in paths: config.read(path) config.validate() return config def fedora_products_test(self): self._check_default_product( "Fedora", "", ["fedora.conf"] ) self._check_default_product( "Fedora", "Server", ["fedora.conf", "fedora-server.conf"] ) self._check_default_product( "Fedora", "Workstation", ["fedora.conf", "fedora-workstation.conf"] ) self._check_default_product( "Fedora", "Workstation Live", ["fedora.conf", "fedora-workstation.conf", "fedora-workstation-live.conf"] ) self._check_default_product( "Fedora", "AtomicHost", ["fedora.conf", "fedora-atomic-host.conf"] ) self._check_default_product( "Fedora", "Silverblue", ["fedora.conf", "fedora-workstation.conf", "fedora-workstation-live.conf", "fedora-silverblue.conf"] ) def rhel_products_test(self): self._check_default_product( "Red Hat Enterprise Linux", "", ["rhel.conf"] ) self._check_default_product( "CentOS Linux", "", ["rhel.conf", "centos.conf"] ) self._check_default_product( "Scientific Linux", "", ["rhel.conf", "scientific-linux.conf"] ) def product_module_list_difference_test(self): """Test for expected Fedora & RHEL module list differences.""" fedora_config = self._get_config("Fedora") fedora_modules = fedora_config.anaconda.kickstart_modules rhel_config = self._get_config("Red Hat Enterprise Linux") rhel_modules = rhel_config.anaconda.kickstart_modules difference = list(set(rhel_modules) - set(fedora_modules)) expected_difference = ["org.fedoraproject.Anaconda.Modules.Subscription"] self.assertListEqual(difference, expected_difference) def valid_product_test(self): content = dedent(""" [Product] product_name = My Product """) base_path = self._load_product(content) self._check_product("My Product", "", [base_path]) content = dedent(""" [Product] product_name = My Next Product [Base Product] product_name = My Product """) path = self._load_product(content) self._check_product("My Next Product", "", [base_path, path]) def valid_variant_test(self): content = dedent(""" [Product] product_name = My Product variant_name = My Variant """) base_path = self._load_product(content) self._check_product("My Product", "My Variant", [base_path]) content = dedent(""" [Product] product_name = My Product variant_name = My Next Variant [Base Product] product_name = My Product variant_name = My Variant """) path = self._load_product(content) self._check_product("My Product", "My Next Variant", [base_path, path]) def invalid_product_test(self): with self.assertRaises(ConfigurationError): self._load_product("") with self.assertRaises(ConfigurationError): self._load_product("[Product]") with self.assertRaises(ConfigurationError): self._load_product("[Base Product]") content = dedent(""" [Product] variant_name = Server """) with self.assertRaises(ConfigurationError): self._load_product(content) content = dedent(""" [Base Product] product_name = My Product """) with self.assertRaises(ConfigurationError): self._load_product(content) def invalid_base_product_test(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = Nonexistent Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def repeated_base_product_test(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def existing_product_test(self): content = dedent(""" [Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._load_product(content) def find_nonexistent_product_test(self): self._loader.check_product("Nonexistent Product") self._loader.check_product("Nonexistent Product", "Nonexistent Variant") def ignore_invalid_product_test(self): with tempfile.TemporaryDirectory() as config_dir: # A correct product config. with open(os.path.join(config_dir, "1.conf"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 1 """)) # An invalid product config. with open(os.path.join(config_dir, "2.conf"), "w") as f: f.write("") # A product config with wrong file name. with open(os.path.join(config_dir, "3"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 3 """)) self._loader.load_products(config_dir) self.assertTrue(self._loader.check_product("My Product 1")) self.assertFalse(self._loader.check_product("My Product 2")) self.assertFalse(self._loader.check_product("My Product 3"))
class ProductConfigurationTestCase(unittest.TestCase): """Test the default product configurations.""" def setUp(self): """Set up the default loader.""" self.maxDiff = None self._loader = ProductLoader() self._loader.load_products(PRODUCT_DIR) def _load_product(self, content): """Load a product configuration with the given content.""" with tempfile.NamedTemporaryFile("w") as f: f.write(content) f.flush() self._loader.load_product(f.name) return f.name def _check_product(self, product_name, variant_name, file_paths): """Check a product.""" self.assertTrue(self._loader.check_product(product_name, variant_name)) config_paths = self._loader.collect_configurations(product_name, variant_name) self.assertEqual(file_paths, config_paths) def _check_partitioning(self, config, partitioning): with patch("pyanaconda.modules.storage.partitioning.automatic.utils.platform") as platform: platform.partitions = [] with patch("pyanaconda.modules.storage.partitioning.automatic.utils.conf", new=config): self.assertEqual(get_default_partitioning(), partitioning) def _check_default_product(self, product_name, variant_name, file_names, partitioning): """Check a default product.""" self._check_product( product_name, variant_name, [os.path.join(PRODUCT_DIR, path) for path in file_names] ) config = AnacondaConfiguration.from_defaults() paths = self._loader.collect_configurations(product_name, variant_name) for path in paths: config.read(path) config.validate() self._check_partitioning(config, partitioning) def _get_config(self, product_name, variant_name=""): """Get parsed config file.""" config = AnacondaConfiguration.from_defaults() paths = self._loader.collect_configurations(product_name, variant_name) for path in paths: config.read(path) config.validate() return config def test_fedora_products(self): self._check_default_product( "Fedora", "", ["fedora.conf"], WORKSTATION_PARTITIONING ) self._check_default_product( "Fedora", "Server", ["fedora.conf", "fedora-server.conf"], SERVER_PARTITIONING ) self._check_default_product( "Fedora", "Workstation", ["fedora.conf", "fedora-workstation.conf"], WORKSTATION_PARTITIONING ) self._check_default_product( "Fedora", "Silverblue", ["fedora.conf", "fedora-workstation.conf", "fedora-silverblue.conf"], WORKSTATION_PARTITIONING ) self._check_default_product( "Fedora-IoT", "IoT", ["fedora.conf", "fedora-iot.conf"], WORKSTATION_PARTITIONING ) self._check_default_product( "Fedora-ELN", "", ["rhel.conf", "fedora-eln.conf"], ENTERPRISE_PARTITIONING ) def test_rhel_products(self): self._check_default_product( "Red Hat Enterprise Linux", "", ["rhel.conf"], ENTERPRISE_PARTITIONING ) self._check_default_product( "CentOS Stream", "", ["rhel.conf", "centos.conf"], ENTERPRISE_PARTITIONING ) self._check_default_product( "RHVH", "", ["rhel.conf", "rhvh.conf"], VIRTUALIZATION_PARTITIONING ) self._check_default_product( "oVirt Node Next", "", ["rhel.conf", "centos.conf", "ovirt.conf"], VIRTUALIZATION_PARTITIONING ) self._check_default_product( "Scientific Linux", "", ["rhel.conf", "scientific-linux.conf"], ENTERPRISE_PARTITIONING ) def test_product_module_list_difference_fedora_rhel(self): """Test for expected Fedora & RHEL module list differences.""" fedora_config = self._get_config("Fedora") fedora_modules = fedora_config.anaconda.kickstart_modules rhel_config = self._get_config("Red Hat Enterprise Linux") rhel_modules = rhel_config.anaconda.kickstart_modules difference = list(set(rhel_modules) - set(fedora_modules)) expected_difference = ["org.fedoraproject.Anaconda.Modules.Subscription"] self.assertListEqual(difference, expected_difference) def test_product_module_difference_centos_rhel(self): """Test for expected CentOS & RHEL module list differences.""" centos_config = self._get_config("CentOS Stream") centos_modules = centos_config.anaconda.kickstart_modules rhel_config = self._get_config("Red Hat Enterprise Linux") rhel_modules = rhel_config.anaconda.kickstart_modules difference = list(set(rhel_modules) - set(centos_modules)) expected_difference = ["org.fedoraproject.Anaconda.Modules.Subscription"] self.assertListEqual(difference, expected_difference) def _compare_product_files(self, file_name, other_file_name, ignored_sections=()): parser = create_parser() read_config(parser, os.path.join(PRODUCT_DIR, file_name)) other_parser = create_parser() read_config(other_parser, os.path.join(PRODUCT_DIR, other_file_name)) # Ignore the specified and product-related sections. ignored_sections += ("Product", "Base Product") sections = set(parser.sections()).difference(ignored_sections) other_sections = set(other_parser.sections()).difference(ignored_sections) # Otherwise, the defined sections should be the same. self.assertEqual(sections, other_sections) for section in sections: # The defined options should be the same. self.assertEqual(parser.options(section), other_parser.options(section)) for key in parser.options(section): # The values of the options should be the same. self.assertEqual(parser.get(section, key), other_parser.get(section, key)) def test_ovirt_and_rhvh(self): """Test the similarity of oVirt Node Next with Red Hat Virtualization Host.""" self._compare_product_files("rhvh.conf", "ovirt.conf", ignored_sections=("License", )) def test_valid_product(self): content = dedent(""" [Product] product_name = My Product """) base_path = self._load_product(content) self._check_product("My Product", "", [base_path]) content = dedent(""" [Product] product_name = My Next Product [Base Product] product_name = My Product """) path = self._load_product(content) self._check_product("My Next Product", "", [base_path, path]) def test_valid_variant(self): content = dedent(""" [Product] product_name = My Product variant_name = My Variant """) base_path = self._load_product(content) self._check_product("My Product", "My Variant", [base_path]) content = dedent(""" [Product] product_name = My Product variant_name = My Next Variant [Base Product] product_name = My Product variant_name = My Variant """) path = self._load_product(content) self._check_product("My Product", "My Next Variant", [base_path, path]) def test_invalid_product(self): with self.assertRaises(ConfigurationError): self._load_product("") with self.assertRaises(ConfigurationError): self._load_product("[Product]") with self.assertRaises(ConfigurationError): self._load_product("[Base Product]") content = dedent(""" [Product] variant_name = Server """) with self.assertRaises(ConfigurationError): self._load_product(content) content = dedent(""" [Base Product] product_name = My Product """) with self.assertRaises(ConfigurationError): self._load_product(content) def test_invalid_base_product(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = Nonexistent Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def test_repeated_base_product(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def test_existing_product(self): content = dedent(""" [Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._load_product(content) def test_find_nonexistent_product(self): self._loader.check_product("Nonexistent Product") self._loader.check_product("Nonexistent Product", "Nonexistent Variant") def test_ignore_invalid_product(self): with tempfile.TemporaryDirectory() as config_dir: # A correct product config. with open(os.path.join(config_dir, "1.conf"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 1 """)) # An invalid product config. with open(os.path.join(config_dir, "2.conf"), "w") as f: f.write("") # A product config with wrong file name. with open(os.path.join(config_dir, "3"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 3 """)) self._loader.load_products(config_dir) self.assertTrue(self._loader.check_product("My Product 1")) self.assertFalse(self._loader.check_product("My Product 2")) self.assertFalse(self._loader.check_product("My Product 3"))
class ProductConfigurationTestCase(unittest.TestCase): """Test the default product configurations.""" def setUp(self): """Set up the default loader.""" self._loader = ProductLoader() self._loader.load_products(PRODUCT_DIR) def _load_product(self, content): """Load a product configuration with the given content.""" with tempfile.NamedTemporaryFile("w") as f: f.write(content) f.flush() self._loader.load_product(f.name) return f.name def _check_product(self, product_name, variant_name, file_paths): """Check a product.""" self.assertTrue(self._loader.check_product(product_name, variant_name)) config_paths = self._loader.collect_configurations(product_name, variant_name) self.assertEqual(file_paths, config_paths) def _check_default_product(self, product_name, variant_name, file_names): """Check a default product.""" self._check_product( product_name, variant_name, [os.path.join(PRODUCT_DIR, path) for path in file_names] ) config = AnacondaConfiguration.from_defaults() paths = self._loader.collect_configurations(product_name, variant_name) for path in paths: config.read(path) config.validate() def fedora_products_test(self): self._check_default_product( "Fedora", "", ["fedora.conf"] ) self._check_default_product( "Fedora", "Server", ["fedora.conf", "fedora-server.conf"] ) self._check_default_product( "Fedora", "Workstation", ["fedora.conf", "fedora-workstation.conf"] ) self._check_default_product( "Fedora", "AtomicHost", ["fedora.conf", "fedora-atomic-host.conf"] ) self._check_default_product( "Fedora", "Silverblue", ["fedora.conf", "fedora-workstation.conf", "fedora-silverblue.conf"] ) def rhel_products_test(self): self._check_default_product( "Red Hat Enterprise Linux", "", ["rhel.conf"] ) self._check_default_product( "CentOS Linux", "", ["rhel.conf", "centos.conf"] ) self._check_default_product( "Scientific Linux", "", ["rhel.conf", "scientific-linux.conf"] ) def valid_product_test(self): content = dedent(""" [Product] product_name = My Product """) base_path = self._load_product(content) self._check_product("My Product", "", [base_path]) content = dedent(""" [Product] product_name = My Next Product [Base Product] product_name = My Product """) path = self._load_product(content) self._check_product("My Next Product", "", [base_path, path]) def valid_variant_test(self): content = dedent(""" [Product] product_name = My Product variant_name = My Variant """) base_path = self._load_product(content) self._check_product("My Product", "My Variant", [base_path]) content = dedent(""" [Product] product_name = My Product variant_name = My Next Variant [Base Product] product_name = My Product variant_name = My Variant """) path = self._load_product(content) self._check_product("My Product", "My Next Variant", [base_path, path]) def invalid_product_test(self): with self.assertRaises(ConfigurationError): self._load_product("") with self.assertRaises(ConfigurationError): self._load_product("[Product]") with self.assertRaises(ConfigurationError): self._load_product("[Base Product]") content = dedent(""" [Product] variant_name = Server """) with self.assertRaises(ConfigurationError): self._load_product(content) content = dedent(""" [Base Product] product_name = My Product """) with self.assertRaises(ConfigurationError): self._load_product(content) def invalid_base_product_test(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = Nonexistent Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def repeated_base_product_test(self): content = dedent(""" [Product] product_name = My Product [Base Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._loader.collect_configurations("My Product") self.assertFalse(self._loader.check_product("My Product")) def existing_product_test(self): content = dedent(""" [Product] product_name = My Product """) self._load_product(content) with self.assertRaises(ConfigurationError): self._load_product(content) def find_nonexistent_product_test(self): self._loader.check_product("Nonexistent Product") self._loader.check_product("Nonexistent Product", "Nonexistent Variant") def ignore_invalid_product_test(self): with tempfile.TemporaryDirectory() as config_dir: # A correct product config. with open(os.path.join(config_dir, "1.conf"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 1 """)) # An invalid product config. with open(os.path.join(config_dir, "2.conf"), "w") as f: f.write("") # A product config with wrong file name. with open(os.path.join(config_dir, "3"), "w") as f: f.write(dedent(""" [Product] product_name = My Product 3 """)) self._loader.load_products(config_dir) self.assertTrue(self._loader.check_product("My Product 1")) self.assertFalse(self._loader.check_product("My Product 2")) self.assertFalse(self._loader.check_product("My Product 3"))