示例#1
0
def test_wait_for_service_max_timeout():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    svc_config['wait_attempts'] = 6
    svc_config['wait_timeout'] = 2
    responses = {
        '/bin/bash -l -c "docker run -it --name testapp-testinstance-db-client --network testapp-testinstance-net --env-file /testing/testapp/testinstance/testapp-testinstance-db.env --rm mysql:5.6 sh -c \'exec mysql -h\\"db\\" -uroot -D\\"\\$MYSQL_DATABASE\\" --execute \\"SELECT VERSION();\\"\'"': ('', 'fail', 1),
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {}
    expected = (
        '  Waiting for the service...\n'
        '  Attempt 1 of 6 (2 seconds to go)\n'
        '[{host}] run: docker run -it --name testapp-testinstance-db-client --network testapp-testinstance-net --env-file /testing/testapp/testinstance/testapp-testinstance-db.env --rm mysql:5.6 sh -c \'exec mysql -h"db" -uroot -D"$MYSQL_DATABASE" --execute "SELECT VERSION();"\'\n'
        '[{host}] out: fail\n'
        '  - Waiting for 1 second...\n'
        '  Attempt 2 of 6 (1 seconds to go)\n'
        '[{host}] run: docker run -it --name testapp-testinstance-db-client --network testapp-testinstance-net --env-file /testing/testapp/testinstance/testapp-testinstance-db.env --rm mysql:5.6 sh -c \'exec mysql -h"db" -uroot -D"$MYSQL_DATABASE" --execute "SELECT VERSION();"\'\n'
        '[{host}] out: fail\n'
        '  - Waiting for 1 second...\n'
        '  Attempt 3 of 6 (0 seconds to go)\n'
        '[{host}] run: docker run -it --name testapp-testinstance-db-client --network testapp-testinstance-net --env-file /testing/testapp/testinstance/testapp-testinstance-db.env --rm mysql:5.6 sh -c \'exec mysql -h"db" -uroot -D"$MYSQL_DATABASE" --execute "SELECT VERSION();"\'\n'
        '[{host}] out: fail\n'
    )

    response = run_fabric_command(mysql._wait_for_service, responses, expected, files, environ, None, svc_config)
    assert response is False
示例#2
0
def test_setup_container_existing():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    svc_config['initial_data_source'] = '/testing/testapp/testinstance/initialdata/db-backup-2018-05-29.sql.gz'
    responses = {
        '/bin/bash -l -c "docker ps -a --filter name=testapp-testinstance-db --format \\"{{.ID}}\\""': '12345',
        '/bin/bash -l -c "docker create --name testapp-testinstance-db --network testapp-testinstance-net --net-alias db -v testapp-testinstance-db-data:/var/lib/mysql --env-file /testing/testapp/testinstance/testapp-testinstance-db.env -v /testing/testapp/testinstance/initialdata/db-backup-2018-05-29.sql.gz:/docker-entrypoint-initdb.d/db-backup-2018-05-29.sql.gz mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"': '',
        'sudo -S -p \'sudo password:\'  /bin/bash -l -c "systemctl stop testapp-testinstance-db"': '',
        '/bin/bash -l -c "docker rm -f testapp-testinstance-db"': '',
        '/bin/bash -l -c "docker volume prune -f"': '',
        'sudo -S -p \'sudo password:\'  /bin/bash -l -c "systemctl start testapp-testinstance-db"': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.env': '',
    }
    expected = (
        '  Setting up the testapp-testinstance-db container\n'
        '[{host}] run: docker ps -a --filter name=testapp-testinstance-db --format "{{{{.ID}}}}"\n'
        '[{host}] out: 12345\n'
        '  Removing existing container.\n'
        '[{host}] sudo: systemctl stop testapp-testinstance-db\n'
        '[{host}] run: docker rm -f testapp-testinstance-db\n'
        '[{host}] run: docker volume prune -f\n'
        '[{host}] run: docker create --name testapp-testinstance-db --network testapp-testinstance-net --net-alias db -v testapp-testinstance-db-data:/var/lib/mysql --env-file /testing/testapp/testinstance/testapp-testinstance-db.env -v /testing/testapp/testinstance/initialdata/db-backup-2018-05-29.sql.gz:/docker-entrypoint-initdb.d/db-backup-2018-05-29.sql.gz mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci\n'
        '  Starting new container.\n'
        '[{host}] sudo: systemctl start testapp-testinstance-db\n'
    )
    run_fabric_command(mysql._setup_container, responses, expected, files, environ, None, svc_config)
示例#3
0
def test_wait_for_service_no():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    svc_config['wait_for_service'] = False
    responses = {}
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {}
    expected = ''
    response = run_fabric_command(mysql._wait_for_service, responses, expected, files, environ, None, svc_config)
    assert response is True
示例#4
0
def test_write_config():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.env': '',
    }
    expected = ''
    run_fabric_command(mysql._write_config, responses, expected, files, environ, None, svc_config, '/testing/testapp/testinstance/testapp-testinstance-db.env')
