=======================
Unified Installer Tests
=======================

Note that we are using "-etc-" for the doctest.ELLIPSIS

-----------
Setup stuff
-----------

    >>> import subprocess, os, os.path, sys, shutil, urllib2, stat, pwd, time
    >>> uid = os.geteuid()
    >>> root = uid == 0

    NOTE: Make sure the test target is in a partition where ownership &
    permissions work. That may not be so in a mountable or tmp partition.
    >>> testTarget = '/home/steve/plonetest'
    >>> withPython = '/usr/bin/python2.7'

    >>> if os.path.exists(testTarget): shutil.rmtree(testTarget)

This test should be run from the directory with install.sh

    >>> os.chdir(os.path.join(os.getcwd(), '..'))
    >>> os.path.exists('install.sh')
    True

install.sh should be executable
    >>> os.access('install.sh', os.X_OK)
    True


Let's set up a convenience function for executing a command line
and getting stdout, stderr and return code.

    >>> def doCommand(command):
    ...    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    ...    out, err = p.communicate()
    ...    return (out, err, p.returncode)


-------------
Usage Message
-------------

Running install.sh with help option should result in a usage message:

    >>> stdout, stderr, returncode = doCommand('./install.sh --help')
    >>> returncode
    0
    >>> stderr
    ''
    >>> print stdout
    <BLANKLINE>
    Usage: -etc-

------------------
Test a ZEO install
------------------

    >>> stdout, stderr, returncode = doCommand('./install.sh zeo --target=%s --password=admin' % testTarget)
    >>> returncode and (stdout + stderr)
    0

    >>> print stdout
    <BLANKLINE>
    -etc-
    Installing Plone 5.0.4 at -etc-
    #####################################################################
    <BLANKLINE>
    ######################  Installation Complete  ######################
    <BLANKLINE>
    Plone successfully installed at -etc-
      Username: admin
      Password: admin-etc-

    target should have basic kit
    >>> sorted(os.listdir(testTarget))
    ['buildout-cache', 'zeocluster']

    There should now be a buildout skeleton in zeocluster
    >>> expected = ['.installed.cfg', 'README.html', 'adminPassword.txt', 'base.cfg', 'bin', 'buildout.cfg', 'develop-eggs', 'develop.cfg', 'lxml_static.cfg', 'parts', 'products', 'src', 'var', 'versions.cfg']
    >>> found = os.listdir('%s/zeocluster' % testTarget)
    >>> [s for s in expected if s not in found]
    []

    Parts should look contain the needed components
    >>> expected = ['README.txt', 'client1', 'client2', 'zeoserver']
    >>> found = os.listdir('%s/zeocluster/parts' % testTarget)
    >>> [s for s in expected if s not in found]
    []

    parts/README.html should be a warning
    >>> print open('%s/zeocluster/parts/README.txt' % testTarget).read()
    WARNING:-etc-run bin/buildout-etc-

    We should have an inituser for admin
    >>> print open('%s/zeocluster/parts/client1/inituser' % testTarget).read()
    admin:{SHA}-etc-

    Check bin contents
    >>> expected = ['backup', 'buildout', 'client1', 'client2', 'fullbackup', 'pilconvert.py', 'plonectl', 'pip', 'python', 'repozo', 'restore', 'snapshotbackup', 'snapshotrestore', 'zeopack', 'zeoserver', 'zopepy']
    >>> found = os.listdir('%s/zeocluster/bin' % testTarget)
    >>> [s for s in expected if s not in found]
    []

    Installing again to the same target should fail
    >>> stdout, stderr, returncode = doCommand('./install.sh zeo --target=%s --password=admin' % testTarget)
    >>> print stdout
    <BLANKLINE>
    -etc-
    Instance target -etc-zeocluster already exists\; aborting install.
    -etc-

    Check the Python
    ----------------

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from PIL._imaging import jpeg_decoder"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from PIL._imaging import zip_decoder"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from lxml import etree"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    Since we didn't specify otherwise, this Python should be a virtualenv.
    >>> os.path.exists(os.path.join(testTarget, 'zeocluster', 'bin', 'activate'))
    True


    Run it
    ------

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zeoserver start' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/client1 start' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/client2 start' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> time.sleep(30)

    Status check
    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/plonectl status' % testTarget)

    >>> returncode
    0

    >>> stderr
    ''

    Fetch root page via client1
    >>> urllib2.urlopen('http://localhost:8080/').read()
    '-etc-Plone is up and running-etc-'

    Fetch root page via client2
    >>> urllib2.urlopen('http://localhost:8081/').read()
    '-etc-Plone is up and running-etc-'

    Check Banner
    >>> print urllib2.urlopen('http://localhost:8080/').headers['server']
    Zope/(2.13-etc-, python 2.7-etc-) ZServer/1.1

    Stop it
    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/plonectl stop' % testTarget)

    >>> returncode
    0

    >>> stderr
    ''

-------------------------------------
Test building Python and dependencies
-------------------------------------

    First, clean out prior work
    >>> if os.path.exists(testTarget): shutil.rmtree(testTarget)

    >>> stdout, stderr, returncode = doCommand('./install.sh zeo --target=%s --password=admin --build-python --static-lxml' % testTarget)
    >>> returncode and (stdout + stderr)
    0

    >>> print stdout
    <BLANKLINE>
    Rootless install method chosen. Will install for use by system user -etc-
    -etc-
    Installing Python-2.7.10. This takes a while...
    Python build looks OK.
    -etc-
    Plone successfully installed at -etc-
    -etc-

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "import readline"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "import zlib"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from PIL._imaging import jpeg_decoder"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from PIL._imaging import zip_decoder"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    >>> stdout, stderr, returncode = doCommand('%s/zeocluster/bin/zopepy -c "from lxml import etree"' % testTarget)
    >>> returncode
    0
    >>> stderr
    ''

    This Python should not be a virtualenv.
    >>> os.path.exists(os.path.join(testTarget, 'Python-2.7', 'bin', 'activate'))
    False
