def test_multiple_groups_only(self): depends = Depends("foo [(bar baz) (quux)]\n") self.assertTrue(depends._evaluate(depends._rules[0][1], ["bar", "baz"])) self.assertTrue(depends._evaluate(depends._rules[0][1], ["quux"])) self.assertFalse(depends._evaluate(depends._rules[0][1], ["baz"])) self.assertFalse(depends._evaluate(depends._rules[0][1], ["bar"]))
def test_platforms_include(self): # 9 tests for the nine cases of _include in Depends depends = Depends(dedent("""\ # False, False -> False install1 [platform:dpkg quark] # False, None -> False install2 [platform:dpkg] # False, True -> False install3 [platform:dpkg test] # None, False -> False install4 [quark] # None, None -> True install5 # None, True -> True install6 [test] # True, False -> False install7 [platform:rpm quark] # True, None -> True install8 [platform:rpm] # True, True -> True install9 [platform:rpm test] """)) # With platform:dpkg and quark False and platform:rpm and test # True, the above mimics the conditions from _include. self.expectThat( set(r[0] for r in depends.active_rules(['platform:rpm', 'test'])), Equals({"install5", "install6", "install8", "install9"}))
def test_check_rule_present(self): depends = Depends("") depends.platform = mock.MagicMock() mock_depend_platform = self.useFixture( fixtures.MockPatchObject(depends.platform, 'get_pkg_version', return_value="123")).mock self.assertEqual([], depends.check_rules([("foo", [], [])])) mock_depend_platform.assert_called_once_with("foo")
def test_parser_accepts_full_path_to_tools(self): # at least yum/dnf allow these instead of mentioning rpm names depends = Depends(dedent("""\ /usr/bin/bash """)) self.assertEqual( [("/usr/bin/bash", [], [])], depends.active_rules(["default"]))
def test_detects_rhel_workstation(self): with DistroFixture("RHELWorkstation"): depends = Depends("") platform_profiles = depends.platform_profiles() self.assertThat(platform_profiles, Contains("platform:redhatenterpriseworkstation")) self.assertThat(platform_profiles, Contains("platform:rhel")) self.assertThat(platform_profiles, Contains("platform:redhat"))
def test_detects_opensuse_leap15(self): with DistroFixture("openSUSEleap15"): depends = Depends("") platform_profiles = depends.platform_profiles() self.assertThat(platform_profiles, Contains("platform:opensuse")) self.assertThat(platform_profiles, Contains("platform:suse"))
def test_check_rule_present(self): depends = Depends("") mocker = mox.Mox() depends.platform = mocker.CreateMock(Platform) depends.platform.get_pkg_version("foo").AndReturn("123") mocker.ReplayAll() self.addCleanup(mocker.VerifyAll) self.assertEqual([], depends.check_rules([("foo", [], [])]))
def test_detects_unknown(self): with DistroFixture("Unknown"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:unknown")) with ExpectedException(Exception, "Uknown package manager for " "current platform."): depends.platform.get_pkg_version('x')
def test_finds_profiles(self): depends = Depends(dedent("""\ foo bar [something] quux [anotherthing !nothing] <=12 """)) self.assertThat( depends.profiles(), MatchesSetwise(*map( Equals, ["something", "anotherthing", "nothing"])))
def test_check_rule_missing_version(self): depends = Depends("") depends.platform = mock.MagicMock() mock_depend_platform = self.useFixture( fixtures.MockPatchObject(depends.platform, 'get_pkg_version', return_value=None)).mock self.assertEqual( [('missing', ['foo'])], depends.check_rules([("foo", [], [(">=", "1.2.3")])])) mock_depend_platform.assert_called_once_with("foo")
def test_check_rule_incompatible(self): depends = Depends("") mocker = mox.Mox() depends.platform = mocker.CreateMock(Platform) depends.platform.get_pkg_version("foo").AndReturn("123") mocker.ReplayAll() self.addCleanup(mocker.VerifyAll) self.assertEqual( [('badversion', [('foo', "!=123", "123")])], depends.check_rules([("foo", [], [("!=", "123")])]))
def test_detects_opensuse_project(self): # TODO what does an os-release for opensuse project look like? # Is this different than sles, leap, and tumbleweed? with DistroFixture("openSUSEleap"): depends = Depends("") platform_profiles = depends.platform_profiles() self.assertThat(platform_profiles, Contains("platform:opensuseproject")) self.assertThat(platform_profiles, Contains("platform:opensuse")) self.assertThat(platform_profiles, Contains("platform:opensuseproject-42.1")) self.assertThat(platform_profiles, Contains("platform:suse"))
def test_detects_rhel_workstation(self): with DistroFixture("RHELWorkstation"): depends = Depends("") platform_profiles = depends.platform_profiles() self.assertThat( platform_profiles, Contains("platform:redhatenterpriseworkstation")) self.assertThat( platform_profiles, Contains("platform:rhel")) self.assertThat( platform_profiles, Contains("platform:redhat"))
def test_platforms(self): depends = Depends(dedent("""\ install1 install2 [test] install3 [platform:rpm] install4 [platform:dpkg] install5 [quark] install6 [platform:dpkg test] install7 [quark test] install8 [platform:dpkg platform:rpm] install9 [platform:dpkg platform:rpm test] installA [!platform:dpkg] installB [!platform:dpkg test] installC [!platform:dpkg !test] installD [platform:dpkg !test] installE [platform:dpkg !platform:rpm] installF [platform:dpkg !platform:rpm test] installG [!platform:dpkg !platform:rpm] installH [!platform:dpkg !platform:rpm test] installI [!platform:dpkg !platform:rpm !test] installJ [platform:dpkg !platform:rpm !test] """)) # Platform-only rules and rules with no platform are activated # by a matching platform. self.expectThat( set(r[0] for r in depends.active_rules(['platform:dpkg'])), Equals({"install1", "install4", "install8", "installD", "installE", "installJ"})) # Non-platform rules matching one-or-more profiles plus any # matching platform guarded rules. self.expectThat( set(r[0] for r in depends.active_rules(['platform:dpkg', 'test'])), Equals({"install1", "install2", "install4", "install6", "install7", "install8", "install9", "installE", "installF"})) # When multiple platforms are present, none-or-any-platform is # enough to match. self.expectThat( set(r[0] for r in depends.active_rules(['platform:rpm'])), Equals({"install1", "install3", "install8", "installA", "installC"})) # If there are any platform profiles on a rule one of them # must match an active platform even when other profiles match # for the rule to be active. self.expectThat( set(r[0] for r in depends.active_rules(['platform:rpm', 'test'])), Equals({"install1", "install2", "install3", "install7", "install8", "install9", "installA", "installB"}))
def test_parser_patterns(self): depends = Depends(dedent("""\ foo bar [something] category/packagename # for gentoo baz [platform:this platform:that-those] blaz [platform:rpm !platform:opensuseproject-42.2] quux [anotherthing !nothing] <=12 womp # and a comment # a standalone comment and a blank line # all's ok? good then """)) self.assertEqual(len(depends.active_rules(['default'])), 3)
def main(depends=None): if depends is None: try: content = open("other-requirements.txt", "rt").read() except IOError: logging.error("No other-requirements.txt file found.") return 1 depends = Depends(content) parser = optparse.OptionParser() parser.add_option("--profiles", action="store_true", help="List the platform and configuration profiles.") opts, args = parser.parse_args() if opts.profiles: logging.info("Platform profiles:") for profile in depends.platform_profiles(): logging.info("%s", profile) logging.info("") logging.info("Configuration profiles:") for profile in depends.profiles(): logging.info("%s", profile) else: if args: profiles = args else: profiles = ["default"] profiles = profiles + depends.platform_profiles() rules = depends.active_rules(profiles) errors = depends.check_rules(rules) for error in errors: if error[0] == "missing": logging.info("Missing packages:") logging.info(" %s", " ".join(error[1])) if error[0] == "badversion": logging.info("Bad versions of installed packages:") for pkg, constraint, version in error[1]: logging.info(" %s version %s does not match %s", pkg, version, constraint) if errors: return 1 return 0
def test_suse_linux_implies_rpm(self): with DistroFixture("SLES"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:rpm")) self.assertIsInstance(depends.platform, Rpm)
def test_no_selector_active(self): depends = Depends("foo\n") self.assertEqual([("foo", [], [])], depends.active_rules(["default"]))
def test_alpine_implies_apk(self): with DistroFixture("Alpine"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:apk")) self.assertIsInstance(depends.platform, Apk)
def test_ubuntu_implies_dpkg(self): with DistroFixture("Ubuntu"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:dpkg")) self.assertIsInstance(depends.platform, Dpkg)
def test_positive_selector_not_in_profiles_inactive(self): depends = Depends("foo [on]\n") self.assertEqual([], depends.active_rules(["default"]))
def test_positive_selector_includes_rule(self): depends = Depends("foo [on]\n") self.assertEqual( [("foo", [(True, "on")], [])], depends.active_rules(["on", "off"]))
def test_opensuse_leap15_implies_rpm(self): with DistroFixture("openSUSEleap15"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:rpm")) self.assertIsInstance(depends.platform, Rpm)
def test_arch_implies_pacman(self): with DistroFixture("Arch"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:pacman")) self.assertIsInstance(depends.platform, Pacman)
def test_3tuple(self): depends = Depends(u"erlang [(infra rabbitmq hipe)]\n") self.assertEqual(sorted([u'infra', u'rabbitmq', u'hipe']), depends.profiles())
def test_list_all(self): depends = Depends(dedent("""\ install1 install2 [test] install3 [platform:rpm] install4 [platform:dpkg] install5 [quark] install6 [platform:dpkg test] install7 [quark test] install8 [platform:dpkg platform:rpm] install9 [platform:dpkg platform:rpm test] installA [!platform:dpkg] installB [!platform:dpkg test] installC [!platform:dpkg !test] installD [platform:dpkg !test] installE [platform:dpkg !platform:rpm] installF [platform:dpkg !platform:rpm test] installG [!platform:dpkg !platform:rpm] installH [!platform:dpkg !platform:rpm test] installI [!platform:dpkg !platform:rpm !test] installJ [platform:dpkg !platform:rpm !test] """)) rules_dpkg = depends.active_rules(['platform:dpkg']) result_dpkg = set(r[0] for r in rules_dpkg) self.assertEqual(result_dpkg, set(depends.list_all_packages(rules_dpkg, output_format='newline'))) self.assertEqual(result_dpkg, set(depends.list_all_packages(rules_dpkg, output_format='csv'))) rules_dpkg_test = depends.active_rules(['platform:dpkg', 'test']) result_dpkg_test = set(r[0] for r in rules_dpkg_test) self.assertEqual(result_dpkg_test, set(depends.list_all_packages( rules_dpkg_test, output_format='newline'))) self.assertEqual(result_dpkg_test, set(depends.list_all_packages( rules_dpkg_test, output_format='csv'))) rules_rpm = depends.active_rules(['platform:rpm']) result_rpm = set(r[0] for r in rules_rpm) self.assertEqual(result_rpm, set(depends.list_all_packages(rules_rpm, output_format='newline'))) self.assertEqual(result_rpm, set(depends.list_all_packages(rules_rpm, output_format='csv'))) rules_rpm_test = depends.active_rules(['platform:rpm', 'test']) result_rpm_test = set(r[0] for r in rules_rpm_test) self.assertEqual(result_rpm_test, set(depends.list_all_packages( rules_rpm_test, output_format='newline'))) self.assertEqual(result_rpm_test, set(depends.list_all_packages( rules_rpm_test, output_format='csv')))
def test_negative_selector_removes_rule(self): depends = Depends("foo [!off]\n") self.assertEqual([], depends.active_rules(["on", "off"]))
def test_empty_file(self): depends = Depends("") self.assertEqual([], depends.profiles())
def test_rhel_implies_rpm(self): with DistroFixture("RHELServer"): depends = Depends("") self.assertThat( depends.platform_profiles(), Contains("platform:rpm")) self.assertIsInstance(depends.platform, Rpm)
def test_platform_profiles_succeeds(self): with DistroFixture('Ubuntu'): depends = Depends("") self.assertIsInstance(depends.platform_profiles(), list)
def test_single_group_only(self): depends = Depends("foo [(bar)]\n") self.assertTrue(depends._evaluate(depends._rules[0][1], ["bar"])) self.assertFalse(depends._evaluate(depends._rules[0][1], ["baz"]))