def test_mod_repo_enabled(self): """ Checks if a repo is enabled or disabled depending on the passed kwargs. """ with patch.dict( aptpkg.__salt__, {"config.option": MagicMock(), "no_proxy": MagicMock(return_value=False)}, ): with patch("salt.modules.aptpkg._check_apt", MagicMock(return_value=True)): with patch( "salt.modules.aptpkg.refresh_db", MagicMock(return_value={}) ): with patch( "salt.utils.data.is_true", MagicMock(return_value=True) ) as data_is_true: with patch( "salt.modules.aptpkg.sourceslist", MagicMock(), create=True ): repo = aptpkg.mod_repo("foo", enabled=False) data_is_true.assert_called_with(False) # with disabled=True; should call salt.utils.data.is_true True data_is_true.reset_mock() repo = aptpkg.mod_repo("foo", disabled=True) data_is_true.assert_called_with(True) # with enabled=True; should call salt.utils.data.is_true with False data_is_true.reset_mock() repo = aptpkg.mod_repo("foo", enabled=True) data_is_true.assert_called_with(True) # with disabled=True; should call salt.utils.data.is_true False data_is_true.reset_mock() repo = aptpkg.mod_repo("foo", disabled=False) data_is_true.assert_called_with(False)
def test_mod_repo_enabled(self): ''' Checks if a repo is enabled or disabled depending on the passed kwargs. ''' with patch.dict( aptpkg.__salt__, { 'config.option': MagicMock(), 'no_proxy': MagicMock(return_value=False) }): with patch('salt.modules.aptpkg._check_apt', MagicMock(return_value=True)): with patch('salt.modules.aptpkg.refresh_db', MagicMock(return_value={})): with patch('salt.utils.data.is_true', MagicMock(return_value=True)) as data_is_true: with patch('salt.modules.aptpkg.sourceslist', MagicMock(), create=True): repo = aptpkg.mod_repo('foo', enabled=False) data_is_true.assert_called_with(False) # with disabled=True; should call salt.utils.data.is_true True data_is_true.reset_mock() repo = aptpkg.mod_repo('foo', disabled=True) data_is_true.assert_called_with(True) # with enabled=True; should call salt.utils.data.is_true with False data_is_true.reset_mock() repo = aptpkg.mod_repo('foo', enabled=True) data_is_true.assert_called_with(True) # with disabled=True; should call salt.utils.data.is_true False data_is_true.reset_mock() repo = aptpkg.mod_repo('foo', disabled=False) data_is_true.assert_called_with(False)
def test_mod_repo_match(): """ Checks if a repo is matched without taking into account any ending "/" in the uri. """ source_type = "deb" source_uri = "http://cdn-aws.deb.debian.org/debian/" source_line = "deb http://cdn-aws.deb.debian.org/debian/ stretch main\n" mock_source = MockSourceEntry( source_uri, source_type, source_line, False, "stretch" ) mock_source_list = MockSourceList() mock_source_list.list = [mock_source] with patch.dict( aptpkg.__salt__, {"config.option": MagicMock(), "no_proxy": MagicMock(return_value=False)}, ): with patch("salt.modules.aptpkg.refresh_db", MagicMock(return_value={})): with patch("salt.utils.data.is_true", MagicMock(return_value=True)): with patch("salt.modules.aptpkg.SourceEntry", MagicMock(), create=True): with patch( "salt.modules.aptpkg.SourcesList", MagicMock(return_value=mock_source_list), create=True, ): with patch( "salt.modules.aptpkg._split_repo_str", MagicMock( return_value=( "deb", [], "http://cdn-aws.deb.debian.org/debian/", "stretch", ["main"], "", ) ), ): source_line_no_slash = ( "deb http://cdn-aws.deb.debian.org/debian" " stretch main" ) if salt.utils.path.which("apt-key"): repo = aptpkg.mod_repo( source_line_no_slash, enabled=False ) assert repo[source_line_no_slash]["uri"] == source_uri else: with pytest.raises(Exception) as err: repo = aptpkg.mod_repo( source_line_no_slash, enabled=False ) assert ( "missing 'signedby' option when apt-key is missing" in str(err.value) )
def test_mod_repo_match(): """ Checks if a repo is matched without taking into account any ending "/" in the uri. """ source_type = "deb" source_uri = "http://cdn-aws.deb.debian.org/debian/" source_line = "deb http://cdn-aws.deb.debian.org/debian/ stretch main\n" mock_source = MockSourceEntry(source_uri, source_type, source_line, False, "stretch") mock_source_list = MockSourceList() mock_source_list.list = [mock_source] with patch.dict( aptpkg.__salt__, { "config.option": MagicMock(), "no_proxy": MagicMock(return_value=False) }, ): with patch("salt.modules.aptpkg._check_apt", MagicMock(return_value=True)): with patch("salt.modules.aptpkg.refresh_db", MagicMock(return_value={})): with patch("salt.utils.data.is_true", MagicMock(return_value=True)): with patch( "salt.modules.aptpkg._check_apt", MagicMock(return_value=True), ): with patch( "salt.modules.aptpkg.sourceslist", MagicMock(), create=True, ): with patch( "salt.modules.aptpkg.sourceslist.SourcesList", MagicMock(return_value=mock_source_list), create=True, ): with patch( "salt.modules.aptpkg._split_repo_str", MagicMock(return_value=( "deb", [], "http://cdn-aws.deb.debian.org/debian/", "stretch", ["main"], )), ): source_line_no_slash = "deb http://cdn-aws.deb.debian.org/debian stretch main" repo = aptpkg.mod_repo( source_line_no_slash, enabled=False) assert (repo[source_line_no_slash]["uri"] == source_uri)
def test_mod_repo(revert_repo_file): """ Test aptpkg.mod_repo when the repo exists. """ test_repo, comps = get_current_repo() msg = "This is a test" with patch.dict(aptpkg.__salt__, {"config.option": Mock()}): ret = aptpkg.mod_repo(repo=test_repo, comments=msg) assert sorted(ret[list(ret.keys())[0]]["comps"]) == sorted(comps) ret = file.grep("/etc/apt/sources.list", msg) assert "#{}".format(msg) in ret["stdout"]
def test_mod_repo_no_file(tmp_path, revert_repo_file): """ Test aptpkg.mod_repo when the file does not exist. It should create the file. """ test_repo, comps = get_current_repo() test_file = str(tmp_path / "test_repo") with patch.dict(aptpkg.__salt__, {"config.option": Mock()}): ret = aptpkg.mod_repo(repo=test_repo, file=test_file) with salt.utils.files.fopen(test_file, "r") as fp: ret = fp.read() assert test_repo.split()[1] in ret.strip() for comp in comps: assert comp in ret