revert isLocal metod and use downloads instead to decide when cache packages.
[platform/upstream/libzypp.git] / tests / zypp / RepoManager_test.cc
1
2 #include <iostream>
3 #include <fstream>
4 #include <list>
5 #include <string>
6
7 #include "zypp/base/LogTools.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"
13
14 #include "zypp/RepoManager.h"
15
16 #include <boost/test/auto_unit_test.hpp>
17
18
19 #include "KeyRingTestReceiver.h"
20
21 using boost::unit_test::test_suite;
22 using boost::unit_test::test_case;
23
24 using namespace std;
25 using namespace zypp;
26 using namespace zypp::filesystem;
27 using namespace zypp::repo;
28
29 #define DATADIR (Pathname(TESTS_SRC_DIR) + "/zypp/data/RepoManager")
30
31 BOOST_AUTO_TEST_CASE(repomanager_test)
32 {
33   TmpDir tmpCachePath;
34   RepoManagerOptions opts( RepoManagerOptions::makeTestSetup( tmpCachePath ) ) ;
35
36   filesystem::mkdir( opts.knownReposPath );
37   BOOST_CHECK_EQUAL( filesystem::copy_dir_content( DATADIR + "/repos.d", opts.knownReposPath ), 0 );
38
39   RepoManager manager(opts);
40
41   list<RepoInfo> repos = manager.knownRepositories();
42   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
43
44   // now add a .repo file with 2 repositories in it
45   Url url;
46   url.setPathName((DATADIR + "/proprietary.repo").asString());
47   url.setScheme("file");
48
49   manager.addRepositories(url);
50
51   // check it was not overwriten the proprietary.repo file
52   BOOST_CHECK( PathInfo(opts.knownReposPath + "/proprietary.repo_1").isExist() );
53
54   // now there should be 6 repos
55   repos = manager.knownRepositories();
56   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 6);
57
58   RepoInfo office_dup;
59   office_dup.setAlias("office");
60   BOOST_CHECK_THROW(manager.addRepository(office_dup), RepoAlreadyExistsException);
61
62   // delete the office repo inside the propietary_1.repo
63   RepoInfo office;
64   office.setAlias("office");
65   manager.removeRepository(office);
66   // now there should be 5 repos
67   repos = manager.knownRepositories();
68   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 5);
69   // the file still contained one repo, so it should still exists
70   BOOST_CHECK( PathInfo(opts.knownReposPath + "/proprietary.repo_1").isExist() );
71
72   // now delete the macromedia one
73   RepoInfo macromedia;
74   macromedia.setAlias("macromedia");
75   manager.removeRepository(macromedia);
76   repos = manager.knownRepositories();
77   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
78   // the file should not exist anymore
79   BOOST_CHECK( ! PathInfo(opts.knownReposPath + "/proprietary.repo_1").isExist() );
80
81
82   // let test cache creation
83
84   RepoInfo repo;
85   repo.setAlias("foo");
86   Url repourl("dir:" + string(TESTS_SRC_DIR) + "/repo/yum/data/10.2-updates-subset");
87   //Url repourl("dir:/mounts/dist/install/stable-x86/suse");
88   //BOOST_CHECK_MESSAGE(0, repourl.asString());
89   repo.setBaseUrl(repourl);
90
91   KeyRingTestReceiver keyring_callbacks;
92   KeyRingTestSignalReceiver receiver;
93
94   // disable sgnature checking
95   keyring_callbacks.answerTrustKey(true);
96   keyring_callbacks.answerAcceptVerFailed(true);
97   keyring_callbacks.answerAcceptUnknownKey(true);
98
99   // We have no metadata and cache yet
100   BOOST_CHECK_MESSAGE( !manager.isCached(repo), "Repo should not yet be cached" );
101
102   // This should download metadata and build the cache
103   manager.buildCache(repo);
104
105   // Now we have metadata and cache
106   BOOST_CHECK_MESSAGE( manager.isCached(repo), "Repo should be cached now" );
107
108   // Metadata are up to date
109   RepoManager::RefreshCheckStatus ref_stat = manager.checkIfToRefreshMetadata(repo, *repo.baseUrlsBegin());
110   SEC << endl << ref_stat << endl;
111   BOOST_CHECK_MESSAGE( ref_stat== RepoManager::REPO_UP_TO_DATE || ref_stat == RepoManager::REPO_CHECK_DELAYED, "Metadata should be up to date" );
112
113    // the solv file should exists now
114   Pathname base = (opts.repoCachePath / "solv" / repo.alias());
115   Pathname solvfile = base / "solv";
116   Pathname cookiefile = base / "cookie";
117   BOOST_CHECK_MESSAGE( PathInfo(solvfile).isExist(), "Solv file is created after caching: " + solvfile.asString());
118   BOOST_CHECK_MESSAGE( PathInfo(cookiefile).isExist(), "Cookie file is created after caching: " + cookiefile.asString());
119
120   MIL << "Repo already in cache, clean cache"<< endl;
121   manager.cleanCache(repo);
122
123   BOOST_CHECK_MESSAGE( !manager.isCached(repo),
124                        "Repo cache was just deleted, should not be cached now" );
125
126   // now cache should build normally
127   manager.buildCache(repo);
128
129   manager.loadFromCache(repo);
130
131   if ( manager.isCached(repo ) )
132   {
133     MIL << "Repo already in cache, clean cache"<< endl;
134     manager.cleanCache(repo);
135   }
136   MIL << "Parsing repository metadata..." << endl;
137   manager.buildCache(repo);
138 }
139
140 BOOST_AUTO_TEST_CASE(repo_seting_test)
141 {
142   RepoInfo repo;
143   repo.setAlias("foo");
144   repo.addBaseUrl(string("http://test.org"));
145   BOOST_CHECK_MESSAGE( repo.keepPackages(), "http scheme is not cached");
146   repo.setBaseUrl(string("ftp://test.org"));
147   BOOST_CHECK_MESSAGE( repo.keepPackages(), "ftp scheme is not cached");
148   repo.setBaseUrl(string("smb://test.org"));
149   BOOST_CHECK_MESSAGE( !repo.keepPackages(), "smb scheme is cached");
150   repo.setBaseUrl(string("file:///test.txt"));
151   BOOST_CHECK_MESSAGE( !repo.keepPackages(), "file scheme is cached");
152   repo.setBaseUrl(string("http://test.org"));
153   BOOST_CHECK_MESSAGE( repo.keepPackages(), "cache is depend on second url.");
154 }