<commit_before>#!/usr/bin/env ruby
# BgpNeighbor Unit Tests
#
# <NAME>, August 2015
#
# Copyright (c) 2015 Cisco and/or its affiliates.
#
# 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.

require File.expand_path("../ciscotest", __FILE__)
require File.expand_path("../../lib/cisco_node_utils/bgp", __FILE__)
require File.expand_path("../../lib/cisco_node_utils/bgp_neighbor", __FILE__)

class TestRouterBgpNeighbor < CiscoTestCase
  @@asn = 55
  @@addr = "1.1.1.1"

  def setup
    # Disable feature bgp before each test to ensure we
    # are starting with a clean slate for each test.
    super
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("feature bgp")
    @device.cmd("router bgp 55")
    @device.cmd("end")
    node.cache_flush
  end

  def teardown
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("end")
    node.cache_flush
  end

  def get_bgpneighbor_match_line(addr, vrf='default')
    if vrf == 'default'
      cmd = "show run bgp all | section 'router bgp' | no-more"
    else
      cmd = "show run bgp all | section 'vrf #{vrf}' | no-more"
    end
    CiscoLogger.debug("matching #{addr} with vrf #{vrf}")
    s = @device.cmd("#{cmd}")
    line = /neighbor #{addr}/.match(s)
    CiscoLogger.debug(line)
    line
  end

  def test_bgpneighbor_collection_empty
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("end")
    node.cache_flush
    neighbors = BgpNeighbor.neighbors
    assert_empty(neighbors, "BGP neighbor collection is not empty")
  end

  def test_bgpneighbor_collection_not_empty
    @device.cmd("configure terminal")
    @device.cmd("router bgp 55")
    @device.cmd("neighbor 1.1.1.1")
    @device.cmd("vrf red")
    @device.cmd("neighbor 1.1.1.2")
    @device.cmd("neighbor 1.1.1.3")
    @device.cmd("vrf blue")
    @device.cmd("end")
    node.cache_flush
    neighbors = BgpNeighbor.neighbors
    refute_empty(neighbors, "BGP neighbor collection is empty")
    # validate the collection
    neighbors.each do |asnum, vrfs|
      vrfs.each do |vrf, neighbors|
          if vrf == 'default'
            assert_equal(1, neighbors.size)
          else
            assert_equal(2, neighbors.size)
          end
          neighbors.each {|addr, neighbor|
             assert_equal(vrf, neighbor.vrf)
             line = get_bgpneighbor_match_line(neighbor.address, vrf)
             refute_nil(line)
          }
      end
    end
  end

  def test_neighbor_create_destroy
    address = { "1.1.1.1" => "1.1.1.1",
                "2.2.2.2/24" => "2.2.2.0/24",
                "fdf8:f53e:61e4::18" => "fdf8:f53e:61e4::18",
                "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b/64" => "2000:123:38::/64", }
    vrfs = %w(default red)
    vrfs.each {|vrf|
      address.each { |addr, expected_addr|
        neighbor = BgpNeighbor.new(@@asn, vrf, addr)
        line = get_bgpneighbor_match_line(expected_addr, vrf)
        refute_nil(line, "Error: failed to create bgp neighbor #{addr}")
        neighbor.destroy
        line = get_bgpneighbor_match_line(expected_addr, vrf)
        assert_nil(line, "Error: failed to delete bgp neighbor #{addr}")
      }
    }
  end

  def test_neighbor_set_get_description
    %w(default test_vrf).each do |vrf|
      neighbor = BgpNeighbor.new(@@asn, vrf, @@addr)
      description = "tested by mini test for vrf #{vrf}"
      neighbor.description = description
      assert_equal(description, neighbor.description)
      description = " "
      neighbor.description = description
      assert(neighbor.description.empty?)
      description = neighbor.default_description
      assert_equal(neighbor.description, neighbor.default_description)<commit_msg>A quick fix for test_neighbor_set_get_description to removed unnecessary variable<commit_after>#!/usr/bin/env ruby
