requested changes applied

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-28 14:33:43 +00:00
parent ac7c1ee1ee
commit e411799d0d
7 changed files with 166 additions and 161 deletions

View File

@ -22,7 +22,7 @@ add_custom_command(
COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
)
include_directories(
list(APPEND JSONNET_SAPI_HEADERS
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/headers
${PROJECT_BINARY_DIR}/gen_files
@ -36,6 +36,10 @@ add_library(jsonnet_helper STATIC
${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
)
target_include_directories(jsonnet_helper PUBLIC
${JSONNET_SAPI_HEADERS}
)
target_link_libraries(jsonnet_helper
libjsonnet_for_binaries
)
@ -51,6 +55,10 @@ foreach(exe base multiple_files yaml_stream formatter)
jsonnet_sapi
sapi::sapi
)
target_include_directories(jsonnet_${exe}_sandboxed PUBLIC
${JSONNET_SAPI_HEADERS}
)
endforeach()
add_executable(jsonnet_base_transacted

View File

@ -25,27 +25,28 @@
#include "jsonnet_sapi.sapi.h"
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h"
#include "sandboxed_api/sandbox2/util/path.h"
class JsonnetTestHelper {
protected:
enum Evaluation { BASE, MULTIPLE_FILES, YAML_STREAM };
enum Evaluation { kBase, kMultipleFiles, kYamlStream };
void TestSetUp();
void TestTearDown();
void Read_input(char* filename);
void Evaluate_jsonnet_code(char* filename, Evaluation type,
void Read_input(const char* filename);
void Evaluate_jsonnet_code(Evaluation type,
bool expected_correct);
void Write_output(char* filename_or_directory, Evaluation type);
std::string Read_output(char* filename);
void Write_output(const char* filename_or_directory, Evaluation type);
std::string Read_output(const char* filename);
std::unique_ptr<JsonnetBaseSandbox> sandbox;
std::unique_ptr<JsonnetApi> api;
std::unique_ptr<sapi::v::RemotePtr> input;
std::unique_ptr<sapi::v::RemotePtr> output;
std::unique_ptr<sapi::v::RemotePtr> vm;
std::unique_ptr<JsonnetBaseSandbox> sandbox_;
std::unique_ptr<JsonnetApi> api_;
std::unique_ptr<sapi::v::RemotePtr> input_;
std::unique_ptr<sapi::v::RemotePtr> output_;
std::unique_ptr<sapi::v::RemotePtr> vm_;
std::string input_filename_in_sandboxee;
bool if_jsonnet_vm_was_used;
bool if_input_was_read;
std::string input_filename_in_sandboxee_;
bool if_jsonnet_vm_was_used_;
bool if_input_was_read_;
};

View File

@ -14,7 +14,7 @@
#include "jsonnet_helper.h" // NOLINT(build/include)
#include <string.h>
#include <cstring>
struct JsonnetVm* c_jsonnet_make(void) {
return jsonnet_make();

View File

@ -24,16 +24,16 @@ add_custom_target(test_preparation ALL
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output
)
include_directories(
${PROJECT_SOURCE_DIR}/headers
)
add_executable(tests
${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h
jsonnet_tests.cc
jsonnet_tests_utils.cc
)
target_include_directories(tests PUBLIC
${PROJECT_SOURCE_DIR}/headers
)
target_link_libraries(tests
jsonnet_sapi sapi::sapi
gtest gmock gtest_main

View File

@ -14,6 +14,8 @@
#include "jsonnet_tests.h"
namespace {
class JsonnetTest : public JsonnetTestHelper, public testing::Test {
protected:
void SetUp() override { TestSetUp(); }
@ -22,61 +24,61 @@ class JsonnetTest : public JsonnetTestHelper, public testing::Test {
// Basic test
TEST_F(JsonnetTest, SetUp_TearDown) {
ASSERT_FALSE(if_jsonnet_vm_was_used);
ASSERT_FALSE(if_input_was_read);
ASSERT_FALSE(if_jsonnet_vm_was_used_);
ASSERT_FALSE(if_input_was_read_);
}
// One file evaluation to one file
TEST_F(JsonnetTest, One_file_no_dependencies) {
char input_file[] = "arith.jsonnet";
char output_file[] = "arith_output";
char output_to_read[] = "tests_output/arith_output";
char output_to_expect[] = "tests_expected_output/arith.golden";
constexpr char kInputFile[] = "arith.jsonnet";
constexpr char kOutputFile[] = "arith_output";
constexpr char kOutputToRead[] = "tests_output/arith_output";
constexpr char kOutputToExpect[] = "tests_expected_output/arith.golden";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE, true);
Write_output(output_file, BASE);
Read_input(kInputFile);
Evaluate_jsonnet_code(kBase, true);
Write_output(kOutputFile, kBase);
std::string produced_output = Read_output(output_to_read);
std::string expected_output = Read_output(output_to_expect);
std::string produced_output = Read_output(kOutputToRead);
std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
}
// One file evaluating to one file, dependent on some other files
TEST_F(JsonnetTest, One_file_some_dependencies) {
char input_file[] = "negroni.jsonnet";
char output_file[] = "negroni_output";
char output_to_read[] = "tests_output/negroni_output";
char output_to_expect[] = "tests_expected_output/negroni.golden";
constexpr char kInputFile[] = "negroni.jsonnet";
constexpr char kOutputFile[] = "negroni_output";
constexpr char kOutputToRead[] = "tests_output/negroni_output";
constexpr char kOutputToExpect[] = "tests_expected_output/negroni.golden";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE, true);
Write_output(output_file, BASE);
Read_input(kInputFile);
Evaluate_jsonnet_code(kBase, true);
Write_output(kOutputFile, kBase);
std::string produced_output = Read_output(output_to_read);
std::string expected_output = Read_output(output_to_expect);
const std::string produced_output = Read_output(kOutputToRead);
const std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
}
// One file evaluating to two files
TEST_F(JsonnetTest, Multiple_files) {
char input_file[] = "multiple_files_example.jsonnet";
char output_file[] = "";
char output_to_read_1[] = "tests_output/first_file.json";
char output_to_read_2[] = "tests_output/second_file.json";
char output_to_expect_1[] = "tests_expected_output/first_file.json";
char output_to_expect_2[] = "tests_expected_output/second_file.json";
constexpr char kInputFile[] = "multiple_files_example.jsonnet";
constexpr char kOutputFile[] = "";
constexpr char kOutputToRead1[] = "tests_output/first_file.json";
constexpr char kOutputToRead2[] = "tests_output/second_file.json";
constexpr char kOutputToExpect1[] = "tests_expected_output/first_file.json";
constexpr char kOutputToExpect2[] = "tests_expected_output/second_file.json";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, MULTIPLE_FILES, true);
Write_output(output_file, MULTIPLE_FILES);
Read_input(kInputFile);
Evaluate_jsonnet_code(kMultipleFiles, true);
Write_output(kOutputFile, kMultipleFiles);
std::string produced_output_1 = Read_output(output_to_read_1);
std::string produced_output_2 = Read_output(output_to_read_2);
std::string expected_output_1 = Read_output(output_to_expect_1);
std::string expected_output_2 = Read_output(output_to_expect_2);
const std::string produced_output_1 = Read_output(kOutputToRead1);
const std::string produced_output_2 = Read_output(kOutputToRead2);
const std::string expected_output_1 = Read_output(kOutputToExpect1);
const std::string expected_output_2 = Read_output(kOutputToExpect2);
ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str());
ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str());
@ -84,25 +86,27 @@ TEST_F(JsonnetTest, Multiple_files) {
// One file evaluating to yaml stream format
TEST_F(JsonnetTest, Yaml_stream) {
char input_file[] = "yaml_stream_example.jsonnet";
char output_file[] = "yaml_stream_example.yaml";
char output_to_read[] = "tests_output/yaml_stream_example.yaml";
char output_to_expect[] = "tests_expected_output/yaml_stream_example.yaml";
constexpr char kInputFile[] = "yaml_stream_example.jsonnet";
constexpr char kOutputFile[] = "yaml_stream_example.yaml";
constexpr char kOutputToRead[] = "tests_output/yaml_stream_example.yaml";
constexpr char kOutputToExpect[] = "tests_expected_output/yaml_stream_example.yaml";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, YAML_STREAM, true);
Write_output(output_file, YAML_STREAM);
Read_input(kInputFile);
Evaluate_jsonnet_code(kYamlStream, true);
Write_output(kOutputFile, kYamlStream);
std::string produced_output = Read_output(output_to_read);
std::string expected_output = Read_output(output_to_expect);
const std::string produced_output = Read_output(kOutputToRead);
const std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
}
// One file depended on some other files not accessible by the sandbox
TEST_F(JsonnetTest, Bad_evaluation) {
char input_file[] = "imports.jsonnet";
constexpr char kInputFile[] = "imports.jsonnet";
Read_input(input_file);
Evaluate_jsonnet_code(input_file, BASE, false);
Read_input(kInputFile);
Evaluate_jsonnet_code(kBase, false);
}
} //namespace

View File

@ -14,91 +14,87 @@
#include "jsonnet_tests.h"
// Prepare what is needed to perform a test.
// Prepares what is needed to perform a test.
void JsonnetTestHelper::TestSetUp() {
// Get paths to where input and output is stored.
char buffer[256];
int error = readlink("/proc/self/exe", buffer, 256);
std::filesystem::path binary_path = dirname(buffer);
std::filesystem::path input_path =
binary_path / "tests_input" / "dummy_input";
std::filesystem::path output_path =
binary_path / "tests_output" / "dummy_output";
ASSERT_GE(error, 0);
std::pair<absl::string_view, absl::string_view> parts_of_path = sandbox2::file::SplitPath(buffer);
absl::string_view binary_path = parts_of_path.first;
std::string input_path = sandbox2::file::JoinPath(binary_path, "tests_input", "dummy_input");
std::string output_path = sandbox2::file::JoinPath(binary_path, "tests_output", "dummy_input");
// Set up sandbox and api.
sandbox = std::make_unique<JsonnetBaseSandbox>(input_path.string(),
output_path.string());
ASSERT_THAT(sandbox->Init(), sapi::IsOk());
api = std::make_unique<JsonnetApi>(sandbox.get());
sandbox_ = absl::make_unique<JsonnetBaseSandbox>(input_path,
output_path);
ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
api_ = absl::make_unique<JsonnetApi>(sandbox_.get());
// Initialize library's main structure.
SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api->c_jsonnet_make());
vm = std::make_unique<sapi::v::RemotePtr>(vm_ptr);
SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api_->c_jsonnet_make());
vm_ = absl::make_unique<sapi::v::RemotePtr>(vm_ptr);
if_jsonnet_vm_was_used = false;
if_input_was_read = false;
return;
if_jsonnet_vm_was_used_ = false;
if_input_was_read_ = false;
}
// Clean up after a test.
// Cleans up after a test.
void JsonnetTestHelper::TestTearDown() {
if (if_jsonnet_vm_was_used) {
if (if_jsonnet_vm_was_used_) {
SAPI_ASSERT_OK_AND_ASSIGN(
char* result, api->c_jsonnet_realloc(vm.get(), output.get(), 0));
char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0));
}
ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk());
if (if_input_was_read_) {
ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk());
}
ASSERT_THAT(api->c_jsonnet_destroy(vm.get()), sapi::IsOk());
if (if_input_was_read) {
ASSERT_THAT(api->c_free_input(input.get()), sapi::IsOk());
}
return;
}
// Read input from file.
void JsonnetTestHelper::Read_input(char* filename) {
// Reads input from file.
void JsonnetTestHelper::Read_input(const char* filename) {
std::string in_file_in_sandboxee(std::string("/input/") +
basename(&filename[0]));
input_filename_in_sandboxee = std::move(in_file_in_sandboxee);
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str());
basename(const_cast<char*>(&filename[0])));
input_filename_in_sandboxee_ = std::move(in_file_in_sandboxee);
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr,
api->c_read_input(0, in_file_var.PtrBefore()));
input = std::make_unique<sapi::v::RemotePtr>(input_ptr);
api_->c_read_input(0, in_file_var.PtrBefore()));
input_ = absl::make_unique<sapi::v::RemotePtr>(input_ptr);
if_input_was_read = true;
return;
if_input_was_read_ = true;
}
// Evaluate jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type,
// Evaluates jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(Evaluation type,
bool expected_correct) {
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str());
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
sapi::v::Int error;
char* output_ptr;
switch (type) {
case BASE: {
case kBase: {
SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr,
api->c_jsonnet_evaluate_snippet(vm.get(), in_file_var.PtrBefore(),
input.get(), error.PtrAfter()));
api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(),
input_.get(), error.PtrAfter()));
break;
}
case MULTIPLE_FILES: {
case kMultipleFiles: {
SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, api->c_jsonnet_evaluate_snippet_multi(
vm.get(), in_file_var.PtrBefore(), input.get(),
output_ptr, api_->c_jsonnet_evaluate_snippet_multi(
vm_.get(), in_file_var.PtrBefore(), input_.get(),
error.PtrAfter()));
break;
}
case YAML_STREAM: {
case kYamlStream: {
SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, api->c_jsonnet_evaluate_snippet_stream(
vm.get(), in_file_var.PtrBefore(), input.get(),
output_ptr, api_->c_jsonnet_evaluate_snippet_stream(
vm_.get(), in_file_var.PtrBefore(), input_.get(),
error.PtrAfter()));
break;
}
@ -110,56 +106,52 @@ void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type,
ASSERT_THAT(error.GetValue(), testing::Eq(1));
}
output = std::make_unique<sapi::v::RemotePtr>(output_ptr);
output_ = absl::make_unique<sapi::v::RemotePtr>(output_ptr);
if_jsonnet_vm_was_used = true;
return;
if_jsonnet_vm_was_used_ = true;
}
// Write output to file.
void JsonnetTestHelper::Write_output(char* filename_or_directory,
// Writes output to file.
void JsonnetTestHelper::Write_output(const char* filename_or_directory,
Evaluation type) {
bool success;
switch (type) {
case BASE: {
case kBase: {
std::string out_file_in_sandboxee(std::string("/output/") +
basename(&filename_or_directory[0]));
basename(const_cast<char*>(&filename_or_directory[0])));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(
success,
api->c_write_output_file(output.get(), out_file_var.PtrBefore()));
api_->c_write_output_file(output_.get(), out_file_var.PtrBefore()));
break;
}
case MULTIPLE_FILES: {
case kMultipleFiles: {
std::string out_file_in_sandboxee(std::string("/output/"));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(
success, api->c_write_multi_output_files(
output.get(), out_file_var.PtrBefore(), false));
success, api_->c_write_multi_output_files(
output_.get(), out_file_var.PtrBefore(), false));
break;
}
case YAML_STREAM: {
case kYamlStream: {
std::string out_file_in_sandboxee(std::string("/output/") +
basename(&filename_or_directory[0]));
basename(const_cast<char*>(&filename_or_directory[0])));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(
success,
api->c_write_output_stream(output.get(), out_file_var.PtrBefore()));
api_->c_write_output_stream(output_.get(), out_file_var.PtrBefore()));
break;
}
}
ASSERT_THAT(success, testing::Eq(true));
return;
}
// Reading the output written to a file by library function / expected output
std::string JsonnetTestHelper::Read_output(char* filename) {
// Reads the output written to a file by library function / expected output
std::string JsonnetTestHelper::Read_output(const char* filename) {
std::ifstream input_stream(filename);
std::string contents((std::istreambuf_iterator<char>(input_stream)),
std::istreambuf_iterator<char>());