Skip to content

Commit

Permalink
Refactor/Fix framework tests (config) (#124)
Browse files Browse the repository at this point in the history
* Use absolute paths -> tests binary doesn't depend on working directory
* Add test_directory_setups:
  Creates for each test setup another directory to allow well-defined environments
* Updates usage of Everest::Config constructor
* Add setting options in config.yaml files to allow 'custom directories'
* Add logging config

Signed-off-by: Andreas Heinrich <andreas.heinrich@rwth-aachen.de>
  • Loading branch information
andistorm authored Nov 22, 2023
1 parent 76710ac commit d234624
Show file tree
Hide file tree
Showing 35 changed files with 367 additions and 96 deletions.
52 changes: 21 additions & 31 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
include(Catch)

add_executable(tests test_config.cpp)
add_executable(tests)

target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_sources(tests PRIVATE
test_config.cpp
helpers.cpp
)

target_link_libraries(tests
PRIVATE
Expand All @@ -11,33 +20,14 @@ target_link_libraries(tests

catch_discover_tests(tests)

# NOTE: using configure_file() here, because file(COPY) can't rename

# valid config.json
configure_file(test_configs/valid_config.yaml valid/config.yaml COPYONLY)

# broken config.json
configure_file(test_configs/broken_config.yaml broken_config/config.yaml COPYONLY)

# missing module config.json
configure_file(test_configs/missing_module_config.yaml missing_module_config/config.yaml COPYONLY)

# broken manifest.json
configure_file(test_configs/broken_manifest_config.yaml broken_manifest/config.yaml COPYONLY)
file(COPY test_modules/TESTBrokenManifest DESTINATION broken_manifest/modules)

# broken manifest.json
configure_file(test_configs/broken_manifest2_config.yaml broken_manifest2/config.yaml COPYONLY)
file(COPY test_modules/TESTBrokenManifest2 DESTINATION broken_manifest2/modules)

# valid manifest.json, missing interface
configure_file(test_configs/valid_manifest_missing_interface_config.yaml valid_manifest_missing_interface/config.yaml COPYONLY)
file(COPY test_modules/TESTValidManifestMissingInterface DESTINATION valid_manifest_missing_interface/modules)

# valid manifest.json, valid interface
configure_file(test_interfaces/TESTmissinginterface.yaml valid_manifest_valid_interface/interfaces/TESTmissinginterface.yaml COPYONLY)
configure_file(test_configs/valid_manifest_missing_interface_config.yaml valid_manifest_valid_interface/config.yaml COPYONLY)
file(COPY test_modules/TESTValidManifestMissingInterface DESTINATION valid_manifest_valid_interface/modules)

# create dummy directory for types
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/dummy_types")
include(test_directory_setups/empty_config.cmake)
include(test_directory_setups/broken_yaml.cmake)
include(test_directory_setups/missing_module.cmake)
include(test_directory_setups/broken_manifest_1.cmake)
include(test_directory_setups/broken_manifest_2.cmake)
include(test_directory_setups/missing_interface.cmake)
include(test_directory_setups/valid_config.cmake)
include(test_directory_setups/empty_yaml_object.cmake)
include(test_directory_setups/empty_yaml.cmake)
include(test_directory_setups/null_yaml.cmake)
include(test_directory_setups/string_yaml.cmake)
18 changes: 18 additions & 0 deletions tests/helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#include <tests/helpers.hpp>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

namespace Everest {
namespace tests {

fs::path get_bin_dir() {
return fs::canonical("/proc/self/exe").parent_path();
}

} // namespace tests
} // namespace Everest
17 changes: 17 additions & 0 deletions tests/include/tests/helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest
#ifndef TESTS_HELPERS_HPP

#include <filesystem>

namespace fs = std::filesystem;

namespace Everest {
namespace tests {

fs::path get_bin_dir();

} // namespace tests
} // namespace Everest

#endif // TESTS_HELPERS_HPP
128 changes: 76 additions & 52 deletions tests/test_config.cpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,106 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
// Copyright Pionix GmbH and Contributors to EVerest
#include <catch2/catch_all.hpp>

#include <framework/everest.hpp>
#include <framework/runtime.hpp>
#include <tests/helpers.hpp>
#include <utils/config.hpp>

// FIXME: convert the configs, interfaces, etc. used in these tests to yaml
namespace fs = std::filesystem;

SCENARIO("Check config parser", "[!throws]") {
GIVEN("An empty config") {
Everest::Config config = Everest::Config("../../schemas", "valid/config.yaml", "test_modules",
"test_interfaces", "dummy_types", "", "");
THEN("It should not contain some module") {
CHECK(!config.contains("some_module"));
SCENARIO("Check RuntimeSetting Constructor", "[!throws]") {
std::string bin_dir = Everest::tests::get_bin_dir().string() + "/";
GIVEN("An invalid prefix, but a valid config file") {
THEN("It should throw BootException") {
CHECK_THROWS_AS(
Everest::RuntimeSettings(bin_dir + "non-valid-prefix/", bin_dir + "valid_config/config.yaml"),
Everest::BootException);
}
}

// FIXME (aw): all the exception checks can't distinguish, what the
// real reason was, this needs to be improved (probably
// by proper ExceptionTypes)
GIVEN("An invalid main dir") {
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "wrong_maindir", "test_modules", "test_interfaces",
"dummy_types", "", ""),
Everest::EverestConfigError);
GIVEN("A valid prefix, but a non existing config file") {
THEN("It should throw BootException") {
CHECK_THROWS_AS(Everest::RuntimeSettings(bin_dir + "valid_config/", bin_dir + "non-existing-config.yaml"),
Everest::BootException);
}
}

GIVEN("A non existing config file") {
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "missing_config", "test_modules", "test_interfaces",
"dummy_types", "", ""),
Everest::EverestConfigError);
GIVEN("A valid prefix and a valid config file") {
THEN("It should not throw") {
CHECK_NOTHROW(Everest::RuntimeSettings(bin_dir + "valid_config/", bin_dir + "valid_config/config.yaml"));
}
}

GIVEN("A broken config file") {
GIVEN("A broken yaml file") {
THEN("It should throw") {
CHECK_THROWS(Everest::RuntimeSettings(bin_dir + "broken_yaml/", bin_dir + "broken_yaml/config.yaml"));
}
}
GIVEN("A empty yaml file") {
THEN("It shouldn't throw") {
CHECK_NOTHROW(Everest::RuntimeSettings(bin_dir + "empty_yaml/", bin_dir + "empty_yaml/config.yaml"));
}
}
GIVEN("A empty yaml object file") {
THEN("It shouldn't throw") {
CHECK_NOTHROW(
Everest::RuntimeSettings(bin_dir + "empty_yaml_object/", bin_dir + "empty_yaml_object/config.yaml"));
}
}
GIVEN("A null yaml file") {
THEN("It shouldn't throw") {
CHECK_NOTHROW(Everest::RuntimeSettings(bin_dir + "null_yaml/", bin_dir + "null_yaml/config.yaml"));
}
}
GIVEN("A string yaml file") {
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "broken_config/config.yaml", "test_modules",
"test_interfaces", "dummy_types", "", ""),
Everest::EverestConfigError);
CHECK_THROWS_AS(Everest::RuntimeSettings(bin_dir + "string_yaml/", bin_dir + "string_yaml/config.yaml"),
Everest::BootException);
}
}
}
SCENARIO("Check Config Constructor", "[!throws]") {
std::string bin_dir = Everest::tests::get_bin_dir().string() + "/";
GIVEN("A config without modules") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "empty_config/", bin_dir + "empty_config/config.yaml"));
Everest::Config config = Everest::Config(rs);
THEN("It should not contain the module some_module") {
CHECK(!config.contains("some_module"));
}
}

GIVEN("A config file referencing a non existent module") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "missing_module/", bin_dir + "missing_module/config.yaml"));
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "missing_module_config/config.yaml", "test_modules",
"test_interfaces", "dummy_types", "", ""),
Everest::EverestConfigError);
CHECK_THROWS_AS(Everest::Config(rs), Everest::EverestConfigError);
}
}

// FIXME
GIVEN("A config file using a module with broken manifest") {
GIVEN("A config file using a module with broken manifest (missing meta data)") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "broken_manifest_1/", bin_dir + "broken_manifest_1/config.yaml"));
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config(rs), Everest::EverestConfigError);
}
}
GIVEN("A config file using a module with broken manifest (empty file)") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "broken_manifest_2/", bin_dir + "broken_manifest_2/config.yaml"));
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "broken_manifest/config.yaml", "broken_manifest/modules",
"test_interfaces", "dummy_types", "", ""),
Everest::EverestConfigError);
// FIXME: an empty manifest breaks the test?
CHECK_THROWS_AS(Everest::Config("../../schemas", "broken_manifest2/config.yaml", "broken_manifest2/modules",
"test_interfaces", "dummy_types", "", ""),
Everest::EverestConfigError);
CHECK_THROWS_AS(Everest::Config(rs), Everest::EverestConfigError);
}
}

GIVEN("A config file using a module with a valid manifest referencing an invalid interface") {
GIVEN("A config file using a module with an invalid interface (missing "
"interface)") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "missing_interface/", bin_dir + "missing_interface/config.yaml"));
THEN("It should throw Everest::EverestConfigError") {
CHECK_THROWS_AS(Everest::Config("../../schemas", "valid_manifest_missing_interface/config.yaml",
"valid_manifest_missing_interface/modules", "test_interfaces",
"dummy_types", "", ""),
Everest::EverestConfigError);
CHECK_THROWS_AS(Everest::Config(rs), Everest::EverestConfigError);
}
}