示例#5
0
def test_delete_volume_no_volume():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "docker volume ls --filter name=testapp-testinstance-db-data -q"': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
    }
    expected = (
        '[{host}] run: docker volume ls --filter name=testapp-testinstance-db-data -q\n'
    )
    run_fabric_command(mysql._delete_volume, responses, expected, files, environ, None, svc_config)
示例#6
0
def test_get_service_config():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    assert svc_config['app_name'] == 'testapp'
    assert svc_config['name'] == 'db'
    assert svc_config['initial_data_source'] == '/backups/testapp/'
    assert svc_config['image'] == 'mysql:5.6'
    assert svc_config['commands'] == [
        "--character-set-server=utf8mb4",
        "--collation-server=utf8mb4_unicode_ci",
    ]
    assert svc_config['environment'] == [
        "MYSQL_ALLOW_EMPTY_PASSWORD=true",
        "MYSQL_DATABASE=drupal",
    ]
示例#7
0
def test_has_config_changed_missing():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "echo \\"Will you echo quotation marks\\""': 'Will you echo quotation marks',
        '/bin/bash -l -c "stat \\"\\$(echo /testing/testapp/testinstance/testapp-testinstance-db.conf.json)\\""': ('', 'no exist', 1),
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.conf.json': '',
    }
    expected = ''
    response = run_fabric_command(mysql._has_config_changed, responses, expected, files, environ, None, svc_config)
    assert response is True
示例#8
0
def test_delete_container_none():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "docker ps -a --filter name=testapp-testinstance-db --format \\"{{.ID}}\\""': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.env': '',
    }
    expected = (
        '[{host}] run: docker ps -a --filter name=testapp-testinstance-db --format "{{{{.ID}}}}"\n'
    )
    run_fabric_command(mysql._delete_container, responses, expected, files, environ, None, svc_config)
示例#9
0
def test_has_config_changed_unchanged():
    import json
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "echo \\"Will you echo quotation marks\\""': 'Will you echo quotation marks',
        '/bin/bash -l -c "stat \\"\\$(echo /testing/testapp/testinstance/testapp-testinstance-db.conf.json)\\""': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.conf.json': json.dumps(svc_config),
    }
    expected = '  Configuration for existing MySQL service is unchanged. Skipping.\n'
    response = run_fabric_command(mysql._has_config_changed, responses, expected, files, environ, None, svc_config)
    assert response is False
示例#10
0
def test_setup_volume():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "docker volume ls --filter name=testapp-testinstance-db-data -q"': '',
        '/bin/bash -l -c "docker volume create --driver rexray/ebs:latest --opt=size=10 testapp-testinstance-db-data"': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
    }
    expected = (
        '[{host}] run: docker volume ls --filter name=testapp-testinstance-db-data -q\n'
        '  Creating a volume for storing MySQL data.\n'
        '[{host}] run: docker volume create --driver rexray/ebs:latest --opt=size=10 testapp-testinstance-db-data\n'
    )
    run_fabric_command(mysql._setup_volume, responses, expected, files, environ, None, svc_config)
