public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] Fix IDcheck.sh - awk problem
@ 2009-07-08 16:08 Garrett Cooper
  2009-07-08 19:43 ` Garrett Cooper
  0 siblings, 1 reply; 3+ messages in thread
From: Garrett Cooper @ 2009-07-08 16:08 UTC (permalink / raw)
  To: michal.simek; +Cc: LTP, Mike Frysinger

[-- Attachment #1: Type: text/plain, Size: 4588 bytes --]

On Wed, Jul 8, 2009 at 7:36 AM, Michal Simek<michal.simek@petalogix.com> wrote:
> Garrett Cooper wrote:
>> On Tue, Jul 7, 2009 at 11:04 PM, Michal Simek<michal.simek@petalogix.com> wrote:
>>
>>> Hi All,
>>>
>>> There will be good to prevent missing /etc/passwd for installing to
>>> different folder.
>>>
>>> export DESTDIR=`pwd`/nfs
>>> mkdir -p nfs
>>> make install
>>
>> That's part of the reason why I wanted it to be reviewed before it was
>> committed, and I halfway expected Mike to provide that assistance but
>> he didn't... *sigh*.
>>
>> Does the /etc/passwd // /etc/group file exist?
>>
> Of course not because I cross compile ltp for Microblaze - correct
> /etc/passwd - group is on
> target system. Maybe worth to remove calling IDcheck.sh from make
> install and call it only before
> running the test.

Well, it's up to you folks how you want to do it, because it should
not be called by default, but it still is because I haven't added my
changes into the top-level Makefile yet, because that's under review.

=================
PATCH FOLLOWS
=================

This fixes the bad DESTDIR logic that was recently checked in, as
validated with the output shown below (see the attached file --
test_IDcheck_sh_regression_set.py -- for more details):

Nose output:

gcooper@orangebox /scratch/ltp-vanilla/ltp $ nosetests
test_IDcheck_sh_regression_set.py
Checking for required user/group ids

'nobody' user id and group found.
'bin' user id and group found.
'daemon' user id and group found.
Users group found.
Sys group found.
Required users/groups exist.
.Checking for required user/group ids


*****************************************
* Required users/groups do NOT exist!!! *
*                                       *
* Some kernel/syscall tests will FAIL!  *
*****************************************
.Checking for required user/group ids


*****************************************
* Required users/groups do NOT exist!!! *
*                                       *
* Some kernel/syscall tests will FAIL!  *
*****************************************
.Checking for required user/group ids


*****************************************
* Required users/groups do NOT exist!!! *
*                                       *
* Some kernel/syscall tests will FAIL!  *
*****************************************
.Checking for required user/group ids

Failed to touch //etc/group or //etc/passwd
.Checking for required user/group ids

Creating entries for nobody
Creating entries for bin
Creating entries for daemon
Required users/groups exist.
.Checking for required user/group ids

Failed to touch /tmp/tmpvQw2kZ/etc/group or /tmp/tmpvQw2kZ/etc/passwd
.Checking for required user/group ids

Failed to touch /some/path/that/does/not/exist/etc/group or
/some/path/that/does/not/exist/etc/passwd
.
----------------------------------------------------------------------
Ran 8 tests in 0.090s

OK

Signed-off-by: Garrett Cooper <yanegomi@gmail.com>

Index: IDcheck.sh
===================================================================
RCS file: /cvsroot/ltp/ltp/IDcheck.sh,v
retrieving revision 1.19
diff -u -r1.19 IDcheck.sh
--- IDcheck.sh  7 Jul 2009 14:30:27 -0000       1.19
+++ IDcheck.sh  8 Jul 2009 16:06:42 -0000
@@ -22,7 +22,6 @@
 #

 # Prompt user if ids/groups should be created
-clear
 echo "Checking for required user/group ids"
 echo ""

@@ -41,12 +40,11 @@

 # find entry.
 fe() {
-       ID=$1; shift
-       FILE=$1; shift
-       awk "/^$ID:/ { FOUND=1 } END { if (\$FOUND == 1) { exit 0; }
exit 1; }" \
-       "$FILE"
-       ec=$?
-       echo "$ID => $ec"
+    ID=$1; shift
+    FILE=$1; shift
+    [ -e "$FILE" ] || return $?
+    awk "/^$ID:/ { FOUND=1 } END { if (\$FOUND == 1) { exit 1; } exit 0; }" \
+    "$FILE"
 }

 prompt_for_create() {
@@ -70,13 +68,12 @@
        EUID=$(id -u)
 fi

-if [ -e "$passwd" -a ! -r "$passwd" ] ; then
-       echo "/etc/passwd not readable by uid $EUID"
+for i in "$passwd" "$group"; do
+    if [ -e "$i" -a ! -r "$i" ] ; then
+       echo "$i not readable by uid $EUID"
        exit 1
-elif [ -e "$group" -a ! -r "$group" ] ; then
-       echo "$group not readable by uid $EUID"
-       exit 1
-fi
+    fi
+done

 fe bin "$passwd"; NO_BIN_ID=$?
 fe daemon "$passwd"; NO_DAEMON_ID=$?
@@ -110,10 +107,10 @@
 #debug_vals

 if [ $CREATE_ENTRIES -ne 0 ] ; then
-       if ! touch "$group" ; then
-               echo "Couldn't touch $group"
-               exit 1
-       fi
+    if ! touch "$group" "$passwd" 2>/dev/null; then
+        echo "Failed to touch $group or $passwd"
+        exit 1
+    fi
 fi

 make_user_group() {

[-- Attachment #2: test_IDcheck_sh_regression_set.py --]
[-- Type: application/octet-stream, Size: 5452 bytes --]

"""

    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

[-- Attachment #3: Type: text/plain, Size: 389 bytes --]

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge

[-- Attachment #4: Type: text/plain, Size: 155 bytes --]

_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-07-08 20:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-08 16:08 [LTP] [PATCH] Fix IDcheck.sh - awk problem Garrett Cooper
2009-07-08 19:43 ` Garrett Cooper
2009-07-08 20:19   ` Michal Simek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox