libzypp  17.38.17
TestcaseSetup.cc
Go to the documentation of this file.
1 #include "TestcaseSetupImpl.h"
2 
3 #include <zypp/ZYpp.h>
4 #include <zypp/ZYppFactory.h>
5 #include <zypp/Resolver.h>
6 #include <zypp/ResPool.h>
7 #include <zypp/VendorAttr.h>
9 #include <zypp/base/Algorithm.h>
10 
11 namespace zypp::misc::testcase
12 {
13 
14  // ─── Helpers (migrated verbatim from deptestomatic, refactor in second pass) ─
15  namespace {
16 
17  struct FindPackage
18  {
19  PoolItem poolItem;
22  Edition edition;
23  bool arch_set;
24  Arch arch;
25 
26  FindPackage( Resolvable::Kind k, const std::string & v,
27  const std::string & r, const std::string & a )
28  : kind( k )
29  , edition_set( !v.empty() )
30  , edition( v, r )
31  , arch_set( !a.empty() )
32  , arch( a )
33  {}
34 
35  void _remember( PoolItem p ) { poolItem = p; }
36 
37  bool operator()( PoolItem p )
38  {
39  if ( arch_set && arch != p->arch() )
40  return true;
41  if ( !p->arch().compatibleWith( ZConfig::instance().systemArchitecture() ) )
42  return true;
43  if ( edition_set && p->edition().match( edition ) != 0 )
44  return true;
45  if ( !poolItem
46  || poolItem->arch().compare( p->arch() ) < 0
47  || poolItem->edition().compare( p->edition() ) < 0 )
48  _remember( p );
49  return true;
50  }
51  };
52 
53  static PoolItem get_poolItem( const std::string & source_alias,
54  const std::string & package_name,
55  const std::string & kind_name = "",
56  const std::string & ver = "",
57  const std::string & rel = "",
58  const std::string & arch = "" )
59  {
60  PoolItem poolItem;
61  ResPool pool = ResPool::instance();
64 
65  try {
66  FindPackage info( kind, ver, rel, arch );
67 
68  invokeOnEach( pool.byIdentBegin( kind, package_name ),
69  pool.byIdentEnd ( kind, package_name ),
70  resfilter::ByRepository( source_alias ),
71  std::ref( info ) );
72 
73  poolItem = info.poolItem;
74  if ( !poolItem ) {
75  // Try all channels — useful for language packages.
76  invokeOnEach( pool.byIdentBegin( kind, package_name ),
77  pool.byIdentEnd ( kind, package_name ),
78  std::ref( info ) );
79  poolItem = info.poolItem;
80  }
81  }
82  catch ( const Exception & e ) {
83  ZYPP_CAUGHT( e );
84  WAR << "Can't find kind[" << kind_name << "]:'" << package_name
85  << "': source '" << source_alias << "' not defined" << std::endl;
86  }
87 
88  if ( !poolItem )
89  WAR << "Can't find kind: " << kind << ":'" << package_name
90  << "' in source '" << source_alias << "': no such name/kind" << std::endl;
91 
92  return poolItem;
93  }
94 
95  void applyLockEntry(const TestcaseSetup::LockEntry &entry, ResPool &pool )
96  {
97  const bool isKeep = ( entry.first == "keep" );
98  const auto & props = entry.second;
99 
100  auto getProp = [&]( const std::string & k ) -> std::string {
101  auto it = props.find(k);
102  return it != props.end() ? it->second : std::string();
103  };
104 
105  if ( !isKeep ) {
106  std::string source_alias = getProp ("channel");
107  std::string package_name = getProp ("name");
108  if (package_name.empty())
109  package_name = getProp ("package");
110  std::string kind_name = getProp ("kind");
111  std::string version = getProp ("version");
112  if ( version.empty() )
113  version = getProp ("ver");
114  std::string release = getProp ("release");
115  if ( release.empty() )
116  release = getProp ("rel");
117  std::string architecture = getProp ("arch");
118 
119  if ( version.empty() )
120  {
121  if ( kind_name.empty() )
122  kind_name = "package";
123  ui::Selectable::Ptr item = ui::Selectable::get( ResKind(kind_name), package_name );
124  if ( item )
125  {
126  // first set to protected, then to taboo so we run through both logic paths
127  // to make sure that if there is a installed object the candidates are also locked
128  item->setStatus( item->hasInstalledObj() ? ui::S_Protected : ui::S_Taboo );
129  item->setStatus( ui::S_Taboo );
130  }
131  else
132  {
133  WAR << "Unknown Selectable " << kind_name << ":" << package_name << std::endl;
134  }
135  }
136  else
137  {
138  PoolItem poolItem;
139  poolItem = get_poolItem (source_alias, package_name, kind_name, version, release, architecture );
140  if (poolItem) {
141  MIL << "Locking " << package_name << " from channel " << source_alias << poolItem << std::endl;
142  poolItem.status().setLock (true, ResStatus::USER);
143  } else {
144  WAR << "Unknown package " << source_alias << "::" << package_name << std::endl;
145  }
146  }
147 
148  } else {
149 
150  std::string kind_name = getProp ("kind");
151  std::string name = getProp ("name");
152  if (name.empty())
153  name = getProp ("package");
154 
155  std::string source_alias = getProp ("channel");
156  if (source_alias.empty())
157  source_alias = "@System";
158 
159  if (name.empty())
160  {
161  WAR << "transact need 'name' parameter" << std::endl;
162  return;
163  }
164 
165  PoolItem poolItem;
166 
167  poolItem = get_poolItem( source_alias, name, kind_name, getProp ("version"), getProp ("release") );
168 
169  if (poolItem) {
170  // first: set anything
171  if (source_alias == "@System") {
172  poolItem.status().setToBeUninstalled( ResStatus::USER );
173  }
174  else {
175  poolItem.status().setToBeInstalled( ResStatus::USER );
176  }
177  // second: keep old state
178  poolItem.status().setTransact( false, ResStatus::USER );
179  }
180  else {
181  WAR << "Unknown item " << source_alias << "::" << name << std::endl;
182  }
183  }
184  }
185  }
186 
187  // ─────────────────────────────────────────────────────────────────────────────
188 
190  {}
191 
193  { }
194 
195  RepoData::RepoData(RepoDataImpl &&data) : _pimpl( new RepoDataImpl( std::move(data)) )
196  { }
197 
199  { return _pimpl->type; }
200 
201  const std::string &RepoData::alias() const
202  { return _pimpl->alias; }
203 
204  uint RepoData::priority() const
205  { return _pimpl->priority; }
206 
207  const std::string &RepoData::path() const
208  { return _pimpl->path; }
209 
211  { return *_pimpl; }
212 
214  { return *_pimpl; }
215 
217  { }
218 
220  { }
221 
222  ForceInstall::ForceInstall(ForceInstallImpl &&data) : _pimpl( new ForceInstallImpl( std::move(data) ))
223  { }
224 
226  { return *_pimpl; }
227 
229  { return *_pimpl; }
230 
231  const std::string &ForceInstall::channel() const
232  { return _pimpl->channel; }
233 
234  const std::string &ForceInstall::package() const
235  { return _pimpl->package; }
236 
237  const std::string &ForceInstall::kind() const
238  { return _pimpl->kind; }
239 
241  { }
242 
244  { }
245 
247  { return _pimpl->architecture; }
248 
249  const std::optional<RepoData> &TestcaseSetup::systemRepo() const
250  { return _pimpl->systemRepo; }
251 
252  const std::vector<RepoData> &TestcaseSetup::repos() const
253  { return _pimpl->repos; }
254 
256  { return _pimpl->resolverFocus; }
257 
259  { return _pimpl->globalPath; }
260 
262  { return _pimpl->hardwareInfoFile; }
263 
265  { return _pimpl->systemCheck; }
266 
268  { return _pimpl->modaliasList; }
269 
271  { return _pimpl->localesTracker; }
272 
273  const std::vector<std::vector<std::string> > &TestcaseSetup::vendorLists() const
274  { return _pimpl->vendorLists; }
275 
277  { return _pimpl->autoinstalled; }
278 
279  const std::set<std::string> &TestcaseSetup::multiversionSpec() const
280  { return _pimpl->multiversionSpec; }
281 
282  const std::vector<ForceInstall> &TestcaseSetup::forceInstallTasks() const
283  { return _pimpl->forceInstallTasks; }
284 
285  const std::vector<TestcaseSetup::LockEntry> &TestcaseSetup::locks() const
286  { return _pimpl->locks; }
287 
289  { return _pimpl->set_licence; }
290 
292  { return _pimpl->show_mediaid; }
293 
295  { return _pimpl->ignorealreadyrecommended; }
296 
298  { return _pimpl->onlyRequires; }
299 
301  { return _pimpl->forceResolve; }
302 
304  { return _pimpl->cleandepsOnRemove; }
305 
307  { return _pimpl->noUpdateProvide; }
308 
310  { return _pimpl->allowDowngrade; }
311 
313  { return _pimpl->allowNameChange; }
314 
316  { return _pimpl->allowArchChange; }
317 
319  { return _pimpl->allowVendorChange; }
320 
322  { return _pimpl->dupAllowDowngrade; }
323 
325  { return _pimpl->dupAllowNameChange; }
326 
328  { return _pimpl->dupAllowArchChange; }
329 
331  { return _pimpl->dupAllowVendorChange; }
332 
334  {
335  const auto &setup = data();
336  if ( !setup.architecture.empty() )
337  {
338  MIL << "Setting architecture to '" << setup.architecture << "'" << std::endl;
339  ZConfig::instance().setSystemArchitecture( setup.architecture );
340  setenv ("ZYPP_TESTSUITE_FAKE_ARCH", setup.architecture.c_str(), 1);
341  }
342 
343  if ( setup.systemRepo ) {
344  if (!loadRepo( manager, *this, *setup.systemRepo ) )
345  {
346  ERR << "Can't setup 'system'" << std::endl;
347  return false;
348  }
349  }
350 
351  if ( !setup.hardwareInfoFile.empty() ) {
352  setenv( "ZYPP_MODALIAS_SYSFS", setup.hardwareInfoFile.asString().c_str(), 1 );
353  MIL << "setting HardwareInfo to: " << setup.hardwareInfoFile.asString() << std::endl;
354  }
355 
356  for ( const auto &channel : setup.repos ) {
357  if ( !loadRepo( manager, *this, channel ) )
358  {
359  ERR << "Can't setup 'channel'" << std::endl;
360  return false;
361  }
362  }
363 
364  if ( !setup.systemCheck.empty() ) {
365  MIL << "setting systemCheck to: " << setup.systemCheck.asString() << std::endl;
366  SystemCheck::instance().setFile( setup.systemCheck );
367  }
368 
369  return true;
370  }
371 
372  bool TestcaseSetup::loadRepo( zypp::RepoManager &manager, const TestcaseSetup &setup, const RepoData &data )
373  {
374  const auto &repoData = data.data();
375  Pathname pathname = Pathname::assertprefix ( setup._pimpl->globalPath, repoData.path );
376  MIL << "'" << pathname << "'" << std::endl;
377 
378  Repository repo;
379 
381 
382  if ( repoData.type == TrType::Url ) {
383  try {
384  MIL << "Load from Url '" << repoData.path << "'" << std::endl;
385 
386  RepoInfo nrepo;
387  nrepo.setAlias ( repoData.alias );
388  nrepo.setName ( repoData.alias );
389  nrepo.setEnabled ( true );
390  nrepo.setAutorefresh( false );
391  nrepo.setPriority ( repoData.priority );
392 
393  Url repoUrl( repoData.path );
394  if ( repoUrl.getScheme() == "testcase" )
395  {
396  // testcase:<relative-path> — a repo bundled alongside the testcase
397  // fixture itself (e.g. a local susetags/rpm-md directory), resolved
398  // against the testcase directory. Any other scheme (http://, dir:,
399  // ftp://, ...) is used verbatim, unchanged from prior behaviour.
400  repoUrl = Pathname::assertprefix ( setup._pimpl->globalPath, repoUrl.getPathName() ).asUrl();
401  // Bundled fixtures are never signed — same precedent as
402  // zypp-logic/tests/lib/TestSetup.h::loadRepo(), which disables
403  // gpgCheck for its own local test repos. Absolute URLs (any other
404  // scheme) are untouched: a testcase pointing at a real repo keeps
405  // whatever signature checking it would normally get.
406  nrepo.setGpgCheck( false );
407  }
408  nrepo.addBaseUrl( repoUrl );
409 
410  manager.refreshMetadata( nrepo );
411  manager.buildCache( nrepo );
412  manager.loadFromCache( nrepo );
413  }
414  catch ( Exception & excpt_r ) {
415  ZYPP_CAUGHT (excpt_r);
416  ERR << "Couldn't load packages from Url '" << repoData.path << "'" << std::endl;
417  return false;
418  }
419  }
420  else {
421  try {
422  MIL << "Load from File '" << pathname << "'" << std::endl;
423  zypp::Repository satRepo;
424 
425  if ( repoData.alias == "@System" ) {
427  } else {
428  satRepo = zypp::sat::Pool::instance().reposInsert( repoData.alias );
429  }
430 
431  RepoInfo nrepo;
432 
433  nrepo.setAlias ( repoData.alias );
434  nrepo.setName ( repoData.alias );
435  nrepo.setEnabled ( true );
436  nrepo.setAutorefresh( false );
437  nrepo.setPriority ( repoData.priority );
438  nrepo.addBaseUrl ( pathname.asUrl() );
439 
440  satRepo.setInfo (nrepo);
441  if ( repoData.type == TrType::Helix )
442  satRepo.addHelix( pathname );
443  else
444  satRepo.addTesttags( pathname );
445  MIL << "Loaded " << satRepo.solvablesSize() << " resolvables from " << ( repoData.path.empty()?pathname.asString():repoData.path) << "." << std::endl;
446  }
447  catch ( Exception & excpt_r ) {
448  ZYPP_CAUGHT (excpt_r);
449  ERR << "Couldn't load packages from XML file '" << repoData.path << "'" << std::endl;
450  return false;
451  }
452  }
453  return true;
454  }
455 
457  {
458  return *_pimpl;
459  }
460 
462  {
463  return *_pimpl;
464  }
465 
466  bool TestcaseSetup::applySetup( zypp::RepoManager &manager, ApplySetupFlags flags_r ) const
467  {
468  // Repos/arch/hardware are always applied via the existing overload.
469  if ( !applySetup( manager ) )
470  return false;
471 
472  if ( flags_r.testFlag( AS_LOCALES ) )
473  {
475  lt.removed().insert( lt.current().begin(), lt.current().end() );
477  lt.added().insert( lt.current().begin(), lt.current().end() );
479  }
480 
481  if ( flags_r.testFlag( AS_AUTOINSTALLED ) )
482  {
484  }
485 
486  if ( flags_r.testFlag( AS_VENDOR_LISTS ) )
487  {
488  for ( const auto & vlist : vendorLists() )
490  }
491 
492  if ( flags_r.testFlag( AS_MODALIAS ) )
493  {
495  }
496 
497  if ( flags_r.testFlag( AS_MULTIVERSION ) )
498  {
500  }
501 
502  if ( flags_r.testFlag( AS_LOCKS ) )
503  {
504  ResPool pool = ResPool::instance();
505  for ( const auto & entry : locks() )
506  applyLockEntry( entry, pool );
507  }
508 
509  if ( flags_r.testFlag( AS_SOLVER_FLAGS ) )
510  {
511  Resolver_Ptr resolver = getZYpp()->resolver();
512  resolver->setFocus ( resolverFocus() );
513  resolver->setIgnoreAlreadyRecommended( ignorealreadyrecommended() );
514  resolver->setOnlyRequires ( onlyRequires() );
515  resolver->setForceResolve ( forceResolve() );
516  resolver->setCleandepsOnRemove ( cleandepsOnRemove() );
517  resolver->setAllowDowngrade ( allowDowngrade() );
518  resolver->setAllowNameChange ( allowNameChange() );
519  resolver->setAllowArchChange ( allowArchChange() );
520  resolver->setAllowVendorChange ( allowVendorChange() );
521  resolver->dupSetAllowDowngrade ( dupAllowDowngrade() );
522  resolver->dupSetAllowNameChange ( dupAllowNameChange() );
523  resolver->dupSetAllowArchChange ( dupAllowArchChange() );
524  resolver->dupSetAllowVendorChange ( dupAllowVendorChange() );
525  }
526 
527  return true;
528  }
529 }
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:560
ResolverFocus resolverFocus() const
const std::string & package() const
void loadFromCache(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Load resolvables into the pool.
Definition: RepoManager.cc:171
Repository reposInsert(const std::string &alias_r)
Return a Repository named alias_r.
Definition: Pool.cc:143
#define MIL
Definition: Logger.h:130
Edition edition
const ModaliasList & modaliasList() const
List of modaliases found on system.
Definition: Modalias.cc:232
void setRequestedLocales(const LocaleSet &locales_r)
Set the requested locales.
Definition: Pool.cc:236
static const ResKind package
Definition: ResKind.h:40
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:92
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:794
const std::string & kind() const
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition: RepoInfo.cc:609
const std::vector< ForceInstall > & forceInstallTasks() const
Architecture.
Definition: Arch.h:36
ResolverFocus
The resolver&#39;s general attitude.
Definition: ResolverFocus.h:23
const RepoDataImpl & data() const
const std::set< std::string > & multiversionSpec() const
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:89
initRequestedLocales + setRequestedLocales
Definition: TestcaseSetup.h:95
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:95
void setAutoInstalled(const Queue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition: Pool.cc:265
Definition: ansi.h:854
What is known about a repository.
Definition: RepoInfo.h:71
static ResKind fromBuiltin(const std::string &str)
Converts the string representation of a kind to its ResKind value or noKind if the passed string is n...
Definition: ResKind.cc:68
void setSystemArchitecture(const Arch &arch_r)
Override the zypp system architecture.
Definition: ZConfig.cc:860
void setGpgCheck(TriBool value_r)
Set the value for gpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:616
const std::vector< RepoData > & repos() const
const ForceInstallImpl & data() const
void buildCache(const RepoInfo &info, CacheBuildPolicy policy=BuildIfNeeded, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Refresh local cache.
Definition: RepoManager.cc:158
const std::vector< LockEntry > & locks() const
RWCOW_pointer< TestcaseSetupImpl > _pimpl
const base::SetTracker< LocaleSet > & localesTracker() const
void addTesttags(const Pathname &file_r)
Load Solvables from a libsolv testtags-file.
Definition: Repository.cc:364
#define ERR
Definition: Logger.h:132
const std::set< std::string > & multiversionSpec() const
Definition: ZConfig.cc:1144
const Pathname & systemCheck() const
const set_type & removed() const
Return the set of removed items.
Definition: SetTracker.h:146
void refreshMetadata(const RepoInfo &info, RawMetadataRefreshPolicy policy=RefreshIfNeeded, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Refresh local raw cache.
Definition: RepoManager.cc:142
Repository systemRepo()
Return the system repository, create it if missing.
Definition: Pool.cc:178
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition: Pool.cc:251
static Pool instance()
Singleton ctor.
Definition: Pool.h:55
const std::string & path() const
Url asUrl(const std::string &scheme_r) const
Url representation using scheme_r schema .
Definition: Pathname.cc:172
RWCOW_pointer< ForceInstallImpl > _pimpl
Definition: TestcaseSetup.h:79
const std::string & asString() const
String representation.
Definition: Pathname.h:94
static VendorAttr & noTargetInstance()
Singleton, settings used if no Target is active.
Definition: VendorAttr.cc:236
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
Definition: Repository.cc:285
creates and provides information about known sources.
Definition: RepoManager.cc:40
const set_type & current() const
Return the current set.
Definition: SetTracker.h:140
static Pathname assertprefix(const Pathname &root_r, const Pathname &path_r)
Unless path_r does not already denote a path below root_r, combine them.
Definition: Pathname.cc:272
#define WAR
Definition: Logger.h:131
std::pair< std::string, std::map< std::string, std::string > > LockEntry
lock/keep node data extracted from trial nodes and applied as pool setup.
ZConfig multiversion spec.
Definition: TestcaseSetup.h:99
const set_type & added() const
Return the set of added items.
Definition: SetTracker.h:143
const std::vector< std::vector< std::string > > & vendorLists() const
const std::string & alias() const
TestcaseRepoType type() const
Arch arch
void addVendorList(VendorList &&list_r)
Adding new equivalent vendor strings container.
Definition: VendorAttr.h:105
Libsolv Id queue wrapper.
Definition: Queue.h:35
ZYpp::Ptr getZYpp()
relates: ZYppFactory Convenience to get the Pointer to the ZYpp instance.
Definition: ZYppFactory.h:77
VendorAttr vendor-equivalence lists.
Definition: TestcaseSetup.h:97
const std::string & channel() const
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:475
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Definition: Repository.cc:345
Resolver flags, focus, DUP flags.
Definition: TestcaseSetup.h:94
static Ptr get(const pool::ByIdent &ident_r)
Get the Selctable.
Definition: Selectable.cc:29
Global ResObject pool.
Definition: ResPool.h:61
static Modalias & instance()
Singleton access.
Definition: Modalias.cc:223
Base class for Exception.
Definition: Exception.h:152
lock/keep nodes extracted from trial nodes
void addBaseUrl(Url url)
Add a base url.
Definition: RepoInfo.cc:752
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:631
bool applySetup(zypp::RepoManager &manager) const
static bool loadRepo(zypp::RepoManager &manager, const TestcaseSetup &setup, const RepoData &data)
std::vector< std::string > ModaliasList
Definition: Modalias.h:45
bool arch_set
const Pathname & globalPath() const
unsigned short a
void setName(const std::string &name)
set the repository name
Definition: RepoInfoBase.cc:98
static const ResKind nokind
Value representing nokind ("")
Definition: ResKind.h:38
int invokeOnEach(TIterator begin_r, TIterator end_r, TFilter filter_r, TFunction fnc_r)
Iterate through [begin_r,end_r) and invoke fnc_r on each item that passes filter_r.
Definition: Algorithm.h:30
size_type solvablesSize() const
Number of solvables in Repository.
Definition: Repository.cc:231
const sat::StringQueue & autoinstalled() const
const Pathname & hardwareInfoFile() const
const std::optional< RepoData > & systemRepo() const
PoolItem poolItem
Url manipulation class.
Definition: Url.h:92
const target::Modalias::ModaliasList & modaliasList() const
intrusive_ptr< Selectable > Ptr
Definition: Selectable.h:58
TraitsType::KindType Kind
Definition: Resolvable.h:57
Resolvable::Kind kind
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:38
RWCOW_pointer< RepoDataImpl > _pimpl
Definition: TestcaseSetup.h:64
bool edition_set