# Copyright 2019 Red Hat, Inc.
#
# 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.


testinfra_hosts = ['mirror01.openafs.provider.opendev.org',
                   'mirror02.openafs.provider.opendev.org']


def test_apache(host):
    apache = host.service('apache2')
    assert apache.is_running

def test_base_mirror(host):
    # BaseMirror
    for addr in host.addr(host.backend.host).ip_addresses:
        cmd = host.run("wget --no-check-certificate -qO- https://%s/" % addr)
        assert '<a href="debian/">' in cmd.stdout

        cmd = host.run("wget -qO- http://%s/" % addr)
        assert '<a href="debian/">' in cmd.stdout

def test_proxy_mirror(host):
    # ProxyMirror
    for addr in host.addr(host.backend.host).ipv4_addresses:
        cmd = host.run("wget --no-check-certificate -qO- "
                       "https://%s:4443/pypi/simple/setuptools" % addr)
        assert 'setuptools' in cmd.stdout

        cmd = host.run("wget -qO- "
                       "http://%s:8080/pypi/simple/setuptools" % addr)
        assert 'setuptools' in cmd.stdout

    # split the test cases so that we can escape the ipv6 addrs properly
    for addr in host.addr(host.backend.host).ipv6_addresses:
        cmd = host.run("wget --no-check-certificate -qO- "
                       "https://[%s]:4443/pypi/simple/setuptools" % addr)
        assert 'setuptools' in cmd.stdout

        cmd = host.run("wget -qO- "
                       "http://[%s]:8080/pypi/simple/setuptools" % addr)
        assert 'setuptools' in cmd.stdout

def test_dockerv2_mirror(host):
    # Dockerv2Mirror
    for addr in host.addr(host.backend.host).ipv4_addresses:
        cmd = host.run("wget --no-check-certificate -O- "
                       "https://%s:4445/v2/" % addr)
        assert '401 Unauthorized' in cmd.stderr

        cmd = host.run("wget -O- http://%s:8082/v2/" %addr)
        assert '401 Unauthorized' in cmd.stderr

    for addr in host.addr(host.backend.host).ipv6_addresses:
        cmd = host.run("wget --no-check-certificate -O- "
                       "https://[%s]:4445/v2/" % addr)
        assert '401 Unauthorized' in cmd.stderr

        cmd = host.run("wget -O- http://[%s]:8082/v2/" %addr)
        assert '401 Unauthorized' in cmd.stderr

def test_quay_mirror(host):
    # QuayRegistryMirror
    for addr in host.addr(host.backend.host).ipv4_addresses:
        cmd = host.run("wget --no-check-certificate -qO- "
                       "https://%s:4447/" % addr)
        assert 'quay' in cmd.stdout

        cmd = host.run("wget -qO- http://%s:8084/" % addr)
        assert 'quay' in cmd.stdout

    for addr in host.addr(host.backend.host).ipv6_addresses:
        cmd = host.run("wget --no-check-certificate -qO- "
                       "https://[%s]:4447/" % addr)
        assert 'quay' in cmd.stdout

        cmd = host.run("wget -qO- http://[%s]:8084/" % addr)
        assert 'quay' in cmd.stdout

# TODO test RHRegistryMirror