<commit_before>#
#

# Copyright (C) 2013 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""QA tests for networks.

"""

import qa_config
import qa_utils

from qa_utils import AssertCommand


def GetNonexistentNetworks(count):
  """Gets network names which shouldn't exist on the cluster.

  @param count: Number of networks to get
  @rtype: integer

  """
  return qa_utils.GetNonexistentEntityNames(count, "networks", "network")


def TestNetworkAddRemove():
  """gnt-network add/remove"""
  (network1, network2) = GetNonexistentNetworks(2)

  # Add some networks of different sizes.
  # Note: Using RFC5737 addresses.
  AssertCommand(["gnt-network", "add", "--network", "192.0.2.0/30", network1])
  AssertCommand(["gnt-network", "add", "--network", "198.51.100.0/24",
                 network2])
  # Try to add a network with an existing name.
  AssertCommand(["gnt-network", "add", "--network", "192.168.127.12/24", network2],
                fail=True)

  AssertCommand(["gnt-network", "list"])

  AssertCommand(["gnt-network", "remove", network1])
  AssertCommand(["gnt-network", "remove", network2])

  AssertCommand(["gnt-network", "list"])


def TestNetworkConnect():
  """gnt-network connect/disconnect"""
  (group1, ) = qa_utils.GetNonexistentGroups(1)
  (network1, ) = GetNonexistentNetworks(1)

  default_mode = "bridged"
  default_link = "xen-br0"
  nicparams = qa_config.get("default-nicparams")
  if nicparams:
    mode = nicparams.get("mode", default_mode)
    link = nicparams.get("link", default_link)
  else:
    mode = default_mode
    link = default_link

  AssertCommand(["gnt-group", "add", group1])
  AssertCommand(["gnt-network", "add", "--network", "192.0.2.0/24", network1])

  AssertCommand(["gnt-network", "connect", network1, mode, link, group1])
  AssertCommand(["gnt-network", "list"])
  AssertCommand(["gnt-network", "disconnect", network1, group1])

  AssertCommand(["gnt-group", "remove", group1])
  AssertCommand(["gnt-network", "remove", network1])<commit_msg>Use the generic query field test for network QA<commit_after>#
#

# Copyright (C) 2013 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""QA tests for networks.

"""

import qa_config
import qa_utils

from ganeti import query

from qa_utils import AssertCommand


def TestNetworkList():
  """gnt-network list"""
  qa_utils.GenericQueryTest("gnt-network", query.NETWORK_FIELDS.keys())


def TestNetworkListFields():
  """gnt-network list-fields"""
  qa_utils.GenericQueryFieldsTest("gnt-network", query.NETWORK_FIELDS.keys())


def GetNonexistentNetworks(count):
  """Gets network names which shouldn't exist on the cluster.

  @param count: Number of networks to get
  @rtype: integer

  """
  return qa_utils.GetNonexistentEntityNames(count, "networks", "network")


def TestNetworkAddRemove():
  """gnt-network add/remove"""
  (network1, network2) = GetNonexistentNetworks(2)

  # Add some networks of different sizes.
  # Note: Using RFC5737 addresses.
  AssertCommand(["gnt-network", "add", "--network", "192.0.2.0/30", network1])
  AssertCommand(["gnt-network", "add", "--network", "198.51.100.0/24",
                 network2])
  # Try to add a network with an existing name.
  AssertCommand(["gnt-network", "add", "--network", "192.168.127.12/24", network2],
                fail=True)

  TestNetworkList()
  TestNetworkListFields()

  AssertCommand(["gnt-network", "remove", network1])
  AssertCommand(["gnt-network", "remove", network2])

  TestNetworkList()


def TestNetworkConnect():
  """gnt-network connect/disconnect"""
  (group1, ) = qa_utils.GetNonexistentGroups(1)
  (network1, ) = GetNonexistentNetworks(1)

  default_mode = "bridged"
  default_link = "xen-br0"
  nicparams = qa_config.get("default-nicparams")
  if nicparams:
    mode = nicparams.get("mode", default_mode)
    link = nicparams.get("link", default_link)
  else:
    mode = default_mode
    link = default_link

  AssertCommand(["gnt-group", "add", group1])
  AssertCommand(["gnt-network", "add", "--network", "192.0.2.0/24", network1])

  AssertCommand(["gnt-network", "connect", network1, mode, link, group1])

  TestNetworkList()

  AssertCommand(["gnt-network", "disconnect", network1, group1])

  AssertCommand(["gnt-group", "remove", group1])
  AssertCommand(["gnt-network", "remove", network1])
{'fraction_non_alphanumeric': 0.13223287184948526, 'fraction_numerical': 0.02875399361022364, 'mean_word_length': 4.470873786407767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '7439358', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1733, 'n_words': 541}