libzypp  17.30.2
YamlTestcaseHelpers.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_MISC_YAMLTESTCASEHELPERS_H
13 #define ZYPP_MISC_YAMLTESTCASEHELPERS_H
14 
15 #include <zypp/base/LogControl.h>
16 #include "LoadTestcase.h"
17 #include "TestcaseSetupImpl.h"
18 
19 #include <yaml-cpp/yaml.h>
20 
21 #include <type_traits>
22 
23 namespace yamltest::detail {
24 
25  bool parseSetup ( const YAML::Node &setup, zypp::misc::testcase::TestcaseSetup &t, std::string *err ) {
26 
27  auto &target = t.data();
28  MIL << "Parsing setup node " << std::endl;
29  for ( YAML::const_iterator it = setup.begin(); it != setup.end(); it++ ) {
30 
31  const std::string &key = it->first.as<std::string>();
32  const auto &data = it->second;
33 
34  MIL << "Found key " << key << std::endl;
35 
36  // reads a sequence either from a file or inline, depending on the type of "data"
37  auto readListInlineOrFromFile = [&]( const auto &cb , std::string *err ) -> bool {
38  if ( data.Type() == YAML::NodeType::Sequence ) {
39  int cnt = 0;
40  for ( const auto &node: data ) {
41  if ( !cb( node, err ) ) return false;
42  cnt ++;
43  }
44  MIL << "Loaded " << cnt << " Elements inline" << std::endl;
45  } else {
46  const std::string &fName = data.as<std::string>();
47  MIL << "Trying to load list from file " << fName << std::endl;
48  try {
49  auto doc = YAML::LoadFile( fName );
50  if ( doc.Type() != YAML::NodeType::Sequence ) {
51  if ( err ) *err = "Expected the top node to be a sequence in external file for key: ";
52  return false;
53  }
54 
55  int cnt = 0;
56  for ( const auto &node : doc ) {
57  if ( !cb( node, err ) ) return false;
58  cnt ++;
59  }
60  MIL << "Loaded " << cnt << " Elements from file" << std::endl;
61  } catch ( YAML::Exception &e ) {
62  if ( err ) {
63  auto errStr = zypp::str::Str();
64  errStr << e.what();
65  if ( !e.mark.is_null() ) {
66  errStr << " Line: " << e.mark.line << " Col: " << e.mark.column << " pos: " << e.mark.pos;
67  }
68  *err = errStr;
69  }
70  return false;
71  } catch ( ... ) {
72  if ( err ) *err = zypp::str::Str() << "Unknown error when parsing the file for " << key;
73  return false;
74  }
75  }
76  return true;
77  };
78 
79  if ( key == "resolverFlags" ) {
80 #define if_SolverFlag( N ) if ( data[#N] ) { target.N = data[#N].as<bool>(); }
81  if_SolverFlag( ignorealreadyrecommended ) if ( data["ignorealready"] ) { target.ignorealreadyrecommended = data["ignorealready"].as<bool>(); }
82  if_SolverFlag( onlyRequires ) if ( data["ignorerecommended"] ) { target.onlyRequires = data["ignorerecommended"].as<bool>(); }
83  if_SolverFlag( forceResolve )
84 
85  if_SolverFlag( cleandepsOnRemove )
86 
87  if_SolverFlag( allowDowngrade )
88  if_SolverFlag( allowNameChange )
89  if_SolverFlag( allowArchChange )
91 
92  if_SolverFlag( dupAllowDowngrade )
94  if_SolverFlag( dupAllowArchChange )
96 #undef if_SolverFlag
97  if ( data["focus"] ) {
98  target.resolverFocus = zypp::resolverFocusFromString( data["focus"].as<std::string>() );
99  }
100  } else if ( key == ("system") ) {
101  target.systemRepo = zypp::misc::testcase::RepoDataImpl {
103  "@System",
104  99,
105  data["file"].as<std::string>()
106  };
107  }
108  else if ( key == ("hardwareInfo") ) {
109  target.hardwareInfoFile = data.as<std::string>();
110  }
111  else if ( key == ("modalias") ) {
112  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, auto ){
113  target.modaliasList.push_back( dataNode.as<std::string>() );
114  return true;
115  }, err );
116  if ( !success ) return false;
117  }
118  else if ( key == ("multiversion") ) {
119  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, auto ){
120  target.multiversionSpec.insert( dataNode.as<std::string>() );
121  return true;
122  }, err );
123  if ( !success ) return false;
124  }
125  else if (key == ("channels")) {
126  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, auto ){
127  std::string name = dataNode["alias"].as<std::string>();
128  std::string file = dataNode["file"].as<std::string>();
129 
130  unsigned prio = 99;
131  if ( dataNode["priority"] )
132  prio = dataNode["priority"].as<unsigned>();
133 
134  target.repos.push_back( zypp::misc::testcase::RepoDataImpl{
136  name,
137  prio,
138  file
139  });
140  return true;
141  }, err );
142  if ( !success ) return false;
143  }
144  else if ( key == ("sources") )
145  {
146  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, auto ){
147  std::string url = dataNode["url"].as<std::string>();
148  std::string alias = dataNode["name"].as<std::string>();
149  target.repos.push_back( zypp::misc::testcase::RepoDataImpl{
151  alias,
152  99,
153  url
154  });
155  return true;
156  }, err );
157  if ( !success ) return false;
158  }
159  else if ( key == ("force-install") )
160  {
161  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, auto ){
162  target.forceInstallTasks.push_back( zypp::misc::testcase::ForceInstallImpl{
163  dataNode["channel"].as<std::string>(),
164  dataNode["package"].as<std::string>(),
165  dataNode["kind"].as<std::string>()
166  });
167  return true;
168  }, err );
169  if ( !success ) return false;
170  }
171  else if ( key == ("mediaid") )
172  {
173  target.show_mediaid = data.as<bool>();
174  }
175  else if ( key == ("arch") ) {
176  std::string architecture = data.as<std::string>();
177  if ( architecture.empty() ) {
178  if (err) *err = zypp::str::Str() << "Property 'arch' in setup can not be empty." << std::endl;
179  return false;
180  }
181  else {
182  MIL << "Setting architecture to '" << architecture << "'" << std::endl;
183  target.architecture = zypp::Arch( architecture );
184  }
185  }
186  else if ( key == ("locales") )
187  {
188  bool success = readListInlineOrFromFile( [&target]( const YAML::Node &dataNode, std::string *err ){
189  zypp::Locale loc( dataNode["name"].as<std::string>() );
190  std::string fate = dataNode["fate"].as<std::string>();
191  if ( !loc ) {
192  if (err) *err = zypp::str::Str() << "Bad or missing name in locale..." << std::endl;
193  return false;
194  }
195  else if ( fate == "added" ) {
196  target.localesTracker.added().insert( loc );
197  }
198  else if ( fate == "removed" ) {
199  target.localesTracker.removed().insert( loc );
200  }
201  else {
202  target.localesTracker.current().insert( loc );
203  }
204  return true;
205  }, err );
206  if ( !success ) return false;
207  }
208  else if ( key == ("vendors") )
209  {
210  bool success = readListInlineOrFromFile( [&target]( const YAML::Node & dataNode, std::string * err ) {
211  std::vector<std::string> vlist;
212  for ( const auto & node : dataNode )
213  vlist.push_back( node.as<std::string>() );
214  if ( ! vlist.empty() )
215  target.vendorLists.push_back( std::move(vlist) );
216  return true;
217  }, err );
218  if ( !success ) return false;
219  }
220  else if ( key == ("autoinst") ) {
221  bool success = readListInlineOrFromFile( [&]( const YAML::Node &dataNode, auto ){
222  target.autoinstalled.push( zypp::IdString( dataNode.as<std::string>() ).id() );
223  return true;
224  }, err );
225  if ( !success ) return false;
226  }
227  else if ( key == ("systemCheck") ) {
228  target.systemCheck = data.as<std::string>();
229  }
230  else if ( key == ("setlicencebit") ) {
231  target.set_licence = data.as<bool>();
232  }
233  else {
234  ERR << "Ignoring unrecognized tag '" << key << "' in setup" << std::endl;
235  }
236  }
237  return true;
238  }
239 
240  template <typename T>
241  bool parseJobs ( const YAML::Node &trial, std::vector<T> &target, std::string *err );
242 
243  template <typename T>
244  bool parseSingleJob ( const YAML::Node &jobNode, std::vector<T> &target, std::string *err ) {
245 
246  constexpr bool isSubNode = std::is_same_v<T, std::shared_ptr<zypp::misc::testcase::TestcaseTrial::Node>>;
247  if ( jobNode["include"] ) {
248  //handle include
249  const auto &fName = jobNode["include"].as<std::string>();
250  MIL << "Including file " << fName << std::endl;
251  try {
252  auto doc = YAML::LoadFile( fName );
253  if ( !parseJobs( doc, target, err ) )
254  return false;
255  MIL << "Including file " << fName << "was successful" << std::endl;
256  } catch ( YAML::Exception &e ) {
257  if ( err ) *err = e.what();
258  return false;
259  } catch ( ... ) {
260  if ( err ) *err = zypp::str::Str() << "Unknown error when parsing the file: " << fName;
261  return false;
262  }
263  return true;
264  }
265 
267  if ( !jobNode["job"] ) {
268  if ( err ) {
269  auto errStr = zypp::str::Str();
270  const auto &mark = jobNode.Mark();
271  errStr << "'job' key missing from trial node.";
272  if ( !mark.is_null() ) {
273  errStr << " Line: " << mark.line << " Col: " << mark.column << " pos: " << mark.pos;
274  }
275  *err = errStr;
276  }
277  return false;
278  }
279 
280  for ( const auto &elem : jobNode ) {
281  const std::string &key = elem.first.as<std::string>();
282  const auto &data = elem.second;
283  if ( key == "job" ) {
284  n.name() = data.as<std::string>();
285  } else if ( key == "__content") {
286  n.value() = data.as<std::string>();
287  } else {
288  if( data.IsScalar() ) {
289  n.properties().insert( { key, data.as<std::string>() } );
290  } else if ( data.IsSequence() ) {
291  // if the type of a data field is a sequence, we treat all the elements in there
292  // as sub elements. Just like in XML you can have sub nodes its the same here
293  // the key name is ignored in those cases and can be chosen freely
294  if ( !parseJobs( data, n.children(), err ) )
295  return false;
296  } else if ( data.IsMap() ) {
297  // if the type of a data field is a map, we build a child node from it.
298  // Just like with sequence but a single field.
299  // The key name is ignored in those cases and can be chosen freely
300  if ( !parseSingleJob( data, n.children(), err) )
301  return false;
302  } else {
303  ERR << "Ignoring field " << key << " with unsupported type:" << data.Type() << std::endl;
304  }
305  }
306  }
307  if constexpr ( isSubNode ) {
308  target.push_back( std::make_shared<zypp::misc::testcase::TestcaseTrial::Node>( std::move(n) ) );
309  } else {
310  target.push_back( std::move(n) );
311  }
312  return true;
313  }
314 
315  template <typename T>
316  bool parseJobs ( const YAML::Node &trial, std::vector<T> &target, std::string *err ) {
317  for ( const auto &jobNode : trial ) {
318  if ( !parseSingleJob( jobNode, target, err ) )
319  return false;
320  }
321  return true;
322  }
323 
324  bool parseTrial ( const YAML::Node &trial, zypp::misc::testcase::TestcaseTrial &target, std::string *err ) {
325  MIL << "Parsing trials." << std::endl;
326  return parseJobs( trial, target.nodes(), err );
327  }
328 }
329 
330 #endif // ZYPP_MISC_YAMLTESTCASEHELPERS_H
#define if_SolverFlag(N)
Architecture.
Definition: Arch.h:37
Access to the sat-pools string space.
Definition: IdString.h:43
IdType id() const
Expert backdoor.
Definition: IdString.h:122
'Language[_Country]' codes.
Definition: Locale.h:50
bool parseSetup(const YAML::Node &setup, zypp::misc::testcase::TestcaseSetup &t, std::string *err)
bool parseTrial(const YAML::Node &trial, zypp::misc::testcase::TestcaseTrial &target, std::string *err)
bool parseJobs(const YAML::Node &trial, std::vector< T > &target, std::string *err)
bool parseSingleJob(const YAML::Node &jobNode, std::vector< T > &target, std::string *err)
_allowvendorchange dupAllowNameChange
Definition: Resolver.cc:176
_allowvendorchange _dup_allownamechange dupAllowVendorChange
Definition: Resolver.cc:178
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:203
const std::vector< std::shared_ptr< Node > > & children() const
Definition: LoadTestcase.cc:91
const std::string & name() const
Definition: LoadTestcase.cc:66
const std::map< std::string, std::string > & properties() const
Definition: LoadTestcase.cc:85
const std::string & value() const
Definition: LoadTestcase.cc:72
const std::vector< Node > & nodes() const
Definition: LoadTestcase.cc:54
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:212
#define MIL
Definition: Logger.h:96
#define ERR
Definition: Logger.h:98