import textwrap

import pytest

from depl import config


def validate(tmpdir, code, fail=False, hosts=(), pool=None):
    p = tmpdir.join("default.yml")
    p.write(textwrap.dedent(code))
    if fail:
        with pytest.raises(config.ValidationError):
            config.Config(str(p), hosts, pool)
    else:
        return config.Config(str(p), hosts, pool)


def servers_to_str(tmpdir, yml, hosts=()):
    return [s.identifier for s in validate(tmpdir, yml, False, hosts)._servers()]

def test_not_existing(tmpdir):
    p = str(tmpdir.join("not_existing.yml"))
    with pytest.raises(IOError):
        config.Config(p, [])


def test_deploy_invalid(tmpdir):
    s = """
    - deploy:
        django
    """
    validate(tmpdir, s, True)

    s = """
    deploy:
      django
      redis
    """
    validate(tmpdir, s, True)

    s = """
    deploy:
      - django
      - redis:
          not_existing_option: 3
    """
    validate(tmpdir, s, True)
    s = """
    deploy:
      - django:
        dict_in_the_wrong_place: 4
    """
    validate(tmpdir, s, True)
    s = """
    deploy:
      - not_existing_deploy_line
    """
    validate(tmpdir, s, True)
    s = """
    deploy:
      - django:
          port: string instead of int
    """
    validate(tmpdir, s, True)
    s = """
    deploy:
      - django:
          not_existing_django_config: 80
    """
    validate(tmpdir, s, True)


def test_deploy_valid(tmpdir):
    def deploys_to_str(yml):
        return [s.name for s in validate(tmpdir, yml, False)._deploys()]

    s = """
    deploy:
      - django
      - redis
    """
    assert deploys_to_str(s) == ['django', 'redis']

    s = """
    deploy:
      - django:
          port: 80
      - redis
    """
    assert deploys_to_str(s) == ['django', 'redis']
    validate(tmpdir, s, False)

    s = """
    deploy:
      - redis
    """
    assert deploys_to_str(s) == ['redis']


def test_server(tmpdir):
    s = """
    deploy:
      - django
    server:
      - foo@bar:22:
          password: password
    """
    assert servers_to_str(tmpdir, s) == ['foo@bar:22']
    s = """
    deploy:
      - django
    server:
      - foo@bar:22:
          password: password
      - example.com
    """
    assert servers_to_str(tmpdir, s) == ['foo@bar:22', 'example.com']


def test_server_invalid(tmpdir):
    s = """
    deploy:
      - django
    server:
      - foo@bar:
          password: password
          wrong_option: foo
    """
    validate(tmpdir, s, True)
    s = """
    deploy:
      - django
    server:
      server_not_in_list
    """
    validate(tmpdir, s, True)


def test_pool(tmpdir):
    s = """
    deploy:
      - &web django
      - redis
    server:
      - &server1 foo@bar
      - other
    pool:
      foo:
        server: [*server1]
        deploy: [*web]
    """
    pools = validate(tmpdir, s, False).pools()
    assert len(pools) == 1

    servers = pools[0].servers
    assert len(servers) == 1
    assert servers[0].identifier == 'foo@bar'
    deploys = pools[0].deploys
    assert len(deploys) == 1
    assert deploys[0].name == 'django'


def test_pool_invalid(tmpdir):
    s = """
    deploy:
      - &web django
    server:
      - &server1 foo@bar
    pool:
      foo:
        server: [*server1]
        deploy: [*web]
        unknown_option: haha
    """
    validate(tmpdir, s, True)


def test_hosts_param(tmpdir):
    s = """
    deploy:
      - &web django
    """
    assert servers_to_str(tmpdir, s, ['foo@bar']) == ['foo@bar']
    s = """
    deploy:
      - &web django
    server:
      - foo@baz
    """
    assert servers_to_str(tmpdir, s, ['foo@bar']) == ['foo@bar']


def test_pool_param(tmpdir):
    s = """
    deploy:
      - &web django
      - &redis redis
    server:
      - &server1 foo@bar
      - other
    pool:
      foo:
        server: [*server1]
        deploy: [*web]
      bar:
        server: [*server1]
        deploy: [*redis]
    """
    assert len(validate(tmpdir, s).pools()) == 2
    assert len(validate(tmpdir, s, pool='foo').pools()) == 1
    with pytest.raises(KeyError):
        validate(tmpdir, s, pool='not_existing').pools()

@@ -4115,16 +4115,517 @@
 sting').pools()%0A
+%0Adef test_pool_hosts_param(tmpdir):%0A    s = %22%22%22%0A    deploy:%0A      - &web django%0A      - &redis redis%0A    server:%0A      - &server1 foo@bar%0A      - other%0A    pool:%0A      foo:%0A        server: %5B*server1%5D%0A        deploy: %5B*web%5D%0A      bar:%0A        server: %5B*server1%5D%0A        deploy: %5B*redis%5D%0A    %22%22%22%0A    assert len(validate(tmpdir, s).pools()) == 2%0A    pool = validate(tmpdir, s, hosts=%5B'baz'%5D, pool='foo').pools()%5B0%5D%0A    assert pool.name == 'foo'%0A    assert %5Bs.identifier for s in pool.servers%5D == %5B'baz'%5D%0A

add test for both hosts and pool params