GIVEN("A valid config") {
std::shared_ptr<Everest::RuntimeSettings> rs = std::make_shared<Everest::RuntimeSettings>(
Everest::RuntimeSettings(bin_dir + "valid_config/", bin_dir + "valid_config/config.yaml"));
THEN("It should not throw at all") {
CHECK_NOTHROW(Everest::Config("../../schemas", "valid_manifest_valid_interface/config.yaml",
"valid_manifest_valid_interface/modules",
"valid_manifest_valid_interface/interfaces", "dummy_types", "", ""));
CHECK_NOTHROW(Everest::Config(rs));
}
}
}
1 change: 0 additions & 1 deletion tests/test_configs/broken_config.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions tests/test_configs/broken_manifest2_config.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions tests/test_configs/broken_manifest_1_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
active_modules:
test_missing:
module: "TESTBrokenManifest"
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
11 changes: 11 additions & 0 deletions tests/test_configs/broken_manifest_2_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
active_modules:
test_missing:
module: "TESTBrokenManifest2"
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
3 changes: 0 additions & 3 deletions tests/test_configs/broken_manifest_config.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions tests/test_configs/broken_yaml_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
active_modules: {}
settings::::
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
9 changes: 9 additions & 0 deletions tests/test_configs/empty_config_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
active_modules: {}
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
Empty file.
1 change: 1 addition & 0 deletions tests/test_configs/empty_yaml_object_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions tests/test_configs/missing_interface_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
active_modules:
missing_interface:
module: "TESTMissingInterface"
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
8 changes: 8 additions & 0 deletions tests/test_configs/missing_module_config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
active_modules:
test_missing:
module: "TESTMissingModule"
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"
1 change: 1 addition & 0 deletions tests/test_configs/null_yaml_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
1 change: 1 addition & 0 deletions tests/test_configs/string_yaml_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"This is a string!"
1 change: 0 additions & 1 deletion tests/test_configs/valid_config.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions tests/test_configs/valid_config_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
active_modules: {}
settings:
interfaces_dir: "interfaces"
modules_dir: "modules"
types_dir: "types"
errors_dir: "errors"
schemas_dir: "schemas"
www_dir: "www"
logging_config_file: "logging.ini"

This file was deleted.

13 changes: 13 additions & 0 deletions tests/test_directory_setups/broken_manifest_1.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(SETUP_NAME "broken_manifest_1")
set(PREFIX_DIR ${CMAKE_CURRENT_BINARY_DIR}/${SETUP_NAME})

configure_file(test_configs/${SETUP_NAME}_config.yaml ${SETUP_NAME}/config.yaml COPYONLY)
configure_file(test_logging.ini ${SETUP_NAME}/logging.ini COPYONLY)
file(COPY ../schemas/ DESTINATION ${SETUP_NAME}/schemas)
file(COPY test_modules/TESTBrokenManifest1 DESTINATION ${SETUP_NAME}/modules)
file(COPY test_interfaces/test_interface.yaml DESTINATION ${SETUP_NAME}/interfaces)
file(MAKE_DIRECTORY "${PREFIX_DIR}/types")
file(MAKE_DIRECTORY "${PREFIX_DIR}/errors")
file(MAKE_DIRECTORY "${PREFIX_DIR}/www")
file(MAKE_DIRECTORY "${PREFIX_DIR}/etc/everest")
file(MAKE_DIRECTORY "${PREFIX_DIR}/share/everest")
14 changes: 14 additions & 0 deletions tests/test_directory_setups/broken_manifest_2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(SETUP_NAME "broken_manifest_2")
set(PREFIX_DIR ${CMAKE_CURRENT_BINARY_DIR}/${SETUP_NAME})

configure_file(test_configs/${SETUP_NAME}_config.yaml ${SETUP_NAME}/config.yaml COPYONLY)
configure_file(test_logging.ini ${SETUP_NAME}/logging.ini COPYONLY)
file(COPY ../schemas/ DESTINATION ${SETUP_NAME}/schemas)
file(COPY test_modules/TESTBrokenManifest2 DESTINATION ${SETUP_NAME}/modules)
file(COPY test_interfaces/test_interface.yaml DESTINATION ${SETUP_NAME}/interfaces)
file(MAKE_DIRECTORY "${PREFIX_DIR}/types")
file(MAKE_DIRECTORY "${PREFIX_DIR}/errors")
file(MAKE_DIRECTORY "${PREFIX_DIR}/interfaces")
file(MAKE_DIRECTORY "${PREFIX_DIR}/www")
file(MAKE_DIRECTORY "${PREFIX_DIR}/etc/everest")
file(MAKE_DIRECTORY "${PREFIX_DIR}/share/everest")
Loading

0 comments on commit d234624

Please sign in to comment.