1 #ifndef INCLUDE_TESTSETUP
2 #define INCLUDE_TESTSETUP
5 #ifndef INCLUDE_TESTSETUP_WITHOUT_BOOST
6 #include <boost/test/auto_unit_test.hpp>
7 using boost::unit_test::test_case;
10 #include "zypp/base/LogControl.h"
11 #include "zypp/base/LogTools.h"
12 #include "zypp/base/InputStream.h"
13 #include "zypp/base/IOStream.h"
14 #include "zypp/base/Flags.h"
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/ZYpp.h"
17 #include "zypp/TmpPath.h"
18 #include "zypp/Glob.h"
19 #include "zypp/PathInfo.h"
20 #include "zypp/RepoManager.h"
21 #include "zypp/Target.h"
22 #include "zypp/ResPool.h"
31 #ifndef BOOST_CHECK_NE
32 #define BOOST_CHECK_NE( L, R ) BOOST_CHECK( (L) != (R) )
35 #define LABELED(V) #V << ":\t" << V
37 inline std::string getXmlNodeVal( const std::string & line_r, const std::string & node_r )
39 std::string::size_type pos = line_r.find( node_r + "=\"" );
40 if ( pos != std::string::npos )
42 pos += node_r.size() + 2;
43 std::string::size_type epos = line_r.find( "\"", pos );
44 return line_r.substr( pos, epos-pos );
49 enum TestSetupOptionBits
51 TSO_CLEANROOT = (1 << 0), // wipe rootdir in ctor
52 TSO_REPO_DEFAULT_GPG = (1 << 1), // dont turn off gpgcheck in repos
54 ZYPP_DECLARE_FLAGS_AND_OPERATORS( TestSetupOptions, TestSetupOptionBits );
56 /** Build a test environment below a temp. root directory.
57 * If a \c rootdir_r was provided to the ctor, this directory
58 * will be used and it will \b not be removed.
60 * \note The lifetime of this objects is the lifetime of the temp. root directory.
63 * #include "TestSetup.h"
65 * BOOST_AUTO_TEST_CASE(WhatProvides)
67 * // enabls loging fot the scope of this block:
68 * // base::LogControl::TmpLineWriter shutUp( new log::FileLineWriter( "/tmp/YLOG" ) );
70 * TestSetup test( Arch_x86_64 );
71 * // test.loadTarget(); // initialize and load target
72 * test.loadRepo( TESTS_SRC_DIR"/data/openSUSE-11.1" );
74 * // Here the pool is ready to be used.
82 typedef TestSetupOptions Options;
85 TestSetup( const Arch & sysarch_r = Arch_empty, const Options & options_r = Options() )
86 { _ctor( Pathname(), sysarch_r, options_r ); }
88 TestSetup( const Pathname & rootdir_r, const Arch & sysarch_r = Arch_empty, const Options & options_r = Options() )
89 { _ctor( rootdir_r, sysarch_r, options_r ); }
91 TestSetup( const Pathname & rootdir_r, const Options & options_r )
92 { _ctor( rootdir_r, Arch_empty, options_r ); }
95 { USR << (_tmprootdir.path() == _rootdir ? "DELETE" : "KEEP") << " TESTSETUP below " << _rootdir << endl; }
98 /** Whether directory \a path_r contains a solver testcase. */
99 static bool isTestcase( const Pathname & path_r )
101 return filesystem::PathInfo( path_r / "solver-test.xml" ).isFile();
104 /** Whether directory \a path_r contains a testsetup. */
105 static bool isTestSetup( const Pathname & path_r )
107 return filesystem::PathInfo( path_r / "repos.d" ).isDir() && filesystem::PathInfo( path_r / "raw" ).isDir();
111 const Pathname & root() const { return _rootdir; }
113 Target & target() { if ( ! getZYpp()->getTarget() ) getZYpp()->initializeTarget( _rootdir ); return *getZYpp()->getTarget(); }
114 RepoManager repomanager() { return RepoManager( RepoManagerOptions::makeTestSetup( _rootdir ) ); }
115 ResPool pool() { return ResPool::instance(); }
116 ResPoolProxy poolProxy() { return pool().proxy(); }
117 sat::Pool satpool() { return sat::Pool::instance(); }
118 Resolver & resolver() { return *getZYpp()->resolver(); }
121 /** Load target repo. */
124 /** Fake @System repo from url. */
125 void loadTargetRepo( const Url & url_r )
126 { loadRepo( url_r, sat::Pool::systemRepoAlias() ); }
127 /** Fake @System repo from Path. */
128 void loadTargetRepo( const Pathname & path_r )
129 { loadRepo( path_r, sat::Pool::systemRepoAlias() ); }
130 /** Fake @System repo from helix repo. */
131 void loadTargetHelix( const Pathname & path_r )
132 { loadHelix( path_r, sat::Pool::systemRepoAlias() ); }
135 /** Directly load repoinfo to pool. */
136 void loadRepo( RepoInfo nrepo )
138 RepoManager rmanager( repomanager() );
139 if ( rmanager.hasRepo( nrepo ) )
140 nrepo.setAlias( RepoManager::makeStupidAlias( nrepo.url() ) );
141 rmanager.addRepository( nrepo );
142 rmanager.buildCache( nrepo );
143 rmanager.loadFromCache( nrepo );
145 /** Directly load repo from url to pool. */
146 void loadRepo( const Url & url_r, const std::string & alias_r = std::string() )
149 nrepo.setAlias( alias_r.empty() ? url_r.getHost()+":"+Pathname::basename(url_r.getPathName()) : alias_r );
150 nrepo.addBaseUrl( url_r );
151 if ( ! _options.testFlag( TSO_REPO_DEFAULT_GPG ) )
152 nrepo.setGpgCheck( false );
155 /** Directly load repo from metadata(dir) or solvfile(file) to pool.
156 * An empty alias is guessed.
158 void loadRepo( const Pathname & path_r, const std::string & alias_r = std::string() )
160 if ( filesystem::PathInfo( path_r ).isDir() )
162 loadRepo( path_r.asUrl(), alias_r );
165 // .solv file is loaded directly using a faked RepoInfo
167 nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
168 satpool().addRepoSolv( path_r, nrepo );
170 /** Directly load repo from some location (url or absolute(!)path).
171 * An empty alias is guessed.
173 void loadRepo( const std::string & loc_r, const std::string & alias_r = std::string() )
175 if ( *loc_r.c_str() == '/' )
177 loadRepo( Pathname( loc_r ), alias_r );
181 loadRepo( Url( loc_r ), alias_r );
184 /** Directly load repo from some location (url or absolute(!)path).
185 * An empty alias is guessed.
187 void loadRepo( const char * loc_r, const std::string & alias_r = std::string() )
188 { loadRepo( std::string( loc_r ? loc_r : "" ), alias_r ); }
191 // repo data from solver-test.xml
193 DefaultIntegral<unsigned,0> priority;
199 /** Directly load a helix repo from some testcase.
200 * An empty alias is guessed.
202 void loadHelix( const Pathname & path_r, const std::string & alias_r = std::string() )
204 // .solv file is loaded directly using a faked RepoInfo
206 nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
207 satpool().addRepoHelix( path_r, nrepo );
210 // Load repos included in a solver testcase.
211 void loadTestcaseRepos( const Pathname & path_r )
213 filesystem::PathInfo pi( path_r / "solver-test.xml" );
216 ERR << "No testcase in " << filesystem::PathInfo( path_r ) << endl;
220 InputStream infile( pi.path() );
221 Arch sysarch( Arch_empty );
223 typedef std::map<std::string,RepoD> RepoI;
225 for( iostr::EachLine in( infile ); in; in.next() )
227 if ( str::hasPrefix( *in, "\t<channel" ) )
229 RepoD & repod( repoi[getXmlNodeVal( *in, "file" )] );
231 repod.alias = getXmlNodeVal( *in, "name" );
232 repod.priority = str::strtonum<unsigned>( getXmlNodeVal( *in, "priority" ) );
233 repod.url = guessedUrl;
236 else if ( str::hasPrefix( *in, "\t- url " ) )
238 std::string::size_type pos = in->find( ": " );
239 if ( pos != std::string::npos )
241 guessedUrl = Url( in->substr( pos+2 ) );
244 else if ( str::hasPrefix( *in, "\t<locale" ) )
246 satpool().addRequestedLocale( Locale( getXmlNodeVal( *in, "name" ) ) );
248 else if ( sysarch.empty() && str::hasPrefix( *in, "<setup" ) )
250 sysarch = Arch( getXmlNodeVal( *in, "arch" ) );
251 if ( ! sysarch.empty() )
252 ZConfig::instance().setSystemArchitecture( sysarch );
257 filesystem::Glob files( path_r/"*{.xml,.xml.gz}", filesystem::Glob::_BRACE );
258 for_( it, files.begin(), files.end() )
260 std::string basename( Pathname::basename( *it ) );
261 if ( str::hasPrefix( basename, "solver-test.xml" ) )
262 continue; // master index currently unevaluated
263 if ( str::hasPrefix( basename, "solver-system.xml" ) )
264 loadTargetHelix( *it );
267 const RepoD & repod( repoi[basename] );
270 nrepo.setAlias( repod.alias.empty() ? basename : repod.alias );
271 nrepo.setPriority( repod.priority );
272 nrepo.setBaseUrl( repod.url );
273 satpool().addRepoHelix( *it, nrepo );
277 poolProxy(); // prepare
281 /** Load all enabled repos in repos.d to pool. */
284 RepoManager repoManager( repomanager() );
285 RepoInfoList repos = repoManager.knownRepositories();
286 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
288 RepoInfo & nrepo( *it );
289 USR << nrepo << endl;
291 if ( ! nrepo.enabled() )
294 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
296 if ( repoManager.isCached( nrepo ) )
298 USR << "cleanCache" << endl;
299 repoManager.cleanCache( nrepo );
301 //USR << "refreshMetadata" << endl;
302 //repoManager.refreshMetadata( nrepo );
303 USR << "buildCache" << endl;
304 repoManager.buildCache( nrepo );
306 USR << "Create from cache" << endl;
307 repoManager.loadFromCache( nrepo );
312 /** Detect and load the system located at \a sysRoot.
314 * \a sysRoot needs to be a directory containing either a SolverTestcase,
315 * a TestSetup system or a real system. The provided repostitories are
316 * loaded into the pool (without refresh).
318 static void LoadSystemAt( const Pathname & sysRoot, const Arch & _testSetupArch_r = Arch_x86_64 )
320 if ( ! PathInfo( sysRoot ).isDir() )
321 ZYPP_THROW( Exception("sysRoot argument needs to be a directory") );
323 if ( TestSetup::isTestcase( sysRoot ) )
325 USR << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
327 test.loadTestcaseRepos( sysRoot );
329 else if ( TestSetup::isTestSetup( sysRoot ) )
331 USR << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
333 TestSetup test( sysRoot, _testSetupArch_r );
336 Pathname solvCachePath( RepoManagerOptions::makeTestSetup( test.root() ).repoSolvCachePath );
337 Pathname fakeTargetSolv( solvCachePath / sat::Pool::systemRepoAlias() / "solv" );
338 if ( PathInfo( fakeTargetSolv ).isFile() )
340 USR << str::form( "*** Fake TestSetup Target from '%s'", fakeTargetSolv.c_str() ) << endl;
342 test.loadTargetRepo( fakeTargetSolv );
347 sat::Pool satpool( sat::Pool::instance() );
349 USR << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
352 USR << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
353 getZYpp()->initializeTarget( sysRoot );
354 getZYpp()->target()->load();
355 USR << satpool.systemRepo() << endl;
360 RepoManager repoManager( sysRoot );
361 RepoInfoList repos = repoManager.knownRepositories();
362 for_( it, repos.begin(), repos.end() )
364 RepoInfo & nrepo( *it );
366 if ( ! nrepo.enabled() )
369 if ( ! repoManager.isCached( nrepo ) )
371 USR << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
375 USR << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
378 repoManager.loadFromCache( nrepo );
379 USR << satpool.reposFind( nrepo.alias() ) << endl;
381 catch ( const Exception & exp )
383 USR << exp.asString() + "\n" + exp.historyAsString() << endl;
384 USR << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
393 void _ctor( const Pathname & rootdir_r, const Arch & sysarch_r, const Options & options_r )
395 _options = options_r;
397 if ( rootdir_r.empty() )
398 _rootdir = _tmprootdir.path();
401 filesystem::assert_dir( (_rootdir = rootdir_r) );
402 if ( _options.testFlag( TSO_CLEANROOT ) )
403 filesystem::clean_dir( _rootdir );
406 if ( ! sysarch_r.empty() )
407 ZConfig::instance().setSystemArchitecture( sysarch_r );
408 USR << "CREATED TESTSETUP below " << _rootdir << endl;
411 filesystem::TmpDir _tmprootdir;
417 #endif //INCLUDE_TESTSETUP