From 75f835725deb42b00d5f746828f2738d15e3bc4f Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Sat, 4 Apr 2015 23:46:01 +0000 Subject: api: use gRPC instead of REST Signed-off-by: ISHIDA Wataru --- test/scenario_test/bgp_router_test.py | 22 +-- test/scenario_test/constant.py | 27 ++- test/scenario_test/docker_control.py | 70 ++++---- test/scenario_test/gobgp_test.py | 140 ++++++++++++--- test/scenario_test/peer_info.py | 19 +- test/scenario_test/route_server_ipv4_v6_test.py | 13 +- test/scenario_test/route_server_malformed_test.py | 12 +- test/scenario_test/route_server_policy_test.py | 133 ++++---------- test/scenario_test/route_server_test.py | 34 ++-- test/scenario_test/scenario_test_util.py | 201 ---------------------- 10 files changed, 260 insertions(+), 411 deletions(-) delete mode 100644 test/scenario_test/scenario_test_util.py (limited to 'test/scenario_test') diff --git a/test/scenario_test/bgp_router_test.py b/test/scenario_test/bgp_router_test.py index f2d3ce16..62ff9b5f 100644 --- a/test/scenario_test/bgp_router_test.py +++ b/test/scenario_test/bgp_router_test.py @@ -73,12 +73,12 @@ class GoBGPTest(GoBGPTestBase): for p in d.paths: print "check of %s's route %s existance in gobgp global rib" % (peer_ip, p.network) exist = False - for dst in rib['Destinations']: - for path in dst['Paths']: - if path['Network'] == p.network: + for dst in rib: + for path in dst['paths']: + if path['network'] == p.network: exist = True if exist: - self.assertEqual(path['Nexthop'] == p.nexthop, True) + self.assertEqual(path['nexthop'] == p.nexthop, True) self.assertEqual(exist, True) # Test of advertising route to each quagga form gobgp @@ -150,12 +150,12 @@ class GoBGPTest(GoBGPTestBase): for p in d.paths: print "check of %s's route %s existance in gobgp global rib" % (peer_ip, p.network) exist = False - for dst in rib['Destinations']: - for path in dst['Paths']: - if path['Network'] == p.network: + for dst in rib: + for path in dst['paths']: + if path['network'] == p.network: exist = True if exist: - self.assertEqual(path['Nexthop'] == p.nexthop, True) + self.assertEqual(path['nexthop'] == p.nexthop, True) self.assertEqual(exist, True) # Test of advertising route to each quagga form gobgp when append quagga container @@ -215,9 +215,9 @@ class GoBGPTest(GoBGPTestBase): removed_prefix = "10.0.0.%d/24" % self.remove_quagga still_exists = False - for dst in rib['Destinations']: - for path in dst['Paths']: - if path['Network'] == removed_prefix: + for dst in rib: + for path in dst['paths']: + if path['network'] == removed_prefix: still_exists = True self.assertEqual(still_exists, False) diff --git a/test/scenario_test/constant.py b/test/scenario_test/constant.py index 5e672a79..131cfd04 100644 --- a/test/scenario_test/constant.py +++ b/test/scenario_test/constant.py @@ -1,3 +1,19 @@ +# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +# +# 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. + + IPv4 = 'ipv4' IPv6 = 'ipv6' GOBGP_IP = "10.0.255.1" @@ -12,12 +28,15 @@ GOBGP_CONFIG_FILE = "gobgpd.conf" CONFIG_DIR = "/tmp/gobgp" CONFIG_DIRR = "/tmp/gobgp/" SHARE_VOLUME = "/root/share_volume" +CLI_CMD = "gobgpcli" EXABGP_CONTAINER_NAME = "exabgp" EXABGP_ADDRESS = "10.0.0.100/16" EXABGP_CONFDIR = SHARE_VOLUME + "/exabgp_test_conf" EXABGP_LOG_FILE = "exabgpd.log" STARTUP_FILE_NAME = "gobgp_startup.sh" STARTUP_FILE = SHARE_VOLUME + "/" + STARTUP_FILE_NAME +INSTALL_FILE_NAME = "gobgp_install.sh" +INSTALL_FILE = SHARE_VOLUME + "/" + INSTALL_FILE_NAME IP_VERSION = IPv4 IF_CONFIG_OPTION = {IPv4: "inet", IPv6: "inet6"} @@ -40,8 +59,8 @@ BASE_MASK = {IPv4: "/16", IPv6: "/64"} A_PART_OF_CURRENT_DIR = "/test/scenario_test" -ADJ_RIB_IN = "adj-rib-in" -ADJ_RIB_OUT = "adj-rib-out" -LOCAL_RIB = "local-rib" -GLOBAL_RIB = "global/rib" +ADJ_RIB_IN = "adj-in" +ADJ_RIB_OUT = "adj-out" +LOCAL_RIB = "local" +GLOBAL_RIB = "global" NEIGHBOR = "neighbor" diff --git a/test/scenario_test/docker_control.py b/test/scenario_test/docker_control.py index 64446111..008b346d 100644 --- a/test/scenario_test/docker_control.py +++ b/test/scenario_test/docker_control.py @@ -154,35 +154,38 @@ def create_config_dir(): cmd = "mkdir " + CONFIG_DIR local(cmd, capture=True) - def make_startup_file(log_opt=""): - file_buff = '#!/bin/bash' + '\n' - file_buff += 'cd /go/src/github.com/osrg/gobgp' + '\n' - file_buff += 'git pull origin master' + '\n' - file_buff += 'cd gobgpd' + '\n' - file_buff += 'go get -v' + '\n' - file_buff += 'go build' + '\n' - file_buff += './gobgpd -f ' + SHARE_VOLUME + '/gobgpd.conf ' + log_opt + ' > ' + SHARE_VOLUME + '/gobgpd.log' + file_buff += "cd /go/src/github.com/osrg/gobgp/gobgpd" + '\n' + file_buff += "./gobgpd -f " + SHARE_VOLUME + "/gobgpd.conf " + log_opt + " > " + SHARE_VOLUME + "/gobgpd.log" + cmd = "echo \"" + file_buff + "\" > " + CONFIG_DIR + "/" + STARTUP_FILE_NAME local(cmd, capture=True) cmd = "chmod 755 " + CONFIG_DIRR + STARTUP_FILE_NAME local(cmd, capture=True) -def make_startup_file_use_local_gobgp(log_opt=""): - +def make_install_file(use_local=False): file_buff = '#!/bin/bash' + '\n' - file_buff += 'rm -rf /go/src/github.com/osrg/gobgp' + '\n' - file_buff += 'cp -r ' + SHARE_VOLUME + '/gobgp /go/src/github.com/osrg/' + '\n' - file_buff += 'cd /go/src/github.com/osrg/gobgp' + '\n' - file_buff += 'cd gobgpd' + '\n' + + if use_local: + file_buff += 'rm -rf /go/src/github.com/osrg/gobgp' + '\n' + file_buff += 'cp -r ' + SHARE_VOLUME + '/gobgp /go/src/github.com/osrg/' + '\n' + file_buff += 'cd /go/src/github.com/osrg/gobgp' + '\n' + else: + file_buff += 'cd /go/src/github.com/osrg/gobgp' + '\n' + file_buff += 'git pull origin master' + '\n' + + file_buff += 'cd gobgp' + '\n' file_buff += 'go get -v' + '\n' file_buff += 'go build' + '\n' - file_buff += './gobgpd -f ' + SHARE_VOLUME + '/gobgpd.conf ' + log_opt + ' > ' + SHARE_VOLUME + '/gobgpd.log' - cmd = "echo \"" + file_buff + "\" > " + CONFIG_DIR + "/" + STARTUP_FILE_NAME + file_buff += 'cp gobgp ' + SHARE_VOLUME + '/' + CLI_CMD + '\n' + file_buff += 'cd ../gobgpd' + '\n' + file_buff += 'go get -v' + '\n' + file_buff += 'go build' + cmd = "echo \"" + file_buff + "\" > " + CONFIG_DIR + "/" + INSTALL_FILE_NAME local(cmd, capture=True) - cmd = "chmod 755 " + CONFIG_DIRR + STARTUP_FILE_NAME + cmd = "chmod 755 " + CONFIG_DIRR + INSTALL_FILE_NAME local(cmd, capture=True) @@ -255,7 +258,9 @@ def bridge_unsetting_for_docker_connection(): def start_gobgp(): - cmd = "docker exec gobgp " + STARTUP_FILE + " > /dev/null 2>&1 &" + cmd = "docker exec -it gobgp " + INSTALL_FILE + local(cmd, capture=True) + cmd = "docker exec -it gobgp " + STARTUP_FILE + "&" local(cmd, capture=True) @@ -334,7 +339,7 @@ def change_exabgp_version(): def reload_config(): - cmd = "docker exec gobgp /usr/bin/pkill gobgp -SIGHUP" + cmd = "docker exec gobgp /usr/bin/pkill gobgpd -SIGHUP" local(cmd, capture=True) print "complete append docker container." @@ -359,6 +364,7 @@ def init_test_env_executor(quagga_num, use_local, go_path, log_debug=False, is_r opt = "-l debug" if log_debug else "" # execute local gobgp program in the docker container if the input option is local + make_startup_file(log_opt=opt) if use_local: print "execute gobgp program in local machine." pwd = local("pwd", capture=True) @@ -366,14 +372,14 @@ def init_test_env_executor(quagga_num, use_local, go_path, log_debug=False, is_r gobgp_path = re.sub(A_PART_OF_CURRENT_DIR, "", pwd) cmd = "cp -r " + gobgp_path + " " + CONFIG_DIRR local(cmd, capture=True) - make_startup_file_use_local_gobgp(log_opt=opt) + make_install_file(use_local=True) else: print "scenario_test directory is not." print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() else: print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() change_owner_to_root(CONFIG_DIR) start_gobgp() @@ -405,6 +411,7 @@ def init_policy_test_env_executor(quagga_num, use_local, go_path, log_debug=Fals opt = "-l debug" if log_debug else "" # execute local gobgp program in the docker container if the input option is local + make_startup_file(log_opt=opt) if use_local: print "execute gobgp program in local machine." pwd = local("pwd", capture=True) @@ -412,14 +419,14 @@ def init_policy_test_env_executor(quagga_num, use_local, go_path, log_debug=Fals gobgp_path = re.sub(A_PART_OF_CURRENT_DIR, "", pwd) cmd = "cp -r " + gobgp_path + " " + CONFIG_DIRR local(cmd, capture=True) - make_startup_file_use_local_gobgp(log_opt=opt) + make_install_file(use_local=True) else: print "scenario_test directory is not." print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() else: print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() change_owner_to_root(CONFIG_DIR) start_gobgp() @@ -451,6 +458,7 @@ def init_ipv6_test_env_executor(quagga_num, use_local, go_path, log_debug=False) opt = "-l debug" if log_debug else "" # execute local gobgp program in the docker container if the input option is local + make_startup_file(log_opt=opt) if use_local: print "execute gobgp program in local machine." pwd = local("pwd", capture=True) @@ -458,14 +466,14 @@ def init_ipv6_test_env_executor(quagga_num, use_local, go_path, log_debug=False) gobgp_path = re.sub(A_PART_OF_CURRENT_DIR, "", pwd) cmd = "cp -r " + gobgp_path + " " + CONFIG_DIRR local(cmd, capture=True) - make_startup_file_use_local_gobgp(log_opt=opt) + make_install_file(use_local=True) else: print "scenario_test directory is not." print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() else: print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() change_owner_to_root(CONFIG_DIR) start_gobgp() @@ -508,14 +516,14 @@ def init_malformed_test_env_executor(conf_file, use_local, go_path, exabgp_path gobgp_path = re.sub(A_PART_OF_CURRENT_DIR, "", pwd) cmd = "cp -r " + gobgp_path + " " + CONFIG_DIRR local(cmd, capture=True) - make_startup_file_use_local_gobgp(log_opt=opt) + make_install_file(use_local=True) else: print "scenario_test directory is not." print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() else: print "execute gobgp program of osrg/master in github." - make_startup_file(log_opt=opt) + make_install_file() change_owner_to_root(CONFIG_DIR) diff --git a/test/scenario_test/gobgp_test.py b/test/scenario_test/gobgp_test.py index 0cf42199..aafc41ed 100644 --- a/test/scenario_test/gobgp_test.py +++ b/test/scenario_test/gobgp_test.py @@ -14,7 +14,7 @@ # limitations under the License. import unittest -import requests +from fabric.api import local import json import toml import os @@ -24,6 +24,7 @@ from peer_info import Peer from peer_info import Destination from peer_info import Path from constant import * +import quagga_access as qaccess class GoBGPTestBase(unittest.TestCase): @@ -42,6 +43,19 @@ class GoBGPTestBase(unittest.TestCase): def setUp(self): self.quagga_configs = [] + def get_neighbor_state(self, neighbor_address): + print "check neighbor state for %s" % (neighbor_address) + state = None + try: + neighbor = self.ask_gobgp(NEIGHBOR, neighbor_address) + state = neighbor['info']['bgp_state'] + remote_ip = neighbor['conf']['remote_ip'] + assert remote_ip == neighbor_address + return state + except Exception as e: + print e + return state + def retry_routine_for_state(self, addresses, allow_state): in_prepare_quagga = True retry_count = 0 @@ -56,15 +70,8 @@ class GoBGPTestBase(unittest.TestCase): success_count = 0 for address in addresses: # get neighbor state and remote ip from gobgp connections - try: - neighbor = self.ask_gobgp(NEIGHBOR, address) - except Exception: - continue - if neighbor is None: - continue - state = neighbor['info']['bgp_state'] - remote_ip = neighbor['conf']['remote_ip'] - if address == remote_ip and state == allow_state: + state = self.get_neighbor_state(address) + if state == allow_state: success_count += 1 if success_count == len(addresses): in_prepare_quagga = False @@ -77,12 +84,11 @@ class GoBGPTestBase(unittest.TestCase): rib = self.ask_gobgp(LOCAL_RIB, check_address) target_exist = False - g_dests = rib['Destinations'] - for g_dest in g_dests: - best_path_idx = g_dest['BestPathIdx'] - if target_network == g_dest['Prefix']: + for g_dest in rib: + best_path_idx = g_dest['best_path_idx'] if 'best_path_idx' in g_dest else 0 + if target_network == g_dest['prefix']: target_exist = True - g_paths = g_dest['Paths'] + g_paths = g_dest['paths'] idx = 0 if len(g_paths) < 2: print "target path has not been bestpath selected yet." @@ -91,10 +97,11 @@ class GoBGPTestBase(unittest.TestCase): self.retry_routine_for_bestpath(check_address, target_network, ans_nexthop) return for g_path in g_paths: - print "best_path_Idx: " + str(best_path_idx) + "idx: " + str(idx) - print "pre: ", g_dest['Prefix'], "net: ", g_path['Network'], "next: ", g_path['Nexthop'] + print "best_path_Idx: " + str(best_path_idx) + ", idx: " + str(idx) + print g_dest + print "pre: ", g_dest['prefix'], "net: ", g_path['network'], "next: ", g_path['nexthop'] if str(best_path_idx) == str(idx): - rep_nexthop = g_path['Nexthop'] + rep_nexthop = g_path['nexthop'] idx += 1 if target_exist is False: print "target path has not been receive yet." @@ -179,13 +186,98 @@ class GoBGPTestBase(unittest.TestCase): return True def ask_gobgp(self, what, who="", af="ipv4"): - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/" + cmd = "%s/%s -j -u %s -p %s show " % (CONFIG_DIR, CLI_CMD, self.gobgp_ip, self.gobgp_port) if what == GLOBAL_RIB: - url += "/".join([what, af]) + cmd += " ".join([what, af]) elif what == NEIGHBOR: - url += "/".join([NEIGHBOR, who]) + cmd += " ".join([NEIGHBOR, who]) else: - url += "/".join([NEIGHBOR, who, what, af]) - r = requests.get(url) - result = json.loads(r.text) + cmd += " ".join([NEIGHBOR, who, what, af]) + j = local(cmd, capture=True) + result = json.loads(j) return result + + def soft_reset(self, neighbor_address, route_family, type="in"): + cmd = "%s/%s -j -u %s -p %s softreset%s " % (CONFIG_DIR, CLI_CMD, self.gobgp_ip, self.gobgp_port, type) + cmd += "neighbor %s %s" % (neighbor_address, route_family) + local(cmd) + + def get_paths_in_localrib(self, neighbor_address, target_prefix, retry=3, interval=5): + retry_count = 0 + while True: + local_rib = self.ask_gobgp(LOCAL_RIB, neighbor_address) + g_dest = [dest for dest in local_rib if dest['prefix'] == target_prefix] + if len(g_dest) > 0: + assert len(g_dest) == 1 + d = g_dest[0] + return d['paths'] + else: + retry_count += 1 + if retry_count > retry: + break + else: + print "destination is none : %s" % neighbor_address + print "please wait more (" + str(interval) + " second)" + time.sleep(interval) + + print "destination is none" + return None + + def get_adj_rib_in(self, neighbor_address, target_prefix, retry=3, interval=-1): + if interval < 0: + interval = self.wait_per_retry + return self.get_adj_rib(neighbor_address, target_prefix, retry, interval, type=ADJ_RIB_IN) + + + def get_adj_rib_out(self, neighbor_address, target_prefix, retry=3, interval=-1): + if interval < 0: + interval = self.wait_per_retry + return self.get_adj_rib(neighbor_address, target_prefix, retry, interval, type=ADJ_RIB_OUT) + + + def get_adj_rib(self, neighbor_address, target_prefix, retry, interval, type=ADJ_RIB_IN): + retry_count = 0 + while True: + rib = self.ask_gobgp(type, neighbor_address) + paths = [p for p in rib if p['network'] == target_prefix] + + if len(paths) > 0: + assert len(paths) == 1 + return paths[0] + else: + retry_count += 1 + if retry_count > retry: + break + else: + print "adj_rib_%s is none" % type + print "wait (" + str(interval) + " seconds)" + time.sleep(interval) + + print "adj_rib_%s is none" % type + return None + + + # get route information on quagga + def get_routing_table(self, neighbor_address, target_prefix, retry=3, interval=-1): + if interval < 0: + interval = self.wait_per_retry + print "check route %s on quagga : %s" % (target_prefix, neighbor_address) + retry_count = 0 + while True: + tn = qaccess.login(neighbor_address) + q_rib = qaccess.show_rib(tn) + qaccess.logout(tn) + for q_path in q_rib: + if target_prefix == q_path['Network']: + return q_path + + retry_count += 1 + if retry_count > retry: + break + else: + print "target_prefix %s is none" % target_prefix + print "wait (" + str(interval) + " seconds)" + time.sleep(interval) + + print "route : %s is none" % target_prefix + return None diff --git a/test/scenario_test/peer_info.py b/test/scenario_test/peer_info.py index 00490d09..5a965f7b 100644 --- a/test/scenario_test/peer_info.py +++ b/test/scenario_test/peer_info.py @@ -1,6 +1,21 @@ +# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +# +# 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. + + class Peer: def __init__(self, peer_ip, peer_id, peer_as, ip_version): - # def __init__(self, peer_ip, peer_id, peer_as): self.peer_ip = peer_ip self.peer_id = peer_id self.peer_as = peer_as @@ -21,4 +36,4 @@ class Path: self.nexthop = nexthop self.origin = None self.as_path = [] - self.metric = None \ No newline at end of file + self.metric = None diff --git a/test/scenario_test/route_server_ipv4_v6_test.py b/test/scenario_test/route_server_ipv4_v6_test.py index a171536b..4233988f 100644 --- a/test/scenario_test/route_server_ipv4_v6_test.py +++ b/test/scenario_test/route_server_ipv4_v6_test.py @@ -76,21 +76,16 @@ class GoBGPIPv6Test(GoBGPTestBase): if quagga_config.peer_ip == address or quagga_config.ip_version != af: for c_dest in quagga_config.destinations.itervalues(): # print "config : ", c_dest.prefix, "my ip or different ip version!!!" - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 0) else: for c_dest in quagga_config.destinations.itervalues(): - # print "config : ", c_dest.prefix" - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 1) diff --git a/test/scenario_test/route_server_malformed_test.py b/test/scenario_test/route_server_malformed_test.py index 4c45a9f7..ab8993ae 100644 --- a/test/scenario_test/route_server_malformed_test.py +++ b/test/scenario_test/route_server_malformed_test.py @@ -20,12 +20,13 @@ import sys import nose import collections import docker_control as fab +from fabric.api import local import requests import json import toml from noseplugin import OptionParser from noseplugin import parser_option - +from constant import CONFIG_DIR, CLI_CMD initial_wait_time = 10 wait_per_retry = 5 @@ -98,13 +99,13 @@ def check_func(exabgp_conf, result): retry_count = 0 # get neighbor addresses from gobgpd.conf addresses = get_neighbor_address() - url = "http://" + gobgp_ip + ":" + gobgp_port + "/v1/bgp/neighbors" neighbors = None q_address = "" e_address = "" q_transitions = 0 q_state = "" notification = "" + while in_prepare_quagga or in_prepare_exabgp: if retry_count != 0: print "please wait more (" + str(wait_per_retry) + " second)" @@ -115,8 +116,9 @@ def check_func(exabgp_conf, result): retry_count += 1 # check whether the service of gobgp is normally try: - r = requests.get(url) - neighbors = json.loads(r.text) + cmd = "%s/%s -j -u %s -p %s show neighbors" % (CONFIG_DIR, CLI_CMD, gobgp_ip, gobgp_port) + j = local(cmd, capture=True) + neighbors = json.loads(j) except Exception: continue if neighbors is None: @@ -173,4 +175,4 @@ if __name__ == '__main__': if fab.docker_pkg_check() is False: print "not install docker package." sys.exit(1) - nose.main(argv=sys.argv, addplugins=[OptionParser()], defaultTest=sys.argv[0]) \ No newline at end of file + nose.main(argv=sys.argv, addplugins=[OptionParser()], defaultTest=sys.argv[0]) diff --git a/test/scenario_test/route_server_policy_test.py b/test/scenario_test/route_server_policy_test.py index 42d74d9e..9ae29a2b 100644 --- a/test/scenario_test/route_server_policy_test.py +++ b/test/scenario_test/route_server_policy_test.py @@ -14,42 +14,25 @@ # limitations under the License. import unittest -import requests -import json -import toml import os import time import sys import nose import quagga_access as qaccess -from peer_info import Peer -from peer_info import Destination -from peer_info import Path -from ciscoconfparse import CiscoConfParse import docker_control as fab from noseplugin import OptionParser from noseplugin import parser_option -import scenario_test_util as util +from gobgp_test import GoBGPTestBase +from constant import * -class GoBGPTest(unittest.TestCase): +class GoBGPTest(GoBGPTestBase): - gobgp_ip = "10.0.255.1" - gobgp_port = "8080" - rest_url_neighbor = "http://" + gobgp_ip + ":" + gobgp_port + "/v1/bgp/neighbor/" - base_dir = "/tmp/gobgp/" - gobgp_config_file = "/tmp/gobgp/gobgpd.conf" - gobgp_config = None quagga_num = 3 - initial_wait_time = 10 - wait_per_retry = 5 def __init__(self, *args, **kwargs): super(GoBGPTest, self).__init__(*args, **kwargs) - def setUp(self): - self.quagga_configs = [] - def initialize(self, policy_pattern=None): use_local = parser_option.use_local go_path = parser_option.go_path @@ -60,12 +43,6 @@ class GoBGPTest(unittest.TestCase): self.assertTrue(self.check_load_config()) - def check_established(self, addresses): - for address in addresses: - result = self.retry_until(address, target_state="BGP_FSM_ESTABLISHED",retry=10) - self.assertEqual(result, True) - - """ import-policy test --------------------------------------- @@ -80,33 +57,32 @@ class GoBGPTest(unittest.TestCase): # policy_pattern:p1 attaches a policy to reject route 192.168.0.0/16 (16...24) # coming from peer2(10.0.0.2) to peer3(10.0.0.3)'s import-policy. self.initialize(policy_pattern="p1") - self.check_established(util.get_neighbor_address(self.gobgp_config)) + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") peer1 = "10.0.0.1" peer2 = "10.0.0.2" peer3 = "10.0.0.3" - base_url = self.rest_url_neighbor - w = self.wait_per_retry - path = util.get_paths_in_localrib(base_url, peer1, "192.168.2.0", retry=3, interval=w) + path = self.get_paths_in_localrib(peer1, "192.168.2.0", retry=3) self.assertIsNotNone(path) # check show ip bgp on peer1(quagga1) - qpath = util.get_routing_table(peer1,"192.168.2.0", retry=3, interval=w) + qpath = self.get_routing_table(peer1,"192.168.2.0", retry=3) print qpath self.assertIsNotNone(qpath) # check adj-rib-out in peer2 - path = util.get_adj_rib_in(base_url, peer2, "192.168.2.0/24", retry=3, interval=w) + path = self.get_adj_rib_in(peer2, "192.168.2.0/24", retry=3) # print path self.assertIsNotNone(path) - path = util.get_paths_in_localrib(base_url, peer3, "192.168.2.0",retry=0, interval=w) + path = self.get_paths_in_localrib(peer3, "192.168.2.0",retry=0) # print path self.assertIsNone(path) # check show ip bgp on peer1(quagga3) - qpath = util.get_routing_table(peer3,"192.168.2.0", retry=3, interval=w) + qpath = self.get_routing_table(peer3,"192.168.2.0", retry=3) # print qpath self.assertIsNone(qpath) @@ -125,38 +101,37 @@ class GoBGPTest(unittest.TestCase): # policy_pattern:p1 attaches a policy to reject route 192.168.0.0/16 (16...24) # coming from peer2(10.0.0.2) to peer3(10.0.0.3)'s export-policy. self.initialize(policy_pattern="p2") - self.check_established(util.get_neighbor_address(self.gobgp_config)) + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") peer1 = "10.0.0.1" peer2 = "10.0.0.2" peer3 = "10.0.0.3" - base_url = self.rest_url_neighbor - w = self.wait_per_retry - paths = util.get_paths_in_localrib(base_url, peer1, "192.168.2.0", retry=3, interval=w) + paths = self.get_paths_in_localrib(peer1, "192.168.2.0", retry=3) # print paths self.assertIsNotNone(paths) # check show ip bgp on peer1(quagga1) - qpath = util.get_routing_table(peer1, "192.168.2.0", retry=3, interval=w) + qpath = self.get_routing_table(peer1, "192.168.2.0", retry=3) # print qpath self.assertIsNotNone(qpath) # check adj-rib-out in peer2 - path = util.get_adj_rib_in(base_url, peer2, "192.168.2.0/24", retry=1, interval=w) + path = self.get_adj_rib_in(peer2, "192.168.2.0/24", retry=1) # print path self.assertIsNotNone(path) - path = util.get_paths_in_localrib(base_url, peer3, "192.168.2.0") + path = self.get_paths_in_localrib(peer3, "192.168.2.0") # print path self.assertIsNotNone(path) - path = util.get_adj_rib_out(base_url, peer3, "192.168.2.0", retry=1, interval=w) + path = self.get_adj_rib_out(peer3, "192.168.2.0", retry=1) # print path self.assertIsNone(path) # check show ip bgp on peer1(quagga3) - qpath = util.get_routing_table(peer3,"192.168.2.0", retry=3, interval=w) + qpath = self.get_routing_table(peer3,"192.168.2.0", retry=3) # print qpath self.assertIsNone(qpath) @@ -195,8 +170,6 @@ class GoBGPTest(unittest.TestCase): peer1 = "10.0.0.1" peer2 = "10.0.0.2" peer3 = "10.0.0.3" - base_url = self.rest_url_neighbor - w = self.wait_per_retry # add other network tn = qaccess.login(peer2) @@ -206,20 +179,21 @@ class GoBGPTest(unittest.TestCase): qaccess.add_network(tn, 65002, "192.168.200.0/24") qaccess.logout(tn) - self.check_established(util.get_neighbor_address(self.gobgp_config)) + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") time.sleep(self.initial_wait_time) def path_exists_in_localrib(peer, prefix,r=10): - paths = util.get_paths_in_localrib(base_url, peer, prefix, retry=r, interval=w) + paths = self.get_paths_in_localrib(peer, prefix, retry=r) return paths is not None def path_exists_in_routing_table(peer, prefix,r=10): - qpath = util.get_routing_table(peer, prefix, retry=r, interval=w) + qpath = self.get_routing_table(peer, prefix, retry=r) return qpath is not None def path_exists_in_adj_rib_in(peer, prefix,r=10): - path = util.get_adj_rib_in(base_url, peer, prefix, retry=r, interval=w) + path = self.get_adj_rib_in(peer, prefix, retry=r) return path is not None @@ -253,7 +227,7 @@ class GoBGPTest(unittest.TestCase): # soft reset print "soft_reset" - self.soft_reset(peer2, "ipv4") + self.soft_reset(peer2, IPv4) # check local-rib self.assertTrue(path_exists_in_localrib(peer3,"192.168.2.0")) @@ -301,8 +275,6 @@ class GoBGPTest(unittest.TestCase): peer1 = "10.0.0.1" peer2 = "10.0.0.2" peer3 = "10.0.0.3" - base_url = self.rest_url_neighbor - w = self.wait_per_retry # add other network tn = qaccess.login(peer2) @@ -312,25 +284,25 @@ class GoBGPTest(unittest.TestCase): qaccess.add_network(tn, 65002, "192.168.200.0/24") qaccess.logout(tn) - self.check_established(self.get_neighbor_address(self.gobgp_config)) + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") time.sleep(self.initial_wait_time) - def path_exists_in_localrib(peer, prefix,r=10): - paths = util.get_paths_in_localrib(base_url, peer, prefix, retry=r, interval=w) + paths = self.get_paths_in_localrib(peer, prefix, retry=r) return paths is not None def path_exists_in_routing_table(peer, prefix,r=10): - qpath = util.get_routing_table(peer, prefix, retry=r, interval=w) + qpath = self.get_routing_table(peer, prefix, retry=r) return qpath is not None def path_exists_in_adj_rib_in(peer, prefix,r=10): - path = util.get_adj_rib_in(base_url, peer, prefix, retry=r, interval=w) + path = self.get_adj_rib_in(peer, prefix, retry=r) return path is not None def path_exists_in_adj_rib_out(peer, prefix,r=10): - path = util.get_adj_rib_out(base_url, peer, prefix, retry=r, interval=w) + path = self.get_adj_rib_out(peer, prefix, retry=r) return path is not None @@ -371,7 +343,7 @@ class GoBGPTest(unittest.TestCase): # soft reset print "soft_reset" - self.soft_reset(peer2, "ipv4") + self.soft_reset(peer2, IPv4) # check local-rib self.assertTrue(path_exists_in_localrib(peer3,"192.168.2.0")) @@ -389,49 +361,6 @@ class GoBGPTest(unittest.TestCase): self.assertTrue(path_exists_in_routing_table(peer3, "192.168.200.0")) - def retry_until(self, neighbor_address, target_state="BGP_FSM_ESTABLISHED", retry=3): - retry_count = 0 - - while True: - - current_state = util.get_neighbor_state(self.rest_url_neighbor, neighbor_address) - if current_state == target_state: - print "state changed to %s : %s" % (current_state, neighbor_address) - return True - else: - retry_count += 1 - if retry_count > retry: - break - else: - print "current state is %s" % current_state - print "please wait more (" + str(self.wait_per_retry) + " second)" - time.sleep(self.wait_per_retry) - - print "exceeded retry count : %s" % neighbor_address - return False - - - def soft_reset(self, neighbor_address, route_family, type="in"): - url = self.rest_url_neighbor + neighbor_address + "/softreset"+type+"/" + route_family - r = requests.post(url) - if r.status_code == requests.codes.ok: - print "Succeed" - else: - print "Failed" - - - def check_load_config(self): - self.gobgp_config = util.load_gobgp_config(self.gobgp_config_file) - self.quagga_configs = util.load_quagga_config(self.base_dir) - if self.gobgp_config is None: - print "Failed to read the gobgp configuration file" - return False - if len(self.quagga_configs) == 0: - print "Failed to read the quagga configuration file" - return False - return True - - if __name__ == '__main__': if fab.test_user_check() is False: print "you are not root." diff --git a/test/scenario_test/route_server_test.py b/test/scenario_test/route_server_test.py index 7621d628..36ea0b20 100644 --- a/test/scenario_test/route_server_test.py +++ b/test/scenario_test/route_server_test.py @@ -73,18 +73,16 @@ class GoBGPTest(GoBGPTestBase): for quagga_config in self.quagga_configs: if quagga_config.peer_ip == address: for c_dest in quagga_config.destinations.itervalues(): - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 0) else: for c_dest in quagga_config.destinations.itervalues(): - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 1) @@ -152,22 +150,17 @@ class GoBGPTest(GoBGPTestBase): for quagga_config in self.quagga_configs: if quagga_config.peer_ip == address: for c_dest in quagga_config.destinations.itervalues(): - # print "config : ", c_dest.prefix, "my ip !!!" - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 0) else: for c_dest in quagga_config.destinations.itervalues(): # print "config : ", c_dest.prefix," - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 1) @@ -241,21 +234,18 @@ class GoBGPTest(GoBGPTestBase): if quagga_config.peer_ip == address: for c_dest in quagga_config.destinations.itervalues(): # print "config : ", c_dest.prefix, "my ip !!!" - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: - # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + for g_dest in local_rib: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 0) else: for c_dest in quagga_config.destinations.itervalues(): # print "config : ", c_dest.prefix - g_dests = local_rib['Destinations'] exist_n = 0 - for g_dest in g_dests: + for g_dest in local_rib: # print "gobgp : ", g_dest['Prefix'] - if c_dest.prefix == g_dest['Prefix']: + if c_dest.prefix == g_dest['prefix']: exist_n += 1 self.assertEqual(exist_n, 1) diff --git a/test/scenario_test/scenario_test_util.py b/test/scenario_test/scenario_test_util.py deleted file mode 100644 index a889c2db..00000000 --- a/test/scenario_test/scenario_test_util.py +++ /dev/null @@ -1,201 +0,0 @@ -# Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation. -# -# 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. - -import os -import time -import json -import requests -import quagga_access as qaccess -import toml -from peer_info import Peer -from peer_info import Destination -from peer_info import Path -from ciscoconfparse import CiscoConfParse - - -# get address of each neighbor from gobpg configration -def get_neighbor_address(config): - address = [] - neighbors_config = config['NeighborList'] - for neighbor_config in neighbors_config: - neighbor_ip = neighbor_config['NeighborAddress'] - address.append(neighbor_ip) - return address - - -# get route information on quagga -def get_routing_table(neighbor_address, target_prefix, retry=3, interval=5): - print "check route %s on quagga : %s" % (target_prefix, neighbor_address) - retry_count = 0 - while True: - - tn = qaccess.login(neighbor_address) - q_rib = qaccess.show_rib(tn) - qaccess.logout(tn) - for q_path in q_rib: - if target_prefix == q_path['Network']: - return q_path - - retry_count += 1 - if retry_count > retry: - break - else: - print "target_prefix %s is none" % target_prefix - print "wait (" + str(interval) + " seconds)" - time.sleep(interval) - - print "route : %s is none" % target_prefix - return None - - -def get_adj_rib_in(url, neighbor_address, target_prefix, retry=3, interval=5): - return get_adj_rib(url, neighbor_address, target_prefix, retry, interval, type="in") - - -def get_adj_rib_out(url, neighbor_address, target_prefix, retry=3, interval=5): - return get_adj_rib(url, neighbor_address, target_prefix, retry, interval, type="out") - - -def get_adj_rib(base_url, neighbor_address, target_prefix, retry, interval, type="in"): - url = base_url + neighbor_address + "/adj-rib-" +type +"/ipv4" - - retry_count = 0 - while True: - - r = requests.get(url) - in_rib = json.loads(r.text) - print in_rib - paths = [p for p in in_rib if p['Network'] == target_prefix] - - if len(paths) > 0: - assert len(paths) == 1 - return paths[0] - else: - retry_count += 1 - if retry_count > retry: - break - else: - print "adj_rib_%s is none" % type - print "wait (" + str(interval) + " seconds)" - time.sleep(interval) - - print "adj_rib_%s is none" % type - return None - - -def get_neighbor_state(base_url, neighbor_address): - print "check neighbor state for %s" % (neighbor_address) - state = None - url = base_url + neighbor_address - try: - r = requests.get(url) - neighbor = json.loads(r.text) - state = neighbor['info']['bgp_state'] - remote_ip = neighbor['conf']['remote_ip'] - assert remote_ip == neighbor_address - return state - except Exception as e: - print e - return state - - -def get_paths_in_localrib(base_url, neighbor_address, target_prefix, retry=3, interval=5): - url = base_url + neighbor_address + "/local-rib" + "/ipv4" - - retry_count = 0 - while True: - - r = requests.get(url) - local_rib = json.loads(r.text) - g_dests = local_rib['Destinations'] - g_dest = [dest for dest in g_dests if dest['Prefix'] == target_prefix] - if len(g_dest) > 0: - assert len(g_dest) == 1 - d = g_dest[0] - return d['Paths'] - else: - retry_count += 1 - if retry_count > retry: - break - else: - print "destination is none : %s" % neighbor_address - print "please wait more (" + str(interval) + " second)" - time.sleep(interval) - - print "destination is none" - return None - - -def load_gobgp_config(gobgp_config_file): - - config = None - try: - config = toml.loads(open(gobgp_config_file).read()) - except IOError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - return config - - -# load configration from quagga(bgpd.conf) -def load_quagga_config(base_dir): - configs = [] - dirs = [] - try: - content = os.listdir(base_dir) - for item in content: - if "q" != item[0]: - continue - if os.path.isdir(os.path.join(base_dir, item)): - dirs.append(item) - except OSError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - for dir in dirs: - config_path = base_dir + dir + "/bgpd.conf" - config = CiscoConfParse(config_path) - - peer_ip = config.find_objects(r"^!\smy\saddress")[0].text.split(" ")[3] - peer_ip_version = config.find_objects(r"^!\smy\sip_version")[0].text.split(" ")[3] - peer_id = config.find_objects(r"^bgp\srouter-id")[0].text.split(" ")[2] - peer_as = config.find_objects(r"^router\sbgp")[0].text.split(" ")[2] - quagga_config = Peer(peer_ip, peer_id, peer_as, peer_ip_version) - - networks = config.find_objects(r"^network") - if len(networks) == 0: - continue - for network in networks: - elems = network.text.split(" ") - prefix = elems[1].split("/")[0] - network = elems[1] - nexthop = peer_ip - path = Path(network, nexthop) - dest = Destination(prefix) - dest.paths.append(path) - quagga_config.destinations[prefix] = dest - # print "prefix: " + prefix - # print "network: " + network - # print "nexthop: " + nexthop - - neighbors = config.find_objects(r"^neighbor\s.*\sremote-as") - if len(neighbors) == 0: - continue - for neighbor in neighbors: - elems = neighbor.text.split(" ") - neighbor = Peer(elems[1], None, elems[3], None) - quagga_config.neighbors.append(neighbor) - configs.append(quagga_config) - - return configs \ No newline at end of file -- cgit v1.2.3