libzypp 17.38.14
Testcase.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <fstream>
14#include <sstream>
15#include <streambuf>
16#include <boost/iterator/function_output_iterator.hpp>
17
18#define ZYPP_USE_RESOLVER_INTERNALS
19
23#include <utility>
24#include <zypp-core/base/GzStream>
29
31#include <zypp/ZConfig.h>
32#include <zypp/PathInfo.h>
33#include <zypp/ResPool.h>
34#include <zypp/Repository.h>
35#include <zypp/VendorAttr.h>
37
41
42#include <yaml-cpp/yaml.h>
43
44extern "C" {
45#include <solv/testcase.h>
46}
47
48using std::endl;
49
51namespace zypp
52{
54 namespace solver
55 {
57 namespace detail
58 {
59
60 //---------------------------------------------------------------------------
61
62 Testcase::Testcase()
63 :dumpPath("/var/log/YaST2/solverTestcase")
64 {}
65
66 Testcase::Testcase(std::string path)
67 :dumpPath(std::move(path))
68 {}
69
70 Testcase::~Testcase()
71 {}
72
73 bool Testcase::createTestcase(Resolver & resolver, bool dumpPool, bool runSolver)
74 {
75 // libzypp/issues/317: make sure a satsolver instance is actually present
76 if ( not resolver.get() ) {
77 WAR << "Can't createTestcase if the satsolver is not yet initialized." << endl;
78 return false;
79 }
80
81 MIL << "createTestcase at " << dumpPath << (dumpPool?" dumpPool":"") << (runSolver?" runSolver":"") << endl;
82 PathInfo path (dumpPath);
83
84 if ( !path.isExist() ) {
85 if (zypp::filesystem::assert_dir (dumpPath)!=0) {
86 ERR << "Cannot create directory " << dumpPath << endl;
87 return false;
88 }
89 } else {
90 if (!path.isDir()) {
91 ERR << dumpPath << " is not a directory." << endl;
92 return false;
93 }
94 // remove old stuff if pool will be dump
95 if (dumpPool)
97 }
98
99 if (runSolver) {
101 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
103
104 resolver.resolvePool();
105 }
106
107 ResPool pool = resolver.pool();
108 PoolItemList items_to_install;
109 PoolItemList items_to_remove;
110 PoolItemList items_locked;
111 PoolItemList items_keep;
112
113
114 const std::string slvTestcaseName = "testcase.t";
115 const std::string slvResult = "solver.result";
116
117 zypp::AutoDispose<const char **> repoFileNames( testcase_mangle_repo_names( resolver.get()->pool ),
118 [ nrepos = resolver.get()->pool->nrepos ]( auto **x ){
119 if (!x) return;
120 for ( int i = 1; i < nrepos; i++ )
121 solv_free((void *)x[i]);
122 solv_free((void *)x);
123 });
124
125 if ( ::testcase_write( resolver.get(), dumpPath.c_str(), TESTCASE_RESULT_TRANSACTION | TESTCASE_RESULT_PROBLEMS, slvTestcaseName.c_str(), slvResult.c_str() ) == 0 ) {
126 ERR << "Failed to write solv data, aborting." << endl;
127 return false;
128 }
129
130 // HACK: directly access sat::pool
131 const sat::Pool & satpool( sat::Pool::instance() );
132
133 YAML::Emitter yOut;
134
135 const auto addTag = [&]( const std::string & tag_r, bool yesno_r = true ){
136 yOut << YAML::Key << tag_r << YAML::Value << yesno_r;
137 };
138
139 yOut << YAML::BeginMap << YAML::Key << "version" << YAML::Value << "1.0";
140
141 yOut << YAML::Key << "setup" << YAML::Value << YAML::BeginMap;
142
143 yOut << YAML::Key << "channels";
144 yOut << YAML::Value << YAML::BeginSeq;
145
146 std::set<Repository::IdType> repos;
147 for ( const PoolItem & pi : pool ) {
148 if ( pi.status().isToBeInstalled()
149 && !(pi.status().isBySolver())) {
150 items_to_install.push_back( pi );
151 }
152 if ( pi.status().isKept()
153 && !(pi.status().isBySolver())) {
154 items_keep.push_back( pi );
155 }
156 if ( pi.status().isToBeUninstalled()
157 && !(pi.status().isBySolver())) {
158 items_to_remove.push_back( pi );
159 }
160 if ( pi.status().isLocked()
161 && !(pi.status().isBySolver())) {
162 items_locked.push_back( pi );
163 }
164
165 const auto &myRepo = pi.repository();
166 const auto &myRepoInfo = myRepo.info();
167 if ( repos.find( myRepo.id()) == repos.end() ) {
168 repos.insert( myRepo.id() );
169 yOut << YAML::Value << YAML::BeginMap;
170 yOut << YAML::Key << "alias" << YAML::Value << myRepo.alias();
171 yOut << YAML::Key << "url" << YAML::BeginSeq;
172 for ( const auto &origin : myRepoInfo.repoOrigins () ) {
173 for ( const auto &originEndpoint : origin ) {
174 yOut << YAML::Value << originEndpoint.url().asString();
175 }
176 }
177 yOut << YAML::EndSeq;
178 yOut << YAML::Key << "path" << YAML::Value << myRepoInfo.path().asString();
179 yOut << YAML::Key << "type" << YAML::Value << myRepoInfo.type().asString();
180 yOut << YAML::Key << "generated" << YAML::Value << myRepo.generatedTimestamp().form( "%Y-%m-%d %H:%M:%S" );
181 yOut << YAML::Key << "outdated" << YAML::Value << myRepo.suggestedExpirationTimestamp().form( "%Y-%m-%d %H:%M:%S" );
182 yOut << YAML::Key << "priority" << YAML::Value << myRepoInfo.priority();
183 yOut << YAML::Key << "file" << YAML::Value << str::Format("%1%.repo.gz") % repoFileNames[myRepo.id()->repoid];
184
185 yOut << YAML::EndMap;
186 }
187
188 }
189
190 yOut << YAML::EndSeq;
191
192 yOut << YAML::Key << "arch" << YAML::Value << ZConfig::instance().systemArchitecture().asString() ;
193 yOut << YAML::Key << "solverTestcase" << YAML::Value << slvTestcaseName ;
194 yOut << YAML::Key << "solverResult" << YAML::Value << slvResult ;
195
196 // RequestedLocales
197 const LocaleSet & addedLocales( satpool.getAddedRequestedLocales() );
198 const LocaleSet & removedLocales( satpool.getRemovedRequestedLocales() );
199 const LocaleSet & requestedLocales( satpool.getRequestedLocales() );
200
201 yOut << YAML::Key << "locales" << YAML::Value << YAML::BeginSeq ;
202 for ( const Locale& l : requestedLocales ) {
203 yOut << YAML::Value << YAML::BeginMap;
204 yOut << YAML::Key << "fate" << YAML::Value << ( addedLocales.count(l) ? "added" : "" ) ;
205 yOut << YAML::Key << "name" << YAML::Value << l.asString() ;
206 yOut << YAML::EndMap;
207 }
208
209 for ( const Locale& l : removedLocales ) {
210 yOut << YAML::Value << YAML::BeginMap;
211 yOut << YAML::Key << "fate" << YAML::Value << "removed" ;
212 yOut << YAML::Key << "name" << YAML::Value << l.asString() ;
213 yOut << YAML::EndMap;
214 }
215 yOut << YAML::EndSeq; // locales
216
217 // Vendor settings
218 yOut << YAML::Key << "vendors" << YAML::Value << YAML::BeginSeq ;
219 VendorAttr::instance().foreachVendorList( [&]( const VendorAttr::VendorList& vlist )->bool {
220 if ( ! vlist.empty() ) {
221 yOut << YAML::Value << YAML::BeginSeq;
222 for( const auto & v : vlist )
223 yOut << YAML::Value << v ;
224 yOut << YAML::EndSeq;
225 }
226 return true;
227 } );
228 yOut << YAML::EndSeq; // vendors
229
230 // helper lambda to write a list of elements into a external file instead of the main file
231 const auto &writeListOrFile = [&]( const std::string &name, const auto &list, const auto &callback ) {
232 if ( list.size() > 10 ) {
233 const std::string fName = str::Format("zypp-%1%.yaml") % name;
234 yOut << YAML::Key << name << YAML::Value << fName;
235
236 YAML::Emitter yOutFile;
237 callback( yOutFile, list );
238
239 std::ofstream fout( dumpPath+"/"+fName );
240 fout << yOutFile.c_str();
241 } else {
242 yOut << YAML::Key << name << YAML::Value ;
243 callback( yOut, list );
244 }
245 };
246
247 // AutoInstalled
248 const auto &writeAutoInst = [] ( YAML::Emitter &out, const auto &autoInstalledList ) {
249 out << YAML::BeginSeq;
250 for ( IdString::IdType n : autoInstalledList ) {
251 out << YAML::Value << IdString(n).asString() ;
252 }
253 out << YAML::EndSeq;
254 };
255 writeListOrFile( "autoinst", satpool.autoInstalled(), writeAutoInst );
256
257 // ModAlias
258 const auto &writeModalias = []( YAML::Emitter &out, const auto &modAliasList ){
259 out << YAML::BeginSeq;
260 for ( const auto &modAlias : modAliasList ) {
261 out << YAML::Value << modAlias ;
262 }
263 out << YAML::EndSeq;
264 };
265 writeListOrFile( "modalias", target::Modalias::instance().modaliasList(), writeModalias );
266
267 // Multiversion
268 const auto &writeMultiVersion = [] ( YAML::Emitter &out, const auto &multiversionList ) {
269 out << YAML::BeginSeq;
270 for ( const auto &multiver : multiversionList ) {
271 out << YAML::Value << multiver ;
272 }
273 out << YAML::EndSeq;
274 };
275 writeListOrFile( "multiversion", ZConfig::instance().multiversionSpec(), writeMultiVersion );
276
277
278 yOut << YAML::Key << "resolverFlags" << YAML::Value << YAML::BeginMap;
279 yOut << YAML::Key << "focus" << YAML::Value << asString( resolver.focus() );
280
281 addTag( "ignorealreadyrecommended", resolver.ignoreAlreadyRecommended() );
282 addTag( "onlyRequires", resolver.onlyRequires() );
283 addTag( "forceResolve", resolver.forceResolve() );
284
285 addTag( "cleandepsOnRemove", resolver.cleandepsOnRemove() );
286 addTag( "noUpdateProvide", resolver.noUpdateProvide() );
287
288 addTag( "allowDowngrade", resolver.allowDowngrade() );
289 addTag( "allowNameChange", resolver.allowNameChange() );
290 addTag( "allowArchChange", resolver.allowArchChange() );
291 addTag( "allowVendorChange", resolver.allowVendorChange() );
292
293 addTag( "dupAllowDowngrade", resolver.dupAllowDowngrade() );
294 addTag( "dupAllowNameChange", resolver.dupAllowNameChange() );
295 addTag( "dupAllowArchChange", resolver.dupAllowArchChange() );
296 addTag( "dupAllowVendorChange", resolver.dupAllowVendorChange() );
297
298
299 yOut << YAML::EndMap; // resolverFlags
300 yOut << YAML::EndMap; // setup
301
302 yOut << YAML::Key << "trials" << YAML::Value << YAML::BeginSeq;
303
304 yOut << YAML::Value << YAML::BeginMap << YAML::Key << "trial" << YAML::Value;
305
306 yOut << YAML::BeginSeq;
307
308 const auto &writeJobsToFile = [&]( const std::string &fName, const auto &data, const auto &cb ){
309 yOut << YAML::Value << YAML::BeginMap;
310 yOut << YAML::Key << "include" << YAML::Value << fName;
311 yOut << YAML::EndMap;
312
313 YAML::Emitter yOutFile;
314 yOutFile << YAML::BeginSeq;
315 cb( yOutFile, data );
316 yOutFile << YAML::EndSeq;
317
318 std::ofstream fout( dumpPath+"/"+fName );
319 fout << yOutFile.c_str();
320 };
321
322 // Multiversion
323 const auto &writePoolItemJobs = []( const std::string &jobName ){
324 return [ &jobName ] ( YAML::Emitter &yOut, const PoolItemList &poolItems, bool shortInfo = false ) {
325 for ( const PoolItem & pi : poolItems ) {
326 yOut << YAML::Value << YAML::BeginMap;
327
328 std::stringstream status;
329 status << pi.status();
330
331 yOut << YAML::Key << "job" << YAML::Value << jobName
332 << YAML::Key << "kind" << YAML::Value << pi.kind().asString()
333 << YAML::Key << "name" << YAML::Value << pi.name()
334 << YAML::Key << "status" << YAML::Value << status.str();
335 if ( !shortInfo ) {
336 yOut << YAML::Key << "channel" << YAML::Value << pi.repoInfo().alias()
337 << YAML::Key << "arch" << YAML::Value << pi.arch().asString()
338 << YAML::Key << "version" << YAML::Value << pi.edition().version()
339 << YAML::Key << "release" << YAML::Value << pi.edition().release();
340 }
341 yOut << YAML::EndMap;
342 }
343 };
344 };
345
346 const auto &writeMapJob = []( YAML::Emitter &yOut, const std::string &name, const std::map<std::string, std::string> &values = std::map<std::string, std::string>() ){
347 yOut << YAML::Value << YAML::BeginMap;
348 yOut << YAML::Key << "job" << YAML::Value << name;
349 for ( const auto &v : values )
350 yOut << YAML::Key << v.first << YAML::Value << v.second;
351 yOut << YAML::EndMap;
352 };
353
354 writePoolItemJobs("install")( yOut, items_to_install );
355 writePoolItemJobs("keep")( yOut, items_keep );
356 writePoolItemJobs("uninstall")( yOut, items_to_remove, true );
357
358 if ( items_locked.size() )
359 writeJobsToFile("zypp-locks.yaml", items_locked, writePoolItemJobs("lock") );
360
361 for ( const auto &v : resolver.extraRequires() )
362 writeMapJob( yOut, "addRequire", { { "name", v.asString() } } );
363 for ( const auto &v : SystemCheck::instance().requiredSystemCap() )
364 writeMapJob( yOut, "addRequire", { { "name", v.asString() } } );
365
366 for ( const auto &v : resolver.extraConflicts() )
367 writeMapJob( yOut, "addConflict", { { "name", v.asString() } } );
368 for ( const auto &v : SystemCheck::instance().conflictSystemCap() )
369 writeMapJob( yOut, "addConflict", { { "name", v.asString() } } );
370
371 for ( const auto &v : resolver.upgradeRepos() )
372 writeMapJob( yOut, "upgradeRepo", { { "name", v.alias() } } );
373
374 if ( resolver.isUpgradeMode() )
375 writeMapJob( yOut, "distupgrade" );
376
377 if ( resolver.isUpdateMode() )
378 writeMapJob( yOut, "update" );
379
380 if ( resolver.isVerifyingMode() )
381 writeMapJob( yOut, "verify" );
382
383 yOut << YAML::EndSeq;
384 yOut << YAML::EndMap; // trial
385 yOut << YAML::EndSeq; // trials list
386 yOut << YAML::EndMap; // trials
387 yOut << YAML::EndMap; // root
388
389 std::ofstream fout( dumpPath+"/zypp-control.yaml" );
390 fout << yOut.c_str();
391
392 MIL << "createTestcase done at " << dumpPath << endl;
393 return true;
394 }
396 };// namespace detail
399 };// namespace solver
402};// namespace zypp
#define MIL
Definition Logger.h:103
#define ERR
Definition Logger.h:105
#define WAR
Definition Logger.h:104
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
std::string asString() const
Conversion to std::string.
Definition IdString.h:110
static LogControl instance()
Singleton access.
Definition LogControl.h:102
void logfile(const Pathname &logfile_r)
Set path for the logfile.
Definition ansi.h:855
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition PathInfo.cc:461
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:338
_dumpPath dumpPath(const Pathname &root_r, const Pathname &sub_r)
dumpPath iomaip to dump '(root_r)sub_r' output,
Definition librpmDb.h:42
Easy-to use interface to the ZYPP dependency resolver.
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::IdString IdString
Definition idstring.h:16
Turn on excessive logging for the lifetime of this object.
Definition LogControl.h:195
Exchange LineWriter for the lifetime of this object.
Definition LogControl.h:204