<commit_before># Copyright (c) 2014-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_relative 'ciscotest'
require_relative '../lib/cisco_node_utils/interface'
require_relative '../lib/cisco_node_utils/vtp'

include Cisco

# TestInterfaceSwitchport - Minitest for switchport config by Interface class.
class TestInterfaceSwitchport < CiscoTestCase
  DEFAULT_IF_SWITCHPORT_ALLOWED_VLAN = '1-4094'
  DEFAULT_IF_SWITCHPORT_NATIVE_VLAN = 1

  def setup
    super
    config('no feature vtp', 'no feature interface-vlan')
  end

  def interface_ethernet_default(ethernet_id)
    config("default interface ethernet #{ethernet_id}")
  end

  def cmd_ref_switchport_autostate_exclude
    ref = cmd_ref.lookup('interface',
                         'switchport_autostate_exclude')
    assert(ref, 'Error, reference not found for switchport_autostate_exclude')
    ref
  end

  # Decides whether to check for a raised Exception or an equal value.
  def assert_result(expected_result, err_msg, &block)
    if expected_result.is_a? Class
      assert_raises(expected_result, &block)
    else
      value = block.call
      assert_equal(expected_result, value, err_msg)
    end
  end

  def system_default_switchport(state='')
    config("#{state} system default switchport")
  end

  def system_default_switchport_shutdown(state='')
    config("#{state} system default switchport shutdown")
  end

  def test_switchport_vtp_disabled_feature_enabled
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    vtp.destroy
  end

  def test_switchport_vtp_disabled_feature_disabled_eth1_1
    interface = Interface.new(interfaces[0])
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
  end

  def test_switchport_vtp_disabled_feature_disabled_mgmt0
    interface = Interface.new('mgmt0')
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
  end

  def test_switchport_vtp_disabled_unsupported_mode_disabled
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :disabled
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_unsupported_mode_fex
    begin
      interface = Interface.new(interfaces[0])
      interface.switchport_mode = :fex_fabric
      refute(interface.switchport_vtp,
             'Error: interface, access, vtp not disabled')
    rescue RuntimeError => e
      msg = "[#{interfaces[0]}] switchport_mode is not supported " \
            'on this interface'
      assert_equal(msg.downcase, e.message)
    end
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_enabled_access
    skip('Cannot apply VTP config because port mode is access') if
      node.product_id =~ /N(5|6|7)/
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access
    config("interface ethernet #{interfaces_id[0]}", 'vtp')

    assert(interface.switchport_vtp,
           'Error: interface, access, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_access
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access
    config("interface ethernet #{interfaces_id[0]}", 'no vtp')

    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_enabled_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :trunk
    config("interface ethernet #{interfaces_id[0]}", 'vtp')

    assert(interface.switchport_vtp,
           'Error: interface, trunk, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :trunk
    refute(interface.switchport_vtp,
           'Error: interface, trunk, vtp not disabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_default_access
    skip('Cannot apply VTP config because port mode is access') if
      node.product_id =~ /N(5|6|7)/
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(1) mode :access, vtp should be default (false)')

    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error:(2) mode :access, vtp should be true')

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(3) mode :access, vtp should be default (false)')

    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_default_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :trunk
    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(1) mode :trunk, vtp should be default (false)')

    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error:(2) mode :trunk, vtp should be true')

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(3) mode :trunk, vtp should be default (false)')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_true_access
    skip('Cannot apply VTP config because port mode is access') if
      node.product_id =~ /N(5|6|7)/
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :access
    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error: interface, access, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end
<commit_msg>Change skip message and make helper method<commit_after># Copyright (c) 2014-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_relative 'ciscotest'
require_relative '../lib/cisco_node_utils/interface'
require_relative '../lib/cisco_node_utils/vtp'

include Cisco

# TestInterfaceSwitchport - Minitest for switchport config by Interface class.
class TestInterfaceSwitchport < CiscoTestCase
  DEFAULT_IF_SWITCHPORT_ALLOWED_VLAN = '1-4094'
  DEFAULT_IF_SWITCHPORT_NATIVE_VLAN = 1

  def setup
    super
    config('no feature vtp', 'no feature interface-vlan')
  end

  def interface_ethernet_default(ethernet_id)
    config("default interface ethernet #{ethernet_id}")
  end

  def cmd_ref_switchport_autostate_exclude
    ref = cmd_ref.lookup('interface',
                         'switchport_autostate_exclude')
    assert(ref, 'Error, reference not found for switchport_autostate_exclude')
    ref
  end

  # Decides whether to check for a raised Exception or an equal value.
  def assert_result(expected_result, err_msg, &block)
    if expected_result.is_a? Class
      assert_raises(expected_result, &block)
    else
      value = block.call
      assert_equal(expected_result, value, err_msg)
    end
  end

  def platform_supports_vtp_switchport_access?
    skip('Platform does not support VTP when switchport mode is access') if
      node.product_id =~ /N(5|6|7)/
  end

  def system_default_switchport(state='')
    config("#{state} system default switchport")
  end

  def system_default_switchport_shutdown(state='')
    config("#{state} system default switchport shutdown")
  end

  def test_switchport_vtp_disabled_feature_enabled
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    vtp.destroy
  end

  def test_switchport_vtp_disabled_feature_disabled_eth1_1
    interface = Interface.new(interfaces[0])
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
  end

  def test_switchport_vtp_disabled_feature_disabled_mgmt0
    interface = Interface.new('mgmt0')
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
  end

  def test_switchport_vtp_disabled_unsupported_mode_disabled
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :disabled
    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_unsupported_mode_fex
    begin
      interface = Interface.new(interfaces[0])
      interface.switchport_mode = :fex_fabric
      refute(interface.switchport_vtp,
             'Error: interface, access, vtp not disabled')
    rescue RuntimeError => e
      msg = "[#{interfaces[0]}] switchport_mode is not supported " \
            'on this interface'
      assert_equal(msg.downcase, e.message)
    end
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_enabled_access
    platform_supports_vtp_switchport_access?
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access
    config("interface ethernet #{interfaces_id[0]}", 'vtp')

    assert(interface.switchport_vtp,
           'Error: interface, access, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_access
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access
    config("interface ethernet #{interfaces_id[0]}", 'no vtp')

    refute(interface.switchport_vtp,
           'Error: interface, access, vtp not disabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_enabled_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :trunk
    config("interface ethernet #{interfaces_id[0]}", 'vtp')

    assert(interface.switchport_vtp,
           'Error: interface, trunk, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_switchport_vtp_disabled_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :trunk
    refute(interface.switchport_vtp,
           'Error: interface, trunk, vtp not disabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_default_access
    platform_supports_vtp_switchport_access?
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])
    interface.switchport_mode = :access

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(1) mode :access, vtp should be default (false)')

    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error:(2) mode :access, vtp should be true')

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(3) mode :access, vtp should be default (false)')

    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_default_trunk
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :trunk
    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(1) mode :trunk, vtp should be default (false)')

    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error:(2) mode :trunk, vtp should be true')

    interface.switchport_vtp = interface.default_switchport_vtp
    refute(interface.switchport_vtp,
           'Error:(3) mode :trunk, vtp should be default (false)')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

  def test_set_switchport_vtp_true_access
    platform_supports_vtp_switchport_access?
    vtp = Vtp.new(true)
    interface = Interface.new(interfaces[0])

    interface.switchport_mode = :access
    interface.switchport_vtp = true
    assert(interface.switchport_vtp,
           'Error: interface, access, vtp not enabled')
    vtp.destroy
    interface_ethernet_default(interfaces_id[0])
  end

{'fraction_non_alphanumeric': 0.08523016507890335, 'fraction_numerical': 0.008435750127263471, 'mean_word_length': 3.9202146690518784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '5697135', 'n_tokens_mistral': 4408, 'n_tokens_neox': 4260, 'n_words': 981}