Merge pull request #1192 from mvdan/shfmt

Add shell formatting via shfmt
This commit is contained in:
Qiang Huang 2016-11-19 09:42:58 +08:00 committed by GitHub
commit 692254b91d
7 changed files with 325 additions and 309 deletions

View File

@ -36,6 +36,15 @@ RUN mkdir -p /usr/src/criu \
&& cd /usr/src/criu \ && cd /usr/src/criu \
&& make install-criu && make install-criu
# install shfmt
RUN mkdir -p /go/src/github.com/mvdan \
&& cd /go/src/github.com/mvdan \
&& git clone https://github.com/mvdan/sh \
&& cd sh \
&& git checkout -f v0.4.0 \
&& go install ./cmd/shfmt \
&& rm -rf /go/src/github.com/mvdan
# setup a playground for us to spawn containers in # setup a playground for us to spawn containers in
ENV ROOTFS /busybox ENV ROOTFS /busybox
RUN mkdir -p ${ROOTFS} \ RUN mkdir -p ${ROOTFS} \

View File

@ -119,6 +119,7 @@ clean:
validate: validate:
script/validate-gofmt script/validate-gofmt
script/validate-shfmt
go vet ./... go vet ./...
ci: validate localtest ci: validate localtest

View File

@ -16,7 +16,6 @@
# Configuration: # Configuration:
# #
# Note for developers: # Note for developers:
# Please arrange options sorted alphabetically by long name with the short # Please arrange options sorted alphabetically by long name with the short
# options immediately following their corresponding long form. # options immediately following their corresponding long form.
@ -26,7 +25,7 @@ __runc_previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob shopt -s extglob
__runc_list_all() { __runc_list_all() {
COMPREPLY=( $( compgen -W "$(runc list -q)" -- $cur) ) COMPREPLY=($(compgen -W "$(runc list -q)" -- $cur))
} }
__runc_pos_first_nonflag() { __runc_pos_first_nonflag() {
@ -35,17 +34,16 @@ __runc_pos_first_nonflag() {
local counter=$((${subcommand_pos:-${command_pos}} + 1)) local counter=$((${subcommand_pos:-${command_pos}} + 1))
while [ $counter -le $cword ]; do while [ $counter -le $cword ]; do
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
(( counter++ )) ((counter++))
else else
case "${words[$counter]}" in case "${words[$counter]}" in
-*) -*) ;;
;;
*) *)
break break
;; ;;
esac esac
fi fi
(( counter++ )) ((counter++))
done done
echo $counter echo $counter
@ -55,7 +53,7 @@ __runc_pos_first_nonflag() {
# with the words separated by "|". # with the words separated by "|".
# This is used to prepare arguments to __runc_pos_first_nonflag(). # This is used to prepare arguments to __runc_pos_first_nonflag().
__runc_to_alternatives() { __runc_to_alternatives() {
local parts=( $1 ) local parts=($1)
local IFS='|' local IFS='|'
echo "${parts[*]}" echo "${parts[*]}"
} }
@ -63,7 +61,7 @@ __runc_to_alternatives() {
# Transforms a multiline list of options into an extglob pattern # Transforms a multiline list of options into an extglob pattern
# suitable for use in case statements. # suitable for use in case statements.
__runc_to_extglob() { __runc_to_extglob() {
local extglob=$( __runc_to_alternatives "$1" ) local extglob=$(__runc_to_alternatives "$1")
echo "@($extglob)" echo "@($extglob)"
} }
@ -83,7 +81,7 @@ __runc_subcommands() {
local counter=$(($command_pos + 1)) local counter=$(($command_pos + 1))
while [ $counter -lt $cword ]; do while [ $counter -lt $cword ]; do
case "${words[$counter]}" in case "${words[$counter]}" in
$(__runc_to_extglob "$subcommands") ) $(__runc_to_extglob "$subcommands"))
subcommand_pos=$counter subcommand_pos=$counter
local subcommand=${words[$counter]} local subcommand=${words[$counter]}
local completions_func=_runc_${command}_${subcommand} local completions_func=_runc_${command}_${subcommand}
@ -91,14 +89,14 @@ __runc_subcommands() {
return 0 return 0
;; ;;
esac esac
(( counter++ )) ((counter++))
done done
return 1 return 1
} }
# List all Signals # List all Signals
__runc_list_signals() { __runc_list_signals() {
COMPREPLY=( $( compgen -W "$(for i in $(kill -l | xargs); do echo $i; done | grep SIG)")) COMPREPLY=($(compgen -W "$(for i in $(kill -l | xargs); do echo $i; done | grep SIG)"))
} }
# suppress trailing whitespace # suppress trailing whitespace
@ -109,7 +107,7 @@ __runc_nospace() {
# The list of capabilities is defined in types.go, ALL was added manually. # The list of capabilities is defined in types.go, ALL was added manually.
__runc_complete_capabilities() { __runc_complete_capabilities() {
COMPREPLY=( $( compgen -W " COMPREPLY=($(compgen -W "
ALL ALL
AUDIT_CONTROL AUDIT_CONTROL
AUDIT_WRITE AUDIT_WRITE
@ -149,10 +147,9 @@ __runc_complete_capabilities() {
SYS_TIME SYS_TIME
SYS_TTY_CONFIG SYS_TTY_CONFIG
WAKE_ALARM WAKE_ALARM
" -- "$cur" ) ) " -- "$cur"))
} }
_runc_exec() { _runc_exec() {
local boolean_options=" local boolean_options="
--help --help
@ -176,18 +173,16 @@ _runc_exec() {
local all_options="$options_with_args $boolean_options" local all_options="$options_with_args $boolean_options"
case "$prev" in case "$prev" in
--cap|-c) --cap | -c)
__runc_complete_capabilities __runc_complete_capabilities
return return
;; ;;
--console|--cwd|--process|--apparmor) --console | --cwd | --process | --apparmor)
case "$cur" in case "$cur" in
*:*) *:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
;;
'') '')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace __runc_nospace
;; ;;
/*) /*)
@ -197,19 +192,19 @@ _runc_exec() {
esac esac
return return
;; ;;
--env|-e) --env | -e)
COMPREPLY=( $( compgen -e -- "$cur" ) ) COMPREPLY=($(compgen -e -- "$cur"))
__runc_nospace __runc_nospace
return return
;; ;;
$(__runc_to_extglob "$options_with_args") ) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) ) COMPREPLY=($(compgen -W "$all_options" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -233,13 +228,11 @@ _runc_runc() {
" "
case "$prev" in case "$prev" in
--log|--root|--criu) --log | --root | --criu)
case "$cur" in case "$cur" in
*:*) *:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
;;
'') '')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace __runc_nospace
;; ;;
*) *)
@ -251,23 +244,23 @@ _runc_runc() {
;; ;;
--log-format) --log-format)
COMPREPLY=( $( compgen -W 'text json' -- "$cur" ) ) COMPREPLY=($(compgen -W 'text json' -- "$cur"))
return return
;; ;;
$(__runc_to_extglob "$options_with_args") ) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
local counter=$( __runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args") ) local counter=$(__runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args"))
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) ) COMPREPLY=($(compgen -W "${commands[*]} help" -- "$cur"))
fi fi
;; ;;
esac esac
@ -281,11 +274,11 @@ _runc_pause() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -297,11 +290,11 @@ _runc_ps() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -313,11 +306,11 @@ _runc_delete() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -330,18 +323,18 @@ _runc_kill() {
" "
case "$prev" in case "$prev" in
"kill") "kill")
__runc_list_all __runc_list_all
return return
;; ;;
*) *)
__runc_list_signals __runc_list_signals
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -360,14 +353,14 @@ _runc_events() {
" "
case "$prev" in case "$prev" in
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -388,22 +381,22 @@ _runc_list() {
" "
case "$prev" in case "$prev" in
--format|-f) --format | -f)
COMPREPLY=( $( compgen -W 'text json' -- "$cur" ) ) COMPREPLY=($(compgen -W 'text json' -- "$cur"))
return return
;; ;;
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
local counter=$( __runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args") ) local counter=$(__runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args"))
;; ;;
esac esac
} }
@ -419,31 +412,31 @@ _runc_spec() {
" "
case "$prev" in case "$prev" in
--bundle|-b) --bundle | -b)
case "$cur" in case "$cur" in
'') '')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace __runc_nospace
;;
/*)
_filedir
__runc_nospace
;;
esac
return
;; ;;
/*)
_filedir
__runc_nospace
;;
esac
return
;;
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
local counter=$( __runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args") ) local counter=$(__runc_pos_first_nonflag $(__runc_to_extglob "$options_with_args"))
;; ;;
esac esac
} }
@ -466,32 +459,32 @@ _runc_run() {
" "
case "$prev" in case "$prev" in
--bundle|-b|--console|--pid-file) --bundle | -b | --console | --pid-file)
case "$cur" in case "$cur" in
'') '')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace __runc_nospace
;;
/*)
_filedir
__runc_nospace
;;
esac
return
;; ;;
/*)
_filedir
__runc_nospace
;;
esac
return
;;
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -514,39 +507,36 @@ _runc_checkpoint() {
" "
case "$prev" in case "$prev" in
--page-server) --page-server) ;;
;;
--manage-cgroups-mode) --manage-cgroups-mode)
COMPREPLY=( $( compgen -W "soft full strict" -- "$cur" ) ) COMPREPLY=($(compgen -W "soft full strict" -- "$cur"))
return return
;; ;;
--image-path|--work-path) --image-path | --work-path)
case "$cur" in case "$cur" in
*:*) *:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine) '')
;; COMPREPLY=($(compgen -W '/' -- "$cur"))
'') __runc_nospace
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) ;;
__runc_nospace *)
;; _filedir
*) __runc_nospace
_filedir ;;
__runc_nospace esac
;; return
esac ;;
return
;;
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -567,28 +557,28 @@ _runc_create() {
--pid-file --pid-file
" "
case "$prev" in case "$prev" in
--bundle|-b|--console|--pid-file) --bundle | -b | --console | --pid-file)
case "$cur" in case "$cur" in
'') '')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace __runc_nospace
;;
/*)
_filedir
__runc_nospace
;;
esac
return
;; ;;
/*)
_filedir
__runc_nospace
;;
esac
return
;;
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -600,7 +590,7 @@ _runc_create() {
_runc_help() { _runc_help() {
local counter=$(__runc_pos_first_nonflag) local counter=$(__runc_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur"))
fi fi
} }
@ -629,40 +619,38 @@ _runc_restore() {
local all_options="$options_with_args $boolean_options" local all_options="$options_with_args $boolean_options"
case "$prev" in case "$prev" in
--manage-cgroups-mode) --manage-cgroups-mode)
COMPREPLY=( $( compgen -W "soft full strict" -- "$cur" ) ) COMPREPLY=($(compgen -W "soft full strict" -- "$cur"))
return return
;;
--pid-file|--image-path|--work-path|--bundle|-b)
case "$cur" in
*:*)
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
;;
'')
COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
__runc_nospace
;; ;;
/*)
_filedir
__runc_nospace
;;
esac
return
;;
$(__runc_to_extglob "$options_with_args") ) --pid-file | --image-path | --work-path | --bundle | -b)
return case "$cur" in
;; *:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
'')
COMPREPLY=($(compgen -W '/' -- "$cur"))
__runc_nospace
;;
/*)
_filedir
__runc_nospace
;;
esac
return
;;
$(__runc_to_extglob "$options_with_args"))
return
;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) ) COMPREPLY=($(compgen -W "$all_options" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -674,11 +662,11 @@ _runc_resume() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
@ -690,11 +678,11 @@ _runc_state() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
_runc_start() { _runc_start() {
@ -705,11 +693,11 @@ _runc_start() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
;; ;;
esac esac
} }
_runc_update() { _runc_update() {
@ -735,14 +723,14 @@ _runc_update() {
" "
case "$prev" in case "$prev" in
$(__runc_to_extglob "$options_with_args")) $(__runc_to_extglob "$options_with_args"))
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) ) COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;; ;;
*) *)
__runc_list_all __runc_list_all
@ -755,25 +743,25 @@ _runc() {
shopt -s extglob shopt -s extglob
local commands=( local commands=(
checkpoint checkpoint
create create
delete delete
events events
exec exec
init init
kill kill
list list
pause pause
ps ps
restore restore
resume resume
run run
spec spec
start start
state state
update update
help help
h h
) )
# These options are valid as global options for all client commands # These options are valid as global options for all client commands
@ -791,10 +779,9 @@ _runc() {
local counter=1 local counter=1
while [ $counter -lt $cword ]; do while [ $counter -lt $cword ]; do
case "${words[$counter]}" in case "${words[$counter]}" in
-*) -*) ;;
;;
=) =)
(( counter++ )) ((counter++))
;; ;;
*) *)
command="${words[$counter]}" command="${words[$counter]}"
@ -802,7 +789,7 @@ _runc() {
break break
;; ;;
esac esac
(( counter++ )) ((counter++))
done done
local completions_func=_runc_${command} local completions_func=_runc_${command}

View File

@ -16,7 +16,7 @@ possibleConfigFiles=(
'.config' '.config'
) )
if ! command -v zgrep &> /dev/null; then if ! command -v zgrep &>/dev/null; then
zgrep() { zgrep() {
zcat "$2" | grep "$1" zcat "$2" | grep "$1"
} }
@ -28,19 +28,19 @@ kernelMinor="${kernelVersion#$kernelMajor.}"
kernelMinor="${kernelMinor%%.*}" kernelMinor="${kernelMinor%%.*}"
is_set() { is_set() {
zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null zgrep "CONFIG_$1=[y|m]" "$CONFIG" >/dev/null
} }
is_set_in_kernel() { is_set_in_kernel() {
zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null zgrep "CONFIG_$1=y" "$CONFIG" >/dev/null
} }
is_set_as_module() { is_set_as_module() {
zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null zgrep "CONFIG_$1=m" "$CONFIG" >/dev/null
} }
color() { color() {
local codes=() local codes=()
if [ "$1" = 'bold' ]; then if [ "$1" = 'bold' ]; then
codes=( "${codes[@]}" '1' ) codes=("${codes[@]}" '1')
shift shift
fi fi
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
@ -57,7 +57,7 @@ color() {
white) code=37 ;; white) code=37 ;;
esac esac
if [ "$code" ]; then if [ "$code" ]; then
codes=( "${codes[@]}" "$code" ) codes=("${codes[@]}" "$code")
fi fi
fi fi
local IFS=';' local IFS=';'
@ -109,8 +109,7 @@ check_distro_userns() {
fi fi
} }
is_config() is_config() {
{
local config="$1" local config="$1"
# Todo: more check # Todo: more check
@ -118,8 +117,7 @@ is_config()
return 1 return 1
} }
search_config() search_config() {
{
local target_dir="$1" local target_dir="$1"
[[ "$target_dir" ]] || target_dir=("${possibleConfigs[@]}") [[ "$target_dir" ]] || target_dir=("${possibleConfigs[@]}")
@ -181,14 +179,14 @@ fi
if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
echo -n '- ' echo -n '- '
if command -v apparmor_parser &> /dev/null; then if command -v apparmor_parser &>/dev/null; then
echo "$(wrap_good 'apparmor' 'enabled and tools installed')" echo "$(wrap_good 'apparmor' 'enabled and tools installed')"
else else
echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')" echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')"
echo -n ' ' echo -n ' '
if command -v apt-get &> /dev/null; then if command -v apt-get &>/dev/null; then
echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')" echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')"
elif command -v yum &> /dev/null; then elif command -v yum &>/dev/null; then
echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')" echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')"
else else
echo "$(wrap_color '(look for an "apparmor" package for your distribution)')" echo "$(wrap_color '(look for an "apparmor" package for your distribution)')"
@ -220,7 +218,7 @@ echo 'Optional Features:'
check_flags CGROUP_PIDS check_flags CGROUP_PIDS
check_flags MEMCG_SWAP MEMCG_SWAP_ENABLED check_flags MEMCG_SWAP MEMCG_SWAP_ENABLED
if is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then if is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
echo " $(wrap_color '(note that cgroup swap accounting is not enabled in your kernel config, you can enable it by setting boot option "swapaccount=1")' bold black)" echo " $(wrap_color '(note that cgroup swap accounting is not enabled in your kernel config, you can enable it by setting boot option "swapaccount=1")' bold black)"
fi fi
} }

View File

@ -3,14 +3,14 @@
source "$(dirname "$BASH_SOURCE")/.validate" source "$(dirname "$BASH_SOURCE")/.validate"
IFS=$'\n' IFS=$'\n'
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^Godeps/' || true) ) files=($(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^Godeps/' || true))
unset IFS unset IFS
badFiles=() badFiles=()
for f in "${files[@]}"; do for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed is formatted # we use "git show" here to validate that what's committed is formatted
if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then
badFiles+=( "$f" ) badFiles+=("$f")
fi fi
done done

21
script/validate-shfmt Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash
badFiles=()
while read f; do
badFiles+=("$f")
done < <(shfmt -l . | grep -v Godeps/)
if [ ${#badFiles[@]} -eq 0 ]; then
echo 'Congratulations! All shell source files are properly formatted.'
else
{
echo "These files are not properly shfmt'd:"
for f in "${badFiles[@]}"; do
echo " - $f"
done
echo
echo 'Please reformat the above files using "shfmt -w" and commit the result.'
echo
} >&2
false
fi

View File

@ -29,24 +29,24 @@ KERNEL_MINOR="${KERNEL_MINOR%%.*}"
ROOT="$BATS_TMPDIR/runc" ROOT="$BATS_TMPDIR/runc"
# Cgroup mount # Cgroup mount
CGROUP_BASE_PATH=$(grep "cgroup" /proc/self/mountinfo | gawk 'toupper($NF) ~ /\<MEMORY\>/ { print $5; exit }') CGROUP_BASE_PATH=$(grep "cgroup" /proc/self/mountinfo | gawk 'toupper($NF) ~ /\<MEMORY\>/ { print $5; exit }')
# CONFIG_MEMCG_KMEM support # CONFIG_MEMCG_KMEM support
KMEM="${CGROUP_BASE_PATH}/memory.kmem.limit_in_bytes" KMEM="${CGROUP_BASE_PATH}/memory.kmem.limit_in_bytes"
# Wrapper for runc. # Wrapper for runc.
function runc() { function runc() {
run __runc "$@" run __runc "$@"
# Some debug information to make life easier. bats will only print it if the # Some debug information to make life easier. bats will only print it if the
# test failed, in which case the output is useful. # test failed, in which case the output is useful.
echo "runc $@ (status=$status):" >&2 echo "runc $@ (status=$status):" >&2
echo "$output" >&2 echo "$output" >&2
} }
# Raw wrapper for runc. # Raw wrapper for runc.
function __runc() { function __runc() {
"$RUNC" --root "$ROOT" "$@" "$RUNC" --root "$ROOT" "$@"
} }
# Fails the current test, providing the error given. # Fails the current test, providing the error given.
@ -79,118 +79,118 @@ function requires() {
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries. # Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
function retry() { function retry() {
local attempts=$1 local attempts=$1
shift shift
local delay=$1 local delay=$1
shift shift
local i local i
for ((i=0; i < attempts; i++)); do for ((i = 0; i < attempts; i++)); do
run "$@" run "$@"
if [[ "$status" -eq 0 ]] ; then if [[ "$status" -eq 0 ]]; then
return 0 return 0
fi fi
sleep $delay sleep $delay
done done
echo "Command \"$@\" failed $attempts times. Output: $output" echo "Command \"$@\" failed $attempts times. Output: $output"
false false
} }
# retry until the given container has state # retry until the given container has state
function wait_for_container() { function wait_for_container() {
local attempts=$1 local attempts=$1
local delay=$2 local delay=$2
local cid=$3 local cid=$3
local i local i
for ((i=0; i < attempts; i++)); do for ((i = 0; i < attempts; i++)); do
runc state $cid runc state $cid
if [[ "$status" -eq 0 ]] ; then if [[ "$status" -eq 0 ]]; then
return 0 return 0
fi fi
sleep $delay sleep $delay
done done
echo "runc state failed to return state $statecheck $attempts times. Output: $output" echo "runc state failed to return state $statecheck $attempts times. Output: $output"
false false
} }
# retry until the given container has state # retry until the given container has state
function wait_for_container_inroot() { function wait_for_container_inroot() {
local attempts=$1 local attempts=$1
local delay=$2 local delay=$2
local cid=$3 local cid=$3
local i local i
for ((i=0; i < attempts; i++)); do for ((i = 0; i < attempts; i++)); do
ROOT=$4 runc state $cid ROOT=$4 runc state $cid
if [[ "$status" -eq 0 ]] ; then if [[ "$status" -eq 0 ]]; then
return 0 return 0
fi fi
sleep $delay sleep $delay
done done
echo "runc state failed to return state $statecheck $attempts times. Output: $output" echo "runc state failed to return state $statecheck $attempts times. Output: $output"
false false
} }
function testcontainer() { function testcontainer() {
# test state of container # test state of container
runc state $1 runc state $1
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[[ "${output}" == *"$2"* ]] [[ "${output}" == *"$2"* ]]
} }
function setup_busybox() { function setup_busybox() {
run mkdir "$BUSYBOX_BUNDLE" run mkdir "$BUSYBOX_BUNDLE"
run mkdir "$BUSYBOX_BUNDLE"/rootfs run mkdir "$BUSYBOX_BUNDLE"/rootfs
if [ -e "/testdata/busybox.tar" ]; then if [ -e "/testdata/busybox.tar" ]; then
BUSYBOX_IMAGE="/testdata/busybox.tar" BUSYBOX_IMAGE="/testdata/busybox.tar"
fi fi
if [ ! -e $BUSYBOX_IMAGE ]; then if [ ! -e $BUSYBOX_IMAGE ]; then
curl -o $BUSYBOX_IMAGE -sSL 'https://github.com/jpetazzo/docker-busybox/raw/buildroot-2014.11/rootfs.tar' curl -o $BUSYBOX_IMAGE -sSL 'https://github.com/jpetazzo/docker-busybox/raw/buildroot-2014.11/rootfs.tar'
fi fi
tar -C "$BUSYBOX_BUNDLE"/rootfs -xf "$BUSYBOX_IMAGE" tar -C "$BUSYBOX_BUNDLE"/rootfs -xf "$BUSYBOX_IMAGE"
cd "$BUSYBOX_BUNDLE" cd "$BUSYBOX_BUNDLE"
runc spec runc spec
} }
function setup_hello() { function setup_hello() {
run mkdir "$HELLO_BUNDLE" run mkdir "$HELLO_BUNDLE"
run mkdir "$HELLO_BUNDLE"/rootfs run mkdir "$HELLO_BUNDLE"/rootfs
tar -C "$HELLO_BUNDLE"/rootfs -xf "$HELLO_IMAGE" tar -C "$HELLO_BUNDLE"/rootfs -xf "$HELLO_IMAGE"
cd "$HELLO_BUNDLE" cd "$HELLO_BUNDLE"
runc spec runc spec
sed -i 's;"sh";"/hello";' config.json sed -i 's;"sh";"/hello";' config.json
} }
function teardown_running_container() { function teardown_running_container() {
runc list runc list
if [[ "${output}" == *"$1"* ]]; then if [[ "${output}" == *"$1"* ]]; then
runc kill $1 KILL runc kill $1 KILL
retry 10 1 eval "__runc state '$1' | grep -q 'stopped'" retry 10 1 eval "__runc state '$1' | grep -q 'stopped'"
runc delete $1 runc delete $1
fi fi
} }
function teardown_running_container_inroot() { function teardown_running_container_inroot() {
ROOT=$2 runc list ROOT=$2 runc list
if [[ "${output}" == *"$1"* ]]; then if [[ "${output}" == *"$1"* ]]; then
ROOT=$2 runc kill $1 KILL ROOT=$2 runc kill $1 KILL
retry 10 1 eval "ROOT='$2' __runc state '$1' | grep -q 'stopped'" retry 10 1 eval "ROOT='$2' __runc state '$1' | grep -q 'stopped'"
ROOT=$2 runc delete $1 ROOT=$2 runc delete $1
fi fi
} }
function teardown_busybox() { function teardown_busybox() {
cd "$INTEGRATION_ROOT" cd "$INTEGRATION_ROOT"
teardown_running_container test_busybox teardown_running_container test_busybox
run rm -f -r "$BUSYBOX_BUNDLE" run rm -f -r "$BUSYBOX_BUNDLE"
} }
function teardown_hello() { function teardown_hello() {
cd "$INTEGRATION_ROOT" cd "$INTEGRATION_ROOT"
teardown_running_container test_hello teardown_running_container test_hello
run rm -f -r "$HELLO_BUNDLE" run rm -f -r "$HELLO_BUNDLE"
} }