# BgpNeighbor Unit Tests
#
# <NAME>, August 2015
#
# Copyright (c) 2015 Cisco and/or its affiliates.
#
# 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.

require File.expand_path("../ciscotest", __FILE__)
require File.expand_path("../../lib/cisco_node_utils/bgp", __FILE__)
require File.expand_path("../../lib/cisco_node_utils/bgp_neighbor", __FILE__)

class TestRouterBgpNeighbor < CiscoTestCase
  @@asn = 55
  @@addr = "1.1.1.1"

  def setup
    # Disable feature bgp before each test to ensure we
    # are starting with a clean slate for each test.
    super
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("feature bgp")
    @device.cmd("router bgp 55")
    @device.cmd("end")
    node.cache_flush
  end

  def teardown
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("end")
    node.cache_flush
  end

  def get_bgpneighbor_match_line(addr, vrf='default')
    if vrf == 'default'
      cmd = "show run bgp all | section 'router bgp' | no-more"
    else
      cmd = "show run bgp all | section 'vrf #{vrf}' | no-more"
    end
    CiscoLogger.debug("matching #{addr} with vrf #{vrf}")
    s = @device.cmd("#{cmd}")
    line = /neighbor #{addr}/.match(s)
    CiscoLogger.debug(line)
    line
  end

  def test_bgpneighbor_collection_empty
    @device.cmd("configure terminal")
    @device.cmd("no feature bgp")
    @device.cmd("end")
    node.cache_flush
    neighbors = BgpNeighbor.neighbors
    assert_empty(neighbors, "BGP neighbor collection is not empty")
  end

  def test_bgpneighbor_collection_not_empty
    @device.cmd("configure terminal")
    @device.cmd("router bgp 55")
    @device.cmd("neighbor 1.1.1.1")
    @device.cmd("vrf red")
    @device.cmd("neighbor 1.1.1.2")
    @device.cmd("neighbor 1.1.1.3")
    @device.cmd("vrf blue")
    @device.cmd("end")
    node.cache_flush
    neighbors = BgpNeighbor.neighbors
    refute_empty(neighbors, "BGP neighbor collection is empty")
    # validate the collection
    neighbors.each do |asnum, vrfs|
      vrfs.each do |vrf, neighbors|
          if vrf == 'default'
            assert_equal(1, neighbors.size)
          else
            assert_equal(2, neighbors.size)
          end
          neighbors.each {|addr, neighbor|
             assert_equal(vrf, neighbor.vrf)
             line = get_bgpneighbor_match_line(neighbor.address, vrf)
             refute_nil(line)
          }
      end
    end
  end

  def test_neighbor_create_destroy
    address = { "1.1.1.1" => "1.1.1.1",
                "2.2.2.2/24" => "2.2.2.0/24",
                "fdf8:f53e:61e4::18" => "fdf8:f53e:61e4::18",
                "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b/64" => "2000:123:38::/64", }
    vrfs = %w(default red)
    vrfs.each {|vrf|
      address.each { |addr, expected_addr|
        neighbor = BgpNeighbor.new(@@asn, vrf, addr)
        line = get_bgpneighbor_match_line(expected_addr, vrf)
        refute_nil(line, "Error: failed to create bgp neighbor #{addr}")
        neighbor.destroy
        line = get_bgpneighbor_match_line(expected_addr, vrf)
        assert_nil(line, "Error: failed to delete bgp neighbor #{addr}")
      }
    }
  end

  def test_neighbor_set_get_description
    %w(default test_vrf).each do |vrf|
      neighbor = BgpNeighbor.new(@@asn, vrf, @@addr)
      description = "tested by mini test for vrf #{vrf}"
      neighbor.description = description
      assert_equal(description, neighbor.description)
      neighbor.description = " "
      assert(neighbor.description.empty?)
      neighbor.description = neighbor.default_description
      assert_equal(neighbor.description, neighbor.default_description)
{'fraction_non_alphanumeric': 0.11995766200164648, 'fraction_numerical': 0.024461954604257322, 'mean_word_length': 3.0360702420503083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 16, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5294676', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2816, 'n_words': 699}