Skip to content

Commit bf04d29

Browse files
authored
Improve answerfile code and add unit tests (#50)
* Improve answerfile code and add unit tests Signed-off-by: lbgracioso <[email protected]> * Clean code Signed-off-by: lbgracioso <[email protected]> * Fix One Defition Rule (ODR) violation on Signed-off-by: lbgracioso <[email protected]> * Fix One Definition Rule (ODR) violation on tests.h Signed-off-by: lbgracioso <[email protected]> * AnswerFile::loadFile was missing m_path declaration Signed-off-by: lbgracioso <[email protected]> --------- Signed-off-by: lbgracioso <[email protected]>
1 parent 4a87d58 commit bf04d29

File tree

7 files changed

+399
-242
lines changed

7 files changed

+399
-242
lines changed

include/cloysterhpc/answerfile.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef CLOYSTERHPC_ANSWERFILE_H_
77
#define CLOYSTERHPC_ANSWERFILE_H_
88

9+
#include "os.h"
910
#include <boost/asio.hpp>
1011
#include <cloysterhpc/inifile.h>
1112
#include <optional>
@@ -14,8 +15,11 @@
1415
using boost::asio::ip::address;
1516

1617
class AnswerFile {
17-
18+
#ifdef BUILD_TESTING
19+
public:
20+
#else
1821
private:
22+
#endif
1923
struct AFNetwork {
2024
std::optional<address> subnet_mask;
2125
std::optional<address> gateway;
@@ -45,7 +49,7 @@ class AnswerFile {
4549

4650
struct AFSystem {
4751
std::filesystem::path disk_image;
48-
std::string distro;
52+
OS::Distro distro;
4953
std::string version;
5054
std::string kernel;
5155
};
@@ -88,6 +92,18 @@ class AnswerFile {
8892
AFNode loadNode(const std::string& section);
8993
AFNode validateNode(AFNode node);
9094

95+
template <typename T>
96+
void validateAttribute(const std::string& sectionName,
97+
const std::string& attributeName, T& objectAttr, const T& genericAttr);
98+
99+
template <typename T>
100+
void convertNetworkAddressAndValidate(const std::string& section,
101+
const std::string& fieldName, T& destination, bool isOptional = true);
102+
103+
template <typename NetworkType>
104+
void loadNetwork(const std::string& networkSection, NetworkType& network,
105+
bool optionalNameservers = true);
106+
91107
public:
92108
AFNetwork external;
93109
AFNetwork management;
@@ -98,6 +114,9 @@ class AnswerFile {
98114
AFSystem system;
99115
AFNodes nodes;
100116

117+
void loadFile(const std::filesystem::path& path);
118+
119+
AnswerFile();
101120
explicit AnswerFile(const std::filesystem::path& path);
102121
};
103122

include/cloysterhpc/inifile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class inifile {
1818
void loadFile(std::string_view filepath);
1919
void loadFile(const std::filesystem::path& filepath);
2020
std::string getValue(const std::string& section, const std::string& key,
21-
const bool optional = true);
21+
const bool optional = true, const bool canBeNull = true);
2222
void setValue(const std::string& section, const std::string& key,
2323
const std::string& newValue);
2424
bool deleteValue(const std::string& section, const std::string& key);

include/cloysterhpc/tests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Created by Lucas Gracioso <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CLOYSTERHPC_TESTS_H_
7+
#define CLOYSTERHPC_TESTS_H_
8+
9+
#include <doctest/doctest.h>
10+
#include <filesystem>
11+
12+
namespace tests {
13+
inline std::filesystem::path sampleDirectory {
14+
std::filesystem::current_path().parent_path().parent_path() / "test/sample"
15+
}; // current_path corresponds to '<cloyster_main_dir>/<build_dir>/test'
16+
}
17+
18+
#endif // CLOYSTERHPC_TESTS_H_

0 commit comments

Comments
 (0)