- compile testsuite
[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/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/ResStore.h"
13 #include "zypp/PathInfo.h"
14
15 #include "zypp/RepoManager.h"
16
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/parameterized_test.hpp>
19 #include <boost/test/unit_test_log.hpp>
20
21 #include "KeyRingTestReceiver.h"
22
23 using boost::unit_test::test_suite;
24 using boost::unit_test::test_case;
25 using namespace boost::unit_test::log;
26
27 using namespace std;
28 using namespace zypp;
29 using namespace zypp::filesystem;
30 using namespace zypp::repo;
31
32 void repomanager_test( const string &dir )
33 {
34   RepoManagerOptions opts;
35   
36   TmpDir tmpCachePath;
37   TmpDir tmpRawCachePath;
38   TmpDir tmpKnownReposPath;
39   
40   BOOST_CHECK_EQUAL( filesystem::copy_dir_content( Pathname(dir) + "/repos.d", tmpKnownReposPath.path() ), 0 );
41   
42   opts.repoCachePath = tmpCachePath.path();
43   opts.repoRawCachePath = tmpRawCachePath.path();
44   opts.knownReposPath = tmpKnownReposPath.path();
45   
46   RepoManager manager(opts);
47   
48   list<RepoInfo> repos = manager.knownRepositories();
49   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
50   
51   // now add a .repo file with 2 repositories in it
52   Url url;
53   url.setPathName((Pathname(dir) + "/proprietary.repo").asString());
54   url.setScheme("file");
55
56   manager.addRepositories(url);
57   
58   // check it was not overwriten the proprietary.repo file
59   BOOST_CHECK( PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
60   
61   // now there should be 6 repos
62   repos = manager.knownRepositories();
63   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 6);
64   
65   RepoInfo office_dup;
66   office_dup.setAlias("office");
67   BOOST_CHECK_THROW(manager.addRepository(office_dup), RepoAlreadyExistsException);
68
69   // delete the office repo inside the propietary_1.repo
70   RepoInfo office;
71   office.setAlias("office");
72   manager.removeRepository(office);
73   // now there should be 5 repos
74   repos = manager.knownRepositories();
75   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 5);
76   // the file still contained one repo, so it should still exists
77   BOOST_CHECK( PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
78   
79   // now delete the macromedia one
80   RepoInfo macromedia;
81   macromedia.setAlias("macromedia");
82   manager.removeRepository(macromedia);
83   repos = manager.knownRepositories();
84   BOOST_CHECK_EQUAL(repos.size(), (unsigned) 4);
85   // the file should not exist anymore
86   BOOST_CHECK( ! PathInfo(tmpKnownReposPath.path() + "/proprietary.repo_1").isExist() );
87   
88
89   // let test cache creation
90
91   RepoInfo repo;
92   repo.setAlias("foo");
93   Url repourl("dir:" + string(TESTS_SRC_DIR) + "/repo/yum/data/10.2-updates-subset");
94   //Url repourl("dir:/mounts/dist/install/stable-x86/suse");
95   //BOOST_CHECK_MESSAGE(0, repourl.asString());
96   repo.setBaseUrl(repourl);
97
98   KeyRingTestReceiver keyring_callbacks;
99   KeyRingTestSignalReceiver receiver;
100   
101   // disable sgnature checking
102   keyring_callbacks.answerTrustKey(true);
103   keyring_callbacks.answerAcceptVerFailed(true);
104   keyring_callbacks.answerAcceptUnknownKey(true);
105
106   // we have no metadata yet so this should throw
107   BOOST_CHECK_THROW( manager.buildCache(repo),
108                      RepoMetadataException );
109
110   // now refresh the metadata
111   manager.refreshMetadata(repo);
112   
113   BOOST_CHECK_MESSAGE( ! manager.isCached(repo),
114                        "Repo is not yet cached" );
115
116   // it is not cached, this should throw
117   BOOST_CHECK_THROW( manager.loadFromCache(repo),
118                      RepoNotCachedException );
119
120   // now cache should build normally
121   manager.buildCache(repo);
122
123    // the solv file should exists now
124   Pathname base = (opts.repoCachePath + repo.alias());
125   Pathname solvfile = base.extend(".solv");
126   Pathname cookiefile = base.extend(".cookie");
127   BOOST_CHECK_MESSAGE( PathInfo(solvfile).isExist(), "Solv file is created after caching: " + solvfile.asString());
128   BOOST_CHECK_MESSAGE( PathInfo(cookiefile).isExist(), "Cookie file is created after caching: " + cookiefile.asString());
129
130   BOOST_CHECK_MESSAGE( manager.isCached(repo),
131                        "Repo is cached now" );
132
133   MIL << "Repo already in cache, clean cache"<< endl;
134   manager.cleanCache(repo);
135
136   BOOST_CHECK_MESSAGE( !manager.isCached(repo),
137                        "Repo cache was just deleted, should not be cached now" );
138
139   // now cache should build normally
140   manager.buildCache(repo);
141
142   manager.loadFromCache(repo);
143
144   if ( manager.isCached(repo ) )
145   {
146     MIL << "Repo already in cache, clean cache"<< endl;
147     manager.cleanCache(repo);
148   }
149   MIL << "Parsing repository metadata..." << endl;
150   manager.buildCache(repo);
151 }
152
153 test_suite*
154 init_unit_test_suite( int argc, char* argv[] )
155 {
156   string datadir;
157   if (argc < 2)
158   {
159     datadir = TESTS_SRC_DIR;
160     datadir = (Pathname(datadir) + "/zypp/data/RepoManager").asString();
161     cout << "repomanager_test:"
162       " path to directory with test data required as parameter. Using " << datadir  << endl;
163     //return (test_suite *)0;
164   }
165   else
166   {
167     datadir = argv[1];
168   }
169
170   std::string const params[] = { datadir };
171     //set_log_stream( std::cout );
172   test_suite* test= BOOST_TEST_SUITE( "RepoManagerTest" );
173   test->add(BOOST_PARAM_TEST_CASE( &repomanager_test,
174                               (std::string const*)params, params+1));
175   return test;
176 }
177