"""

    IDcheck.sh regression test suite.

    Copyright (C) 2009, Cisco Systems Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Garrett Cooper, July 2009

Copyright (c) 2009 by Cisco Systems, Inc.
All rights reserved

Requires nose 0.10+.

"""

from copy import deepcopy
import os
try:
    # check_call only exists in 2.5+, as well as the `finally' call used later
    # on in the file.
    from subprocess import CalledProcessError, check_call
except ImportError:
    raise ImportError("You're running Python <= v2.4. Please upgrade and try"
                      "again...")
import sys
from tempfile import mkdtemp

def run_IDcheck_sh(env, positive_tc, cmd='./IDcheck.sh'):
    """ Run the command, expect a result (positive or negative). """
    try:
        sys.stdout.write(' '.join([str(i) for i in [cmd, env, positive_tc]]) +
                         '\n')
        check_call([ '' ], executable=cmd, env=env, shell=True)
    except CalledProcessError:
        if positive_tc:
            raise
    else:
        if not positive_tc:
            raise Exception('%s failed silently' % cmd)

ENV = {}

#
# What password entries do we expect to be within each file in order to pass
# when CREATE_ENTRIES=0 ?
#
EXPECTED_ELEMENTS = {}
EXPECTED_ELEMENTS['group'] = [ 'bin', 'daemon', 'nobody' ]
EXPECTED_ELEMENTS['passwd'] = [ 'bin', 'daemon', 'nobody', 'sys', 'users' ]

NEG_DESTDIR, POS_DESTDIR = None, None

def setup_module():
    """ Generate the positive and negative DESTDIR's. """
    global ENV, NEG_DESTDIR, POS_DESTDIR

    # We need to establish a minimum environment for the script, or IDcheck.sh
    # will print out more spurious messages than necessary when used with
    # subprocess, even though shell=True.
    for ev in [ 'PATH', 'TERM' ]:
        ENV[ev] = os.getenv(ev, '')

    POS_DESTDIR = mkdtemp()
    NEG_DESTDIR = mkdtemp()

    os.makedirs(os.path.join(POS_DESTDIR, 'etc'))

def teardown_module():
    """ Get rid of the positive DESTDIR. os.removedirs() always fails, so let's
    use os.walk + os.remove instead. """

    if os.path.isdir(POS_DESTDIR):

        for root, dirs, files in os.walk(POS_DESTDIR, topdown=False):
            for _file in files:
                os.remove(os.path.join(root, _file))
            for _dir in dirs:
                os.removedirs(os.path.join(root, _dir))

def test_IDcheck_sh():
    """ Does DESTDIR functionality really work?

    Try 4 different permutations of DESTDIR:

    1. Current rootfs. Will fail if I don't have write access to 

    Sean Connery: I've been trying to get a DESTDIR to work for ages!
    (SNL / Analbumcover reference :)...). """

    env = ENV

    for CREATE_ENTRIES in [ '0', '1' ]:

        for DESTDIR in [ '/', POS_DESTDIR, NEG_DESTDIR, '/some/path/that/does/not/exist' ]:

            etcdir = os.path.join(DESTDIR, 'etc')

            expect_pos = False

            if bool(int(CREATE_ENTRIES)):

                # We expect the results to be positive if CREATE_ENTRIES=1 and
                # the path is writable, because IDcheck.sh will properly fill
                # in the blanks.

                expect_pos = os.access(os.path.join(etcdir), os.W_OK)

            else:

                # Otherwise, if all of the entries exist and CREATE_ENTRIES=0,
                # we have a winner!

                for i in EXPECTED_ELEMENTS.keys():

                    _file = os.path.join(etcdir, i)

                    sys.stdout.write('Will open: %s\n' % _file)

                    if not os.access(_file, os.R_OK):
                        # Expect negative results.
                        break

                    fd = open(_file)

                    try:
                        lines = fd.readlines()
                    finally:
                        fd.close()

                    for expected_element in EXPECTED_ELEMENTS[i]:

                        sys.stdout.write('%s - %s' % (_file, expected_element))

                        for line in lines:

                            if line.startswith(expected_element + ":"):
                                expect_pos = True
                                sys.stdout.write(' - found\n')
                                break

                        else:
                            # If we iterated through all the lines and didn't
                            # break (and hence didn't end up in this else
                            # statement), we didn't find our entry of interest.
                            sys.stdout.write('- not found\n')
                            break

                    if not expect_pos:
                        break

            env["CREATE_ENTRIES"] = CREATE_ENTRIES
            env["DESTDIR"] = DESTDIR

            yield run_IDcheck_sh, env, expect_pos
