# 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.

#
# This script is executed in the OpenStack CI job that runs DevStack + tempest.
# You can find the CI job configuration here:
#
# http://git.openstack.org/cgit/openstack-infra/project-config/tree/jenkins/jobs/networking-odl.yaml
#

# TODO(yamahata): tempest test is run serially at the moment and
# we're occasionally hitting timeout of 120 mins. For now as work around,
# lengthen timeout a bit.
# In near future(Ocata cycle) after migrating to new ODL netvirt(conserves),
# parallel execution should be enabled and remove this work around.
if [[ -z "${RALLY_SCENARIO}" && -z "${GRENADE_PLUGINRC}" ]] ; then
   export BUILD_TIMEOUT=180
   export DEVSTACK_GATE_TIMEOUT=$(expr $BUILD_TIMEOUT - $DEVSTACK_GATE_TIMEOUT_BUFFER)
fi

export OVERRIDE_ENABLED_SERVICES=c-api,c-bak,c-sch,c-vol,cinder,dstat,g-api,g-reg,key,mysql,n-api,n-cond,n-cpu,n-crt,n-obj,n-sch,q-dhcp,q-meta,q-svc,quantum,rabbit,placement-api
if [ -z "${RALLY_SCENARIO}" ] ; then
    # Only include tempest if this is not a rally job, As running tempest in Rally is likely to cause failure
    export OVERRIDE_ENABLED_SERVICES=${OVERRIDE_ENABLED_SERVICES},tempest
fi

# NOTE(manjeets) To prevent create of public network twice
if [[ "$DEVSTACK_GATE_TOPOLOGY" == "multinode" ]] ; then

    # NOTE(manjeets) Temporarily disabling LM test due to bug 1643678
    # https://bugs.launchpad.net/networking-odl/+bug/1643678
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"LIVE_MIGRATION_AVAILABLE=False"
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"USE_BLOCK_MIGRATION_FOR_LIVE_MIGRATION=False"
    # DEVSTACK_GATE_NEUTRON_DVR in devstack-gate set Q_DVR_MODE as dvr_snat
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"Q_DVR_MODE=legacy"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"Q_DVR_MODE=legacy"

    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"disable_all_services"
    # use non-exist q-odl to workaroud post-config failure in neutron configuration
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"ENABLED_SERVICES=n-cpu,dstat,c-vol,c-bak,q-odl"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"RABBIT_HOST=\$SERVICE_HOST"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"ODL_MODE=compute"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"enable_plugin networking-odl https://git.openstack.org/openstack/networking-odl"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"DATABASE_TYPE=mysql"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"LIBVIRT_TYPE=qemu"
fi

# For V2 driver some test are failing randomly so run them in a separate job
if [[ "$ODL_TEST_SUFFIX" == "-nv" && "$ODL_GATE_V2DRIVER" == "v2driver" && -z "$ODL_GATE_SERVICE_PROVIDER" ]]; then
    r="^tempest\.scenario\.test_network_advanced_server_ops\.TestNetworkAdvancedServerOps.*$"
    r="$r|^tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_network_basic_ops.*$"
    r="$r|^tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_mtu_sized_frames.*$"
    r="$r|^tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_port_security_macspoofing_port.*$"
    r="$r|^tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_subnet_details.*$"
    r="$r|^tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_in_tenant_traffic.*$"
    r="$r|^tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_multiple_security_groups.*$"
    r="$r|^tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_port_update_new_security_group.*$"
    export DEVSTACK_GATE_TEMPEST_REGEX="$r"
    return 0
fi

# Begin list of exclusions.
r="^(?!.*"

# exclude the slow tag (part of the default for 'full')
r="$r(?:.*\[.*\bslow\b.*\])"

