33from behave import step
44from hamcrest import *
55import requests
6+ import yaml
7+ from yaml .loader import SafeLoader
68from retry import retry
9+ import random
10+ from policies import *
711
812PKTVISOR_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 )
98135def 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 } " )
0 commit comments