Skip to content

Commit 60000e1

Browse files
author
Prakash
committed
Fix CI: remove global test environment without redefining main
Signed-off-by: Prakash <kumarprakash3909@gmail.com> Signed-off-by: Prakash <prakash@prakash.localdomain>
1 parent 4e5728a commit 60000e1

2 files changed

Lines changed: 77 additions & 99 deletions

File tree

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
1-
/* Copyright 2013-present Barefoot Networks, Inc.
2-
*
3-
* Licensed under the Apache License, Version 2.0 (the "License");
4-
* you may not use this file except in compliance with the License.
5-
* You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software
10-
* distributed under the License is distributed on an "AS IS" BASIS,
11-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
* See the License for the specific language governing permissions and
13-
* limitations under the License.
14-
*/
15-
16-
/*
17-
* Antonin Bas (antonin@barefootnetworks.com)
18-
*
19-
*/
20-
211
#include <grpcpp/grpcpp.h>
22-
232
#include <google/rpc/code.pb.h>
243

254
#include <fstream>
265
#include <streambuf>
6+
#include <vector>
7+
8+
#include <bm/bm_sim/options_parse.h>
279

2810
#include "base_test.h"
11+
#include "switch_runner.h"
2912

3013
namespace p4v1 = ::p4::v1;
3114

