7 #include "zypp/base/Logger.h"
8 #include "zypp/base/Exception.h"
9 #include "zypp/KeyRing.h"
10 #include "zypp/PublicKey.h"
11 #include "zypp/TmpPath.h"
12 #include "zypp/PathInfo.h"
14 #include "zypp/RepoManager.h"
16 #include <boost/test/unit_test.hpp>
17 #include <boost/test/parameterized_test.hpp>
18 #include <boost/test/unit_test_log.hpp>
20 #include "KeyRingTestReceiver.h"
22 using boost::unit_test::test_suite;
23 using boost::unit_test::test_case;
24 using namespace boost::unit_test::log;
28 using namespace zypp::filesystem;
29 using namespace zypp::repo;
31 void repomanager_test( const string &dir )
33 RepoManagerOptions opts;
36 TmpDir tmpRawCachePath;
37 TmpDir tmpKnownReposPath;
39 BOOST_CHECK_EQUAL( filesystem::copy_dir_content( Pathname(dir) + "/repos.d", tmpKnownReposPath.path() ), 0 );
41 opts.repoCachePath = tmpCachePath.path();
42 opts.repoRawCachePath = tmpRawCachePath.path();
43 opts.knownReposPath = tmpKnownReposPath.path();
45 RepoManager manager(opts);
47 list<RepoInfo> repos = manager.knownRepositories();
48 BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
50 // now add a .repo file with 2 repositories in it
52 url.setPathName((Pathname(dir) + "/proprietary.repo").asString());
53 url.setScheme("file");
55 manager.addRepositories(url);
57 // check it was not overwriten the proprietary.repo file
58 BOOST_CHECK( PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
60 // now there should be 6 repos
61 repos = manager.knownRepositories();
62 BOOST_CHECK_EQUAL(repos.size(), (unsigned) 6);
65 office_dup.setAlias("office");
66 BOOST_CHECK_THROW(manager.addRepository(office_dup), RepoAlreadyExistsException);
68 // delete the office repo inside the propietary_1.repo
70 office.setAlias("office");
71 manager.removeRepository(office);
72 // now there should be 5 repos
73 repos = manager.knownRepositories();
74 BOOST_CHECK_EQUAL(repos.size(), (unsigned) 5);
75 // the file still contained one repo, so it should still exists
76 BOOST_CHECK( PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
78 // now delete the macromedia one
80 macromedia.setAlias("macromedia");
81 manager.removeRepository(macromedia);
82 repos = manager.knownRepositories();
83 BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
84 // the file should not exist anymore
85 BOOST_CHECK( ! PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
88 // let test cache creation
92 Url repourl("dir:" + string(TESTS_SRC_DIR) + "/repo/yum/data/10.2-updates-subset");
93 //Url repourl("dir:/mounts/dist/install/stable-x86/suse");
94 //BOOST_CHECK_MESSAGE(0, repourl.asString());
95 repo.setBaseUrl(repourl);
97 KeyRingTestReceiver keyring_callbacks;
98 KeyRingTestSignalReceiver receiver;
100 // disable sgnature checking
101 keyring_callbacks.answerTrustKey(true);
102 keyring_callbacks.answerAcceptVerFailed(true);
103 keyring_callbacks.answerAcceptUnknownKey(true);
105 // we have no metadata yet so this should throw
106 BOOST_CHECK_THROW( manager.buildCache(repo),
107 RepoMetadataException );
109 // now refresh the metadata
110 manager.refreshMetadata(repo);
112 BOOST_CHECK_MESSAGE( ! manager.isCached(repo),
113 "Repo is not yet cached" );
115 // it is not cached, this should throw
116 BOOST_CHECK_THROW( manager.loadFromCache(repo),
117 RepoNotCachedException );
119 // now cache should build normally
120 manager.buildCache(repo);
122 // the solv file should exists now
123 Pathname base = (opts.repoCachePath + repo.alias());
124 Pathname solvfile = base.extend(".solv");
125 Pathname cookiefile = base.extend(".cookie");
126 BOOST_CHECK_MESSAGE( PathInfo(solvfile).isExist(), "Solv file is created after caching: " + solvfile.asString());
127 BOOST_CHECK_MESSAGE( PathInfo(cookiefile).isExist(), "Cookie file is created after caching: " + cookiefile.asString());
129 BOOST_CHECK_MESSAGE( manager.isCached(repo),
130 "Repo is cached now" );
132 MIL << "Repo already in cache, clean cache"<< endl;
133 manager.cleanCache(repo);
135 BOOST_CHECK_MESSAGE( !manager.isCached(repo),
136 "Repo cache was just deleted, should not be cached now" );
138 // now cache should build normally
139 manager.buildCache(repo);
141 manager.loadFromCache(repo);
143 if ( manager.isCached(repo ) )
145 MIL << "Repo already in cache, clean cache"<< endl;
146 manager.cleanCache(repo);
148 MIL << "Parsing repository metadata..." << endl;
149 manager.buildCache(repo);
153 init_unit_test_suite( int argc, char* argv[] )
158 datadir = TESTS_SRC_DIR;
159 datadir = (Pathname(datadir) + "/zypp/data/RepoManager").asString();
160 cout << "repomanager_test:"
161 " path to directory with test data required as parameter. Using " << datadir << endl;
162 //return (test_suite *)0;
169 std::string const params[] = { datadir };
170 //set_log_stream( std::cout );
171 test_suite* test= BOOST_TEST_SUITE( "RepoManagerTest" );
172 test->add(BOOST_PARAM_TEST_CASE( &repomanager_test,
173 (std::string const*)params, params+1));