Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 41 additions & 33 deletions targets/simple_switch_grpc/tests/base_test.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
/* Copyright 2013-present Barefoot Networks, Inc.
*
* 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.
*/

/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/
#include "base_test.h"

#include <grpcpp/grpcpp.h>

#include <google/rpc/code.pb.h>

#include <fstream>
#include <streambuf>
#include <vector>

#include "base_test.h"
#include <bm/bm_sim/options_parse.h>

#include "switch_runner.h"

namespace p4v1 = ::p4::v1;

namespace sswitch_grpc {

namespace testing {

constexpr char SimpleSwitchGrpcBaseTest::grpc_server_addr[];
Expand All @@ -49,22 +32,32 @@ SimpleSwitchGrpcBaseTest::SimpleSwitchGrpcBaseTest(
void
SimpleSwitchGrpcBaseTest::SetUp() {
stream = p4runtime_stub->StreamChannel(&stream_context);

p4v1::StreamMessageRequest request;
auto arbitration = request.mutable_arbitration();

arbitration->set_device_id(device_id);
set_election_id(arbitration->mutable_election_id());

stream->Write(request);

p4v1::StreamMessageResponse response;
stream->Read(&response);
ASSERT_EQ(response.update_case(), p4v1::StreamMessageResponse::kArbitration);
ASSERT_EQ(response.arbitration().status().code(), ::google::rpc::Code::OK);

ASSERT_EQ(response.update_case(),
p4v1::StreamMessageResponse::kArbitration);

ASSERT_EQ(response.arbitration().status().code(),
::google::rpc::Code::OK);
}

void
SimpleSwitchGrpcBaseTest::TearDown() {
stream->WritesDone();

p4v1::StreamMessageResponse response;
while (stream->Read(&response)) { }
while (stream->Read(&response)) {}

auto status = stream->Finish();
EXPECT_TRUE(status.ok());
}
Expand All @@ -75,20 +68,28 @@ SimpleSwitchGrpcBaseTest::update_json(const char *json_path) {
request.set_device_id(device_id);
request.set_action(
p4v1::SetForwardingPipelineConfigRequest_Action_VERIFY_AND_COMMIT);

set_election_id(request.mutable_election_id());

auto config = request.mutable_config();

std::ifstream istream(json_path);
ASSERT_TRUE(istream.good());

config->mutable_p4_device_config()->assign(
(std::istreambuf_iterator<char>(istream)),
std::istreambuf_iterator<char>());
std::istreambuf_iterator<char>());

p4v1::SetForwardingPipelineConfigResponse rep;
ClientContext context;
grpc::ClientContext context;

config->set_allocated_p4info(&p4info);

auto status = p4runtime_stub->SetForwardingPipelineConfig(
&context, request, &rep);

config->release_p4info();

ASSERT_TRUE(status.ok());
}

Expand All @@ -103,11 +104,14 @@ SimpleSwitchGrpcBaseTest::write(const p4v1::Entity &entity,
p4v1::Update::Type type) const {
p4v1::WriteRequest request;
request.set_device_id(device_id);

auto update = request.add_updates();
update->set_type(type);
update->mutable_entity()->CopyFrom(entity);
ClientContext context;

grpc::ClientContext context;
p4v1::WriteResponse rep;

return Write(&context, request, &rep);
}

Expand All @@ -132,22 +136,26 @@ SimpleSwitchGrpcBaseTest::read(const p4v1::Entity &entity,
p4v1::ReadRequest request;
request.set_device_id(device_id);
request.add_entities()->CopyFrom(entity);
ClientContext context;
std::unique_ptr<grpc::ClientReader<p4v1::ReadResponse> > reader(

grpc::ClientContext context;

std::unique_ptr<grpc::ClientReader<p4v1::ReadResponse>> reader(
p4runtime_stub->Read(&context, request));

reader->Read(rep);

return reader->Finish();
}

grpc::Status
SimpleSwitchGrpcBaseTest::Write(ClientContext *context,
SimpleSwitchGrpcBaseTest::Write(grpc::ClientContext *context,
p4v1::WriteRequest &request,
p4v1::WriteResponse *response) const {
request.set_device_id(device_id);
set_election_id(request.mutable_election_id());

return p4runtime_stub->Write(context, request, response);
}

} // namespace testing

} // namespace sswitch_grpc
} // namespace sswitch_grpc
57 changes: 17 additions & 40 deletions targets/simple_switch_grpc/tests/main.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
/* Copyright 2013-present Barefoot Networks, Inc.
*
* 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.
*/

/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/

#include <bm/bm_sim/options_parse.h>

#include <gtest/gtest.h>

#include <vector>

#include "base_test.h"
#include "switch_runner.h"
#include "base_test.h"

namespace sswitch_grpc {

namespace testing {

namespace {

constexpr char start_json[] = TESTDATADIR "/loopback.json";

class SimpleSwitchGrpcEnv : public ::testing::Environment {
public:
// We make the switch a shared resource for all tests. This is mainly because
// simple_switch detaches threads.
// TODO(antonin): the issue with this is that tests may affect each other; in
// particular tests which modify port operational status.
void SetUp() override {
auto &runner = SimpleSwitchGrpcRunner::get_instance(
true, SimpleSwitchGrpcBaseTest::grpc_server_addr,
true,
SimpleSwitchGrpcBaseTest::grpc_server_addr,
SimpleSwitchGrpcBaseTest::cpu_port,
SimpleSwitchGrpcBaseTest::dp_grpc_server_addr);

bm::OptionsParser parser;

std::vector<const char *> argv = {"test", "--device-id", "3"};

#ifdef WITH_THRIFT
argv.push_back("--thrift-port");
argv.push_back("45459");
#endif // WITH_THRIFT
// you can uncomment this when debugging
#endif

argv.push_back("--log-console");
argv.push_back(start_json);
auto argc = static_cast<int>(argv.size());
argv.push_back(TESTDATADIR "/loopback.json");

int argc = static_cast<int>(argv.size());

parser.parse(argc, const_cast<char **>(argv.data()), nullptr);

ASSERT_EQ(0, runner.init_and_start(parser));
}

Expand All @@ -65,15 +43,14 @@ class SimpleSwitchGrpcEnv : public ::testing::Environment {
}
};

} // namespace

} // namespace testing

} // namespace sswitch_grpc

int main(int argc, char *argv[]) {
::testing::InitGoogleTest(&argc, argv);

::testing::AddGlobalTestEnvironment(
new sswitch_grpc::testing::SimpleSwitchGrpcEnv);
new sswitch_grpc::testing::SimpleSwitchGrpcEnv);

return RUN_ALL_TESTS();
}
}
Loading