Imported Upstream version 17.25.3
[platform/upstream/libzypp.git] / zypp / misc / TestcaseSetup.cc
1 #include "TestcaseSetupImpl.h"
2
3 namespace zypp::misc::testcase
4 {
5   RepoData::RepoData() : _pimpl( new RepoDataImpl )
6   {}
7
8   RepoData::~RepoData()
9   { }
10
11   RepoData::RepoData(RepoDataImpl &&data) : _pimpl( new RepoDataImpl( std::move(data)) )
12   { }
13
14   TestcaseRepoType RepoData::type() const
15   { return _pimpl->type; }
16
17   const std::string &RepoData::alias() const
18   { return _pimpl->alias; }
19
20   uint RepoData::priority() const
21   { return _pimpl->priority; }
22
23   const std::string &RepoData::path() const
24   { return _pimpl->path; }
25
26   const RepoDataImpl &RepoData::data() const
27   { return *_pimpl; }
28
29   RepoDataImpl &RepoData::data()
30   { return *_pimpl; }
31
32   ForceInstall::ForceInstall() : _pimpl( new ForceInstallImpl )
33   { }
34
35   ForceInstall::~ForceInstall()
36   { }
37
38   ForceInstall::ForceInstall(ForceInstallImpl &&data) : _pimpl( new ForceInstallImpl( std::move(data) ))
39   { }
40
41   const ForceInstallImpl &ForceInstall::data() const
42   { return *_pimpl; }
43
44   ForceInstallImpl &ForceInstall::data()
45   { return *_pimpl; }
46
47   const std::string &ForceInstall::channel() const
48   { return _pimpl->channel; }
49
50   const std::string &ForceInstall::package() const
51   { return _pimpl->package; }
52
53   const std::string &ForceInstall::kind() const
54   { return _pimpl->kind; }
55
56   TestcaseSetup::TestcaseSetup() : _pimpl( new TestcaseSetupImpl )
57   { }
58
59   TestcaseSetup::~TestcaseSetup()
60   { }
61
62   Arch TestcaseSetup::architecture() const
63   { return _pimpl->architecture; }
64
65   const std::optional<RepoData> &TestcaseSetup::systemRepo() const
66   { return _pimpl->systemRepo; }
67
68   const std::vector<RepoData> &TestcaseSetup::repos() const
69   { return _pimpl->repos; }
70
71   ResolverFocus TestcaseSetup::resolverFocus() const
72   { return _pimpl->resolverFocus; }
73
74   const zypp::filesystem::Pathname &TestcaseSetup::globalPath() const
75   { return _pimpl->globalPath; }
76
77   const zypp::filesystem::Pathname &TestcaseSetup::hardwareInfoFile() const
78   { return _pimpl->hardwareInfoFile; }
79
80   const zypp::filesystem::Pathname &TestcaseSetup::systemCheck() const
81   { return _pimpl->systemCheck; }
82
83   const target::Modalias::ModaliasList &TestcaseSetup::modaliasList() const
84   { return _pimpl->modaliasList; }
85
86   const base::SetTracker<LocaleSet> &TestcaseSetup::localesTracker() const
87   { return _pimpl->localesTracker; }
88
89   const std::vector<std::vector<std::string> > &TestcaseSetup::vendorLists() const
90   { return _pimpl->vendorLists; }
91
92   const sat::StringQueue &TestcaseSetup::autoinstalled() const
93   { return _pimpl->autoinstalled; }
94
95   const std::set<std::string> &TestcaseSetup::multiversionSpec() const
96   { return _pimpl->multiversionSpec; }
97
98   const std::vector<ForceInstall> &TestcaseSetup::forceInstallTasks() const
99   { return _pimpl->forceInstallTasks; }
100
101   bool TestcaseSetup::set_licence() const
102   { return _pimpl->set_licence; }
103
104   bool TestcaseSetup::show_mediaid() const
105   { return _pimpl->show_mediaid; }
106
107   bool TestcaseSetup::ignorealreadyrecommended() const
108   { return _pimpl->ignorealreadyrecommended; }
109
110   bool TestcaseSetup::onlyRequires() const
111   { return _pimpl->onlyRequires; }
112
113   bool TestcaseSetup::forceResolve() const
114   { return _pimpl->forceResolve; }
115
116   bool TestcaseSetup::cleandepsOnRemove() const
117   { return _pimpl->cleandepsOnRemove; }
118
119   bool TestcaseSetup::allowDowngrade() const
120   { return _pimpl->allowDowngrade; }
121
122   bool TestcaseSetup::allowNameChange() const
123   { return _pimpl->allowNameChange; }
124
125   bool TestcaseSetup::allowArchChange() const
126   { return _pimpl->allowArchChange; }
127
128   bool TestcaseSetup::allowVendorChange() const
129   { return _pimpl->allowVendorChange; }
130
131   bool TestcaseSetup::dupAllowDowngrade() const
132   { return _pimpl->dupAllowDowngrade; }
133
134   bool TestcaseSetup::dupAllowNameChange() const
135   { return _pimpl->dupAllowNameChange; }
136
137   bool TestcaseSetup::dupAllowArchChange() const
138   { return _pimpl->dupAllowArchChange; }
139
140   bool TestcaseSetup::dupAllowVendorChange() const
141   { return _pimpl->dupAllowVendorChange; }
142
143   bool TestcaseSetup::applySetup( zypp::RepoManager &manager ) const
144   {
145     const auto &setup = data();
146     if ( !setup.architecture.empty() )
147     {
148       MIL << "Setting architecture to '" << setup.architecture << "'" << std::endl;
149       ZConfig::instance().setSystemArchitecture( setup.architecture );
150       setenv ("ZYPP_TESTSUITE_FAKE_ARCH", setup.architecture.c_str(), 1);
151     }
152
153     if ( setup.systemRepo ) {
154       if (!loadRepo( manager, *this, *setup.systemRepo ) )
155       {
156         ERR << "Can't setup 'system'" << std::endl;
157         return false;
158       }
159     }
160
161     if ( !setup.hardwareInfoFile.empty() ) {
162       setenv( "ZYPP_MODALIAS_SYSFS", setup.hardwareInfoFile.asString().c_str(), 1 );
163       MIL << "setting HardwareInfo to: " << setup.hardwareInfoFile.asString() << std::endl;
164     }
165
166     for ( const auto &channel : setup.repos ) {
167       if ( !loadRepo( manager, *this, channel )  )
168       {
169         ERR << "Can't setup 'channel'" << std::endl;
170         return false;
171       }
172     }
173
174     if ( !setup.systemCheck.empty() ) {
175       MIL << "setting systemCheck to: " << setup.systemCheck.asString() << std::endl;
176       SystemCheck::instance().setFile( setup.systemCheck );
177     }
178
179     return true;
180   }
181
182   bool TestcaseSetup::loadRepo( zypp::RepoManager &manager, const TestcaseSetup &setup, const RepoData &data )
183   {
184     const auto &repoData = data.data();
185     Pathname pathname = setup._pimpl->globalPath + repoData.path;
186     MIL << "'" << pathname << "'" << std::endl;
187
188     Repository repo;
189
190     using TrType = zypp::misc::testcase::TestcaseRepoType;
191
192     if ( repoData.type == TrType::Url ) {
193       try {
194         MIL << "Load from Url '" << repoData.path << "'" << std::endl;
195
196         RepoInfo nrepo;
197         nrepo.setAlias      ( repoData.alias );
198         nrepo.setName       ( repoData.alias );
199         nrepo.setEnabled    ( true );
200         nrepo.setAutorefresh( false );
201         nrepo.setPriority   ( repoData.priority );
202         nrepo.addBaseUrl   ( Url(repoData.path) );
203
204         manager.refreshMetadata( nrepo );
205         manager.buildCache( nrepo );
206         manager.loadFromCache( nrepo );
207       }
208       catch ( Exception & excpt_r ) {
209         ZYPP_CAUGHT (excpt_r);
210         ERR << "Couldn't load packages from Url '" << repoData.path << "'" << std::endl;
211         return false;
212       }
213     }
214     else {
215       try {
216         MIL << "Load from File '" << pathname << "'" << std::endl;
217         zypp::Repository satRepo;
218
219         if ( repoData.alias == "@System" ) {
220           satRepo = zypp::sat::Pool::instance().systemRepo();
221         } else {
222           satRepo = zypp::sat::Pool::instance().reposInsert( repoData.alias );
223         }
224
225         RepoInfo nrepo;
226
227         nrepo.setAlias      ( repoData.alias );
228         nrepo.setName       ( repoData.alias );
229         nrepo.setEnabled    ( true );
230         nrepo.setAutorefresh( false );
231         nrepo.setPriority   ( repoData.priority );
232         nrepo.addBaseUrl   ( pathname.asUrl() );
233
234         satRepo.setInfo (nrepo);
235         if ( repoData.type == TrType::Helix )
236           satRepo.addHelix( pathname );
237         else
238           satRepo.addTesttags( pathname );
239         MIL << "Loaded " << satRepo.solvablesSize() << " resolvables from " << ( repoData.path.empty()?pathname.asString():repoData.path) << "." << std::endl;
240       }
241       catch ( Exception & excpt_r ) {
242         ZYPP_CAUGHT (excpt_r);
243         ERR << "Couldn't load packages from XML file '" << repoData.path << "'" << std::endl;
244         return false;
245       }
246     }
247     return true;
248   }
249
250   TestcaseSetupImpl &TestcaseSetup::data()
251   {
252     return *_pimpl;
253   }
254
255   const TestcaseSetupImpl &TestcaseSetup::data() const
256   {
257     return *_pimpl;
258   }
259
260 }