# -*-mode: doctest; tab-width: 0; py-indent-offset: 4; coding: utf-8-unix -*-

== testserver box testing ==

    >>> import os # doctest: +REPORT_ONLY_FIRST_FAILURE

This is a Python doctest file that is executable documentation.
It is built to run in the host against a Vagranted VirtualBox, and is run
from the directory that contains the box's {{{.vagrant}}} subdirectory.

    >>> import subprocess
    >>> import sys
    >>> import time

And, now run tests locally

    >>> sys.stderr.write("Running tests locally" +'\n')
    22

=== Box settings ===

We'll need the settings defined in  {{{/usr/local/etc/testforge/testforge.yml}}}

    >>> import yaml
    >>> sFacts = open('/usr/local/etc/testforge/testforge.yml', 'rt').read()
    >>> assert sFacts
    >>> dFacts = yaml.safe_load(sFacts)

=== .gitconfig ===

We have a .gitconfig file in this directory that has our template
of what we need up in the box to checkout from https://git.example.com
You can edit the file and customize it, and we will use it as a
Python string template, so look out for the {{{%()s}}} template fields.

    >>> sDir =  '/var/local/share/doc/txt'
    >>> sFile = os.path.join(sDir, 'example.gitconfig')
    >>> assert os.path.isfile(sFile), "ERROR: File not found " +sFile
    >>> sGitConfig = open(sFile, 'r').read()
    >>> assert sGitConfig, "ERROR: Nothing in " +sFile
    
We will look for the environment variables:
* {{{AAA_CERT}}} for the filename of your example certificate
* {{{AAA_KEY}}} for the filename of your example key

    >>> sCertFile = os.environ.get('AAA_CERT')
    >>> assert sCertFile, "ERROR: we need AAA_CERT set in the environment"
    >>> assert os.path.isfile(sCertFile), "ERROR: the AAA_CERT in the environment is not a file"

    >>> sKeyFile = os.environ.get('AAA_KEY')
    >>> assert sKeyFile, "ERROR: we need AAA_KEY set in the environment"
    >>> assert os.path.isfile(sKeyFile), "ERROR: the AAA_KEY in the environment is not a file"

    >>> sIdentityFile = os.path.expandvars('$HOME/.ssh/id_rsa')
    >>> assert os.path.isfile(sIdentityFile), "ERROR: the file ~/.ssh/id_rsa is not a file"


The directory we push to should have been created by Ansible.

    >>> sBoxHome = dFacts['BOX_HOME']
    >>> sDir = sBoxHome +'/etc/ssl/keys'
    >>> run( "[ -d " +sDir +" ] || mkdir -p " +sDir) or None

We will push these files up to the box so that we can use them.

    >>> sUser = os.environ.get('USERNAME') or os.environ.get('USER')
    >>> sTo = 'dd of=%s/%s@example.com-nodes.key' % (sDir, sUser,)
    >>> ssh_run_with_stdin(sTo, sKeyFile) or None
    >>> sTo = 'dd of=%s/%s@example.com-clcerts.key' % (sDir, sUser,)
    >>> ssh_run_with_stdin(sTo, sCertFile) or None
    >>> sTo = 'dd of=%s/%s@example.com-id_rsa' % (sDir, sUser,)
    >>> ssh_run_with_stdin(sTo, sIdentityFile) or None
    >>> sToDir = '%s/%s@*' % (sDir, sUser,)
    >>> run( "chown 600 " +sToDir) or None

Now we have the cert and key up we can write our templated {{{~/.gitconfig}}}

    >>> sTempDir = os.environ.get('temp') or os.environ.get('TMP') or '/tmp'
    >>> assert os.path.isdir(sTempDir)
    >>> sFile = os.path.join(sTempDir, '.gitconfig')
    >>> oFile = open(sFile, 'w')
    >>> sGitConfig = sGitConfig % dict(USER=sUser, KEYSDIR=sDir,
    ...               BOX_HOME=sBoxHome)
    >>> try:
    ...   oFile.write(sGitConfig)
    ... finally:
    ...   oFile.close()
    >>> assert os.path.isfile(sFile)
    >>> sTo = sBoxHome +'/.gitconfig'
    >>> ssh_run_with_stdin('dd of=' +sTo, sFile) or None
    >>> sys.stderr.write("Wrote templated .gitconfig to " +sFile +'\n')

QED.
