diff options
author | Michal Minar <miminar@redhat.com> | 2014-02-02 12:32:03 +0100 |
---|---|---|
committer | Michal Minar <miminar@redhat.com> | 2014-02-05 14:44:04 +0100 |
commit | e84003f58ae52a4e4becdb0033104b6d9f2fb852 (patch) | |
tree | b9eb407a4fb6806ac196972122818cf82de8552a | |
parent | cc39d3c73844eadd17047581bae63b81bd64f97e (diff) | |
download | openlmi-scripts-e84003f58ae52a4e4becdb0033104b6d9f2fb852.tar.gz openlmi-scripts-e84003f58ae52a4e4becdb0033104b6d9f2fb852.tar.xz openlmi-scripts-e84003f58ae52a4e4becdb0033104b6d9f2fb852.zip |
added tests for lmi meta-command
34 files changed, 952 insertions, 0 deletions
diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..03219b3 --- /dev/null +++ b/test/README.md @@ -0,0 +1,25 @@ +Tests Description +================= +Here are tests for lmi meta-command. +Interactive and non-interactive modes are tested. + +Dependencies +------------ + * bash + * beakerlib + * expect + * openlmi-tools + +Openlmi scripts need not be installed. + +Run +--- +Install and run any CIM broker on this or any remote host. Ensure that lmishell +can connect to it. Export these variables: + + * LMI_CIMOM_URL + * LMI_CIMOM_USERNAME + * LMI_CIMOM_PASSWORD + +Execute: + $ ./run.sh diff --git a/test/base.sh b/test/base.sh new file mode 100644 index 0000000..540df99 --- /dev/null +++ b/test/base.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Jan Safranek <jsafrane@redhat.com> +# Authors: Michal Minar <miminar@redhat.com> + +# Basic framework for all 'lmi' tests. +# It is based on Beakerlib, https://fedorahosted.org/beakerlib/ + +. /usr/share/beakerlib/beakerlib.sh + +LMI_CIMOM_URL=${LMI_CIMOM_URL:-kvm-rhel7} +LMI_CIMOM_USERNAME=${LMI_CIMOM_USERNAME:-root} +LMI_CIMOM_PASSWORD=${LMI_CIMOM_PASSWORD:-redhat} + +SCHEMA=$(echo $LMI_CIMOM_URL | sed -n 's!^\([[:alpha:]]\+://\).*!p') +HOSTNAME=$(echo $LMI_CIMOM_URL | sed -e 's!^.*//!!' -e 's!/.*!!') +export LMI="lmi -n -h $SCHEMA$LMI_CIMOM_USERNAME:$LMI_CIMOM_PASSWORD@$HOSTNAME" diff --git a/test/imode/builtin_help.out b/test/imode/builtin_help.out new file mode 100644 index 0000000..016468d --- /dev/null +++ b/test/imode/builtin_help.out @@ -0,0 +1,15 @@ +Built-ins. + +Usage: + : cd [<path>] + : .. + : pwd + : (help | -h | --help) + +Description: + cd Nest to a subcommand. Accepts a sequence of subcommands separated + with "/". If "/" is given, top-level command becomes the active one. + Other supported special symbols are ".." and ".". + .. Make parent command the active one. Same as issuing ":cd ..". + pwd Print current command path. + help Show this help. diff --git a/test/imode/cd_test.exp b/test/imode/cd_test.exp new file mode 100644 index 0000000..a575933 --- /dev/null +++ b/test/imode/cd_test.exp @@ -0,0 +1,69 @@ +#!/usr/bin/expect -f +# Test :cd and :pwd built-in commands. +# +# Environment variables: +# LMI - path to lmi meta-command executable with optional argumenta. +# +# Last change dir is unsuccessful. +# Exits with the exit code os spawned lmi shell. + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +proc send1l {in out} { + send "$in\r" + expect "$out" +} + +proc send2l {in out1 out2} { + send "$in\r" + expect "$out1" + expect "$out2" +} + +set LMI lmi +if {[info exists env(LMI)]} { + set LMI $env(LMI) +} +spawn {*}$LMI +expect "lmi> " +send2l ": pwd" "/lmi" "lmi> " +send1l ": cd ." "lmi> " +send2l " : pwd " "/lmi" "lmi> " +send1l ":cd .." "lmi> " +send1l ":cd /" "lmi> " +send1l " : cd // " "lmi> " +send1l ": cd test" ">test> " +send2l ": pwd" "/lmi/test" ">test> " +send1l ":cd ." ">test> " +send1l ": .." "lmi> " +send2l ":pwd" "/lmi" "lmi> " +send1l ":cd test" ">test> " +send1l ":cd / " "lmi> " +send1l ":cd test/" ">test> " +send1l ":cd ../test" ">test> " +send1l ":cd .././test/.././test/../" "lmi> " +send1l ":cd test/./" ">test> " +send1l ":cd list" ">>list> " +send2l ":pwd" "/lmi/test/list" ">>list> " +send1l ":cd .." ">test> " +send1l ":cd /lmi/test/list" "list> " +send1l ":cd /lmi" "lmi> " +send1l ":cd /test/list" "list> " +send1l ":cd" "list> " +send1l ":cd /./test/../test/./list" ">>list> " +send2l ":cd /foo" "no such subcommand \"foo\"" ">>list> " +send2l ":cd pkgs" "ERROR: can not nest to subcommand \"pkgs\" which is not a multiplexer" ">>list> " +send2l ":cd ../show" "ERROR: can not nest to subcommand \"../show\" which lacks any help message" ">>list> " +send2l ":cd foo" "ERROR: no such subcommand \"foo\"" ">>list> " +send "" +expect ">test> " +send "" +expect "lmi> " +send "" +expect "" +catch wait result +exit [lindex $result 3] diff --git a/test/imode/complete_builtin.exp b/test/imode/complete_builtin.exp new file mode 100644 index 0000000..9f9ef88 --- /dev/null +++ b/test/imode/complete_builtin.exp @@ -0,0 +1,37 @@ +#!/usr/bin/expect -f + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +set LMI lmi +if {[info exists env(LMI)]} { + set LMI $env(LMI) +} +spawn {*}$LMI +expect "lmi> " +send ": " +expect ".. cd help pwd" +expect "lmi> :" +send ". " +expect "." +send "" +send "p " +expect "wd" +send "c " +expect "d" +send " test\r" +expect ">test> " +send " : " +expect ".. cd help pwd" +send "h " +expect "elp" +send "" +expect ">test> " +send "EOF\r" +expect "lmi> " +send "EOF\r" +catch wait result +exit [lindex $result 3] diff --git a/test/imode/completion.exp b/test/imode/completion.exp new file mode 100644 index 0000000..fe871f5 --- /dev/null +++ b/test/imode/completion.exp @@ -0,0 +1,51 @@ +#!/usr/bin/expect -f + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +set LMI lmi +if {[info exists env(LMI)]} { + set LMI $env(LMI) +} + +spawn {*}$LMI +expect "lmi> " +send " " +expect "EOF exit help test" +send "E " +expect "EOF" +send "" +expect "lmi> " +send "e " +expect "xit" +send " " +expect "exit" +expect "lmi> exit" +send "\r" +expect "" +catch wait result +if {[lindex $result 3] != 0} { + exit [expr 10 + [lindex $result 3]] +} else { + puts "\n" +} + +spawn {*}$LMI +expect "lmi> " +send ":cd test\r" +expect ">test> " +send " " +expect "EOF exit help list show" +send "l " +expect "ist" +send "" +expect ">test> " +send "ex " +expect "it" +send " 20\r" +expect "" +catch wait result +exit [lindex $result 3] diff --git a/test/imode/empty_lines.exp b/test/imode/empty_lines.exp new file mode 100644 index 0000000..c5fdb77 --- /dev/null +++ b/test/imode/empty_lines.exp @@ -0,0 +1,26 @@ +#!/usr/bin/expect -f + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +set LMI lmi +if {[info exists env(LMI)]} { + set LMI $env(LMI) +} +spawn {*}$LMI +expect "lmi> " +send "\r" +expect "lmi> " +send "\r" +expect "lmi> " +send " \r" +expect "lmi> " +send " : pwd\r" +expect "/lmi" +expect "lmi> " +send "exit\r" +catch wait result +exit [lindex $result 3] diff --git a/test/imode/empty_lines.out b/test/imode/empty_lines.out new file mode 100644 index 0000000..e03a6dc --- /dev/null +++ b/test/imode/empty_lines.out @@ -0,0 +1 @@ +/lmi diff --git a/test/imode/empty_lines.txt b/test/imode/empty_lines.txt new file mode 100644 index 0000000..006be60 --- /dev/null +++ b/test/imode/empty_lines.txt @@ -0,0 +1,6 @@ + + +:cd . +:pwd + + diff --git a/test/imode/eof.exp b/test/imode/eof.exp new file mode 100644 index 0000000..a78cbef --- /dev/null +++ b/test/imode/eof.exp @@ -0,0 +1,21 @@ +#!/usr/bin/expect -f + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +spawn lmi +expect "lmi> " +if {[llength $argv] > 0} { + send "[lindex $argv 0]\r" + expect { + timeout { exit -2 } + -re "[lindex $argv 1]" + } + expect "lmi> " +} +close +catch wait result +exit [lindex $result 3] diff --git a/test/imode/exit.exp b/test/imode/exit.exp new file mode 100644 index 0000000..55cf207 --- /dev/null +++ b/test/imode/exit.exp @@ -0,0 +1,20 @@ +#!/usr/bin/expect -f + +set timeout 1 + +expect_after { + timeout { exit -1 } +} + +if {[llength $argv] > 0} { + set exit_arg " [lindex $argv 0]" +} else { + set exit_arg "" +} + +spawn lmi +expect "lmi> " +send "exit$exit_arg\r" +expect eof +catch wait result +exit [lindex $result 3] diff --git a/test/imode/help.out b/test/imode/help.out new file mode 100644 index 0000000..4a89747 --- /dev/null +++ b/test/imode/help.out @@ -0,0 +1,13 @@ + +Static commands +=============== +EOF exit help + +Application commands (type help <topic>): +========================================= +test + +Built-in commands (type :help): +=============================== +:.. :cd :pwd :help + diff --git a/test/imode/help_exit.out b/test/imode/help_exit.out new file mode 100644 index 0000000..c6ab1d1 --- /dev/null +++ b/test/imode/help_exit.out @@ -0,0 +1,3 @@ +Terminate the shell. + +Usage: exit [<exit_code>] diff --git a/test/imode/help_foo.out b/test/imode/help_foo.out new file mode 100644 index 0000000..80693ac --- /dev/null +++ b/test/imode/help_foo.out @@ -0,0 +1,14 @@ +ERROR: no such command "foo" + +Static commands +=============== +EOF exit help + +Application commands (type help <topic>): +========================================= +test + +Built-in commands (type :help): +=============================== +:.. :cd :pwd :help + diff --git a/test/imode/help_help.out b/test/imode/help_help.out new file mode 100644 index 0000000..bddf2cd --- /dev/null +++ b/test/imode/help_help.out @@ -0,0 +1,4 @@ +Print the list of supported commands with short description. +If a subcommand is given, its detailed help will be printed. + +Usage: help [<subcommand>...] diff --git a/test/imode/help_test.out b/test/imode/help_test.out new file mode 100644 index 0000000..e1e2510 --- /dev/null +++ b/test/imode/help_test.out @@ -0,0 +1,6 @@ +Test command for lmi-metacommand. + +Usage: + test list <cmd> [<args> ...] + test show pkg <name> + test show repo <name> diff --git a/test/imode/lmi_test_help.out b/test/imode/lmi_test_help.out new file mode 100644 index 0000000..8b93267 --- /dev/null +++ b/test/imode/lmi_test_help.out @@ -0,0 +1,6 @@ +Commands: + help - Print the list of supported commands with short description. + test - Test command for lmi-metacommand. + +For more informations about particular command type: + help <command> diff --git a/test/imode/run_test_cmd.exp b/test/imode/run_test_cmd.exp new file mode 100644 index 0000000..f61b7a0 --- /dev/null +++ b/test/imode/run_test_cmd.exp @@ -0,0 +1,39 @@ +#!/usr/bin/expect -f +# Environment variables: +# LMI - path to lmi meta-command executable with optional argumenta. +# +# Arguments: +# OUTFILE - Where to write the output of COMMAND. +# PATH - Command path where we change before execution. +# PROMPT - Prompt that is expected to appear after changing to PATH. +# COMMAND - Command to execute on given PATH. +# +# Exits with the exit code of spawned lmi shell. + +set timeout 2 + +expect_after { + timeout { exit -1 } +} + +set OUTFILE [open [lindex $argv 0] w] +set PROMPT [lindex $argv 2] +set LMI lmi +if {[info exists env(LMI)]} { + set LMI $env(LMI) +} +spawn -noecho sh -c "$LMI 2> /dev/null" +expect "lmi> " +send ":cd [lindex $argv 1]\r" +expect $PROMPT +send "[lindex $argv 3]\r" +expect -re ".*" {} +set REG {([^\n]+)\n(.*)} +expect -re "$REG$PROMPT\s*" { + puts -nonewline $OUTFILE [string map {"\r" ""} "$expect_out(2,string)"] +} +send "exit\r" +expect eof + +catch wait result +exit [lindex $result 3] diff --git a/test/imode/test_builtin_help.txt b/test/imode/test_builtin_help.txt new file mode 100644 index 0000000..99cdb2b --- /dev/null +++ b/test/imode/test_builtin_help.txt @@ -0,0 +1,2 @@ +: cd test +: help diff --git a/test/imode/test_help.out b/test/imode/test_help.out new file mode 100644 index 0000000..dd5489b --- /dev/null +++ b/test/imode/test_help.out @@ -0,0 +1,9 @@ +Test command for lmi-metacommand. + +Usage: + test list <cmd> [<args> ...] + test show pkg <name> + test show repo <name> + +To get help for built-in commands, type: + :help diff --git a/test/imode/test_help.txt b/test/imode/test_help.txt new file mode 100644 index 0000000..e17ac65 --- /dev/null +++ b/test/imode/test_help.txt @@ -0,0 +1,2 @@ +:cd test +help diff --git a/test/imode/test_help_exit.txt b/test/imode/test_help_exit.txt new file mode 100644 index 0000000..5470ee3 --- /dev/null +++ b/test/imode/test_help_exit.txt @@ -0,0 +1,2 @@ +:cd test +help exit diff --git a/test/imode/test_help_list.out b/test/imode/test_help_list.out new file mode 100644 index 0000000..3f81637 --- /dev/null +++ b/test/imode/test_help_list.out @@ -0,0 +1,5 @@ +List various types of software data. + +Usage: + list pkgs [(--installed | --available)] + list repos [(--enabled | --disabled)] diff --git a/test/imode/test_help_list.txt b/test/imode/test_help_list.txt new file mode 100644 index 0000000..6ccd253 --- /dev/null +++ b/test/imode/test_help_list.txt @@ -0,0 +1,2 @@ +:cd test +help list diff --git a/test/imode/test_help_show.out b/test/imode/test_help_show.out new file mode 100644 index 0000000..e1e2510 --- /dev/null +++ b/test/imode/test_help_show.out @@ -0,0 +1,6 @@ +Test command for lmi-metacommand. + +Usage: + test list <cmd> [<args> ...] + test show pkg <name> + test show repo <name> diff --git a/test/imode/test_help_show.txt b/test/imode/test_help_show.txt new file mode 100644 index 0000000..bd8ae57 --- /dev/null +++ b/test/imode/test_help_show.txt @@ -0,0 +1,2 @@ +:cd test +help show diff --git a/test/imode/test_list_opt_help.out b/test/imode/test_list_opt_help.out new file mode 100644 index 0000000..92f76ee --- /dev/null +++ b/test/imode/test_list_opt_help.out @@ -0,0 +1,5 @@ +List various types of software data. + +Usage: + test list pkgs [(--installed | --available)] + test list repos [(--enabled | --disabled)] diff --git a/test/imode/test_opt_help.out b/test/imode/test_opt_help.out new file mode 100644 index 0000000..e1e2510 --- /dev/null +++ b/test/imode/test_opt_help.out @@ -0,0 +1,6 @@ +Test command for lmi-metacommand. + +Usage: + test list <cmd> [<args> ...] + test show pkg <name> + test show repo <name> diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 0000000..03c87ae --- /dev/null +++ b/test/run.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Michal Minar <miminar@redhat.com + +retval=0 +for test in test_*.sh; do + bash ./$test + [ "$retval" = 0 ] && retval=$? +done + +exit $retval diff --git a/test/subcmd/lmi/__init__.py b/test/subcmd/lmi/__init__.py new file mode 100644 index 0000000..f752629 --- /dev/null +++ b/test/subcmd/lmi/__init__.py @@ -0,0 +1,30 @@ +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Michal Minar <miminar@redhat.com> +# +__import__('pkg_resources').declare_namespace(__name__) diff --git a/test/subcmd/lmi/scripts/__init__.py b/test/subcmd/lmi/scripts/__init__.py new file mode 100644 index 0000000..f752629 --- /dev/null +++ b/test/subcmd/lmi/scripts/__init__.py @@ -0,0 +1,30 @@ +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Michal Minar <miminar@redhat.com> +# +__import__('pkg_resources').declare_namespace(__name__) diff --git a/test/subcmd/lmi/scripts/cmdtest/__init__.py b/test/subcmd/lmi/scripts/cmdtest/__init__.py new file mode 100644 index 0000000..9d3acf1 --- /dev/null +++ b/test/subcmd/lmi/scripts/cmdtest/__init__.py @@ -0,0 +1,128 @@ +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Michal Minar <miminar@redhat.com> +# +""" +Test command for lmi-metacommand. + +Usage: + %(cmd)s list <cmd> [<args> ...] + %(cmd)s show pkg <name> + %(cmd)s show repo <name> +""" + +from lmi.scripts.common import errors +from lmi.scripts.common import command + +class ListPackages(command.LmiLister): + OPT_NO_UNDERSCORES = True + COLUMNS = ('Name', 'Arch') + + def execute(self, ns, installed, available): + fltr = lambda a: True + if installed: + fltr = lambda a: a[2] + elif available: + fltr = lambda a: not a[2] + for pkg in PACKAGES: + if fltr(pkg): + yield (pkg[0], pkg[1]) + +class ListRepos(command.LmiLister): + OPT_NO_UNDERSCORES = True + COLUMNS = ('Name', ) + + def execute(self, ns, enabled, disabled): + fltr = lambda a: True + if enabled: + fltr = lambda a: a[1] + elif disabled: + fltr = lambda a: not a[1] + for repo in REPOSITORIES: + if fltr(repo): + yield (repo[0], ) + +class Lister(command.LmiCommandMultiplexer): + """ + List various types of software data. + + Usage: + %(cmd)s pkgs [(--installed | --available)] + %(cmd)s repos [(--enabled | --disabled)] + """ + COMMANDS = { + 'pkgs' : ListPackages, + 'repos' : ListRepos + } + OWN_USAGE = True + +class ShowPackage(command.LmiLister): + COLUMNS = ("Prop", "Value") + + def execute(self, ns, name): + pkgd = { p[0] : p for p in PACKAGES } + if not name in pkgd: + raise errors.LmiFailed('no such package "%s"' % name) + for n, v in zip(('Name', 'Architecture', 'Installed'), pkgd[name]): + yield n, v + +class ShowRepository(command.LmiLister): + COLUMNS = ("Prop", "Value") + + def execute(self, ns, name): + repod = { r[0] : r for r in REPOSITORIES } + if not name in repod: + raise errors.LmiFailed('no such repository "%s"' % name) + for n, v in zip(('Name', 'Enabled'), repod[name]): + yield n, v + +class Show(command.LmiCommandMultiplexer): + COMMANDS = { + 'pkg' : ShowPackage, + 'repo' : ShowRepository + } + +Test = command.register_subcommands( + 'Test', __doc__, + { 'list' : Lister, + 'show' : Show, + }, + ) + +PACKAGES = ( + # Name, Arch, Installed + ('pywbem', 'noarch', True), + ('hwdata', 'noarch', True), + ('tog-pegasus', 'x86_64', False), + ('python-docopt', 'noarch', False)) + +REPOSITORIES = ( + # Name, Enabled + ('fedora', True), + ('updates', False)) + diff --git a/test/subcmd/setup.py b/test/subcmd/setup.py new file mode 100644 index 0000000..20a50a1 --- /dev/null +++ b/test/subcmd/setup.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages + +setup( + name="openlmi-scripts-cmdtest", + version="0.0.1", + description='Test command for lmi', + author='Michal Minar', + author_email='miminar@redhat.com', + url='http://openlmi.org', + platforms=['Any'], + license="BSD", + classifiers=[ + 'License :: OSI Approved :: BSD License', + 'Operating System :: POSIX :: Linux', + 'Topic :: System :: Systems Administration', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Intended Audience :: Developers', + 'Environment :: Console', + ], + + install_requires=['openlmi-scripts >= 0.2.6'], + + namespace_packages=['lmi', 'lmi.scripts'], + packages=['lmi', 'lmi.scripts', 'lmi.scripts.cmdtest'], + include_package_data=True, + + entry_points={ + 'lmi.scripts.cmd': [ + 'test = lmi.scripts.cmdtest:Test', + ], + }, + ) diff --git a/test/test_imode.sh b/test/test_imode.sh new file mode 100644 index 0000000..0718654 --- /dev/null +++ b/test/test_imode.sh @@ -0,0 +1,249 @@ +#!/bin/bash +# +# Copyright (c) 2014, Red Hat, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# The views and conclusions contained in the software and documentation are +# those of the authors and should not be interpreted as representing official +# policies, either expressed or implied, of the FreeBSD Project. +# +# Authors: Michal Minar <miminar@redhat.com> + +. ./base.sh + +# Set the full test name +TEST="openlmi-scripts/test/test_imode.sh" + +# Package being tested +PACKAGE="openlmi-scripts" + +function run_and_compare_output() { + # Run lmi command and compare its output to a contents of given file. + # + # Args: + # command - command to run which is executed in subshell + # expected - file name with expected output stored in imode without + # extension + rlRun -s "$1" 0 + rlRun -l "cmp imode/$2.out $rlRun_LOG" 0 + rm $rlRun_LOG +} + +function run_test_command() { + # Run command in interactive mode with expect script + # `imode/run_test_cmd.exp`. Command is run iteratively from different + # command namespaces. stderr is redirected to /dev/null when running + # the `command`. + # + # Args: + # command - command path (without `lmi` prefix) + # expected - path to a file with expected output + # dir... - command namespaces to nest to before running command + # + # Common prefix of `command` and current namespace is removed from command + # in each iteration before running it. + cmd="$1" + expected_output="$2" + shift 2 + while [ $# -gt 0 ]; do + dir="$1" + cmd_="${cmd#$dir }" + out_file=`mktemp out_fileXXXX` + prompt='lmi> ' + path="$(echo $dir | tr ' ' '/')" + [ "$dir" = '.' ] || prompt=">$(echo $dir | sed 's/\([^ ]\+ \)/>/g')> " + rlRun "expect imode/run_test_cmd.exp $out_file \"$path\" \"$prompt\" \"$cmd_\"" 0 + rlAssertNotDiffer $expected_output $out_file + rm $out_file + shift + done +} + +rlJournalStart + +rlPhaseStartSetup + rlLogInfo "Creating temporary python sandbox" + sandbox=`mktemp -d` + export PYTHONPATH="$sandbox" + pushd .. + rlLogInfo "Installing lmi meta-command" + rlRun "python setup.py develop --install-dir=$sandbox" 0 + popd + export "$sandbox:$PATH" +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Try to import lmi meta-command" + rlRun 'python -c "import lmi.scripts._metacommand"' 0 +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Test exit command" + rlRun "expect imode/exit.exp" 0 + rlRun "expect imode/exit.exp 0" 0 + rlRun "expect imode/exit.exp 1" 1 + rlRun "expect imode/exit.exp 2" 2 + rlRun "expect imode/exit.exp 10" 10 +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Test end of file" + rlRun "expect imode/eof.exp" 0 + rlRun "expect imode/eof.exp ':pwd' '/lmi'" 0 + rlRun 'expect imode/eof.exp ":cd foo" "ERROR: no such subcommand \"foo\""' 0 + rlRun "expect imode/empty_lines.exp" 0 + run_and_compare_output "$LMI < imode/empty_lines.txt" empty_lines +rlPhaseEnd + +rlPhaseStartSetup + rlLogInfo "Installing testing command" + pushd subcmd + rlRun "python setup.py develop --install-dir=$sandbox" + popd +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Check whether test command is installed" + run_and_compare_output "$LMI help 2>/dev/null" lmi_test_help +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Test nesting to subcommands" + rlRun "expect imode/cd_test.exp" 1 +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Test help messages in interactive mode" + run_and_compare_output "echo help | $LMI" "help" + run_and_compare_output "echo help help | $LMI" "help_help" + run_and_compare_output "echo \": help\" | $LMI" "builtin_help" + run_and_compare_output "echo help exit | $LMI 2>/dev/null" "help_exit" + run_and_compare_output "echo help test | $LMI 2>/dev/null" "help_test" + run_and_compare_output "$LMI 2>/dev/null < imode/test_help_exit.txt" \ + "help_exit" + run_and_compare_output "$LMI 2>/dev/null < imode/test_help.txt" \ + "test_help" + run_and_compare_output "$LMI 2>/dev/null < imode/test_builtin_help.txt" \ + "builtin_help" + run_and_compare_output "$LMI 2>/dev/null < imode/test_help_list.txt" \ + "test_help_list" + run_and_compare_output "$LMI 2>/dev/null < imode/test_help_show.txt" \ + "test_help_show" + run_and_compare_output "echo help foo | $LMI" "help_foo" + + rlLogInfo "Test --help" + outfile=`mktemp outfileXXXX` + expected=`mktemp expectedXXXX` + rlRun "expect imode/run_test_cmd.exp $outfile . 'lmi> ' 'test --help'" 0 + rlAssertNotDiffer imode/test_opt_help.out $outfile + + rlRun "expect imode/run_test_cmd.exp $outfile . 'lmi> ' 'test list --help'" 0 + rlAssertNotDiffer imode/test_list_opt_help.out $outfile + + cmd="test list pkgs --help" + rlRun "expect imode/run_test_cmd.exp $outfile . 'lmi> ' '$cmd'" 0 + rlAssertNotDiffer imode/test_list_opt_help.out $outfile + + cmd="test show --help" + rlRun "expect imode/run_test_cmd.exp $outfile . 'lmi> ' '$cmd'" 0 + rlAssertNotDiffer imode/test_opt_help.out $outfile + + cmd="test foo --help" + rlRun "expect imode/run_test_cmd.exp $outfile . 'lmi> ' '$cmd'" 0 + rlAssertNotDiffer imode/test_opt_help.out $outfile + + cmd="list --help" + sed 's/^ test / /' imode/test_list_opt_help.out >$expected + rlRun "expect imode/run_test_cmd.exp $outfile test '>test> ' '$cmd'" 0 + rlAssertNotDiffer $expected $outfile + + cmd="list pkgs --help" + rlRun "expect imode/run_test_cmd.exp $outfile test '>test> ' '$cmd'" 0 + rlAssertNotDiffer $expected $outfile + + cmd="show --help" + rlRun "expect imode/run_test_cmd.exp $outfile test '>test> ' '$cmd'" 0 + rlAssertNotDiffer imode/test_opt_help.out $outfile + + rm $expected $outfile + +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Test completion in interactive mode" + # this also tests exit command nested in subcommand (with 20 as argument) + rlRun "expect imode/completion.exp" 20 + rlRun "expect imode/complete_builtin.exp" 0 +rlPhaseEnd + +rlPhaseStartTest + rlLogInfo "Check behaviour of test subcommand" + + rlLogInfo \ + "First create files in non-interactive mode we will then compare to" + rlRun -s "$LMI test list pkgs 2>/dev/null" 0 + rlAssertEquals "List of all packages is sane" \ + `grep -E '^(tog-pegasus|pywbem|hwdata|python-docopt)' $rlRun_LOG | wc -l` 4 + all_packages=`mktemp all_packagesXXXX` + mv $rlRun_LOG $all_packages + + rlRun -s "$LMI test list pkgs --installed 2>/dev/null" 0 + rlAssertEquals "List of installed packages is sane"\ + `grep -E '^(pywbem|hwdata)' $rlRun_LOG | wc -l` 2 + installed_packages=`mktemp installed_packagesXXXX` + mv $rlRun_LOG $installed_packages + + rlRun -s "$LMI test list repos 2>/dev/null" 0 + rlAssertEquals "List of repositories is sane" \ + `grep -E '^(fedora|updates)' $rlRun_LOG | wc -l` 2 + all_repos=`mktemp all_reposXXXX` + mv $rlRun_LOG $all_repos + + rlRun -s "$LMI test show pkg hwdata 2>/dev/null" 0 + rlAssertEquals "Package description is sane" \ + `grep -E '^(Name|Arch|Installed)' $rlRun_LOG | wc -l` 3 + one_package=`mktemp one_packageXXXX` + mv $rlRun_LOG $one_package + + rlLogInfo "Now compare outputs to interactive mode" + + run_test_command 'test list pkgs' $all_packages \ + '.' 'test' 'test list' + run_test_command 'test list pkgs --installed' $installed_packages \ + '.' 'test' 'test list' + run_test_command 'test list repos' $all_repos \ + '.' 'test' 'test list' + run_test_command 'test show pkg hwdata' $one_package \ + '.' 'test' + + rm $all_packages $installed_packages $all_repos $one_package + +rlPhaseEnd + +rlPhaseStartCleanup + rlLogInfo "Removing temporary python sandbox" + rm -rf "$sandbox" +rlPhaseEnd + +rlJournalPrintText +rlJournalEnd |