# 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 tempest-dsvm-networking-ovn
# OpenStack CI job that runs DevStack + tempest.  It is also used by the
# rally job. You can find the CI job configuration here:
#
# http://git.openstack.org/cgit/openstack-infra/project-config/tree/jenkins/jobs/networking-ovn.yaml
#

OVN_OPTS=$@

OVERRIDE_ENABLED_SERVICES=key,n-api,n-cpu,n-cond,n-sch,n-crt,n-cauth,n-obj,n-api-meta,placement-api,g-api,g-reg,c-sch,c-api,c-vol,rabbit,mysql,dstat,ovn-northd,ovn-controller,q-svc,networking-ovn-metadata-agent,br-ex-tcpdump,br-int-flows,q-trunk
export OVERRIDE_ENABLED_SERVICES

if [ -z "${RALLY_SCENARIO}" ] ; then
    # Only include tempest if this is not a rally job.
    export OVERRIDE_ENABLED_SERVICES=${OVERRIDE_ENABLED_SERVICES},tempest
fi
export DEVSTACK_LOCAL_CONFIG+=$'\n'"Q_USE_PROVIDERNET_FOR_PUBLIC=True"
export DEVSTACK_LOCAL_CONFIG+=$'\n'"PHYSICAL_NETWORK=public"

if [[ "${OVN_OPTS}" == *"latest-release"* ]] ; then
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"OVN_BRANCH=branch-2.8"
elif [[ "${OVN_OPTS}" == *"master"* ]] ; then
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"OVN_BRANCH=master"
else
    echo "No ovs branch specified, using the default from the devstack plugin"
fi

if [[ "$DEVSTACK_GATE_TOPOLOGY" == "multinode" ]] ; then
    # NOTE(rtheis): Multinode does not require creating an OVN L3 public network.
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"OVN_L3_CREATE_PUBLIC_NETWORK=False"

    # NOTE(rtheis): Configure the enabled services on the compute node.
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"ENABLED_SERVICES=n-cpu,dstat,c-vol,c-bak,ovn-controller"

    # NOTE(rtheis): Configure OVN on the compute node.
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"OVN_SB_REMOTE=tcp:\$SERVICE_HOST:6642"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"OVN_NB_REMOTE=tcp:\$SERVICE_HOST:6641"

    # NOTE(rtheis): Since we are overriding the enabled services, we must
    # also configure the database and rabbit services on the compute node.
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"DATABASE_HOST=\$SERVICE_HOST"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"DATABASE_TYPE=mysql"
    export DEVSTACK_SUBNODE_CONFIG+=$'\n'"RABBIT_HOST=\$SERVICE_HOST"
else
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"OVN_L3_CREATE_PUBLIC_NETWORK=True"
fi

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

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

# TODO(numans): Remove this if once OVS 2.9 branch is created.
if [[ "${OVN_OPTS}" == *"latest-release"* ]] ; then
    r="$r|(?:tempest\.scenario\.test_network_v6.*)"
fi

# exclude things that just aren't enabled with OVN
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_metering_extensions.*)"
r="$r|(?:tempest\.thirdparty\.boto\.test_s3.*)"

# TODO(dalvarez): remove this exclusion when https://bugs.launchpad.net/tempest/+bug/1728886 is fixed
r="$r|(?:tempest\.scenario\.test_network_basic_ops\.TestNetworkBasicOps\.test_port_security_macspoofing_port)"

# exclude some unrelated stuff to make networking-ovn targeted runs go faster
r="$r|(?:tempest\.api\.identity*)"
r="$r|(?:tempest\.api\.image*)"
r="$r|(?:tempest\.api\.volume*)"
r="$r|(?:tempest\.api\.compute\.images*)"
r="$r|(?:tempest\.api\.compute\.keypairs*)"
r="$r|(?:tempest\.api\.compute\.certificates*)"
r="$r|(?:tempest\.api\.compute\.flavors*)"
r="$r|(?:tempest\.api\.compute\.test_quotas*)"
r="$r|(?:tempest\.api\.compute\.test_versions*)"
r="$r|(?:tempest\.api\.compute\.volumes*)"
r="$r|(?:tempest\.api\.compute\.admin\.test_flavor*)"
r="$r|(?:tempest\.api\.compute\.admin\.test_volume*)"
r="$r|(?:tempest\.api\.compute\.admin\.test_hypervisor*)"
r="$r|(?:tempest\.api\.compute\.admin\.test_aggregate*)"
r="$r|(?:tempest\.api\.compute\.admin\.test_quota*)"

r="$r|(?:tempest\.scenario\.test_volume*)"

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

if [[ "${OVN_OPTS}" == *"neutron-api-scenario-tests"* ]] ; then
    r="$r((^neutron_tempest_plugin\.api)|(^neutron_tempest_plugin\.scenario)).*$"
    export DEVSTACK_LOCAL_CONFIG+=$'\n'"enable_plugin neutron-tempest-plugin git://git.openstack.org/openstack/neutron-tempest-plugin"
    export DEVSTACK_GATE_TEMPEST=1
    export DEVSTACK_GATE_TEMPEST_ALL_PLUGINS=1
else
    # only run tempest.api/scenario/thirdparty tests (part of the default for 'full')
    r="$r(tempest\.(api|scenario|thirdparty)).*$"
fi

if [ -z $DEVSTACK_GATE_GRENADE ]; then
    # Do not run the tempest test cases on grenade jobs. By not setting this,
    # we still do run the tempest smoke tests. The pre-upgraded stack too runs
    # just the smoke tests. This is how openstack/neutron runs its post-upgrade
    # tempest tests.
    export DEVSTACK_GATE_TEMPEST_REGEX="$r"
fi
