diff options
author | jmmv <jmmv@pkgsrc.org> | 2012-08-15 21:21:15 +0000 |
---|---|---|
committer | jmmv <jmmv@pkgsrc.org> | 2012-08-15 21:21:15 +0000 |
commit | 010ff0569d95ba7edb874e4b9098b618222a5523 (patch) | |
tree | c293fc14ce2bb4f29a2f922eea0b6de99c8fb7e4 /sysutils | |
parent | a51c808c6de933fe6a5ebac5199e64984f614203 (diff) | |
download | pkgsrc-010ff0569d95ba7edb874e4b9098b618222a5523.tar.gz |
Update to 1.1:
- Use shtk for the common utilities and configuration file parsing
functionality. The local copies of the "config" and "utils" modules
are gone.
Diffstat (limited to 'sysutils')
-rw-r--r-- | sysutils/sysupgrade/Makefile | 38 | ||||
-rw-r--r-- | sysutils/sysupgrade/PLIST | 4 | ||||
-rw-r--r-- | sysutils/sysupgrade/TODO | 14 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/Kyuafile | 2 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/config.subr | 299 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/config_test.sh | 422 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/sysupgrade.sh | 177 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/sysupgrade_test.sh | 1 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/utils.subr | 125 | ||||
-rw-r--r-- | sysutils/sysupgrade/files/utils_test.sh | 186 |
10 files changed, 122 insertions, 1146 deletions
diff --git a/sysutils/sysupgrade/Makefile b/sysutils/sysupgrade/Makefile index 9dc54dde232..177c4c536e3 100644 --- a/sysutils/sysupgrade/Makefile +++ b/sysutils/sysupgrade/Makefile @@ -1,6 +1,6 @@ -# $NetBSD: Makefile,v 1.1 2012/08/06 17:06:17 jmmv Exp $ +# $NetBSD: Makefile,v 1.2 2012/08/15 21:21:15 jmmv Exp $ -DISTNAME= sysupgrade-1.0 +DISTNAME= sysupgrade-1.1 CATEGORIES= sysutils MASTER_SITES= # empty DISTFILES= # empty @@ -15,8 +15,8 @@ PKG_DESTDIR_SUPPORT= user-destdir WRKSRC= ${WRKDIR} NO_CONFIGURE= YES -UPGRADE_SUBST+= -e 's,@SYSUPGRADE_CACHEDIR@,${CACHEDIR},g' -UPGRADE_SUBST+= -e 's,@SYSUPGRADE_ETCDIR@,${PKG_SYSCONFDIR},g' +BUILD_SUBST+= -e 's,@SYSUPGRADE_CACHEDIR@,${CACHEDIR},g' +BUILD_SUBST+= -e 's,@SYSUPGRADE_ETCDIR@,${PKG_SYSCONFDIR},g' BUILD_DEFS+= VARBASE CACHEDIR= ${VARBASE}/cache/sysupgrade @@ -32,31 +32,25 @@ PKG_SUGGESTED_OPTIONS= tests .include "../../mk/bsd.options.mk" .if $(PKG_OPTIONS:Mtests) -TEST_PROGS= config_test sysupgrade_test utils_test - PLIST_SUBST+= TESTS= . include "../../devel/atf-libs/buildlink3.mk" +BUILD_SUBST+= -e 's,@ATF_SH@,${BUILDLINK_PREFIX.atf-libs}/bin/atf-sh,g' + do-build: build-tests build-tests: cp ${FILESDIR}/Kyuafile ${WRKSRC} -.for file in ${TEST_PROGS} - ${ECHO} '#! ${BUILDLINK_PREFIX.atf-libs}/bin/atf-sh' \ - >${WRKSRC}/${file} - ${CAT} ${FILESDIR}/*.subr ${FILESDIR}/${file}.sh \ - | ${SED} ${UPGRADE_SUBST} >>${WRKSRC}/${file} - ${CHMOD} +x ${WRKSRC}/${file} -.endfor + sed ${BUILD_SUBST} ${FILESDIR}/sysupgrade_test.sh \ + >${WRKSRC}/sysupgrade_test + chmod +x ${WRKSRC}/sysupgrade_test INSTALLATION_DIRS+= tests/sysupgrade do-install: install-tests install-tests: ${INSTALL_DATA} ${WRKSRC}/Kyuafile ${DESTDIR}${PREFIX}/tests/sysupgrade -.for file in ${TEST_PROGS} - ${INSTALL_SCRIPT} ${WRKSRC}/${file} \ + ${INSTALL_SCRIPT} ${WRKSRC}/sysupgrade_test \ ${DESTDIR}${PREFIX}/tests/sysupgrade/ -.endfor do-test: cd ${WRKSRC} && PATH="${WRKSRC}:${PATH}" kyua test @@ -65,14 +59,11 @@ PLIST_SUBST+= TESTS=@comment .endif do-build: - ${ECHO} '#! ${SH}' >${WRKSRC}/sysupgrade - ${ECHO} 'set -e' >>${WRKSRC}/sysupgrade - ${CAT} ${FILESDIR}/*.subr ${FILESDIR}/sysupgrade.sh \ - | ${SED} ${UPGRADE_SUBST} >>${WRKSRC}/sysupgrade - ${ECHO} 'sysupgrade_main "$${@}"' >>${WRKSRC}/sysupgrade - ${CHMOD} +x ${WRKSRC}/sysupgrade + sed ${BUILD_SUBST} ${FILESDIR}/sysupgrade.sh \ + | ${BUILDLINK_PREFIX.shtk}/bin/shtk build \ + -s ${SH} -m sysupgrade_main -o ${WRKSRC}/sysupgrade - .for file in sysupgrade.8 sysupgrade.conf.5 default.conf - sed ${UPGRADE_SUBST} <${FILESDIR}/${file} >${WRKSRC}/${file} + sed ${BUILD_SUBST} <${FILESDIR}/${file} >${WRKSRC}/${file} .endfor INSTALLATION_DIRS+= bin ${PKGMANDIR}/man5 ${PKGMANDIR}/man8 \ @@ -86,4 +77,5 @@ do-install: ${DESTDIR}${PREFIX}/${PKGMANDIR}/man5/ ${INSTALL_DATA} ${WRKSRC}/default.conf ${DESTDIR}${EGDIR} +.include "../../devel/shtk/buildlink3.mk" .include "../../mk/bsd.pkg.mk" diff --git a/sysutils/sysupgrade/PLIST b/sysutils/sysupgrade/PLIST index 2f495d47dff..7f08f68c0b3 100644 --- a/sysutils/sysupgrade/PLIST +++ b/sysutils/sysupgrade/PLIST @@ -1,9 +1,7 @@ -@comment $NetBSD: PLIST,v 1.1 2012/08/06 17:06:17 jmmv Exp $ +@comment $NetBSD: PLIST,v 1.2 2012/08/15 21:21:15 jmmv Exp $ bin/sysupgrade man/man5/sysupgrade.conf.5 man/man8/sysupgrade.8 share/examples/sysupgrade/default.conf ${TESTS}tests/sysupgrade/Kyuafile -${TESTS}tests/sysupgrade/config_test ${TESTS}tests/sysupgrade/sysupgrade_test -${TESTS}tests/sysupgrade/utils_test diff --git a/sysutils/sysupgrade/TODO b/sysutils/sysupgrade/TODO index d02130ac626..f7d9a78e898 100644 --- a/sysutils/sysupgrade/TODO +++ b/sysutils/sysupgrade/TODO @@ -1,3 +1,6 @@ +Things that sysupgrade could do +=============================== + - Deduce the current NetBSD release from /etc/release and the target release from etc.tgz and inform the user about the changes. This will be necessary if the upgrade process needs to apply specific tweaks depending @@ -28,3 +31,14 @@ providing a "quiet mode" flag instead. etcupdate is interactive anyway, so adding more interactive steps (as long as they can be disabled) does not seem a big deal. + + +Things that sysupgrade will NOT do +================================== + +- Non-trivial rollbacks. If rollbacks are ever implemented, they should + be in the form of file system snapshots OR in the form of syspkgs. + Getting sysupgrade to magically store files aside to allow a later + rollback is just too fragile and hard to get right: rollbacks will + rarely will be necessary, but when they are it's very likely that a + tool like this is broken. diff --git a/sysutils/sysupgrade/files/Kyuafile b/sysutils/sysupgrade/files/Kyuafile index 135b420c442..bd7f14095f4 100644 --- a/sysutils/sysupgrade/files/Kyuafile +++ b/sysutils/sysupgrade/files/Kyuafile @@ -2,6 +2,4 @@ syntax("kyuafile", 1) test_suite("sysupgrade") -atf_test_program{name="config_test"} atf_test_program{name="sysupgrade_test"} -atf_test_program{name="utils_test"} diff --git a/sysutils/sysupgrade/files/config.subr b/sysutils/sysupgrade/files/config.subr deleted file mode 100644 index d2f6a90bd35..00000000000 --- a/sysutils/sysupgrade/files/config.subr +++ /dev/null @@ -1,299 +0,0 @@ -# Copyright 2012 Google 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: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * 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. -# * Neither the name of Google Inc. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# 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. - -# \file config.subr -# Configuration file processing and queries. - - -# List of valid configuration variables. -# -# This is initialized by config_init and should remain unchanged thorough the -# execution of the program. -CONFIG_VARS= - - -# List of overrides to apply by config_apply_overrides. -# -# The overrides are recorded and applied separately because they need to happen -# after a configuration file has been loaded. The contents of this list are -# words of the form: set:<variable> or unset:<variable>. For those variables -# listed in set:, there is a corresponding config_override_var_<variable> -# variable containing the new value. -CONFIG_OVERRIDES= - - -# Initializes the configuration module. -# -# \param ... List of configuration variables to recognize in configuration files -# and user overrides. -config_init() { - CONFIG_VARS="${@}" -} - - -# Checks if a configuration variable is known. -# -# \param var Name of the variable to check. -# -# \return True if the variable was registered by config_init, false otherwise. -config_is_valid() { - local var="${1}"; shift - - local known_var - for known_var in ${CONFIG_VARS}; do - if [ "${known_var}" = "${var}" ]; then - return 0 - fi - done - return 1 -} - - -# Checks if a configuration variable is defined. -# -# \param var The name of the variable to check. -# -# \return True if the variable is defined (even if empty), false otherwise. -config_has() { - local var="${1}"; shift - - local is_unset - eval is_unset="\${config_var_${var}-yes_this_is_unset}" - if [ "${is_unset}" = yes_this_is_unset ]; then - return 1 - else - return 0 - fi -} - - -# Gets the value of a defined configuration variable. -# -# \post The value of the variable is printed to stdout, if any. -# -# \post If the variable is not defined, this terminates execution with an error. -# -# \param var Name of the configuration variable to query. -config_get() { - local var="${1}"; shift - - if config_has "${var}"; then - eval echo "\${config_var_${var}}" - else - utils_error "Required configuration variable ${var} not set" - fi -} - - -# Gets the value of configuration variable interpreting it as a boolean. -# -# \param var The variable to query. -# -# \return True if the variable is set to a truth value, false if its value is -# false or if it is not defined. -config_get_bool() { - local var="${1}"; shift - - if config_has "${var}"; then - case "$(config_get "${var}")" in - [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) - return 0 - ;; - [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]) - return 1 - ;; - *) - utils_error "Invalid boolean value in variable ${var}" - ;; - esac - else - return 1 - fi -} - - -# Gets the value of a configuration variable with a default fallback. -# -# \post The value of the variable is printed to stdout, if any. -# -# \param var Name of the configuration variable to query. -# \param default Default value to return if the variable is not defined. -config_get_default() { - local var="${1}"; shift - local default="${1}"; shift - - if config_has "${var}"; then - config_get "${var}" - else - echo "${default}" - fi -} - - -# Sets a configuration variable. -# -# \post Execution terminates if the variable is not valid. -# -# \param var The name of the configuration variable to set. -# \param value The value to set. -config_set() { - local var="${1}"; shift - local value="${1}"; shift - - config_is_valid "${var}" \ - || utils_usage_error "Unknown configuration variable ${var}" - eval "config_var_${var}=\"\${value}\"" -} - - -# Unsets a configuration variable. -# -# \param var The name of the configuration variable to unset. -config_unset() { - local var="${1}"; shift - - config_is_valid "${var}" \ - || utils_usage_error "Unknown configuration variable ${var}" - eval unset "config_var_${var}" -} - - -# Loads a configuration file. -# -# \pre config_init should have been called to register the valid configuration -# variables. Any non-registered configuration variable found in the file will -# not be available through any of the functions in this module. -# -# \post The configuration module is updated with the values defined in the -# configuration file. -# -# \post Any errors in the processing of the configuration file terminate the -# execution of the script. -# -# \param config_file Path to the file to load. -config_load() { - local config_file="${1}"; shift - - [ -e "${config_file}" ] || utils_error "Configuration file ${config_file}" \ - "does not exist" - - # User-facing variables. - local var - for var in ${CONFIG_VARS}; do - unset "${var}" || true - eval local "${var}" - done - - local real_config_file - case "${config_file}" in - */*) real_config_file="${config_file}" ;; - *) real_config_file="./${config_file}" ;; - esac - ( . "${real_config_file}" ) || utils_error "Failed to load configuration" \ - "file ${config_file}" - . "${real_config_file}" - - for var in ${CONFIG_VARS}; do - local value - eval value="\${${var}-unset}" - [ "${value-unset}" != unset ] || continue - - if [ -n "${value}" ]; then - config_set "${var}" "${value}" - else - unset "config_var_${var}" || true - fi - done - - config_apply_overrides -} - - -# Applies recorded overrides to the current configuration. -# -# This is just a helper function for config_load and should not be used -# directly. -# -# \post The configuration data in memory is modified according to the data -# recorded in the overrides. -# -# \post The contents of CONFIG_OVERRIDES is cleared. -config_apply_overrides() { - for override in ${CONFIG_OVERRIDES}; do - case "${override}" in - set:*) - local var="$(echo ${override} | cut -d : -f 2)" - local value - eval value="\"\${config_override_var_${var}}\"" - config_set "${var}" "${value}" - ;; - unset:*) - local var="$(echo ${override} | cut -d : -f 2)" - unset "config_var_${var}" || true - ;; - esac - done - CONFIG_OVERRIDES= -} - - -# Records an override to be applied to the configuration. -# -# Overrides are configuration variables set through the command line. These can -# be set before loading the configuration file with config_load. -# -# \post Any errors in the processing of the configuration override terminate the -# execution of the script. -# -# \param arg An override of the form variable=value. -config_override() { - local arg="${1}"; shift - - case "${arg}" in - *=*) - ;; - *) - utils_usage_error "Invalid configuration override" \ - "${arg}; must be of the form variable=value" - ;; - esac - local var="$(echo "${arg}" | cut -d = -f 1)" - local value="$(echo "${arg}" | cut -d = -f 2-)" - - [ -n "${var}" ] || utils_usage_error "Invalid configuration override" \ - "${arg}; must be of the form variable=value" - config_is_valid "${var}" \ - || utils_usage_error "Unknown configuration variable ${var}" - - if [ -n "${value}" ]; then - eval "config_override_var_${var}=\"${value}\"" - CONFIG_OVERRIDES="${CONFIG_OVERRIDES} set:${var}" - else - CONFIG_OVERRIDES="${CONFIG_OVERRIDES} unset:${var}" - fi -} diff --git a/sysutils/sysupgrade/files/config_test.sh b/sysutils/sysupgrade/files/config_test.sh deleted file mode 100644 index 0de28814523..00000000000 --- a/sysutils/sysupgrade/files/config_test.sh +++ /dev/null @@ -1,422 +0,0 @@ -# Copyright 2012 Google 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: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * 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. -# * Neither the name of Google Inc. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# 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. - - -atf_test_case is_valid__true -is_valid__true_body() { - config_init VAR1 VAR3 - for var in VAR1 VAR3; do - config_is_valid "${var}" || atf_fail "${var} not found" - done -} - - -atf_test_case is_valid__false -is_valid__false_body() { - config_init VAR1 VAR3 - for var in VAR11 VAR2 VAR; do - if config_is_valid "${var}"; then - atf_fail "${var} found but was not registered" - fi - done -} - - -atf_test_case has__true__empty -has__true__empty_body() { - config_init TESTVAR - config_set TESTVAR "" - config_has TESTVAR || atf_fail "Expected variable not found" -} - - -atf_test_case has__true__not_empty -has__true__not_empty_body() { - config_init TESTVAR - config_set TESTVAR "foo" - config_has TESTVAR || atf_fail "Expected variable not found" -} - - -atf_test_case has__false -has__false_body() { - config_init TESTVAR - if config_has TESTVAR; then - atf_fail "Unexpected variable found" - fi -} - - -atf_test_case get__ok__empty -get__ok__empty_body() { - config_init TESTVAR - - config_set TESTVAR "" - [ -z "$(config_get TESTVAR)" ] || atf_fail "Failed to query value" -} - - -atf_test_case get__ok__not_empty -get__ok__not_empty_body() { - config_init TESTVAR - - config_set TESTVAR some-value - [ "$(config_get TESTVAR)" = some-value ] || atf_fail "Failed to query value" -} - - -atf_test_case get__undefined_variable -get__undefined_variable_body() { - config_init TESTVAR - - if ( config_get TESTVAR ) >out 2>err; then - atf_fail "Got unset variable successfully" - else - grep "Required configuration variable TESTVAR not set" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case get_bool__true -get_bool__true_body() { - config_init TESTVAR - - for value in yes Yes true True; do - config_set TESTVAR "${value}" - config_get_bool TESTVAR || atf_fail "Expected true, but got false" - done -} - - -atf_test_case get_bool__false -get_bool__false_body() { - config_init TESTVAR - - for value in no No false False; do - config_set TESTVAR "${value}" - if config_get_bool TESTVAR; then - atf_fail "Expected false, but got true" - fi - done -} - - -atf_test_case get_bool__undefined_variable -get_bool__undefined_variable_body() { - config_init TESTVAR - - if config_get_bool TESTVAR; then - atf_fail "Expected false, but got true" - fi -} - - -atf_test_case get_bool__invalid_value -get_bool__invalid_value_body() { - config_init TESTVAR - - config_set TESTVAR not-a-boolean - if ( config_get_bool TESTVAR ) >out 2>err; then - atf_fail "Got invalid boolean value successfully" - else - grep "Invalid boolean value in variable TESTVAR" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case get_default__defined__empty -get_default__defined__empty_body() { - config_init TESTVAR - config_set TESTVAR "" - [ "$(config_get_default TESTVAR 'foo')" = "" ] \ - || atf_fail "Did not fetch defined value" -} - - -atf_test_case get_default__defined__not_empty -get_default__defined__not_empty_body() { - config_init TESTVAR - config_set TESTVAR "bar" - [ "$(config_get_default TESTVAR 'foo')" = "bar" ] \ - || atf_fail "Did not fetch defined value" -} - - -atf_test_case get_default__default__empty -get_default__default__empty_body() { - config_init TESTVAR - [ "$(config_get_default TESTVAR '')" = "" ] \ - || atf_fail "Did not fetch default value" -} - - -atf_test_case get_default__default__not_empty -get_default__default__not_empty_body() { - config_init TESTVAR - [ "$(config_get_default TESTVAR 'foo')" = "foo" ] \ - || atf_fail "Did not fetch default value" -} - - -atf_test_case set__ok -set__ok_body() { - config_init TESTVAR - - config_set TESTVAR some-value - [ "${config_var_TESTVAR}" = some-value ] || atf_fail "Failed to set value" -} - - -atf_test_case set__unknown_variable -set__unknown_variable_body() { - config_init TESTVAR - - if ( config_set TESTVAR2 some-value ) >out 2>err; then - atf_fail "Set unknown variable successfully" - else - grep "Unknown configuration variable TESTVAR2" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case unset__ok -unset__ok_body() { - config_init TESTVAR - - config_var_TESTVAR=some-value - config_unset TESTVAR - [ "${config_var_TESTVAR-unset}" = unset ] \ - || atf_fail "Failed to unset variable" -} - - -atf_test_case unset__unknown_variable -unset__unknown_variable_body() { - config_init TESTVAR - - if ( config_unset TESTVAR2 ) >out 2>err; then - atf_fail "Unset unknown variable successfully" - else - grep "Unknown configuration variable TESTVAR2" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case load__filter_variables -load__filter_variables_body() { - config_init Z VAR1 EMPTY - - cat >test.conf <<EOF -A=foo -Z=bar -VAR1="some text" -VAR2="some other text" -EOF - - config_load $(pwd)/test.conf || atf_fail "Failed to load test configuration" - - [ "${config_var_Z}" = bar ] || \ - atf_fail "Z not found in configuration" - [ "${config_var_VAR1}" = "some text" ] || \ - atf_fail "VAR1 not found in configuration" - - [ "${config_var_EMPTY-has_not_been_set}" = has_not_been_set ] || \ - atf_fail "Undefined variable set, but should not have been" - - [ "${config_var_A-unset}" = unset ] || \ - atf_fail "A set in configuration, but not expected" - [ "${config_var_VAR2-unset}" = unset ] || \ - atf_fail "VAR2 set in configuration, but not expected" -} - - -atf_test_case load__allow_undefine -load__allow_undefine_body() { - config_init UNDEFINE - - cat >test.conf <<EOF -UNDEFINE= -EOF - - config_set UNDEFINE "remove me" - config_load $(pwd)/test.conf || atf_fail "Failed to load test configuration" - if config_has UNDEFINE; then - atf_fail "Undefine attempt from configuration did not work" - fi -} - - -atf_test_case load__current_directory -load__current_directory_body() { - config_init A - - cat >test.conf <<EOF -A=foo -EOF - - config_load test.conf || atf_fail "Failed to load test configuration" - - [ "${config_var_A}" = foo ] || \ - atf_fail "A not found in configuration" -} - - -atf_test_case load__missing_file -load__missing_file_body() { - if ( config_load missing.conf ) >out 2>err; then - atf_fail "Missing configuration file load succeeded" - else - grep "Configuration file missing.conf does not exist" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case load__invalid_file -load__invalid_file_body() { - echo "this file is invalid" >invalid.conf - - if ( config_load invalid.conf ) >out 2>err; then - atf_fail "Invalid configuration file load succeeded" - else - cat err - grep "Failed to load configuration file invalid.conf" err >/dev/null \ - || atf_fail "Expected error message not found" - fi -} - - -atf_test_case override__ok_before_load -override__ok_before_load_body() { - config_init VAR1 VAR2 - - cat >test.conf <<EOF -VAR1="override me" -VAR2="do not override me" -EOF - - config_override "VAR1=new value" - config_load test.conf || atf_fail "Failed to load test configuration" - - [ "${config_var_VAR1}" = "new value" ] || atf_fail "Override failed" - [ "${config_var_VAR2}" = "do not override me" ] \ - || atf_fail "Overrode more than one variable" -} - - -atf_test_case override__not_ok_after_load -override__not_ok_after_load_body() { - config_init VAR1 VAR2 - - cat >test.conf <<EOF -VAR1="override me" -VAR2="do not override me" -EOF - - config_load test.conf || atf_fail "Failed to load test configuration" - config_override "VAR1=new value" - - [ "${config_var_VAR1}" = "override me" ] \ - || atf_fail "Override succeeded, but it should not have" - [ "${config_var_VAR2}" = "do not override me" ] \ - || atf_fail "Overrode more than one variable" -} - - -atf_test_case override__invalid_format -override__invalid_format_body() { - for arg in foo =bar ''; do - if ( config_override "${arg}" ) >out 2>err; then - atf_fail "Invalid configuration override ${arg} succeeded" - else - cat err - grep "Invalid configuration override ${arg}" err >/dev/null \ - || atf_fail "Expected error message not found" - fi - done -} - - -atf_test_case override__unknown_variable -override__unknown_variable_body() { - config_init Z VAR1 - for arg in A=b VAR2=d; do - if ( config_override "${arg}" ) >out 2>err; then - atf_fail "Invalid configuration override ${arg} succeeded" - else - cat err - grep "Unknown configuration variable ${var}" err >/dev/null \ - || atf_fail "Expected error message not found" - fi - done -} - - -atf_init_test_cases() { - atf_add_test_case is_valid__true - atf_add_test_case is_valid__false - - atf_add_test_case has__true__empty - atf_add_test_case has__true__not_empty - atf_add_test_case has__false - - atf_add_test_case get__ok__empty - atf_add_test_case get__ok__not_empty - atf_add_test_case get__undefined_variable - - atf_add_test_case get_bool__true - atf_add_test_case get_bool__false - atf_add_test_case get_bool__undefined_variable - atf_add_test_case get_bool__invalid_value - - atf_add_test_case get_default__defined__empty - atf_add_test_case get_default__defined__not_empty - atf_add_test_case get_default__default__empty - atf_add_test_case get_default__default__not_empty - - atf_add_test_case set__ok - atf_add_test_case set__unknown_variable - - atf_add_test_case unset__ok - atf_add_test_case unset__unknown_variable - - atf_add_test_case load__filter_variables - atf_add_test_case load__allow_undefine - atf_add_test_case load__current_directory - atf_add_test_case load__missing_file - atf_add_test_case load__invalid_file - - atf_add_test_case override__ok_before_load - atf_add_test_case override__not_ok_after_load - atf_add_test_case override__invalid_format - atf_add_test_case override__unknown_variable -} diff --git a/sysutils/sysupgrade/files/sysupgrade.sh b/sysutils/sysupgrade/files/sysupgrade.sh index 78b97605521..7631391c0cb 100644 --- a/sysutils/sysupgrade/files/sysupgrade.sh +++ b/sysutils/sysupgrade/files/sysupgrade.sh @@ -29,6 +29,11 @@ # \file sysupgrade.sh # Entry point and main program logic. +shtk_import cli +shtk_import config +shtk_import list +shtk_import process + # List of valid configuration variables. # @@ -56,11 +61,11 @@ SYSUPGRADE_CONFIG_VARS="AUTOCLEAN CACHEDIR DESTDIR ETCUPDATE KERNEL # we let him shoot himself in the foot if he so desires. sysupgrade_set_defaults() { # Please remember to update sysupgrade(8) if you change any default values. - config_set AUTOCLEAN "yes" - config_set CACHEDIR "${SYSUPGRADE_CACHEDIR}" - config_set ETCUPDATE "yes" - config_set KERNEL "AUTO" - config_set SETS "AUTO" + shtk_config_set AUTOCLEAN "yes" + shtk_config_set CACHEDIR "${SYSUPGRADE_CACHEDIR}" + shtk_config_set ETCUPDATE "yes" + shtk_config_set KERNEL "AUTO" + shtk_config_set SETS "AUTO" } @@ -70,27 +75,27 @@ sysupgrade_set_defaults() { # It takes care of performing any required post-processing on the configuration # variables, such as expanding the magic AUTO keyword to the actual value. sysupgrade_auto_config() { - if [ "$(config_get_default KERNEL "")" = "AUTO" ]; then - local kernel="$(config_get_default DESTDIR "")/netbsd" + if [ "$(shtk_config_get_default KERNEL "")" = "AUTO" ]; then + local kernel="$(shtk_config_get_default DESTDIR "")/netbsd" if [ -e "${kernel}" ]; then local kernel_name="$(config -x "${kernel}" | head -n 1 \ | cut -d \" -f 2)" - [ -n "${kernel_name}" ] || utils_error "Failed to determine" \ + [ -n "${kernel_name}" ] || shtk_cli_error "Failed to determine" \ "kernel name; please set KERNEL explicitly" - config_set KERNEL "${kernel_name}" + shtk_config_set KERNEL "${kernel_name}" else - config_unset KERNEL + shtk_config_unset KERNEL fi fi - if [ "$(config_get_default SETS "")" = "AUTO" ]; then - local mtree="$(config_get_default DESTDIR "")/etc/mtree/" + if [ "$(shtk_config_get_default SETS "")" = "AUTO" ]; then + local mtree="$(shtk_config_get_default DESTDIR "")/etc/mtree/" if [ -d "${mtree}" ]; then local all_sets="$(cd "${mtree}" && echo set.* \ | sed -e 's,set\.,,g')" - config_set SETS "${all_sets}" + shtk_config_set SETS "${all_sets}" else - config_unset SETS + shtk_config_unset SETS fi fi } @@ -108,19 +113,19 @@ sysupgrade_config() { ;; \?) - utils_usage_error "Unknown option -${OPTARG}" + shtk_cli_usage_error "Unknown option -${OPTARG}" ;; esac done shift $((${OPTIND} - 1)) - [ ${#} -eq 0 ] || utils_usage_error "config does not take any arguments" + [ ${#} -eq 0 ] || shtk_cli_usage_error "config does not take any arguments" [ "${eval_auto}" = no ] || sysupgrade_auto_config for var in ${SYSUPGRADE_CONFIG_VARS}; do - if config_has "${var}"; then - echo "${var} = $(config_get "${var}")" + if shtk_config_has "${var}"; then + echo "${var} = $(shtk_config_get "${var}")" else echo "${var} is undefined" fi @@ -136,7 +141,7 @@ sysupgrade_config() { get_set() { local set_name="${1}"; shift - echo "$(config_get CACHEDIR)/${set_name}.tgz" + echo "$(shtk_config_get CACHEDIR)/${set_name}.tgz" } @@ -147,8 +152,8 @@ require_set() { local set_name="${1}"; shift local set_tgz="$(get_set "${set_name}")" - [ -f "${set_tgz}" ] || utils_error "Cannot find ${set_name}; did you run" \ - "'${Utils_ProgName} fetch' first?" + [ -f "${set_tgz}" ] || shtk_cli_error "Cannot find ${set_name}; did you run" \ + "'$(shtk_cli_progname) fetch' first?" } @@ -163,11 +168,11 @@ extract_set() { require_set "${set_name}" - local destdir="$(config_get_default DESTDIR "")" + local destdir="$(shtk_config_get_default DESTDIR "")" local set_tgz="$(get_set "${set_name}")" - utils_info "Extracting ${set_name} into ${destdir}/" - [ -z "${destdir}" ] || utils_run mkdir -p "${destdir}" + shtk_cli_info "Extracting ${set_name} into ${destdir}/" + [ -z "${destdir}" ] || shtk_process_run mkdir -p "${destdir}" progress -zf "${set_tgz}" tar -xp -C "${destdir}/" -f - } @@ -177,15 +182,15 @@ extract_set() { # \param releasedir Optional override of the release directory to use. sysupgrade_fetch() { [ ${#} -lt 2 ] \ - || utils_usage_error "fetch takes zero or one arguments" + || shtk_cli_usage_error "fetch takes zero or one arguments" - [ -z "${1}" ] || config_set "RELEASEDIR" "${1}" + [ -z "${1}" ] || shtk_config_set "RELEASEDIR" "${1}" - local releasedir="$(config_get RELEASEDIR)" - local cachedir="$(config_get CACHEDIR)" - local fetch_sets="$(config_get SETS)" - if config_has KERNEL; then - fetch_sets="${fetch_sets} kern-$(config_get KERNEL)" + local releasedir="$(shtk_config_get RELEASEDIR)" + local cachedir="$(shtk_config_get CACHEDIR)" + local fetch_sets="$(shtk_config_get SETS)" + if shtk_config_has KERNEL; then + fetch_sets="${fetch_sets} kern-$(shtk_config_get KERNEL)" fi case "${releasedir}" in @@ -195,13 +200,13 @@ sysupgrade_fetch() { for set_name in ${fetch_sets}; do local file="${cachedir}/${set_name}.tgz" if [ -f "${file}" ]; then - utils_warning "Reusing existing ${file}" + shtk_cli_warning "Reusing existing ${file}" else local url="${releasedir}/binary/sets/${set_name}.tgz" - utils_info "Downloading ${url} into ${cachedir}" + shtk_cli_info "Downloading ${url} into ${cachedir}" rm -f "${file}" ftp -R -o"${file}.tmp" "${url}" \ - || utils_error "Failed to fetch ${url}" + || shtk_cli_error "Failed to fetch ${url}" mv "${file}.tmp" "${file}" fi done @@ -212,15 +217,15 @@ sysupgrade_fetch() { for set_name in ${fetch_sets}; do local src="${releasedir}/binary/sets/${set_name}.tgz" - utils_info "Linking local ${src} into ${cachedir}" - [ -f "${src}" ] || utils_error "Cannot open ${src}" + shtk_cli_info "Linking local ${src} into ${cachedir}" + [ -f "${src}" ] || shtk_cli_error "Cannot open ${src}" ln -s -f "${src}" "${cachedir}/${set_name}.tgz" \ - || utils_error "Failed to link ${src} into ${cachedir}" + || shtk_cli_error "Failed to link ${src} into ${cachedir}" done ;; *) - utils_error "Don't know how to fetch from ${releasedir}; must" \ + shtk_cli_error "Don't know how to fetch from ${releasedir}; must" \ "be an absolute path or an FTP/HTTP site" ;; esac @@ -232,25 +237,25 @@ sysupgrade_fetch() { # \param kernel_name Name of the kernel set to use; optional. sysupgrade_kernel() { [ ${#} -lt 2 ] \ - || utils_usage_error "kernel takes zero or one arguments" + || shtk_cli_usage_error "kernel takes zero or one arguments" local kernel_name if [ -n "${1}" ]; then kernel_name="${1}" - elif config_has KERNEL; then - kernel_name="$(config_get KERNEL)" + elif shtk_config_has KERNEL; then + kernel_name="$(shtk_config_get KERNEL)" else - utils_info "Skipping kernel installation (KERNEL not set)" + shtk_cli_info "Skipping kernel installation (KERNEL not set)" return 0 fi require_set "kern-${kernel_name}" - local destdir="$(config_get_default DESTDIR "")" - utils_info "Upgrading kernel using ${kernel_name} in ${destdir}/" + local destdir="$(shtk_config_get_default DESTDIR "")" + shtk_cli_info "Upgrading kernel using ${kernel_name} in ${destdir}/" if [ -f "${destdir}/netbsd" ]; then - utils_info "Backing up 'netbsd' kernel as 'onetbsd'" + shtk_cli_info "Backing up 'netbsd' kernel as 'onetbsd'" cp "${destdir}/netbsd" "${destdir}/onetbsd" fi extract_set "kern-${kernel_name}" @@ -260,14 +265,14 @@ sysupgrade_kernel() { # Installs new kernel modules. sysupgrade_modules() { [ ${#} -eq 0 ] \ - || utils_usage_error "modules does not take any arguments" + || shtk_cli_usage_error "modules does not take any arguments" - if ! utils_contains modules $(config_get SETS); then - utils_info "Skipping modules installation (modules not in SETS)" + if ! shtk_list_contains modules $(shtk_config_get SETS); then + shtk_cli_info "Skipping modules installation (modules not in SETS)" return 0 fi - utils_info "Upgrading kernel modules" + shtk_cli_info "Upgrading kernel modules" extract_set modules } @@ -277,10 +282,10 @@ sysupgrade_modules() { # # \param ... Names of the sets to extract, to override SETS. sysupgrade_sets() { - utils_info "Upgrading base system" + shtk_cli_info "Upgrading base system" local sets= - for set_name in "${@:-$(config_get SETS)}"; do + for set_name in "${@:-$(shtk_config_get SETS)}"; do case "${set_name}" in *etc) ;; # Handled by etcupdate. kern-*) ;; # Handled by kernel. @@ -301,20 +306,20 @@ sysupgrade_sets() { # Runs etcupdate to install new configuration files. sysupgrade_etcupdate() { - [ ${#} -eq 0 ] || utils_usage_error "etcupdate does not take any arguments" + [ ${#} -eq 0 ] || shtk_cli_usage_error "etcupdate does not take any arguments" - if config_has DESTDIR; then - utils_info "Skipping etcupdate (DESTDIR upgrades not supported)" + if shtk_config_has DESTDIR; then + shtk_cli_info "Skipping etcupdate (DESTDIR upgrades not supported)" return 0 fi - local sets="$(utils_filter '*etc' $(config_get SETS))" + local sets="$(shtk_list_filter '*etc' $(shtk_config_get SETS))" if [ -z "${sets}" ]; then - utils_info "Skipping etcupdate (no etc sets in SETS)" + shtk_cli_info "Skipping etcupdate (no etc sets in SETS)" return 0 fi - if ! utils_contains etc ${sets}; then - utils_info "Skipping etcupdate (required etc not in SETS)" + if ! shtk_list_contains etc ${sets}; then + shtk_cli_info "Skipping etcupdate (required etc not in SETS)" return 0 fi @@ -324,7 +329,7 @@ sysupgrade_etcupdate() { sflags="${sflags} -s$(get_set "${set_name}")" done - utils_info "Upgrading /etc interactively" + shtk_cli_info "Upgrading /etc interactively" etcupdate -a -l ${sflags} } @@ -334,13 +339,13 @@ sysupgrade_etcupdate() { # \param ... Arguments to pass to postinstall(8). sysupgrade_postinstall() { local sets= - local sets="$(utils_filter '*etc' $(config_get SETS))" + local sets="$(shtk_list_filter '*etc' $(shtk_config_get SETS))" if [ -z "${sets}" ]; then - utils_info "Skipping postinstall (no etc sets in SETS)" + shtk_cli_info "Skipping postinstall (no etc sets in SETS)" return 0 fi - if ! utils_contains etc ${sets}; then - utils_info "Skipping postinstall (required etc not in SETS)" + if ! shtk_list_contains etc ${sets}; then + shtk_cli_info "Skipping postinstall (required etc not in SETS)" return 0 fi @@ -350,23 +355,23 @@ sysupgrade_postinstall() { sflags="${sflags} -s$(get_set "${set_name}")" done - utils_info "Performing postinstall checks" - local destdir="$(config_get_default DESTDIR "")" - if config_has POSTINSTALL_AUTOFIX; then + shtk_cli_info "Performing postinstall checks" + local destdir="$(shtk_config_get_default DESTDIR "")" + if shtk_config_has POSTINSTALL_AUTOFIX; then postinstall "-d${destdir}/" ${sflags} fix \ - $(config_get POSTINSTALL_AUTOFIX) + $(shtk_config_get POSTINSTALL_AUTOFIX) fi postinstall "-d${destdir}/" ${sflags} "${@:-check}" \ - || utils_error "Some postinstall(8) checks have failed" + || shtk_cli_error "Some postinstall(8) checks have failed" } # Cleans up the cache directory. sysupgrade_clean() { - [ ${#} -eq 0 ] || utils_usage_error "clean does not take any arguments" + [ ${#} -eq 0 ] || shtk_cli_usage_error "clean does not take any arguments" - utils_info "Cleaning downloaded files" - rm -f "$(config_get CACHEDIR)"/*.tgz* + shtk_cli_info "Cleaning downloaded files" + rm -f "$(shtk_config_get CACHEDIR)"/*.tgz* } @@ -378,23 +383,23 @@ sysupgrade_clean() { # \param releasedir Optional override of the release directory to use. sysupgrade_auto() { [ ${#} -lt 2 ] \ - || utils_usage_error "auto takes zero or one arguments" + || shtk_cli_usage_error "auto takes zero or one arguments" - [ -z "${1}" ] || config_set "RELEASEDIR" "${1}" + [ -z "${1}" ] || shtk_config_set "RELEASEDIR" "${1}" local stages= stages="fetch modules kernel sets" - config_get_bool "ETCUPDATE" && stages="${stages} etcupdate" + shtk_config_get_bool "ETCUPDATE" && stages="${stages} etcupdate" stages="${stages} postinstall" - config_get_bool "AUTOCLEAN" && stages="${stages} clean" + shtk_config_get_bool "AUTOCLEAN" && stages="${stages} clean" - utils_info "Starting auto-update with stages: ${stages}" + shtk_cli_info "Starting auto-update with stages: ${stages}" for stage in ${stages}; do sysupgrade_${stage} done - config_get_bool "AUTOCLEAN" || utils_info "Distribution sets not deleted;" \ - "further ${Utils_ProgName} commands will reuse them" + shtk_config_get_bool "AUTOCLEAN" || shtk_cli_info "Distribution sets not deleted;" \ + "further $(shtk_cli_progname) commands will reuse them" } @@ -406,7 +411,7 @@ sysupgrade_auto() { sysupgrade_main() { local config_file="${SYSUPGRADE_ETCDIR}/sysupgrade.conf" - config_init ${SYSUPGRADE_CONFIG_VARS} + shtk_config_init ${SYSUPGRADE_CONFIG_VARS} while getopts ':c:d:o:' arg "${@}"; do case "${arg}" in @@ -415,21 +420,21 @@ sysupgrade_main() { ;; d) # Path to the destdir. - config_set DESTDIR "${OPTARG}" + shtk_config_set DESTDIR "${OPTARG}" ;; o) # Override for a particular configuration variable. - config_override "${OPTARG}" + shtk_config_override "${OPTARG}" ;; \?) - utils_usage_error "Unknown option -${OPTARG}" + shtk_cli_usage_error "Unknown option -${OPTARG}" ;; esac done shift $((${OPTIND} - 1)) - [ ${#} -ge 1 ] || utils_usage_error "No command specified" + [ ${#} -ge 1 ] || shtk_cli_usage_error "No command specified" local exit_code=0 @@ -437,7 +442,7 @@ sysupgrade_main() { case "${command}" in auto|clean|etcupdate|fetch|kernel|modules|sets|postinstall) sysupgrade_set_defaults - config_load "${config_file}" + shtk_config_load "${config_file}" sysupgrade_auto_config "sysupgrade_$(echo "${command}" | tr - _)" "${@}" \ || exit_code="${?}" @@ -445,13 +450,13 @@ sysupgrade_main() { config) sysupgrade_set_defaults - config_load "${config_file}" + shtk_config_load "${config_file}" "sysupgrade_$(echo "${command}" | tr - _)" "${@}" \ || exit_code="${?}" ;; *) - utils_usage_error "Unknown command ${command}" + shtk_cli_usage_error "Unknown command ${command}" ;; esac diff --git a/sysutils/sysupgrade/files/sysupgrade_test.sh b/sysutils/sysupgrade/files/sysupgrade_test.sh index 6c9b2b1dc88..af13dfdf154 100644 --- a/sysutils/sysupgrade/files/sysupgrade_test.sh +++ b/sysutils/sysupgrade/files/sysupgrade_test.sh @@ -1,3 +1,4 @@ +#! @ATF_SH@ # Copyright 2012 Google Inc. # All rights reserved. # diff --git a/sysutils/sysupgrade/files/utils.subr b/sysutils/sysupgrade/files/utils.subr deleted file mode 100644 index 2d63bb78d1f..00000000000 --- a/sysutils/sysupgrade/files/utils.subr +++ /dev/null @@ -1,125 +0,0 @@ -# Copyright 2012 Google 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: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * 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. -# * Neither the name of Google Inc. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# 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. - -# \file utils.subr -# Miscellaneous utility functions and global variables. - - -# Base name of the running script. -Utils_ProgName="${0##*/}" - - -# Prints a runtime error and exits. -# -# \param ... The message to print. Can be provided as multiple words and, in -# that case, they are joined together by a single whitespace. -utils_error() { - echo "${Utils_ProgName}: E: $*" 1>&2 - exit 1 -} - - -# Prints an informational message. -# -# \param ... The message to print. Can be provided as multiple words and, in -# that case, they are joined together by a single whitespace. -utils_info() { - echo "${Utils_ProgName}: I: $*" 1>&2 -} - - -# Prints a runtime warning. -# -# \param ... The message to print. Can be provided as multiple words and, in -# that case, they are joined together by a single whitespace. -utils_warning() { - echo "${Utils_ProgName}: W: $*" 1>&2 -} - - -# Prints an usage error and exits. -# -# \param ... The message to print. Can be provided as multiple words and, in -# that case, they are joined together by a single whitespace. -utils_usage_error() { - echo "${Utils_ProgName}: E: $*" 1>&2 - echo "Type 'man ${Utils_ProgName}' for help" 1>&2 - exit 1 -} - - -# Executes a command with logging. -# -# \param ... The command to execute. -# -# \return The exit code of the executed command. -utils_run() { - utils_info "Running '${@}' in $(pwd)" - local ret=0 - "${@}" || ret="${?}" - if [ ${ret} -eq 0 ]; then - utils_info "Command finished successfully" - else - utils_warning "Command failed with code ${ret}" - fi - return "${ret}" -} - - -# Looks for an item in a list of items -# -# \param item The item to look for. -# \param ... Contents of the list in which to search for the item. -# -# \return 0 if the item is found, 1 otherwise. -utils_contains() { - local item="${1}"; shift - - while [ ${#} -gt 0 ]; do - [ "${1}" != "${item}" ] || return 0 - shift - done - return 1 -} - - -# Filters a list based on a pattern. -# -# \param pattern Pattern to use for filtering. -# \param ... Contents of the list to filter. -# -# \post The matched elements are printed on stdout. -utils_filter() { - local pattern="${1}"; shift - - for arg in "${@}"; do - case "${arg}" in - ${pattern}) echo "${arg}" ;; - esac - done -} diff --git a/sysutils/sysupgrade/files/utils_test.sh b/sysutils/sysupgrade/files/utils_test.sh deleted file mode 100644 index 48c7570236d..00000000000 --- a/sysutils/sysupgrade/files/utils_test.sh +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright 2012 Google 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: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * 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. -# * Neither the name of Google Inc. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# 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. - - -atf_test_case global_progname -global_progname_body() { - [ "${Utils_ProgName}" = utils_test ] \ - || atf_fail "Invalid value in Utils_ProgName" -} - - -atf_test_case error -error_body() { - if ( utils_error "This is" "a message"; echo "not seen" ) >out 2>err; then - atf_fail "utils_error did not exit with an error" - else - grep "utils_test: E: This is a message" err >/dev/null \ - || atf_fail "Expected error message not found" - [ ! -s out ] || atf_fail "Unexpected output in stdout" - fi -} - - -atf_test_case info -info_body() { - ( utils_info "This is" "a message"; echo "continuing" ) >out 2>err - grep "utils_test: I: This is a message" err >/dev/null \ - || atf_fail "Expected info message not found" - grep "continuing" out >/dev/null || atf_fail "Execution aborted" -} - - -atf_test_case usage_error -usage_error_body() { - if ( utils_usage_error "This is" "a message"; echo "not seen" ) >out 2>err - then - atf_fail "utils_usage_error did not exit with an error" - else - grep "utils_test: E: This is a message" err >/dev/null \ - || atf_fail "Expected error message not found" - grep "Type 'man utils_test' for help" err >/dev/null \ - || atf_fail "Expected instructional message not found" - [ ! -s out ] || atf_fail "Unexpected output in stdout" - fi -} - - -atf_test_case warning -warning_body() { - ( utils_warning "This is" "a message"; echo "continuing" ) >out 2>err - grep "utils_test: W: This is a message" err >/dev/null \ - || atf_fail "Expected info message not found" - grep "continuing" out >/dev/null || atf_fail "Execution aborted" -} - - -atf_test_case run__ok -run__ok_body() { - cat >helper.sh <<EOF -#! /bin/sh -echo "This exits cleanly:" "\${@}" -exit 0 -EOF - chmod +x helper.sh - - utils_run ./helper.sh one two three >out 2>err \ - || atf_fail "Got an unexpected error code" - - cat >expout <<EOF -This exits cleanly: one two three -EOF - atf_check -o file:expout cat out - - cat >experr <<EOF -utils_test: I: Running './helper.sh one two three' in $(pwd) -utils_test: I: Command finished successfully -EOF - atf_check -o file:experr cat err -} - - -atf_test_case run__fail -run__fail_body() { - cat >helper.sh <<EOF -#! /bin/sh -echo "This exits with an error:" "\${@}" -exit 42 -EOF - chmod +x helper.sh - - code=0 - utils_run ./helper.sh one two three >out 2>err || code="${?}" - [ ${code} -eq 42 ] \ - || atf_fail "Did not get the expected error code; got ${code}" - - cat >expout <<EOF -This exits with an error: one two three -EOF - atf_check -o file:expout cat out - - cat >experr <<EOF -utils_test: I: Running './helper.sh one two three' in $(pwd) -utils_test: W: Command failed with code 42 -EOF - atf_check -o file:experr cat err -} - - -atf_test_case contains__yes -contains__yes_body() { - items="bar foo baz" - utils_contains foo ${items} || atf_fail "Element not found in list" -} - - -atf_test_case contains__no -contains__no_body() { - items="bar foo baz" - ! utils_contains fo ${items} || atf_fail "Element found in list" -} - - -atf_test_case filter__no_items -filter__no_items_body() { - atf_check_equal "" "$(utils_filter '*')" -} - - -atf_test_case filter__no_results -filter__no_results_body() { - items="abc a1 foo a2 a3 bar" - atf_check_equal "" "$(utils_filter '*a' ${items})" -} - - -atf_test_case filter__some_results -filter__some_results_body() { - items="abc a1 foo a2 a3 bar" - atf_check_equal "a1 a2 a3" "$(utils_filter 'a[0-9]*' ${items})" -} - - -atf_init_test_cases() { - atf_add_test_case global_progname - - atf_add_test_case error - atf_add_test_case info - atf_add_test_case warning - - atf_add_test_case usage_error - - atf_add_test_case run__ok - atf_add_test_case run__fail - - atf_add_test_case contains__yes - atf_add_test_case contains__no - - atf_add_test_case filter__no_items - atf_add_test_case filter__no_results - atf_add_test_case filter__some_results -} |