From f2336c801d0b606f5a6b82a1cea898cf8f392d10 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Sun, 29 Mar 2015 10:25:32 +0000 Subject: scenario_test: kill duplicated code gather utility code in gobgp_test.py Signed-off-by: ISHIDA Wataru --- test/scenario_test/constant.py | 47 ++++++ test/scenario_test/docker_control.py | 46 +----- test/scenario_test/gobgp_test.py | 191 +++++++++++++++++++++++ test/scenario_test/quagga-rsconfig.go | 16 +- test/scenario_test/quagga_access.py | 18 +-- test/scenario_test/route_server_ipv4_v6_test.py | 171 +++----------------- test/scenario_test/route_server_test.py | 199 ++---------------------- 7 files changed, 287 insertions(+), 401 deletions(-) create mode 100644 test/scenario_test/constant.py create mode 100644 test/scenario_test/gobgp_test.py (limited to 'test/scenario_test') diff --git a/test/scenario_test/constant.py b/test/scenario_test/constant.py new file mode 100644 index 00000000..5e672a79 --- /dev/null +++ b/test/scenario_test/constant.py @@ -0,0 +1,47 @@ +IPv4 = 'ipv4' +IPv6 = 'ipv6' +GOBGP_IP = "10.0.255.1" +GOBGP_CONTAINER_NAME = "gobgp" +GOBGP_ADDRESS_0 = {IPv4: GOBGP_IP, + IPv6: "2001::0:192:168:255:1"} +GOBGP_ADDRESS_1 = {IPv4: "11.0.255.1", + IPv6: "2001::1:192:168:255:1"} +GOBGP_ADDRESS_2 = {IPv4: "12.0.255.1", + IPv6: "2001::2:192:168:255:1"} +GOBGP_CONFIG_FILE = "gobgpd.conf" +CONFIG_DIR = "/tmp/gobgp" +CONFIG_DIRR = "/tmp/gobgp/" +SHARE_VOLUME = "/root/share_volume" +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 + +IP_VERSION = IPv4 +IF_CONFIG_OPTION = {IPv4: "inet", IPv6: "inet6"} +BRIDGE_0 = {"BRIDGE_NAME": "br0", + IPv4: "10.0.255.2", + IPv6: "2001::0:192:168:255:2"} +BRIDGE_1 = {"BRIDGE_NAME": "br1", + IPv4: "11.0.255.2", + IPv6: "2001::1:192:168:255:2"} +BRIDGE_2 = {"BRIDGE_NAME": "br2", + IPv4: "12.0.255.2", + IPv6: "2001::2:192:168:255:2"} +BRIDGES = [BRIDGE_0, BRIDGE_1, BRIDGE_2] + +BASE_NET = {BRIDGE_0["BRIDGE_NAME"]: {IPv4: "10.0.0.", IPv6: "2001::0:192:168:0:"}, + BRIDGE_1["BRIDGE_NAME"]: {IPv4: "11.0.0.", IPv6: "2001::1:192:168:0:"}, + BRIDGE_2["BRIDGE_NAME"]: {IPv4: "12.0.0.", IPv6: "2001::2:192:168:0:"}} + +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" +NEIGHBOR = "neighbor" diff --git a/test/scenario_test/docker_control.py b/test/scenario_test/docker_control.py index b7ad0ef0..9650f418 100644 --- a/test/scenario_test/docker_control.py +++ b/test/scenario_test/docker_control.py @@ -17,47 +17,7 @@ from fabric.api import local import re import os import time - - -GOBGP_CONTAINER_NAME = "gobgp" -GOBGP_ADDRESS_0 = {"IPv4": "10.0.255.1", - "IPv6": "2001::0:192:168:255:1"} -GOBGP_ADDRESS_1 = {"IPv4": "11.0.255.1", - "IPv6": "2001::1:192:168:255:1"} -GOBGP_ADDRESS_2 = {"IPv4": "12.0.255.1", - "IPv6": "2001::2:192:168:255:1"} -GOBGP_CONFIG_FILE = "gobgpd.conf" -CONFIG_DIR = "/tmp/gobgp" -CONFIG_DIRR = "/tmp/gobgp/" -SHARE_VOLUME = "/root/share_volume" -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 - -IP_VERSION = "IPv4" -IF_CONFIG_OPTION = {"IPv4": "inet", "IPv6": "inet6"} -BRIDGE_0 = {"BRIDGE_NAME": "br0", - "IPv4": "10.0.255.2", - "IPv6": "2001::0:192:168:255:2"} -BRIDGE_1 = {"BRIDGE_NAME": "br1", - "IPv4": "11.0.255.2", - "IPv6": "2001::1:192:168:255:2"} -BRIDGE_2 = {"BRIDGE_NAME": "br2", - "IPv4": "12.0.255.2", - "IPv6": "2001::2:192:168:255:2"} -BRIDGES = [BRIDGE_0, BRIDGE_1, BRIDGE_2] - -BASE_NET = {BRIDGE_0["BRIDGE_NAME"]: {"IPv4": "10.0.0.", "IPv6": "2001::0:192:168:0:"}, - BRIDGE_1["BRIDGE_NAME"]: {"IPv4": "11.0.0.", "IPv6": "2001::1:192:168:0:"}, - BRIDGE_2["BRIDGE_NAME"]: {"IPv4": "12.0.0.", "IPv6": "2001::2:192:168:0:"}} - -BASE_MASK = {"IPv4": "/16", "IPv6": "/64"} - -A_PART_OF_CURRENT_DIR = "/test/scenario_test" - +from constant import * def test_user_check(): root = False @@ -272,7 +232,7 @@ def bridge_setting_for_docker_connection(bridges): for bridge in bridges: cmd = "brctl addbr " + bridge["BRIDGE_NAME"] local(cmd, capture=True) - if IP_VERSION == "IPv6": + if IP_VERSION == IPv6: cmd = "ifconfig " + bridge["BRIDGE_NAME"] + " " + IF_CONFIG_OPTION[IP_VERSION] +\ " add " + bridge[IP_VERSION] + BASE_MASK[IP_VERSION] else: @@ -507,7 +467,7 @@ def docker_container_quagga_append_executor(quagga_num, go_path, is_route_server def docker_container_ipv6_quagga_append_executor(quagga_nums, go_path): print "append ipv6 quagga container." global IP_VERSION - IP_VERSION = "IPv6" + IP_VERSION = IPv6 bridge_setting_for_docker_connection([BRIDGE_1]) docker_container_set_ipaddress(BRIDGE_1, GOBGP_CONTAINER_NAME, GOBGP_ADDRESS_1[IP_VERSION] + BASE_MASK[IP_VERSION]) for quagga_num in quagga_nums: diff --git a/test/scenario_test/gobgp_test.py b/test/scenario_test/gobgp_test.py new file mode 100644 index 00000000..0cf42199 --- /dev/null +++ b/test/scenario_test/gobgp_test.py @@ -0,0 +1,191 @@ +# Copyright (C) 2014 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 unittest +import requests +import json +import toml +import os +import time +from ciscoconfparse import CiscoConfParse +from peer_info import Peer +from peer_info import Destination +from peer_info import Path +from constant import * + +class GoBGPTestBase(unittest.TestCase): + + gobgp_ip = GOBGP_IP + gobgp_port = "8080" + base_dir = CONFIG_DIRR + gobgp_config_file = CONFIG_DIRR + "gobgpd.conf" + gobgp_config = None + initial_wait_time = 10 + wait_per_retry = 5 + retry_limit = (60 - initial_wait_time) / wait_per_retry + + def __init__(self, *args, **kwargs): + super(GoBGPTestBase, self).__init__(*args, **kwargs) + + def setUp(self): + self.quagga_configs = [] + + def retry_routine_for_state(self, addresses, allow_state): + in_prepare_quagga = True + retry_count = 0 + while in_prepare_quagga: + if retry_count != 0: + print "please wait more (" + str(self.wait_per_retry) + " second)" + time.sleep(self.wait_per_retry) + if retry_count >= self.retry_limit: + print "retry limit" + break + retry_count += 1 + 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: + success_count += 1 + if success_count == len(addresses): + in_prepare_quagga = False + + def retry_routine_for_bestpath(self, check_address, target_network, ans_nexthop): + # get rib + if check_address == "": + rib = self.ask_gobgp(GLOBAL_RIB) + else: + 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']: + target_exist = True + g_paths = g_dest['Paths'] + idx = 0 + if len(g_paths) < 2: + print "target path has not been bestpath selected yet." + print "please wait more (" + str(self.wait_per_retry) + " second)" + time.sleep(self.wait_per_retry) + 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'] + if str(best_path_idx) == str(idx): + rep_nexthop = g_path['Nexthop'] + idx += 1 + if target_exist is False: + print "target path has not been receive yet." + print "please wait more (" + str(self.wait_per_retry) + " second)" + time.sleep(self.wait_per_retry) + self.retry_routine_for_bestpath(check_address, target_network, ans_nexthop) + return + self.assertEqual(ans_nexthop, rep_nexthop) + + def load_gobgp_config(self): + try: + self.gobgp_config = toml.loads(open(self.gobgp_config_file).read()) + except IOError, (errno, strerror): + print "I/O error(%s): %s" % (errno, strerror) + + # load configration from quagga(bgpd.conf) + def load_quagga_config(self): + dirs = [] + try: + content = os.listdir(self.base_dir) + for item in content: + if "q" != item[0]: + continue + if os.path.isdir(os.path.join(self.base_dir, item)): + dirs.append(item) + except OSError, (errno, strerror): + print "I/O error(%s): %s" % (errno, strerror) + + for dir in dirs: + config_path = self.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) + self.quagga_configs.append(quagga_config) + + # get address of each neighbor from gobpg configration + def get_neighbor_address(self, config): + address = [] + neighbors_config = config['NeighborList'] + for neighbor_config in neighbors_config: + neighbor_ip = neighbor_config['NeighborAddress'] + address.append(neighbor_ip) + return address + + def check_load_config(self): + self.load_gobgp_config() + self.load_quagga_config() + 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 + + def ask_gobgp(self, what, who="", af="ipv4"): + url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/" + if what == GLOBAL_RIB: + url += "/".join([what, af]) + elif what == NEIGHBOR: + url += "/".join([NEIGHBOR, who]) + else: + url += "/".join([NEIGHBOR, who, what, af]) + r = requests.get(url) + result = json.loads(r.text) + return result diff --git a/test/scenario_test/quagga-rsconfig.go b/test/scenario_test/quagga-rsconfig.go index 9c92cb50..9f9592d3 100644 --- a/test/scenario_test/quagga-rsconfig.go +++ b/test/scenario_test/quagga-rsconfig.go @@ -19,8 +19,8 @@ var baseNeighborNetwork = make(map[string]string) var baseNeighborNetMask = make(map[string]string) const ( - IPv4 = "IPv4" - IPv6 = "IPv6" + IPv4 = "ipv4" + IPv6 = "ipv6" ) type QuaggaConfig struct { @@ -42,12 +42,12 @@ func NewQuaggaConfig(id int, gConfig *config.Global, myConfig *config.Neighbor, func (qt *QuaggaConfig) IPv4Config() *bytes.Buffer { buf := bytes.NewBuffer(nil) buf.WriteString(fmt.Sprintf("! my address %s\n", qt.config.NeighborAddress)) - buf.WriteString("! my ip_version IPv4\n") + buf.WriteString(fmt.Sprintf("! my ip_version %s\n", IPv4)) buf.WriteString("hostname bgpd\n") buf.WriteString("password zebra\n") buf.WriteString(fmt.Sprintf("router bgp %d\n", qt.config.PeerAs)) buf.WriteString(fmt.Sprintf("bgp router-id 192.168.0.%d\n", qt.id)) - buf.WriteString(fmt.Sprintf("network %s%d%s\n", baseNeighborNetwork["IPv4"], qt.id, baseNeighborNetMask["IPv4"])) + buf.WriteString(fmt.Sprintf("network %s%d%s\n", baseNeighborNetwork[IPv4], qt.id, baseNeighborNetMask[IPv4])) buf.WriteString(fmt.Sprintf("neighbor %s remote-as %d\n", qt.serverIP, qt.gobgpConfig.As)) buf.WriteString(fmt.Sprintf("neighbor %s password %s\n", qt.serverIP, qt.config.AuthPassword)) buf.WriteString("debug bgp as4\n") @@ -62,7 +62,7 @@ func (qt *QuaggaConfig) IPv4Config() *bytes.Buffer { func (qt *QuaggaConfig) IPv6Config() *bytes.Buffer { buf := bytes.NewBuffer(nil) buf.WriteString(fmt.Sprintf("! my address %s\n", qt.config.NeighborAddress)) - buf.WriteString("! my ip_version IPv6\n") + buf.WriteString(fmt.Sprintf("! my ip_version %s\n", IPv6)) buf.WriteString("hostname bgpd\n") buf.WriteString("password zebra\n") buf.WriteString(fmt.Sprintf("router bgp %d\n", qt.config.PeerAs)) @@ -71,7 +71,7 @@ func (qt *QuaggaConfig) IPv6Config() *bytes.Buffer { buf.WriteString(fmt.Sprintf("neighbor %s remote-as %d\n", qt.serverIP, qt.gobgpConfig.As)) buf.WriteString(fmt.Sprintf("neighbor %s password %s\n", qt.serverIP, qt.config.AuthPassword)) buf.WriteString("address-family ipv6\n") - buf.WriteString(fmt.Sprintf("network %s%d%s\n", baseNeighborNetwork["IPv6"], qt.id, baseNeighborNetMask["IPv6"])) + buf.WriteString(fmt.Sprintf("network %s%d%s\n", baseNeighborNetwork[IPv6], qt.id, baseNeighborNetMask[IPv6])) buf.WriteString(fmt.Sprintf("neighbor %s activate\n", qt.serverIP)) buf.WriteString(fmt.Sprintf("neighbor %s route-map IPV6-OUT out\n", qt.serverIP)) buf.WriteString("exit-address-family\n") @@ -114,7 +114,7 @@ func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bo quaggaConfigList = append(quaggaConfigList, q) os.Mkdir(fmt.Sprintf("%s/q%d", outputDir, i), 0755) var err error - if IPVersion == "IPv6" { + if IPVersion == IPv6 { err = ioutil.WriteFile(fmt.Sprintf("%s/q%d/bgpd.conf", outputDir, i), q.IPv6Config().Bytes(), 0644) } else { err = ioutil.WriteFile(fmt.Sprintf("%s/q%d/bgpd.conf", outputDir, i), q.IPv4Config().Bytes(), 0644) @@ -156,7 +156,7 @@ func append_config_files(ar int, outputDir string, IPVersion string, nonePeer bo q := NewQuaggaConfig(ar, &gobgpConf.Global, &c, net.ParseIP(serverAddress[IPVersion])) os.Mkdir(fmt.Sprintf("%s/q%d", outputDir, ar), 0755) var err error - if IPVersion == "IPv6" { + if IPVersion == IPv6 { err = ioutil.WriteFile(fmt.Sprintf("%s/q%d/bgpd.conf", outputDir, ar), q.IPv6Config().Bytes(), 0644) } else { err = ioutil.WriteFile(fmt.Sprintf("%s/q%d/bgpd.conf", outputDir, ar), q.IPv4Config().Bytes(), 0644) diff --git a/test/scenario_test/quagga_access.py b/test/scenario_test/quagga_access.py index 1e51afaf..dc77ca07 100644 --- a/test/scenario_test/quagga_access.py +++ b/test/scenario_test/quagga_access.py @@ -15,6 +15,7 @@ import sys import telnetlib +from constant import * PASSWORD = "zebra" CONN_PASSWORD = "hogehoge" @@ -78,15 +79,14 @@ def show_config(tn): print tn.read_all() -def show_rib(tn): - tn.write("show ip bgp\n") - tn.read_until(" Network Next Hop Metric LocPrf Weight Path") - rib = tn.read_until("bgpd#") - return rib_parser(rib) - - -def show_ipv6_rib(tn): - tn.write("show bgp ipv6\n") +def show_rib(tn, af=IPv4): + if af == IPv4: + tn.write("show ip bgp\n") + elif af == IPv6: + tn.write("show bgp ipv6\n") + else: + print "invalid af: ", af + return tn.read_until(" Network Next Hop Metric LocPrf Weight Path") rib = tn.read_until("bgpd#") return rib_parser(rib) diff --git a/test/scenario_test/route_server_ipv4_v6_test.py b/test/scenario_test/route_server_ipv4_v6_test.py index fa882f1b..a171536b 100644 --- a/test/scenario_test/route_server_ipv4_v6_test.py +++ b/test/scenario_test/route_server_ipv4_v6_test.py @@ -13,45 +13,26 @@ # See the License for the specific language governing permissions and # 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 gobgp_test import GoBGPTestBase +from gobgp_test import ADJ_RIB_IN, ADJ_RIB_OUT, LOCAL_RIB, GLOBAL_RIB +from gobgp_test import NEIGHBOR from noseplugin import OptionParser from noseplugin import parser_option - -class GoBGPIPv6Test(unittest.TestCase): - - gobgp_ip = "10.0.255.1" - gobgp_port = "8080" - base_dir = "/tmp/gobgp/" - gobgp_config_file = "/tmp/gobgp/gobgpd.conf" - gobgp_config = None +class GoBGPIPv6Test(GoBGPTestBase): quagga_num = 2 append_quagga = 10 remove_quagga = 10 append_quagga_best = 20 - initial_wait_time = 10 - wait_per_retry = 5 - retry_limit = (60 - initial_wait_time) / wait_per_retry def __init__(self, *args, **kwargs): super(GoBGPIPv6Test, self).__init__(*args, **kwargs) - def setUp(self): - self.quagga_configs = [] - # test each neighbor state is turned establish def test_01_ipv4_ipv6_neighbor_established(self): print "test_ipv4_ipv6_neighbor_established" @@ -69,17 +50,15 @@ class GoBGPIPv6Test(unittest.TestCase): return addresses = self.get_neighbor_address(self.gobgp_config) - self.retry_routine_for_stete(addresses) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") for address in addresses: # get neighbor state and remote ip from gobgp connections - print "check of [ " + address[0] + " : " + address[1] + " ]" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + address[0] - r = requests.get(url) - neighbor = json.loads(r.text) + print "check of [ " + address + " ]" + neighbor = self.ask_gobgp(NEIGHBOR, address) state = neighbor['info']['bgp_state'] remote_ip = neighbor['conf']['remote_ip'] - self.assertEqual(address[0], remote_ip) + self.assertEqual(address, remote_ip) self.assertEqual(state, "BGP_FSM_ESTABLISHED") print "state" + state @@ -89,17 +68,12 @@ class GoBGPIPv6Test(unittest.TestCase): return for address in self.get_neighbor_address(self.gobgp_config): - print "check of [ " + address[0] + " : " + address[1] + " ]" - # get local-rib per peer - af = "/ipv6" if address[1] == "IPv6" else "/ipv4" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port \ - + "/v1/bgp/neighbor/" + address[0] + "/local-rib" + af - r = requests.get(url) - local_rib = json.loads(r.text) + print "check of [ " + address + " ]" + af = fab.IPv6 if ":" in address else fab.IPv4 + local_rib = self.ask_gobgp(LOCAL_RIB, address, af) for quagga_config in self.quagga_configs: - # print quagga_config.peer_ip + " : " + address[0] + " | " + quagga_config.ip_version + " : " + address[1] - if quagga_config.peer_ip == address[0] or quagga_config.ip_version != address[1]: + 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'] @@ -126,14 +100,13 @@ class GoBGPIPv6Test(unittest.TestCase): return for address in self.get_neighbor_address(self.gobgp_config): - print "check of [ " + address[0] + " : " + address[1] + " ]" - tn = qaccess.login(address[0]) - if address[1] == "IPv6": - q_rib = qaccess.show_ipv6_rib(tn) - else: - q_rib = qaccess.show_rib(tn) + print "check of [ " + address + " ]" + af = fab.IPv6 if ":" in address else fab.IPv4 + tn = qaccess.login(address) + q_rib = qaccess.show_rib(tn, af) + for quagga_config in self.quagga_configs: - if quagga_config.peer_ip == address[0] or quagga_config.ip_version != address[1]: + if quagga_config.peer_ip == address or quagga_config.ip_version != af: for c_dest in quagga_config.destinations.itervalues(): exist_n = 0 for c_path in c_dest.paths: @@ -153,118 +126,12 @@ class GoBGPIPv6Test(unittest.TestCase): # print "conf : ", c_path.network, c_path.nexthop for q_path in q_rib: # print "quag : ", q_path['Network'], q_path['Next Hop'] - if quagga_config.ip_version != "IPv6": + if quagga_config.ip_version != fab.IPv6: c_path.network = c_path.network.split("/")[0] if c_path.network == q_path['Network'] and c_path.nexthop == q_path['Next Hop']: exist_n += 1 self.assertEqual(exist_n, 1) - def retry_routine_for_stete(self, addresses): - in_prepare_quagga = True - retry_count = 0 - while in_prepare_quagga: - if retry_count != 0: - print "please wait more (" + str(self.wait_per_retry) + " second)" - time.sleep(self.wait_per_retry) - if retry_count >= self.retry_limit: - print "retry limit" - break - retry_count += 1 - success_count = 0 - for address in addresses: - # get neighbor state and remote ip from gobgp connections - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + address[0] - try: - r = requests.get(url) - neighbor = json.loads(r.text) - except Exception: - continue - if neighbor is None: - continue - state = neighbor['info']['bgp_state'] - remote_ip = neighbor['conf']['remote_ip'] - if address[0] == remote_ip and state == "BGP_FSM_ESTABLISHED": - success_count += 1 - if success_count == len(addresses): - in_prepare_quagga = False - time.sleep(self.wait_per_retry) - - def load_gobgp_config(self): - try: - self.gobgp_config = toml.loads(open(self.gobgp_config_file).read()) - except IOError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - # load configration from quagga(bgpd.conf) - def load_quagga_config(self): - dirs = [] - try: - content = os.listdir(self.base_dir) - for item in content: - if "q" != item[0]: - continue - if os.path.isdir(os.path.join(self.base_dir, item)): - dirs.append(item) - except OSError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - for dir in dirs: - config_path = self.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 - - 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) - self.quagga_configs.append(quagga_config) - - # get address of each neighbor from gobpg configration - def get_neighbor_address(self, config): - address = [] - neighbors_config = config['NeighborList'] - for neighbor_config in neighbors_config: - neighbor_ip = neighbor_config['NeighborAddress'] - address_version = "IPv4" - if ":" in neighbor_ip: - address_version = "IPv6" - neighbor_ip_info = [neighbor_ip, address_version] - address.append(neighbor_ip_info) - return address - - def check_load_config(self): - self.load_gobgp_config() - self.load_quagga_config() - 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 c93de865..7621d628 100644 --- a/test/scenario_test/route_server_test.py +++ b/test/scenario_test/route_server_test.py @@ -13,45 +13,26 @@ # See the License for the specific language governing permissions and # 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 gobgp_test import GoBGPTestBase +from gobgp_test import ADJ_RIB_IN, ADJ_RIB_OUT, LOCAL_RIB, GLOBAL_RIB +from gobgp_test import NEIGHBOR from noseplugin import OptionParser from noseplugin import parser_option - -class GoBGPTest(unittest.TestCase): - - gobgp_ip = "10.0.255.1" - gobgp_port = "8080" - base_dir = "/tmp/gobgp/" - gobgp_config_file = "/tmp/gobgp/gobgpd.conf" - gobgp_config = None +class GoBGPTest(GoBGPTestBase): quagga_num = 3 append_quagga = 10 remove_quagga = 10 append_quagga_best = 20 - initial_wait_time = 10 - wait_per_retry = 5 - retry_limit = (60 - initial_wait_time) / wait_per_retry def __init__(self, *args, **kwargs): super(GoBGPTest, self).__init__(*args, **kwargs) - def setUp(self): - self.quagga_configs = [] - # test each neighbor state is turned establish def test_01_neighbor_established(self): print "test_neighbor_established" @@ -72,9 +53,7 @@ class GoBGPTest(unittest.TestCase): for address in addresses: # get neighbor state and remote ip from gobgp connections print "check of [ " + address + " ]" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + address - r = requests.get(url) - neighbor = json.loads(r.text) + neighbor = self.ask_gobgp(NEIGHBOR, address) state = neighbor['info']['bgp_state'] remote_ip = neighbor['conf']['remote_ip'] self.assertEqual(address, remote_ip) @@ -89,11 +68,7 @@ class GoBGPTest(unittest.TestCase): for address in self.get_neighbor_address(self.gobgp_config): print "check of [ " + address + " ]" # get local-rib per peer - af = "/ipv4" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port +\ - "/v1/bgp/neighbor/" + address + "/local-rib" + af - r = requests.get(url) - local_rib = json.loads(r.text) + local_rib = self.ask_gobgp(LOCAL_RIB, address) for quagga_config in self.quagga_configs: if quagga_config.peer_ip == address: @@ -157,9 +132,7 @@ class GoBGPTest(unittest.TestCase): # get neighbor state and remote ip of new quagga print "check of [" + append_quagga_address + " ]" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + append_quagga_address - r = requests.get(url) - neighbor = json.loads(r.text) + neighbor = self.ask_gobgp(NEIGHBOR, append_quagga_address) state = neighbor['info']['bgp_state'] remote_ip = neighbor['conf']['remote_ip'] self.assertEqual(append_quagga_address, remote_ip) @@ -174,11 +147,7 @@ class GoBGPTest(unittest.TestCase): for address in self.get_neighbor_address(self.gobgp_config): print "check of [ " + address + " ]" # get local-rib per peer - af = "/ipv4" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port +\ - "/v1/bgp/neighbor/" + address + "/local-rib" + af - r = requests.get(url) - local_rib = json.loads(r.text) + local_rib = self.ask_gobgp(LOCAL_RIB, address) for quagga_config in self.quagga_configs: if quagga_config.peer_ip == address: @@ -248,9 +217,7 @@ class GoBGPTest(unittest.TestCase): # get neighbor state and remote ip of removed quagga print "check of [" + removed_quagga_address + " ]" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + removed_quagga_address - r = requests.get(url) - neighbor = json.loads(r.text) + neighbor = self.ask_gobgp(NEIGHBOR, removed_quagga_address) state = neighbor['info']['bgp_state'] remote_ip = neighbor['conf']['remote_ip'] self.assertEqual(removed_quagga_address, remote_ip) @@ -268,11 +235,7 @@ class GoBGPTest(unittest.TestCase): print "check of [ " + address + " ]" # get local-rib per peer - af = "/ipv4" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port +\ - "/v1/bgp/neighbor/" + address + "/local-rib" + af - r = requests.get(url) - local_rib = json.loads(r.text) + local_rib = self.ask_gobgp(LOCAL_RIB, address) for quagga_config in self.quagga_configs: if quagga_config.peer_ip == address: @@ -366,148 +329,6 @@ class GoBGPTest(unittest.TestCase): print "check of [ " + check_address + " ]" self.retry_routine_for_bestpath(check_address, target_network, ans_nexthop) - def retry_routine_for_state(self, addresses, allow_state): - in_prepare_quagga = True - retry_count = 0 - while in_prepare_quagga: - if retry_count != 0: - print "please wait more (" + str(self.wait_per_retry) + " second)" - time.sleep(self.wait_per_retry) - if retry_count >= self.retry_limit: - print "retry limit" - break - retry_count += 1 - success_count = 0 - for address in addresses: - # get neighbor state and remote ip from gobgp connections - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port + "/v1/bgp/neighbor/" + address - try: - r = requests.get(url) - neighbor = json.loads(r.text) - 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: - success_count += 1 - if success_count == len(addresses): - in_prepare_quagga = False - time.sleep(self.wait_per_retry) - - # load configration from gobgp(gobgpd.conf) - def retry_routine_for_bestpath(self, check_address, target_network, ans_nexthop): - # get local-rib - rep_nexthop = "" - target_exist = False - af = "/ipv4" - url = "http://" + self.gobgp_ip + ":" + self.gobgp_port +\ - "/v1/bgp/neighbor/" + check_address + "/local-rib" + af - r = requests.get(url) - local_rib = json.loads(r.text) - g_dests = local_rib['Destinations'] - for g_dest in g_dests: - best_path_idx = g_dest['BestPathIdx'] - if target_network == g_dest['Prefix']: - target_exist = True - g_paths = g_dest['Paths'] - idx = 0 - if len(g_paths) < 2: - print "target path has not been bestpath selected yet." - print "please wait more (" + str(self.wait_per_retry) + " second)" - time.sleep(self.wait_per_retry) - 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'] - if str(best_path_idx) == str(idx): - rep_nexthop = g_path['Nexthop'] - idx += 1 - if target_exist is False: - print "target path has not been receive yet." - print "please wait more (" + str(self.wait_per_retry) + " second)" - time.sleep(self.wait_per_retry) - self.retry_routine_for_bestpath(check_address, target_network, ans_nexthop) - return - self.assertEqual(ans_nexthop, rep_nexthop) - - def load_gobgp_config(self): - try: - self.gobgp_config = toml.loads(open(self.gobgp_config_file).read()) - except IOError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - # load configration from quagga(bgpd.conf) - def load_quagga_config(self): - dirs = [] - try: - content = os.listdir(self.base_dir) - for item in content: - if "q" != item[0]: - continue - if os.path.isdir(os.path.join(self.base_dir, item)): - dirs.append(item) - except OSError, (errno, strerror): - print "I/O error(%s): %s" % (errno, strerror) - - for dir in dirs: - config_path = self.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) - self.quagga_configs.append(quagga_config) - - # get address of each neighbor from gobpg configration - def get_neighbor_address(self, config): - address = [] - neighbors_config = config['NeighborList'] - for neighbor_config in neighbors_config: - neighbor_ip = neighbor_config['NeighborAddress'] - address.append(neighbor_ip) - return address - - def check_load_config(self): - self.load_gobgp_config() - self.load_quagga_config() - 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." -- cgit v1.2.3