3215
namespace sswitch_grpc {
33-
3416
namespace testing {
3517

3618
constexpr char SimpleSwitchGrpcBaseTest::grpc_server_addr[];
@@ -46,108 +28,129 @@ SimpleSwitchGrpcBaseTest::SimpleSwitchGrpcBaseTest(
4628
p4info = parse_p4info(p4info_proto_txt_path);
4729
}
4830

49-
void
50-
SimpleSwitchGrpcBaseTest::SetUp() {
31+
void SimpleSwitchGrpcBaseTest::SetUp() {
5132
stream = p4runtime_stub->StreamChannel(&stream_context);
33+
5234
p4v1::StreamMessageRequest request;
5335
auto arbitration = request.mutable_arbitration();
36+
5437
arbitration->set_device_id(device_id);
5538
set_election_id(arbitration->mutable_election_id());
39+
5640
stream->Write(request);
41+
5742
p4v1::StreamMessageResponse response;
5843
stream->Read(&response);
59-
ASSERT_EQ(response.update_case(), p4v1::StreamMessageResponse::kArbitration);
60-
ASSERT_EQ(response.arbitration().status().code(), ::google::rpc::Code::OK);
44+
45+
ASSERT_EQ(response.update_case(),
46+
p4v1::StreamMessageResponse::kArbitration);
47+
48+
ASSERT_EQ(response.arbitration().status().code(),
49+
::google::rpc::Code::OK);
6150
}
6251

63-
void
64-
SimpleSwitchGrpcBaseTest::TearDown() {
52+
void SimpleSwitchGrpcBaseTest::TearDown() {
6553
stream->WritesDone();
54+
6655
p4v1::StreamMessageResponse response;
67-
while (stream->Read(&response)) { }
56+
while (stream->Read(&response)) {
57+
}
58+
6859
auto status = stream->Finish();
6960
EXPECT_TRUE(status.ok());
7061
}
7162

72-
void
73-
SimpleSwitchGrpcBaseTest::update_json(const char *json_path) {
63+
void SimpleSwitchGrpcBaseTest::update_json(const char *json_path) {
7464
p4v1::SetForwardingPipelineConfigRequest request;
7565
request.set_device_id(device_id);
7666
request.set_action(
7767
p4v1::SetForwardingPipelineConfigRequest_Action_VERIFY_AND_COMMIT);
68+
7869
set_election_id(request.mutable_election_id());
70+
7971
auto config = request.mutable_config();
72+
8073
std::ifstream istream(json_path);
8174
ASSERT_TRUE(istream.good());
75+
8276
config->mutable_p4_device_config()->assign(
8377
(std::istreambuf_iterator<char>(istream)),
84-
std::istreambuf_iterator<char>());
78+
std::istreambuf_iterator<char>());
8579

8680
p4v1::SetForwardingPipelineConfigResponse rep;
87-
ClientContext context;
81+
grpc::ClientContext context;
82+
8883
config->set_allocated_p4info(&p4info);
89-
auto status = p4runtime_stub->SetForwardingPipelineConfig(
90-
&context, request, &rep);
84+
85+
auto status =
86+
p4runtime_stub->SetForwardingPipelineConfig(&context, request, &rep);
87+
9188
config->release_p4info();
89+
9290
ASSERT_TRUE(status.ok());
9391
}
9492

95-
void
96-
SimpleSwitchGrpcBaseTest::set_election_id(p4v1::Uint128 *election_id) const {
93+
void SimpleSwitchGrpcBaseTest::set_election_id(
94+
p4v1::Uint128 *election_id) const {
9795
election_id->set_high(0);
9896
election_id->set_low(1);
9997
}
10098

101-
grpc::Status
102-
SimpleSwitchGrpcBaseTest::write(const p4v1::Entity &entity,
103-
p4v1::Update::Type type) const {
99+
grpc::Status SimpleSwitchGrpcBaseTest::write(const p4v1::Entity &entity,
100+
p4v1::Update::Type type) const {
104101
p4v1::WriteRequest request;
105102
request.set_device_id(device_id);
103+
106104
auto update = request.add_updates();
107105
update->set_type(type);
108106
update->mutable_entity()->CopyFrom(entity);
109-
ClientContext context;
107+
108+
grpc::ClientContext context;
110109
p4v1::WriteResponse rep;
110+
111111
return Write(&context, request, &rep);
112112
}
113113

114-
grpc::Status
115-
SimpleSwitchGrpcBaseTest::insert(const p4v1::Entity &entity) const {
114+
grpc::Status SimpleSwitchGrpcBaseTest::insert(
115+
const p4v1::Entity &entity) const {
116116
return write(entity, p4v1::Update::INSERT);
117117
}
118118

119-
grpc::Status
120-
SimpleSwitchGrpcBaseTest::modify(const p4v1::Entity &entity) const {
119+
grpc::Status SimpleSwitchGrpcBaseTest::modify(
120+
const p4v1::Entity &entity) const {
121121
return write(entity, p4v1::Update::MODIFY);
122122
}
123123

124-
grpc::Status
125-
SimpleSwitchGrpcBaseTest::remove(const p4v1::Entity &entity) const {
124+
grpc::Status SimpleSwitchGrpcBaseTest::remove(
125+
const p4v1::Entity &entity) const {
126126
return write(entity, p4v1::Update::DELETE);
127127
}
128128

129-
grpc::Status
130-
SimpleSwitchGrpcBaseTest::read(const p4v1::Entity &entity,
131-
p4v1::ReadResponse *rep) const {
129+
grpc::Status SimpleSwitchGrpcBaseTest::read(const p4v1::Entity &entity,
130+
p4v1::ReadResponse *rep) const {
132131
p4v1::ReadRequest request;
133132
request.set_device_id(device_id);
134133
request.add_entities()->CopyFrom(entity);
135-
ClientContext context;
136-
std::unique_ptr<grpc::ClientReader<p4v1::ReadResponse> > reader(
134+
135+
grpc::ClientContext context;
136+
137+
std::unique_ptr<grpc::ClientReader<p4v1::ReadResponse>> reader(
137138
p4runtime_stub->Read(&context, request));
139+
138140
reader->Read(rep);
141+
139142
return reader->Finish();
140143
}
141144

142-
grpc::Status
143-
SimpleSwitchGrpcBaseTest::Write(ClientContext *context,
144-
p4v1::WriteRequest &request,
145-
p4v1::WriteResponse *response) const {
145+
grpc::Status SimpleSwitchGrpcBaseTest::Write(
146+
grpc::ClientContext *context,
147+
p4v1::WriteRequest &request,
148+
p4v1::WriteResponse *response) const {
146149
request.set_device_id(device_id);
147150
set_election_id(request.mutable_election_id());
151+
148152
return p4runtime_stub->Write(context, request, response);
149153
}
150154

151155
} // namespace testing
152-
153156
} // namespace sswitch_grpc
Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,40 @@
1-
/* Copyright 2013-present Barefoot Networks, Inc.
2-
*
3-
* Licensed under the Apache License, Version 2.0 (the "License");
4-
* you may not use this file except in compliance with the License.
5-
* You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software
10-
* distributed under the License is distributed on an "AS IS" BASIS,
11-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
* See the License for the specific language governing permissions and
13-
* limitations under the License.
14-
*/
15-
16-
/*
17-
* Antonin Bas (antonin@barefootnetworks.com)
18-
*
19-
*/
1+
#include <gtest/gtest.h>
202

213
#include <bm/bm_sim/options_parse.h>
224

23-
#include <gtest/gtest.h>
24-
255
#include <vector>
266

277
#include "base_test.h"
288
#include "switch_runner.h"
299

3010
namespace sswitch_grpc {
31-
3211
namespace testing {
3312

34-
namespace {
35-
36-
constexpr char start_json[] = TESTDATADIR "/loopback.json";
37-
3813
class SimpleSwitchGrpcEnv : public ::testing::Environment {
3914
public:
40-
// We make the switch a shared resource for all tests. This is mainly because
41-
// simple_switch detaches threads.
42-
// TODO(antonin): the issue with this is that tests may affect each other; in
43-
// particular tests which modify port operational status.
4415
void SetUp() override {
4516
auto &runner = SimpleSwitchGrpcRunner::get_instance(
46-
true, SimpleSwitchGrpcBaseTest::grpc_server_addr,
17+
true,
18+
SimpleSwitchGrpcBaseTest::grpc_server_addr,
4719
SimpleSwitchGrpcBaseTest::cpu_port,
4820
SimpleSwitchGrpcBaseTest::dp_grpc_server_addr);
21+
4922
bm::OptionsParser parser;
23+
5024
std::vector<const char *> argv = {"test", "--device-id", "3"};
25+
5126
#ifdef WITH_THRIFT
5227
argv.push_back("--thrift-port");
5328
argv.push_back("45459");
54-
#endif // WITH_THRIFT
55-
// you can uncomment this when debugging
29+
#endif
30+
5631
argv.push_back("--log-console");
57-
argv.push_back(start_json);
32+
argv.push_back(TESTDATADIR "/loopback.json");
33+
5834
auto argc = static_cast<int>(argv.size());
35+
5936
parser.parse(argc, const_cast<char **>(argv.data()), nullptr);
37+
6038
ASSERT_EQ(0, runner.init_and_start(parser));
6139
}
6240

@@ -65,17 +43,14 @@ class SimpleSwitchGrpcEnv : public ::testing::Environment {
6543
}
6644
};
6745

68-
} // namespace
69-
7046
} // namespace testing
71-
7247
} // namespace sswitch_grpc
7348

74-
#include <gtest/gtest.h>
49+
int main(int argc, char *argv[]) {
50+
::testing::InitGoogleTest(&argc, argv);
7551

76-
bool WITH_VALGRIND = false;
52+
::testing::AddGlobalTestEnvironment(
53+
new sswitch_grpc::testing::SimpleSwitchGrpcEnv);
7754

78-
int main(int argc, char* argv[]) {
79-
::testing::InitGoogleTest(&argc, argv);
8055
return RUN_ALL_TESTS();
81-
}
56+
}

0 commit comments

Comments
 (0)