Skip to content

Commit ccdfb18

Browse files
authored
Tests/policies (#248)
get, create and remove a policy
1 parent 0c122d3 commit ccdfb18

File tree

3 files changed

+172
-29
lines changed

3 files changed

+172
-29
lines changed
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
Feature: pktvisor tests
22

33
Scenario: pktvisor bootstrap
4-
When run pktvisor instance on port default
4+
When run pktvisor instance on port default with user permission
55
Then the pktvisor container status must be running
66
And pktvisor API must be enabled
7+
And 1 policies must be running
78

89
Scenario: run multiple pktvisors instances using different ports
9-
When run pktvisor instance on port default
10-
And run pktvisor instance on port 10854
11-
And run pktvisor instance on port 10855
10+
When run pktvisor instance on port default with user permission
11+
And run pktvisor instance on port 10854 with user permission
12+
And run pktvisor instance on port 10855 with user permission
1213
Then all the pktvisor containers must be running
1314
And 3 pktvisor's containers must be running
1415

1516
Scenario: run multiple pktvisors instances using the same port
16-
When run pktvisor instance on port default
17-
And run pktvisor instance on port default
17+
When run pktvisor instance on port default with user permission
18+
And run pktvisor instance on port default with user permission
1819
Then 1 pktvisor's containers must be running
19-
And 1 pktvisor's containers must be exited
20+
And 1 pktvisor's containers must be exited
21+
22+
Scenario: create a policy
23+
Given that a pktvisor instance is running on port default with admin permission
24+
When create a new policy
25+
Then 2 policies must be running
26+
27+
Scenario: delete all policies
28+
Given that a pktvisor instance is running on port default with admin permission
29+
When delete 1 policies
30+
Then 0 policies must be running
31+
32+
Scenario: delete 1 policy
33+
Given that a pktvisor instance is running on port default with admin permission
34+
When create a new policy
35+
And delete 1 policies
36+
Then 1 policies must be running

automated_tests/features/steps/pktvisor.py

Lines changed: 117 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,58 @@
33
from behave import step
44
from hamcrest import *
55
import requests
6+
import yaml
7+
from yaml.loader import SafeLoader
68
from retry import retry
9+
import random
10+
from policies import *
711

812
PKTVISOR_CONTAINER_NAME = "pktvisor-test"
913

1014

11-
def run_pktvisor_container(container_image, port="default", container_name=PKTVISOR_CONTAINER_NAME):
12-
"""
13-
Run a pktvisor container
15+
@step("run pktvisor instance on port {pkt_port} with {role} permission")
16+
def run_pktvisor(context, pkt_port, role):
17+
context.pkt_port = pkt_port
18+
context.container_id = run_pktvisor_container("ns1labs/pktvisor", pkt_port, role)
1419

15-
:param (str) container_image: that will be used for running the container
16-
:param (str) port: Port on which the web service must be run [default: 10853]
17-
:param (dict) env_vars: that will be passed to the container context
18-
:param (str) container_name: base of container name
19-
:returns: (str) the container ID
20-
"""
21-
PKTVISOR_CONTAINER_NAME = container_name + random_string(3)
22-
client = docker.from_env()
23-
pkt_command = ["pktvisord", "wlo1"]
24-
if port != "default":
25-
pkt_command.insert(-1, '-p')
26-
pkt_command.insert(-1, port)
27-
container = client.containers.run(container_image, name=PKTVISOR_CONTAINER_NAME, detach=True,
28-
network_mode='host', command=pkt_command)
29-
return container.id
3020

21+
@step("that a pktvisor instance is running on port {pkt_port} with {role} permission")
22+
def pkt_running(context, pkt_port, role):
23+
run_pktvisor(context, pkt_port, role)
24+
25+
26+
@step("{amount_of_policies} policies {status_condition} be running")
27+
def amount_of_policies_per_status(context, amount_of_policies, status_condition):
28+
assert_that(status_condition, any_of(equal_to("must"), equal_to("must not")),
29+
'Unexpect condition for policy status')
30+
status_condition_dict = {"must": True, "must not": False}
31+
all_policies = make_get_request('policies').json()
32+
amount_of_policies_per_status = list()
33+
for key, value in all_policies.items():
34+
if value['input'][list(value['input'].keys())[0]]['input']['running'] is status_condition_dict[status_condition]:
35+
amount_of_policies_per_status.append(key)
36+
assert_that(len(amount_of_policies_per_status), equal_to(int(amount_of_policies)),
37+
f"Unexpect amount of policies that {status_condition} be running")
38+
39+
40+
@step("create a new policy")
41+
def create_new_policy(context):
42+
policy_yaml = policies.generate_pcap_policy_with_all_handlers(random_string(10))
43+
policy_yaml_parsed = yaml.load(policy_yaml, Loader=SafeLoader)
44+
yaml_policy_data = yaml.dump(policy_yaml_parsed)
45+
create_policy(yaml_policy_data)
46+
47+
@step("delete {amount_of_policies} policies")
48+
def remove_policies(context, amount_of_policies):
49+
names_of_all_policies = make_get_request('policies').json().keys()
50+
policies_to_remove = random.sample(names_of_all_policies, int(amount_of_policies))
51+
for policy in policies_to_remove:
52+
remove_policy(policy)
53+
response = get_policy(policy, 10853, 404)
54+
assert_that(response.json(), has_key('error'), "Unexpected message for non existing policy")
55+
assert_that(response.json(), has_value('policy does not exists'), "Unexpected message for non existing policy")
3156

3257

33-
@step("run pktvisor instance on port {pkt_port}")
34-
def run_pktvisor(context, pkt_port):
35-
context.pkt_port = pkt_port
36-
context.container_id = run_pktvisor_container("ns1labs/pktvisor", pkt_port)
3758

3859

3960
@step("the pktvisor container status must be {pkt_status}")
@@ -94,11 +115,85 @@ def check_pkt_base_API(context):
94115
make_get_request(endpoint, context.pkt_port)
95116

96117

118+
@retry(tries=3, delay=1)
119+
def create_policy(yaml_data, pkt_port=10853, expected_status_code=201):
120+
"""
121+
122+
:param yaml_data: policy configurations
123+
:param pkt_port: port on which pktvisor is running
124+
:param expected_status_code: expected status from response
125+
:return: response
126+
"""
127+
pkt_api = 'http://localhost:'+str(pkt_port)+'/api/v1/policies'
128+
headers_request = {'Content-type': 'application/x-yaml'}
129+
response = requests.post(pkt_api, data=yaml_data, headers=headers_request)
130+
assert_that(response.status_code, equal_to(int(expected_status_code)), f"Post request to create a policy failed with status {response.status_code}")
131+
return response
132+
133+
97134
@retry(tries=3, delay=1)
98135
def make_get_request(end_point, pkt_port=10853, expected_status_code=200):
136+
"""
137+
138+
:param end_point: endpoint to which the request must be sent
139+
:param pkt_port: port on which pktvisor is running
140+
:param expected_status_code: expected status from response
141+
:return: response
142+
"""
99143
pkt_base_api = 'http://localhost:'+str(pkt_port)+'/api/v1/'
100144
path = pkt_base_api+end_point
101145
response = requests.get(path)
102146
assert_that(response.status_code, equal_to(int(expected_status_code)),
103147
f"Get request to endpoint {path} failed with status {response.status_code}")
104148
return response
149+
150+
151+
def run_pktvisor_container(container_image, port="default", role="user", container_name=PKTVISOR_CONTAINER_NAME):
152+
"""
153+
Run a pktvisor container
154+
155+
:param (str) container_image: that will be used for running the container
156+
:param (str) port: Port on which the web service must be run [default: 10853]
157+
:param (str) role: that manage the permissions. [Default: 'user']
158+
:param (str) container_name: base of container name
159+
:returns: (str) the container ID
160+
"""
161+
assert_that(role, any_of(equal_to('user'), equal_to('admin')), "Unexpect permission role")
162+
PKTVISOR_CONTAINER_NAME = container_name + random_string(3)
163+
client = docker.from_env()
164+
pkt_command = ["pktvisord", "wlo1"]
165+
if port != "default":
166+
pkt_command.insert(-1, '-p')
167+
pkt_command.insert(-1, port)
168+
if role == "admin":
169+
pkt_command.insert(-1, '--admin-api')
170+
container = client.containers.run(container_image, name=PKTVISOR_CONTAINER_NAME, detach=True,
171+
network_mode='host', command=pkt_command)
172+
return container.id
173+
174+
175+
def get_policy(policy_name, pkt_port=10853, expected_status_code=200):
176+
"""
177+
178+
:param (str) policy_name: name of the policy to be fetched
179+
:param pkt_port: port on which pktvisor is running
180+
:param expected_status_code: expected status from response
181+
:return: (dict) referred policy data
182+
"""
183+
endpoint = f"policies/{policy_name}"
184+
return make_get_request(endpoint, pkt_port, expected_status_code)
185+
186+
187+
def remove_policy(policy_name, pkt_port=10853, expected_status_code=204):
188+
189+
"""
190+
:param (str) policy_name: name of the policy to be fetched
191+
:param pkt_port: port on which pktvisor is . Default: 10853
192+
:param expected_status_code: expected status from response. Default: 204
193+
:return: response
194+
"""
195+
pkt_base_api = 'http://localhost:'+str(pkt_port)+'/api/v1/policies/'
196+
path = pkt_base_api+policy_name
197+
response = requests.delete(path)
198+
assert_that(response.status_code, equal_to(int(expected_status_code)),
199+
f"Delete request of policy {policy_name} failed with status {response.status_code}")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class policies:
2+
def __init__(self):
3+
pass
4+
5+
@classmethod
6+
def generate_pcap_policy_with_all_handlers(cls, name):
7+
policy_yaml = f"""
8+
version: "1.0"
9+
10+
visor:
11+
policies:
12+
{name}:
13+
kind: collection
14+
input:
15+
tap: default
16+
input_type: pcap
17+
handlers:
18+
window_config:
19+
num_periods: 5
20+
deep_sample_rate: 100
21+
modules:
22+
net:
23+
type: net
24+
dhcp:
25+
type: dhcp
26+
dns:
27+
type: dns
28+
pcap_stats:
29+
type: pcap
30+
"""
31+
return policy_yaml

0 commit comments

Comments
 (0)