Skip to content

Commit

Permalink
[tests] Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcopik committed Oct 14, 2024
1 parent 7892974 commit 3e0657b
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ if(PRAAS_WITH_TESTING)
set(TESTS
#tests/integration/invocation.cpp
tests/integration/allocation_invocation.cpp
tests/integration/state_swap.cpp
)
foreach(test ${TESTS})

Expand Down
11 changes: 11 additions & 0 deletions examples/state-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.11)
project(praas-benchmarks)

find_package(runtime)

add_library(state SHARED state.cpp)
target_link_libraries(state PRIVATE praas::runtime)
target_link_libraries(state PRIVATE praas::cereal)
set_target_properties(state PROPERTIES LIBRARY_OUTPUT_DIRECTORY functions)
set(FUNCTIONS_DIRECTORY ${CMAKE_BINARY_DIR})
configure_file(functions.json.in functions/functions.json @ONLY)
26 changes: 26 additions & 0 deletions examples/state-cpp/functions.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"functions": {
"cpp": {
"state-put": {
"code": {
"module": "@FUNCTIONS_DIRECTORY@/libstate.so",
"function": "state_put"
},
"trigger": {
"type": "direct",
"nargs": 1
}
},
"state-get": {
"code": {
"module": "@FUNCTIONS_DIRECTORY@/libstate.so",
"function": "state_get"
},
"trigger": {
"type": "direct",
"nargs": 1
}
}
}
}
}
72 changes: 72 additions & 0 deletions examples/state-cpp/state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#include <praas/process/runtime/context.hpp>
#include <praas/process/runtime/invocation.hpp>

#include <cereal/cereal.hpp>
#include <cereal/types/string.hpp>

#include <fstream>
#include <filesystem>
#include <ios>
#include <string>

struct IO {
std::string message;

template<typename Ar>
void save(Ar & archive) const
{
archive(CEREAL_NVP(message));
}

template<typename Ar>
void load(Ar & archive)
{
archive(CEREAL_NVP(message));
}
};

extern "C" int state_put(
praas::process::runtime::Invocation invocation, praas::process::runtime::Context& context
)
{
IO msg;
invocation.args[0].deserialize(msg);

praas::process::runtime::Buffer buf = context.state("STATE_MSG_1");

auto send_buf = context.get_buffer(1024);
send_buf.serialize(msg);

context.state("STATE_MSG_1", send_buf);

msg.message = "second_state_msg";
send_buf.serialize(msg);
context.state("STATE_MSG_2", send_buf);

msg.message = "42424242";
send_buf.serialize(msg);
context.state("STATE_MSG_3", send_buf);

std::filesystem::create_directories("/state/test1/test2/");
std::ofstream out{"/state/test1/test2/TEST"};
out << "newfiledata";
out.close();

return 0;
}

extern "C" int state_get(
praas::process::runtime::Invocation invocation, praas::process::runtime::Context& context
)
{
IO key;
praas::process::runtime::Buffer buf = context.state("STATE_MSG_1");
buf.deserialize(key);

auto& output_buf = context.get_output_buffer(1024);
output_buf.serialize(key);
context.set_output_buffer(output_buf);

return 0;
}
93 changes: 93 additions & 0 deletions tests/integration/state_swap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

#include <praas/common/http.hpp>
#include <praas/control-plane/config.hpp>
#include <praas/control-plane/server.hpp>
#include <praas/sdk/praas.hpp>

#include <boost/interprocess/streams/bufferstream.hpp>
#include <chrono>
#include <thread>

#include <boost/iostreams/stream.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

struct Result {
std::string msg;

template <typename Ar>
void save(Ar& archive) const
{
archive(CEREAL_NVP(msg));
}

template <typename Ar>
void load(Ar& archive)
{
archive(CEREAL_NVP(msg));
}
};

class IntegrationLocalInvocation : public ::testing::Test {};

std::string get_output_binary(const char* buffer, size_t size)
{
Result out;
boost::iostreams::stream<boost::iostreams::array_source> stream(buffer, size);
cereal::BinaryInputArchive archive_in{stream};
out.load(archive_in);

return out.msg;
}

std::string get_input_binary(Result& msg)
{
std::string result;
result.resize(1024);

boost::interprocess::bufferstream out_stream(reinterpret_cast<char*>(result.data()), 1024);
cereal::BinaryOutputArchive archive_out{out_stream};
archive_out(msg);

return result;
}

TEST_F(IntegrationLocalInvocation, AllocationInvoke)
{
praas::common::http::HTTPClientFactory::initialize(1);
{
praas::sdk::PraaS praas{fmt::format("http://127.0.0.1:{}", 9000)};

std::string app_name("test_alloc_invoc");
ASSERT_TRUE(
praas.create_application(app_name, "spcleth/praas:proc-local") //spcleth/praas-examples:hello-world-cpp")
);

auto proc = praas.create_process(app_name, "alloc_invoc_process", "1", "1024");
ASSERT_TRUE(proc.has_value());
spdlog::info("Created process");

ASSERT_TRUE(proc->connect());

Result msg;
msg.msg = "TEST_SWAP";
auto res = get_input_binary(msg);

auto invoc = proc->invoke("state-put", "invocation-id", res.data(), res.size());
spdlog::info("Invoked");

// auto invoc = praas.invoke("test_app", "hello-world", "");

ASSERT_EQ(invoc.return_code, 0);

auto swap_res = praas.swap_process(proc.value());
ASSERT_TRUE(swap_res.has_value());
spdlog::info(swap_res.value());

ASSERT_TRUE(praas.delete_process(proc.value()));

ASSERT_TRUE(praas.delete_application(app_name));
}
}

0 comments on commit 3e0657b

Please sign in to comment.