1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
#ifndef INCLUDE_TESTSETUP
#define INCLUDE_TESTSETUP
#include <iostream>
#ifndef INCLUDE_TESTSETUP_WITHOUT_BOOST
#include <boost/test/auto_unit_test.hpp>
using boost::unit_test::test_case;
#endif
#include "zypp/base/LogControl.h"
#include "zypp/base/LogTools.h"
#include "zypp/base/InputStream.h"
#include "zypp/base/IOStream.h"
#include "zypp/base/Flags.h"
#include "zypp/ZYppFactory.h"
#include "zypp/ZYpp.h"
#include "zypp/TmpPath.h"
#include "zypp/Glob.h"
#include "zypp/PathInfo.h"
#include "zypp/RepoManager.h"
#include "zypp/Target.h"
#include "zypp/ResPool.h"
#include "Zypper.h"
#include "output/OutNormal.h"
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::flush;
using namespace zypp;
#ifndef BOOST_CHECK_NE
#define BOOST_CHECK_NE( L, R ) BOOST_CHECK( (L) != (R) )
#endif
#define LABELED(V) #V << ":\t" << V
inline std::string getXmlNodeVal( const std::string & line_r, const std::string & node_r )
{
std::string::size_type pos = line_r.find( node_r + "=\"" );
if ( pos != std::string::npos )
{
pos += node_r.size() + 2;
std::string::size_type epos = line_r.find( "\"", pos );
return line_r.substr( pos, epos-pos );
}
return std::string();
}
enum TestSetupOptionBits
{
TSO_CLEANROOT = (1 << 0)
};
ZYPP_DECLARE_FLAGS_AND_OPERATORS( TestSetupOptions, TestSetupOptionBits );
/** Build a test environment below a temp. root directory.
* If a \c rootdir_r was provided to the ctor, this directory
* will be used and it will \b not be removed.
*
* \note The lifetime of this objects is the lifetime of the temp. root directory.
*
* \code
* #include "TestSetup.h"
*
* BOOST_AUTO_TEST_CASE(WhatProvides)
* {
* // enables logging fot the scope of this block:
* // base::LogControl::TmpLineWriter shutUp( new log::FileLineWriter( "/tmp/YLOG" ) );
*
* TestSetup test( Arch_x86_64 );
* // test.loadTarget(); // initialize and load target
* test.loadRepo( TESTS_SRC_DIR"/data/openSUSE-11.1" );
*
* // Here the pool is ready to be used.
*
* }
* \endcode
*/
class TestSetup
{
public:
typedef TestSetupOptions Options;
public:
TestSetup( const Arch & sysarch_r = Arch_empty )
{ _ctor( Pathname(), sysarch_r, Options() ); }
TestSetup( const Pathname & rootdir_r, const Arch & sysarch_r = Arch_empty, const Options & options_r = Options() )
{ _ctor( rootdir_r, sysarch_r, options_r ); }
TestSetup( const Pathname & rootdir_r, const Options & options_r )
{ _ctor( rootdir_r, Arch_empty, options_r ); }
~TestSetup()
{ USR << (_tmprootdir.path() == _rootdir ? "DELETE" : "KEEP") << " TESTSETUP below " << _rootdir << endl; }
public:
/** Whether directory \a path_r contains a solver testcase. */
static bool isTestcase( const Pathname & path_r )
{
return filesystem::PathInfo( path_r / "solver-test.xml" ).isFile();
}
/** Whether directory \a path_r contains a testsetup. */
static bool isTestSetup( const Pathname & path_r )
{
return filesystem::PathInfo( path_r / "repos.d" ).isDir() && filesystem::PathInfo( path_r / "raw" ).isDir();
}
public:
const Pathname & root() const { return _rootdir; }
Target & target() { if ( ! getZYpp()->getTarget() ) getZYpp()->initializeTarget( _rootdir ); return *getZYpp()->getTarget(); }
RepoManager repomanager() { return RepoManager( RepoManagerOptions::makeTestSetup( _rootdir ) ); }
ResPool pool() { return ResPool::instance(); }
ResPoolProxy poolProxy() { return pool().proxy(); }
sat::Pool satpool() { return sat::Pool::instance(); }
Resolver & resolver() { return *getZYpp()->resolver(); }
Zypper & zypper() { return _zypper; }
public:
/** Load target repo. */
void loadTarget()
{ target().load(); }
/** Fake @System repo from url. */
void loadTargetRepo( const Url & url_r )
{ loadRepo( url_r, sat::Pool::systemRepoAlias() ); }
/** Fake @System repo from Path. */
void loadTargetRepo( const Pathname & path_r )
{ loadRepo( path_r, sat::Pool::systemRepoAlias() ); }
/** Fake @System repo from helix repo. */
void loadTargetHelix( const Pathname & path_r )
{ loadHelix( path_r, sat::Pool::systemRepoAlias() ); }
public:
/** Directly load repoinfo to pool. */
void loadRepo( RepoInfo nrepo )
{
RepoManager rmanager( repomanager() );
if ( rmanager.hasRepo( nrepo ) )
nrepo.setAlias( RepoManager::makeStupidAlias( nrepo.url() ) );
rmanager.addRepository( nrepo );
rmanager.buildCache( nrepo );
rmanager.loadFromCache( nrepo );
}
/** Directly load repo from url to pool. */
void loadRepo( const Url & url_r, const std::string & alias_r = std::string() )
{
RepoInfo nrepo;
nrepo.setAlias( alias_r.empty() ? url_r.getHost()+":"+Pathname::basename(url_r.getPathName()) : alias_r );
nrepo.addBaseUrl( url_r );
nrepo.setGpgCheck( false );
loadRepo( nrepo );
}
/** Directly load repo from metadata(dir) or solvfile(file) to pool.
* An empty alias is guessed.
*/
void loadRepo( const Pathname & path_r, const std::string & alias_r = std::string() )
{
if ( filesystem::PathInfo( path_r ).isDir() )
{
loadRepo( path_r.asUrl(), alias_r );
return;
}
// .solv file is loaded directly using a faked RepoInfo
RepoInfo nrepo;
nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
satpool().addRepoSolv( path_r, nrepo );
}
/** Directly load repo from some location (url or absolute(!)path).
* An empty alias is guessed.
*/
void loadRepo( const std::string & loc_r, const std::string & alias_r = std::string() )
{
if ( *loc_r.c_str() == '/' )
{
loadRepo( Pathname( loc_r ), alias_r );
}
else
{
loadRepo( Url( loc_r ), alias_r );
}
}
/** Directly load repo from some location (url or absolute(!)path).
* An empty alias is guessed.
*/
void loadRepo( const char * loc_r, const std::string & alias_r = std::string() )
{ loadRepo( std::string( loc_r ? loc_r : "" ), alias_r ); }
private:
// repo data from solver-test.xml
struct RepoD {
DefaultIntegral<unsigned,0> priority;
std::string alias;
Url url;
};
public:
/** Directly load a helix repo from some testcase.
* An empty alias is guessed.
*/
void loadHelix( const Pathname & path_r, const std::string & alias_r = std::string() )
{
// .solv file is loaded directly using a faked RepoInfo
RepoInfo nrepo;
nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
satpool().addRepoHelix( path_r, nrepo );
}
// Load repos included in a solver testcase.
void loadTestcaseRepos( const Pathname & path_r )
{
filesystem::PathInfo pi( path_r / "solver-test.xml" );
if ( ! pi.isFile() )
{
ERR << "No testcase in " << filesystem::PathInfo( path_r ) << endl;
return;
}
// dumb parse
InputStream infile( pi.path() );
Arch sysarch( Arch_empty );
Url guessedUrl;
typedef std::map<std::string,RepoD> RepoI;
RepoI repoi;
for( iostr::EachLine in( infile ); in; in.next() )
{
if ( str::hasPrefix( *in, "\t<channel" ) )
{
RepoD & repod( repoi[getXmlNodeVal( *in, "file" )] );
repod.alias = getXmlNodeVal( *in, "name" );
repod.priority = str::strtonum<unsigned>( getXmlNodeVal( *in, "priority" ) );
repod.url = guessedUrl;
guessedUrl = Url();
}
else if ( str::hasPrefix( *in, "\t- url " ) )
{
std::string::size_type pos = in->find( ": " );
if ( pos != std::string::npos )
{
guessedUrl = Url( in->substr( pos+2 ) );
}
}
else if ( str::hasPrefix( *in, "\t<locale" ) )
{
satpool().addRequestedLocale( Locale( getXmlNodeVal( *in, "name" ) ) );
}
else if ( sysarch.empty() && str::hasPrefix( *in, "<setup" ) )
{
sysarch = Arch( getXmlNodeVal( *in, "arch" ) );
if ( ! sysarch.empty() )
ZConfig::instance().setSystemArchitecture( sysarch );
}
}
//
filesystem::Glob files( path_r/"*{.xml,.xml.gz}", filesystem::Glob::_BRACE );
for_( it, files.begin(), files.end() )
{
std::string basename( Pathname::basename( *it ) );
if ( str::hasPrefix( basename, "solver-test.xml" ) )
continue; // master index currently unevaluated
if ( str::hasPrefix( basename, "solver-system.xml" ) )
loadTargetHelix( *it );
else
{
const RepoD & repod( repoi[basename] );
RepoInfo nrepo;
nrepo.setAlias( repod.alias.empty() ? basename : repod.alias );
nrepo.setPriority( repod.priority );
nrepo.setBaseUrl( repod.url );
satpool().addRepoHelix( *it, nrepo );
}
}
poolProxy(); // prepare
}
public:
/** Load all enabled repos in repos.d to pool. */
void loadRepos()
{
RepoManager repoManager( repomanager() );
RepoInfoList repos = repoManager.knownRepositories();
for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
{
RepoInfo & nrepo( *it );
USR << nrepo << endl;
if ( ! nrepo.enabled() )
continue;
if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
{
if ( repoManager.isCached( nrepo ) )
{
USR << "cleanCache" << endl;
repoManager.cleanCache( nrepo );
}
//USR << "refreshMetadata" << endl;
//repoManager.refreshMetadata( nrepo );
USR << "buildCache" << endl;
repoManager.buildCache( nrepo );
}
USR << "Create from cache" << endl;
repoManager.loadFromCache( nrepo );
}
}
public:
/** Detect and load the system located at \a sysRoot.
*
* \a sysRoot needs to be a directory containing either a SolverTestcase,
* a TestSetup system or a real system. The provided repostitories are
* loaded into the pool (without refresh).
*/
static void LoadSystemAt( const Pathname & sysRoot, const Arch & _testSetupArch_r = Arch_x86_64 )
{
if ( ! PathInfo( sysRoot ).isDir() )
ZYPP_THROW( Exception("sysRoot argument needs to be a directory") );
if ( TestSetup::isTestcase( sysRoot ) )
{
USR << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
TestSetup test;
test.loadTestcaseRepos( sysRoot );
}
else if ( TestSetup::isTestSetup( sysRoot ) )
{
USR << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
TestSetup test( sysRoot, _testSetupArch_r );
test.loadRepos();
Pathname solvCachePath( RepoManagerOptions::makeTestSetup( test.root() ).repoSolvCachePath );
Pathname fakeTargetSolv( solvCachePath / sat::Pool::systemRepoAlias() / "solv" );
if ( PathInfo( fakeTargetSolv ).isFile() )
{
USR << str::form( "*** Fake TestSetup Target from '%s'", fakeTargetSolv.c_str() ) << endl;
test.target();
test.loadTargetRepo( fakeTargetSolv );
}
}
else
{
sat::Pool satpool( sat::Pool::instance() );
// a system
USR << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
if ( 1 )
{
USR << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
getZYpp()->initializeTarget( sysRoot );
getZYpp()->target()->load();
USR << satpool.systemRepo() << endl;
}
if ( 1 )
{
RepoManager repoManager( sysRoot );
RepoInfoList repos = repoManager.knownRepositories();
for_( it, repos.begin(), repos.end() )
{
RepoInfo & nrepo( *it );
if ( ! nrepo.enabled() )
continue;
if ( ! repoManager.isCached( nrepo ) )
{
USR << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
continue;
}
USR << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
try
{
repoManager.loadFromCache( nrepo );
USR << satpool.reposFind( nrepo.alias() ) << endl;
}
catch ( const Exception & exp )
{
USR << exp.asString() + "\n" + exp.historyAsString() << endl;
USR << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
continue;
}
}
}
}
}
private:
void _ctor( const Pathname & rootdir_r, const Arch & sysarch_r, const Options & options_r )
{
if ( rootdir_r.empty() )
_rootdir = _tmprootdir.path();
else
{
filesystem::assert_dir( (_rootdir = rootdir_r) );
if ( options_r.testFlag( TSO_CLEANROOT ) )
filesystem::clean_dir( _rootdir );
}
// set up the Zypper instance
_zypper.globalOptsNoConst().root_dir = _rootdir.asString();
_zypper.globalOptsNoConst().rm_options = zypp::RepoManagerOptions(_rootdir.asString());
_zypper.globalOptsNoConst().rm_options.knownReposPath = _rootdir / "repos.d";
_zypper.setOutputWriter(new OutNormal(Out::DEBUG));
if ( ! sysarch_r.empty() )
ZConfig::instance().setSystemArchitecture( sysarch_r );
USR << "CREATED TESTSETUP below " << _rootdir << endl;
}
private:
filesystem::TmpDir _tmprootdir;
Pathname _rootdir;
Zypper & _zypper = *Zypper::instance();
};
#endif //INCLUDE_TESTSETUP
|