# exclude things that just aren't enabled:
r="$r|(?:tempest\.api\.network\.admin\.test_quotas\.QuotasTest\.test_lbaas_quotas.*)"
r="$r|(?:tempest\.api\.network\.test_load_balancer.*)"
r="$r|(?:tempest\.scenario\.test_load_balancer.*)"
r="$r|(?:tempest\.api\.network\.admin\.test_load_balancer.*)"
r="$r|(?:tempest\.api\.network\.admin\.test_lbaas.*)"
r="$r|(?:tempest\.api\.network\.test_fwaas_extensions.*)"
r="$r|(?:tempest\.api\.network\.test_vpnaas_extensions.*)"
r="$r|(?:tempest\.api\.network\.test_metering_extensions.*)"
r="$r|(?:tempest\.thirdparty\.boto\.test_s3.*)"

# exclude stuff we're less likely to break because i'm impatient
r="$r|(?:tempest\.api\.identity.*)"
r="$r|(?:tempest\.api\.image.*)"
r="$r|(?:tempest\.api\.volume.*)"

# unsupported features
# ODL legacy netvirt doesn't support ipv6
r="$r|(?:tempest\.scenario\.test_network_v6\.TestGettingAddress.*)"

# V2 driver is finicky with these tests, run them in a separate job.
if [[ "$ODL_GATE_V2DRIVER" == "v2driver" && -z "$ODL_GATE_SERVICE_PROVIDER" ]]; then
    r="$r|(?:tempest\.scenario\.test_network_advanced_server_ops\.TestNetworkAdvancedServerOps.*)"
    r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_network_basic_ops.*)"
    r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_mtu_sized_frames.*)"
    r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_port_security_macspoofing_port.*)"
    r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_subnet_details.*)"
    r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_in_tenant_traffic.*)"
    r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_multiple_security_groups.*)"
    r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_port_update_new_security_group.*)"
fi

# Current list of failing tests that need to be triaged, have bugs filed, and
# fixed as appropriate.
# (none)

if [[ "${ODL_RELEASE_BASE}" == "beryllium-snapshot" ]]; then
   # Beryllium release has bug on security group and there is no plan
   # for new release. Its last release is Beryllium-SR4.
   r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops.*)"
fi

# TODO(yamahata): fix bugs and remove those tests from here
# BUG: https://bugs.launchpad.net/networking-odl/+bug/1642158
# legacy netvirt ignores admin-state-up state for network/port
r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_update_instance_port_admin_state.*)"
r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_update_router_admin_state.*)"

# BUG: https://bugs.launchpad.net/networking-odl/+bug/1643033
# stateful security group: conntracking needs to be enabled
r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_hotplug_nic.*)"
r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_cross_tenant_traffic.*)"
r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_port_security_disable_security_group.*)"

# BUG: https://bugs.launchpad.net/networking-odl/+bug/1672462
# In order to unblock pending patches. Needs to be fixed as soon as possible
r="$r|(?:tempest\.scenario\.test_network_advanced_server_ops\.TestNetworkAdvancedServerOps\.test_server_connectivity_reboot.*)"
r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_network_basic_ops.*)"
r="$r|(?:tempest\.scenario\.test_security_groups_basic_ops\.TestSecurityGroupsBasicOps\.test_in_tenant_traffic.*)"
r="$r|(?:tempest\.scenario\.test_volume_boot_pattern\.TestVolumeBootPattern\.test_volume_boot_pattern.*)"

# BUG: https://bugs.launchpad.net/networking-odl/+bug/1656129
# exluding some tests temporarily
if [ -n $DEVSTACK_GATE_GRENADE ]; then
    # Disable some tempest tests temporarily on
    # grenade job
    r="$r|(?:tempest\.scenario\.test_encrypted_cinder_volumes\.TestEncryptedCinderVolumes\.test_encrypted_cinder_volumes_cryptsetup.*)"
    r="$r|(?:tempest\.scenario\.test_encrypted_cinder_volumes\.TestEncryptedCinderVolumes\.test_encrypted_cinder_volumes_luks.*)"
    r="$r|(?:tempest\.scenario\.test_minimum_basic\.TestMinimumBasicScenario\.test_minimum_basic_scenario.*)"
fi

# End list of exclusions.
r="$r)"

# only run tempest.api/scenario/thirdparty tests (part of the default for 'full')
r="$r(tempest\.(api|scenario|thirdparty)).*$"

export DEVSTACK_GATE_TEMPEST_REGEX="$r"
