mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Merge pull request #101 from cblichmann:02jsonnet-contrib
PiperOrigin-RevId: 424813033 Change-Id: I617ac515e471ea220f34a9ca636aa0669799b968
This commit is contained in:
commit
e8b54f7088
|
@ -4,7 +4,7 @@
|
|||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http:#www.apache.org/licenses/LICENSE-2.0
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
@ -28,7 +28,7 @@ FetchContent_Declare(jsonnet
|
|||
GIT_REPOSITORY https://github.com/google/jsonnet.git
|
||||
GIT_TAG v0.18.0 # 2021-12-21
|
||||
)
|
||||
set(BUILD_TESTS OFF)
|
||||
set(BUILD_TESTS OFF CACHE BOOL "") # Do not build jsonnet tests
|
||||
FetchContent_MakeAvailable(jsonnet)
|
||||
create_directory_symlink("${jsonnet_SOURCE_DIR}"
|
||||
"${PROJECT_BINARY_DIR}/jsonnet")
|
||||
|
@ -64,5 +64,27 @@ if(SAPI_ENABLE_TESTS)
|
|||
include(GoogleTest)
|
||||
enable_testing()
|
||||
|
||||
add_subdirectory(tests)
|
||||
# Create directories so the tests will be able to access them
|
||||
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests_input"
|
||||
"${PROJECT_BINARY_DIR}/tests_output"
|
||||
"${PROJECT_BINARY_DIR}/tests_expected_output")
|
||||
|
||||
add_custom_target(test_preparation ALL
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/*
|
||||
${PROJECT_BINARY_DIR}/tests_input
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/*
|
||||
${PROJECT_BINARY_DIR}/tests_expected_output
|
||||
)
|
||||
|
||||
add_executable(jsonnet_tests
|
||||
jsonnet_tests.cc
|
||||
)
|
||||
target_include_directories(jsonnet_tests PUBLIC
|
||||
${PROJECT_SOURCE_DIR}
|
||||
)
|
||||
target_link_libraries(jsonnet_tests
|
||||
jsonnet_sapi
|
||||
sapi::test_main
|
||||
)
|
||||
gtest_discover_tests(jsonnet_tests)
|
||||
endif()
|
|
@ -69,9 +69,9 @@ directory, in a file called `formatter_example.jsonnet`.
|
|||
## Testing
|
||||
|
||||
A few tests prepared with a use of
|
||||
[Google Test](https://github.com/google/googletest) framework can be found in
|
||||
the `tests/` directory. To run them type:
|
||||
[Google Test](https://github.com/google/googletest) framework are included. To
|
||||
run them type:
|
||||
|
||||
```
|
||||
ctest ./tests
|
||||
ctest -R JsonnetTest.
|
||||
```
|
|
@ -12,19 +12,18 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files")
|
||||
file(COPY "${jsonnet_SOURCE_DIR}/cmd/jsonnet.cpp" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/")
|
||||
file(COPY "${PROJECT_SOURCE_DIR}/jsonnet.patch" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/")
|
||||
configure_file("${jsonnet_SOURCE_DIR}/cmd/jsonnet.cpp"
|
||||
"${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp" COPYONLY)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
|
||||
COMMAND cd ${PROJECT_BINARY_DIR}/gen_files && patch < ${PROJECT_SOURCE_DIR}/jsonnet.patch > /dev/null
|
||||
COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
|
||||
OUTPUT "${PROJECT_BINARY_DIR}/gen_files/write_helper.cc"
|
||||
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files"
|
||||
COMMAND patch -o write_helper.cc
|
||||
< "${PROJECT_SOURCE_DIR}/jsonnet.patch" > /dev/null
|
||||
)
|
||||
|
||||
list(APPEND JSONNET_SAPI_INCLUDE_DIRS
|
||||
${PROJECT_SOURCE_DIR}
|
||||
${PROJECT_SOURCE_DIR}/headers
|
||||
${PROJECT_BINARY_DIR}
|
||||
${PROJECT_BINARY_DIR}/gen_files
|
||||
)
|
|
@ -1,18 +1,16 @@
|
|||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
{
|
||||
concat_array: [1, 2, 3] + [4],
|
|
@ -12,7 +12,8 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This is a poorly written jsonnet file. Given to the formatter executable will be changed into a canonical jsonnet file form.
|
||||
// This is a poorly written jsonnet file. Given to the formatter executable
|
||||
// will be changed into a canonical jsonnet file form.
|
||||
local b = import "somefile.libsonnet"; # comment
|
||||
local a = import "differentfile.libsonnet"; // another comment in different style
|
||||
|
28
contrib/jsonnet/examples/jsonnet_codes/imports.jsonnet
Normal file
28
contrib/jsonnet/examples/jsonnet_codes/imports.jsonnet
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2015 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
local martinis = import 'martinis.libsonnet';
|
||||
|
||||
{
|
||||
'Vodka Martini': martinis['Vodka Martini'],
|
||||
Manhattan: {
|
||||
ingredients: [
|
||||
{ kind: 'Rye', qty: 2.5 },
|
||||
{ kind: 'Sweet Red Vermouth', qty: 1 },
|
||||
{ kind: 'Angostura', qty: 'dash' },
|
||||
],
|
||||
garnish: importstr 'garnish.txt',
|
||||
served: 'Straight Up',
|
||||
},
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
@ -12,7 +12,7 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This is a jsonnet code which evaluates to mutliple output files.
|
||||
// This is jsonnet code which evaluates to multiple output files.
|
||||
{
|
||||
"first_file.json": {
|
||||
name: 'This is the first file created by the multiple-files example code.',
|
27
contrib/jsonnet/examples/jsonnet_codes/negroni.jsonnet
Normal file
27
contrib/jsonnet/examples/jsonnet_codes/negroni.jsonnet
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2015 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
local utils = import 'utils.libsonnet';
|
||||
{
|
||||
Negroni: {
|
||||
// Divide 3oz among the 3 ingredients.
|
||||
ingredients: utils.equal_parts(3, [
|
||||
'Farmers Gin',
|
||||
'Sweet Red Vermouth',
|
||||
'Campari',
|
||||
]),
|
||||
garnish: 'Orange Peel',
|
||||
served: 'On The Rocks',
|
||||
},
|
||||
}
|
24
contrib/jsonnet/examples/jsonnet_codes/utils.libsonnet
Normal file
24
contrib/jsonnet/examples/jsonnet_codes/utils.libsonnet
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2015 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
{
|
||||
equal_parts(size, ingredients)::
|
||||
// Define a function-scoped variable.
|
||||
local qty = size / std.length(ingredients);
|
||||
// Return an array.
|
||||
[
|
||||
{ kind: i, qty: qty }
|
||||
for i in ingredients
|
||||
],
|
||||
}
|
|
@ -12,7 +12,8 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This is a jsonnet code which evaluates to json file, which can be interpreted as YAML stream.
|
||||
// This is jsonnet code which evaluates to json file, which can be
|
||||
// interpreted as YAML stream.
|
||||
local
|
||||
first_object = {
|
||||
name: 'First object\'s name.',
|
|
@ -12,12 +12,16 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef JSONNET_BASE_SANDBOX_H_
|
||||
#define JSONNET_BASE_SANDBOX_H_
|
||||
#ifndef CONTRIB_JSONNET_BASE_SANDBOX_H_
|
||||
#define CONTRIB_JSONNET_BASE_SANDBOX_H_
|
||||
|
||||
#include <libgen.h>
|
||||
#include <syscall.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "jsonnet_sapi.sapi.h" // NOLINT(build/include)
|
||||
#include "sandboxed_api/util/flag.h"
|
||||
#include "sandboxed_api/transaction.h"
|
||||
|
@ -26,7 +30,7 @@
|
|||
class JsonnetBaseSandbox : public JsonnetSandbox {
|
||||
public:
|
||||
explicit JsonnetBaseSandbox(std::string in_file, std::string out_file)
|
||||
: in_file_(in_file), out_file_(out_file) {}
|
||||
: in_file_(std::move(in_file)), out_file_(std::move(out_file)) {}
|
||||
|
||||
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||
sandbox2::PolicyBuilder *) override {
|
||||
|
@ -52,4 +56,4 @@ class JsonnetBaseSandbox : public JsonnetSandbox {
|
|||
std::string out_file_;
|
||||
};
|
||||
|
||||
#endif // JSONNET_BASE_SANDBOX_H_
|
||||
#endif // CONTRIB_JSONNET_BASE_SANDBOX_H_
|
|
@ -12,8 +12,11 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef JSNONNET_BASE_TRANSACTION_H_
|
||||
#define JSNONNET_BASE_TRANSACTION_H_
|
||||
#ifndef CONTRIB_JSNONNET_BASE_TRANSACTION_H_
|
||||
#define CONTRIB_JSNONNET_BASE_TRANSACTION_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||
|
||||
|
@ -35,4 +38,4 @@ class JsonnetTransaction : public sapi::Transaction {
|
|||
absl::Status Main() override;
|
||||
};
|
||||
|
||||
#endif // JSNONNET_BASE_TRANSACTION_H_
|
||||
#endif // CONTRIB_JSNONNET_BASE_TRANSACTION_H_
|
63
contrib/jsonnet/jsonnet_helper.h
Normal file
63
contrib/jsonnet/jsonnet_helper.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef CONTRIB_JSONNET_HELPER_H_
|
||||
#define CONTRIB_JSONNET_HELPER_H_
|
||||
|
||||
extern "C" {
|
||||
#include <libjsonnet.h> // NOLINT(build/include)
|
||||
#include <libjsonnet_fmt.h> // NOLINT(build/include)
|
||||
}
|
||||
|
||||
#include "jsonnet/cmd/utils.h" // NOLINT(build/include)
|
||||
|
||||
extern "C" {
|
||||
struct JsonnetVm* c_jsonnet_make(void);
|
||||
|
||||
void c_jsonnet_destroy(struct JsonnetVm* vm);
|
||||
|
||||
char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename,
|
||||
char* snippet, int* error);
|
||||
|
||||
char* c_jsonnet_evaluate_snippet_multi(struct JsonnetVm* vm,
|
||||
const char* filename,
|
||||
const char* snippet, int* error);
|
||||
|
||||
char* c_jsonnet_evaluate_snippet_stream(struct JsonnetVm* vm,
|
||||
const char* filename,
|
||||
const char* snippet, int* error);
|
||||
|
||||
char* c_read_input(bool filename_is_code, const char* filename);
|
||||
|
||||
void c_free_input(char* input);
|
||||
|
||||
bool c_write_output_file(const char* output, const char* output_file);
|
||||
|
||||
char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz);
|
||||
|
||||
bool c_write_multi_output_files(char* output, char* output_dir,
|
||||
bool show_output_file_names);
|
||||
|
||||
bool write_multi_output_files(char* output, const std::string& output_dir,
|
||||
bool show_output_file_names);
|
||||
|
||||
bool c_write_output_stream(char* output, char* output_file);
|
||||
|
||||
bool write_output_stream(char* output, const std::string& output_file);
|
||||
|
||||
char* c_jsonnet_fmt_snippet(struct JsonnetVm* vm, const char* filename,
|
||||
const char* snippet, int* error);
|
||||
}
|
||||
|
||||
#endif // CONTRIB_JSONNET_HELPER_H_
|
279
contrib/jsonnet/jsonnet_tests.cc
Normal file
279
contrib/jsonnet/jsonnet_tests.cc
Normal file
|
@ -0,0 +1,279 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <streambuf>
|
||||
#include <string>
|
||||
|
||||
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||
#include "jsonnet_sapi.sapi.h" // NOLINT(build/include)
|
||||
#include "gtest/gtest.h"
|
||||
#include "sandboxed_api/util/flag.h"
|
||||
#include "sandboxed_api/util/path.h"
|
||||
#include "sandboxed_api/util/status_matchers.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class JsonnetTest : public ::testing::Test {
|
||||
protected:
|
||||
enum Evaluation { kBase, kMultipleFiles, kYamlStream };
|
||||
|
||||
void SetUp() override {
|
||||
// Get paths to where input and output is stored.
|
||||
char buffer[256];
|
||||
int error = readlink("/proc/self/exe", buffer, 256);
|
||||
ASSERT_GE(error, 0);
|
||||
|
||||
std::pair<absl::string_view, absl::string_view> parts_of_path =
|
||||
sapi::file::SplitPath(buffer);
|
||||
absl::string_view binary_path = parts_of_path.first;
|
||||
|
||||
std::string input_path =
|
||||
sapi::file::JoinPath(binary_path, "tests_input", "dummy_input");
|
||||
std::string output_path =
|
||||
sapi::file::JoinPath(binary_path, "tests_output", "dummy_input");
|
||||
|
||||
// Set up sandbox and api.
|
||||
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_ = absl::make_unique<sapi::v::RemotePtr>(vm_ptr);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
if (jsonnet_vm_was_used_) {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0));
|
||||
}
|
||||
ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk());
|
||||
if (input_was_read_) {
|
||||
ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk());
|
||||
}
|
||||
}
|
||||
|
||||
// Reads input from file.
|
||||
void ReadInput(const char* filename);
|
||||
|
||||
// Evaluates jsonnet code.
|
||||
void EvaluateJsonnetCode(Evaluation type, bool expected_correct);
|
||||
|
||||
// Writes output to file.
|
||||
void WriteOutput(const char* filename_or_directory, Evaluation type);
|
||||
|
||||
// Reads the output written to a file by library function / expected output.
|
||||
std::string ReadOutput(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::string input_filename_in_sandboxee_;
|
||||
bool jsonnet_vm_was_used_ = false;
|
||||
bool input_was_read_ = false;
|
||||
};
|
||||
|
||||
void JsonnetTest::ReadInput(const char* filename) {
|
||||
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||
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_ = absl::make_unique<sapi::v::RemotePtr>(input_ptr);
|
||||
|
||||
input_was_read_ = true;
|
||||
}
|
||||
|
||||
void JsonnetTest::EvaluateJsonnetCode(Evaluation type, bool expected_correct) {
|
||||
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
|
||||
sapi::v::Int error;
|
||||
char* output_ptr;
|
||||
|
||||
switch (type) {
|
||||
case kBase: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr,
|
||||
api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(),
|
||||
input_.get(), error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
|
||||
case kMultipleFiles: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr, api_->c_jsonnet_evaluate_snippet_multi(
|
||||
vm_.get(), in_file_var.PtrBefore(), input_.get(),
|
||||
error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
|
||||
case kYamlStream: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr, api_->c_jsonnet_evaluate_snippet_stream(
|
||||
vm_.get(), in_file_var.PtrBefore(), input_.get(),
|
||||
error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (expected_correct) {
|
||||
ASSERT_THAT(error.GetValue(), testing::Eq(0));
|
||||
} else {
|
||||
ASSERT_THAT(error.GetValue(), testing::Eq(1));
|
||||
}
|
||||
|
||||
output_ = absl::make_unique<sapi::v::RemotePtr>(output_ptr);
|
||||
|
||||
jsonnet_vm_was_used_ = true;
|
||||
}
|
||||
|
||||
void JsonnetTest::WriteOutput(const char* filename_or_directory,
|
||||
Evaluation type) {
|
||||
bool success;
|
||||
|
||||
switch (type) {
|
||||
case kBase: {
|
||||
std::string out_file_in_sandboxee(
|
||||
std::string("/output/") +
|
||||
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()));
|
||||
break;
|
||||
}
|
||||
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));
|
||||
break;
|
||||
}
|
||||
|
||||
case kYamlStream: {
|
||||
std::string out_file_in_sandboxee(
|
||||
std::string("/output/") +
|
||||
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()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_THAT(success, testing::Eq(true));
|
||||
}
|
||||
|
||||
std::string JsonnetTest::ReadOutput(const char* filename) {
|
||||
std::ifstream input_stream(filename);
|
||||
std::string contents((std::istreambuf_iterator<char>(input_stream)),
|
||||
std::istreambuf_iterator<char>());
|
||||
return contents;
|
||||
}
|
||||
|
||||
// One file evaluation to one file
|
||||
TEST_F(JsonnetTest, One_file_no_dependencies) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, true);
|
||||
WriteOutput(kOutputFile, kBase);
|
||||
|
||||
std::string produced_output = ReadOutput(kOutputToRead);
|
||||
std::string expected_output = ReadOutput(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) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, true);
|
||||
WriteOutput(kOutputFile, kBase);
|
||||
|
||||
const std::string produced_output = ReadOutput(kOutputToRead);
|
||||
const std::string expected_output = ReadOutput(kOutputToExpect);
|
||||
|
||||
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
|
||||
}
|
||||
|
||||
// One file evaluating to two files
|
||||
TEST_F(JsonnetTest, Multiple_files) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kMultipleFiles, true);
|
||||
WriteOutput(kOutputFile, kMultipleFiles);
|
||||
|
||||
const std::string produced_output_1 = ReadOutput(kOutputToRead1);
|
||||
const std::string produced_output_2 = ReadOutput(kOutputToRead2);
|
||||
const std::string expected_output_1 = ReadOutput(kOutputToExpect1);
|
||||
const std::string expected_output_2 = ReadOutput(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());
|
||||
}
|
||||
|
||||
// One file evaluating to yaml stream format
|
||||
TEST_F(JsonnetTest, Yaml_stream) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kYamlStream, true);
|
||||
WriteOutput(kOutputFile, kYamlStream);
|
||||
|
||||
const std::string produced_output = ReadOutput(kOutputToRead);
|
||||
const std::string expected_output = ReadOutput(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) {
|
||||
constexpr char kInputFile[] = "imports.jsonnet";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, false);
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
local martinis = import 'martinis.libsonnet';
|
||||
|
||||
{
|
||||
'Vodka Martini': martinis['Vodka Martini'],
|
||||
Manhattan: {
|
||||
ingredients: [
|
||||
{ kind: 'Rye', qty: 2.5 },
|
||||
{ kind: 'Sweet Red Vermouth', qty: 1 },
|
||||
{ kind: 'Angostura', qty: 'dash' },
|
||||
],
|
||||
garnish: importstr 'garnish.txt',
|
||||
served: 'Straight Up',
|
||||
},
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
local utils = import 'utils.libsonnet';
|
||||
{
|
||||
Negroni: {
|
||||
// Divide 3oz among the 3 ingredients.
|
||||
ingredients: utils.equal_parts(3, [
|
||||
'Farmers Gin',
|
||||
'Sweet Red Vermouth',
|
||||
'Campari',
|
||||
]),
|
||||
garnish: 'Orange Peel',
|
||||
served: 'On The Rocks',
|
||||
},
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
{
|
||||
equal_parts(size, ingredients)::
|
||||
// Define a function-scoped variable.
|
||||
local qty = size / std.length(ingredients);
|
||||
// Return an array.
|
||||
[
|
||||
{ kind: i, qty: qty }
|
||||
for i in ingredients
|
||||
],
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef JSONNET_TESTS_H_
|
||||
#define JSONNET_TESTS_H_
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <streambuf>
|
||||
#include <string>
|
||||
|
||||
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||
#include "jsonnet_sapi.sapi.h" // NOLINT(build/include)
|
||||
#include "gtest/gtest.h"
|
||||
#include "sandboxed_api/util/flag.h"
|
||||
#include "sandboxed_api/util/path.h"
|
||||
#include "sandboxed_api/util/status_matchers.h"
|
||||
|
||||
class JsonnetTestHelper {
|
||||
protected:
|
||||
enum Evaluation { kBase, kMultipleFiles, kYamlStream };
|
||||
|
||||
void TestSetUp();
|
||||
void TestTearDown();
|
||||
|
||||
void ReadInput(const char* filename);
|
||||
void EvaluateJsonnetCode(Evaluation type, bool expected_correct);
|
||||
void WriteOutput(const char* filename_or_directory, Evaluation type);
|
||||
std::string ReadOutput(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::string input_filename_in_sandboxee_;
|
||||
bool jsonnet_vm_was_used_;
|
||||
bool input_was_read_;
|
||||
};
|
||||
|
||||
#endif // JSONNET_TESTS_H_
|
|
@ -1,66 +0,0 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef JSONNET_HELPER_H_
|
||||
#define JSONNET_HELPER_H_
|
||||
|
||||
extern "C" {
|
||||
#include <libjsonnet.h> // NOLINT(build/include)
|
||||
#include <libjsonnet_fmt.h> // NOLINT(build/include)
|
||||
}
|
||||
|
||||
#include "jsonnet/cmd/utils.h" // NOLINT(build/include)
|
||||
|
||||
extern "C" struct JsonnetVm* c_jsonnet_make(void);
|
||||
|
||||
extern "C" void c_jsonnet_destroy(struct JsonnetVm* vm);
|
||||
|
||||
extern "C" char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm,
|
||||
const char* filename, char* snippet,
|
||||
int* error);
|
||||
|
||||
extern "C" char* c_jsonnet_evaluate_snippet_multi(struct JsonnetVm* vm,
|
||||
const char* filename,
|
||||
const char* snippet,
|
||||
int* error);
|
||||
|
||||
extern "C" char* c_jsonnet_evaluate_snippet_stream(struct JsonnetVm* vm,
|
||||
const char* filename,
|
||||
const char* snippet,
|
||||
int* error);
|
||||
|
||||
extern "C" char* c_read_input(bool filename_is_code, const char* filename);
|
||||
|
||||
extern "C" void c_free_input(char* input);
|
||||
|
||||
extern "C" bool c_write_output_file(const char* output,
|
||||
const char* output_file);
|
||||
|
||||
extern "C" char* c_jsonnet_realloc(struct JsonnetVm* vm, char* str, size_t sz);
|
||||
|
||||
extern "C" bool c_write_multi_output_files(char* output, char* output_dir,
|
||||
bool show_output_file_names);
|
||||
|
||||
bool write_multi_output_files(char* output, const std::string& output_dir,
|
||||
bool show_output_file_names);
|
||||
|
||||
extern "C" bool c_write_output_stream(char* output, char* output_file);
|
||||
|
||||
bool write_output_stream(char* output, const std::string& output_file);
|
||||
|
||||
extern "C" char* c_jsonnet_fmt_snippet(struct JsonnetVm* vm,
|
||||
const char* filename,
|
||||
const char* snippet, int* error);
|
||||
|
||||
#endif // JSONNET_HELPER_H_
|
|
@ -1,40 +0,0 @@
|
|||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http:#www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# We need to prepare convenient directories so the tests will be able to access them
|
||||
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_input)
|
||||
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_output)
|
||||
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_expected_output)
|
||||
|
||||
add_custom_target(test_preparation ALL
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* ${PROJECT_BINARY_DIR}/tests/tests_input
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
gtest_discover_tests(tests)
|
|
@ -1,113 +0,0 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "jsonnet_tests.h" // NOLINT(build/include)
|
||||
|
||||
namespace {
|
||||
|
||||
class JsonnetTest : public JsonnetTestHelper, public testing::Test {
|
||||
protected:
|
||||
void SetUp() override { TestSetUp(); }
|
||||
void TearDown() override { TestTearDown(); }
|
||||
};
|
||||
|
||||
// Basic test
|
||||
TEST_F(JsonnetTest, SetUp_TearDown) {
|
||||
ASSERT_FALSE(jsonnet_vm_was_used_);
|
||||
ASSERT_FALSE(input_was_read_);
|
||||
}
|
||||
|
||||
// One file evaluation to one file
|
||||
TEST_F(JsonnetTest, One_file_no_dependencies) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, true);
|
||||
WriteOutput(kOutputFile, kBase);
|
||||
|
||||
std::string produced_output = ReadOutput(kOutputToRead);
|
||||
std::string expected_output = ReadOutput(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) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, true);
|
||||
WriteOutput(kOutputFile, kBase);
|
||||
|
||||
const std::string produced_output = ReadOutput(kOutputToRead);
|
||||
const std::string expected_output = ReadOutput(kOutputToExpect);
|
||||
|
||||
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
|
||||
}
|
||||
|
||||
// One file evaluating to two files
|
||||
TEST_F(JsonnetTest, Multiple_files) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kMultipleFiles, true);
|
||||
WriteOutput(kOutputFile, kMultipleFiles);
|
||||
|
||||
const std::string produced_output_1 = ReadOutput(kOutputToRead1);
|
||||
const std::string produced_output_2 = ReadOutput(kOutputToRead2);
|
||||
const std::string expected_output_1 = ReadOutput(kOutputToExpect1);
|
||||
const std::string expected_output_2 = ReadOutput(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());
|
||||
}
|
||||
|
||||
// One file evaluating to yaml stream format
|
||||
TEST_F(JsonnetTest, Yaml_stream) {
|
||||
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";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kYamlStream, true);
|
||||
WriteOutput(kOutputFile, kYamlStream);
|
||||
|
||||
const std::string produced_output = ReadOutput(kOutputToRead);
|
||||
const std::string expected_output = ReadOutput(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) {
|
||||
constexpr char kInputFile[] = "imports.jsonnet";
|
||||
|
||||
ReadInput(kInputFile);
|
||||
EvaluateJsonnetCode(kBase, false);
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -1,163 +0,0 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "jsonnet_tests.h" // NOLINT(build/include)
|
||||
|
||||
// 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);
|
||||
ASSERT_GE(error, 0);
|
||||
|
||||
std::pair<absl::string_view, absl::string_view> parts_of_path =
|
||||
sapi::file::SplitPath(buffer);
|
||||
absl::string_view binary_path = parts_of_path.first;
|
||||
|
||||
std::string input_path =
|
||||
sapi::file::JoinPath(binary_path, "tests_input", "dummy_input");
|
||||
std::string output_path =
|
||||
sapi::file::JoinPath(binary_path, "tests_output", "dummy_input");
|
||||
|
||||
// Set up sandbox and api.
|
||||
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_ = absl::make_unique<sapi::v::RemotePtr>(vm_ptr);
|
||||
|
||||
jsonnet_vm_was_used_ = false;
|
||||
input_was_read_ = false;
|
||||
}
|
||||
|
||||
// Cleans up after a test.
|
||||
void JsonnetTestHelper::TestTearDown() {
|
||||
if (jsonnet_vm_was_used_) {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0));
|
||||
}
|
||||
ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk());
|
||||
if (input_was_read_) {
|
||||
ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk());
|
||||
}
|
||||
}
|
||||
|
||||
// Reads input from file.
|
||||
void JsonnetTestHelper::ReadInput(const char* filename) {
|
||||
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||
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_ = absl::make_unique<sapi::v::RemotePtr>(input_ptr);
|
||||
|
||||
input_was_read_ = true;
|
||||
}
|
||||
|
||||
// Evaluates jsonnet code.
|
||||
void JsonnetTestHelper::EvaluateJsonnetCode(Evaluation type,
|
||||
bool expected_correct) {
|
||||
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
|
||||
sapi::v::Int error;
|
||||
char* output_ptr;
|
||||
|
||||
switch (type) {
|
||||
case kBase: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr,
|
||||
api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(),
|
||||
input_.get(), error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
|
||||
case kMultipleFiles: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr, api_->c_jsonnet_evaluate_snippet_multi(
|
||||
vm_.get(), in_file_var.PtrBefore(), input_.get(),
|
||||
error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
|
||||
case kYamlStream: {
|
||||
SAPI_ASSERT_OK_AND_ASSIGN(
|
||||
output_ptr, api_->c_jsonnet_evaluate_snippet_stream(
|
||||
vm_.get(), in_file_var.PtrBefore(), input_.get(),
|
||||
error.PtrAfter()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (expected_correct) {
|
||||
ASSERT_THAT(error.GetValue(), testing::Eq(0));
|
||||
} else {
|
||||
ASSERT_THAT(error.GetValue(), testing::Eq(1));
|
||||
}
|
||||
|
||||
output_ = absl::make_unique<sapi::v::RemotePtr>(output_ptr);
|
||||
|
||||
jsonnet_vm_was_used_ = true;
|
||||
}
|
||||
|
||||
// Writes output to file.
|
||||
void JsonnetTestHelper::WriteOutput(const char* filename_or_directory,
|
||||
Evaluation type) {
|
||||
bool success;
|
||||
|
||||
switch (type) {
|
||||
case kBase: {
|
||||
std::string out_file_in_sandboxee(
|
||||
std::string("/output/") +
|
||||
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()));
|
||||
break;
|
||||
}
|
||||
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));
|
||||
break;
|
||||
}
|
||||
|
||||
case kYamlStream: {
|
||||
std::string out_file_in_sandboxee(
|
||||
std::string("/output/") +
|
||||
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()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_THAT(success, testing::Eq(true));
|
||||
}
|
||||
|
||||
// Reads the output written to a file by library function / expected output
|
||||
std::string JsonnetTestHelper::ReadOutput(const char* filename) {
|
||||
std::ifstream input_stream(filename);
|
||||
std::string contents((std::istreambuf_iterator<char>(input_stream)),
|
||||
std::istreambuf_iterator<char>());
|
||||
return contents;
|
||||
}
|
Loading…
Reference in New Issue
Block a user