OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
policy_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/asio.hpp>
6 #include <boost/bind.hpp>
7 
8 #include <db/db_entry.h>
9 #include <db/db_table.h>
10 #include <db/db_table_partition.h>
11 
12 #include "base/logging.h"
13 #include "testing/gunit.h"
14 
17 
18 
19 class MyTest {
20  public:
22  return;
23  }
24 };
25 
26 namespace {
27 class PolicyTest : public ::testing::Test {
28 protected:
29 };
30 
31 
32 // Create and delete Policy
33 TEST_F(PolicyTest, Basic) {
34  PolicyConfigSpec policy_c_s1;
35 
36  uuid vpc_id1 = StringToUuid("00000000-0000-0000-0000-000000000001");
37  uuid policy_id1 = StringToUuid("00000000-0000-0000-0000-000000000001");
38  policy_c_s1.vpc_id = vpc_id1;
39  policy_c_s1.policy_id = policy_id1;
40  policy_c_s1.name.clear();
41  policy_c_s1.name.append("Contrail Employee SG");
42  policy_c_s1.inbound = true;
43  policy_c_s1.acl_id = StringToUuid("00000000-0000-0000-0000-000000000001");
44 
45  DB db;
47  DBTableBase *table = db.CreateTable("db.policy.0");
48  assert(table);
49 
50  PolicyTable *ptable = static_cast<PolicyTable *>(db.FindTable("db.policy.0"));
51  assert(ptable);
52 
53  DBRequest req;
54  req.key.reset(new PolicyKey(policy_id1));
55 
56  PolicyData *pd;
57  pd = new PolicyData();
58  pd->Init(policy_c_s1);
59  req.data.reset(pd);
61  table->Enqueue(&req);
62  sleep(1);
63  EXPECT_TRUE(NULL != ptable->FindActiveEntry(new PolicyKey(policy_id1)));
64 }
65 
66 
67 // Modify policy and delete
68 TEST_F(PolicyTest, ChangeAnEntry) {
69  PolicyConfigSpec policy_c_s1;
70 
71  uuid vpc_id1 = StringToUuid("00000000-0000-0000-0000-000000000001");
72  uuid policy_id1 = StringToUuid("00000000-0000-0000-0000-000000000001");
73  policy_c_s1.vpc_id = vpc_id1;
74  policy_c_s1.policy_id = policy_id1;
75  policy_c_s1.name.clear();
76  policy_c_s1.name.append("Contrail Employee SG");
77  policy_c_s1.inbound = true;
78  policy_c_s1.acl_id = StringToUuid("00000000-0000-0000-0000-000000000001");
79 
80  DB db;
82  DBTableBase *table = db.CreateTable("db.policy.0");
83  assert(table);
84 
85  PolicyTable *ptable = static_cast<PolicyTable *>(db.FindTable("db.policy.0"));
86  assert(ptable);
87 
88  DBRequest req;
89  req.key.reset(new PolicyKey(policy_id1));
90 
91  PolicyData *pd;
92  pd = new PolicyData();
93  pd->Init(policy_c_s1);
94  req.data.reset(pd);
96  table->Enqueue(&req);
97 
98  sleep(1);
99  EXPECT_TRUE(NULL != ptable->FindActiveEntry(new PolicyKey(policy_id1)));
100 
101  // Modifing a existing policy
102  policy_c_s1.inbound = true;
103  policy_c_s1.acl_id = StringToUuid("00000000-0000-0000-0000-000000000002");
104 
105  Policy *pe = static_cast<Policy *>
106  (ptable->FindActiveEntry(new PolicyKey(policy_id1)));
107  EXPECT_TRUE(NULL != pe);
108 
109  DBRequest req1;
110  req1.key.reset(new PolicyKey(policy_id1));
111  PolicyData *pd1;
112  pd1 = new PolicyData();
113  pd1->Init(policy_c_s1);
114  req1.data.reset(pd1);
116  ptable->Enqueue(&req1);
117  sleep(1);
118  AclPtr ap = pe->FindAcl(true,
119  StringToUuid("00000000-0000-0000-0000-000000000002"));
120  EXPECT_TRUE(NULL != ap);
121  sleep(1);
122 
123  DBRequest req2;
124  req2.key.reset(new PolicyKey(policy_id1));
126  ptable->Enqueue(&req2);
127  sleep(1);
128 }
129 
130 
131 } //namespace
132 
133 int main (int argc, char **argv) {
134  LoggingInit();
135  ::testing::InitGoogleTest(&argc, argv);
136  return RUN_ALL_TESTS();
137 }
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:145
DBTableBase * CreateTable(const std::string &name)
Definition: db.cc:99
std::unique_ptr< DBRequestData > data
Definition: db_table.h:49
bool Enqueue(DBRequest *req)
Definition: db_table.cc:194
boost::uuids::uuid uuid
Definition: db.h:24
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:48
static void Register()
Definition: policy.cc:194
DBOperation oper
Definition: db_table.h:42
void LoggingInit()
Definition: logging.cc:51
TEST_F(TestDummy, test_1)
void Init(const PolicyConfigSpec &policy_cfg)
Definition: policy.cc:11
void TableListener(DBTablePartBase *root, DBEntryBase *entry)
Definition: policy_test.cc:21
AclPtr FindAcl(const bool inbound, const uuid acl_id)
Definition: policy.cc:59
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68