示例#11
0
def test_has_config_changed_bad_file():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "echo \\"Will you echo quotation marks\\""': 'Will you echo quotation marks',
        '/bin/bash -l -c "stat \\"\\$(echo /testing/testapp/testinstance/testapp-testinstance-db.conf.json)\\""': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.conf.json': '',
    }
    expected = (
        '  Error: No JSON object could be decoded\n'
        '  Configuration for existing MySQL service is unreadable. Re-creating it.\n'
    )
    response = run_fabric_command(mysql._has_config_changed, responses, expected, files, environ, None, svc_config)
    assert response is True
示例#12
0
def test_has_config_changed_change_options():
    import json
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "echo \\"Will you echo quotation marks\\""': 'Will you echo quotation marks',
        '/bin/bash -l -c "stat \\"\\$(echo /testing/testapp/testinstance/testapp-testinstance-db.conf.json)\\""': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.conf.json': json.dumps(svc_config),
    }
    svc_config['foo'] = 'bar'
    expected = (
        '  Configuration for existing MySQL service has different options from the new configuration.\n'
        '    - foo\n'
        '  Re-creating it.\n'
    )
    response = run_fabric_command(mysql._has_config_changed, responses, expected, files, environ, None, svc_config)
    assert response is True
示例#13
0
def test_has_config_changed_changed():
    import json
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "echo \\"Will you echo quotation marks\\""': 'Will you echo quotation marks',
        '/bin/bash -l -c "stat \\"\\$(echo /testing/testapp/testinstance/testapp-testinstance-db.conf.json)\\""': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.conf.json': json.dumps(svc_config),
    }
    svc_config['environment'].append('FOO=bar')
    expected = (
        '  Configuration for existing MySQL service has changed.\n'
        '    environment was [u\'MYSQL_ALLOW_EMPTY_PASSWORD=true\', u\'MYSQL_DATABASE=drupal\'] now [\'MYSQL_ALLOW_EMPTY_PASSWORD=true\', \'MYSQL_DATABASE=drupal\', \'FOO=bar\']\n'
        '  Re-creating it.\n'
    )
    response = run_fabric_command(mysql._has_config_changed, responses, expected, files, environ, None, svc_config)
    assert response is True
示例#14
0
def test_delete_container():
    from .fabric_runner import setup_config
    cfg = setup_config(filepath=os.path.join(FIXTURE_DIR, 'mysql-config.yml'))
    svc_config = mysql._get_service_config(cfg.services['db'], 'db')
    responses = {
        '/bin/bash -l -c "docker ps -a --filter name=testapp-testinstance-db --format \\"{{.ID}}\\""': '12345',
        'sudo -S -p \'sudo password:\'  /bin/bash -l -c "systemctl stop testapp-testinstance-db"': '',
        '/bin/bash -l -c "docker rm -f testapp-testinstance-db"': '',
    }
    environ = {
        'filepath': os.path.join(FIXTURE_DIR, 'mysql-config.yml')
    }
    files = {
        '/testing/testapp/testinstance/testapp-testinstance-db.env': '',
    }
    expected = (
        '[{host}] run: docker ps -a --filter name=testapp-testinstance-db --format "{{{{.ID}}}}"\n'
        '[{host}] out: 12345\n'
        '  Deleting the testapp-testinstance-db container\n'
        '[{host}] sudo: systemctl stop testapp-testinstance-db\n'
        '[{host}] run: docker rm -f testapp-testinstance-db\n'
    )
    run_fabric_command(mysql._delete_container, responses, expected, files, environ, None, svc_config)