4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 readonly bash_completion=${TOP_BUILDDIR:-.}/doc/examples/bash_completion-debug
27 readonly default_wordbreaks=$' \t\n"'\''@><=;|&(:'
36 local -r needle="$1"; shift
39 if [[ "$value" = "$needle" ]]; then
47 # Prepares a subshell for testing bash completion functions
49 local -r unused=UNUSED
59 COMP_WORDBREAKS="$default_wordbreaks"
67 # Check if default wordbreaks are still valid (this detects cases where Bash
68 # were to change its built-in default value)
69 # TODO: May need an update for older Bash versions; some didn't include the
70 # colon character (':') in COMP_WORDBREAKS
72 bashdef=$(env - bash --noprofile --norc -c 'echo -n "$COMP_WORDBREAKS"')
74 $default_wordbreaks) ;;
76 err 'Bash uses unknown value for COMP_WORDBREAKS'
82 for cmd in gnt-{instance,node,group,job}; do
86 COMP_WORDS=( $cmd list - )
88 contains --help "${COMPREPLY[@]}" || \
89 err "'$cmd list' did not list --help as an option"
93 # Completing a yes/no option
97 COMP_WORDS=( gnt-node modify --drained )
99 if [[ "${COMPREPLY[*]}" != 'no yes' ]]; then
100 err "Completing '${COMP_WORDS[@]}' did not give correct result"
104 # Completing a multiple-choice option
108 COMP_WORDS=( gnt-debug allocator --disk-template=sh foo )
110 if [[ "${COMPREPLY[*]}" != sharedfile ]]; then
111 err "Completing '${COMP_WORDS[*]}' did not give correct result"
115 # Completing a node name
119 # Override built-in function
121 echo aanode1 bbnode2 aanode3
125 COMP_WORDS=( gnt-node modify --drained yes aa )
127 if [[ "${COMPREPLY[*]}" != 'aanode1 aanode3' ]]; then
128 err 'Completing node names failed'
132 # Completing an option when it's not at the end
136 # Override built-in function
137 _ganeti_instances() {
141 # Completing word in the middle
143 COMP_WORDS=( gnt-instance list --o inst3 inst inst5 )
145 contains --output "${COMPREPLY[@]}" || err 'Did not complete parameter'
148 # Completing an instance name
152 # Override built-in function
153 _ganeti_instances() {
157 # Completing word in the middle
159 COMP_WORDS=( gnt-instance list -o foobar inst1 inst inst5 )
161 if [[ "${COMPREPLY[*]}" != "$(echo inst{1..5})" ]]; then
162 err "Completing '${COMP_WORDS[*]}' did not give correct result"
166 # Need to test node expansion with different wordbreak settings
167 [[ "$default_wordbreaks" == *:* ]] || \
168 err 'No colon in default wordbreak characters'
169 for wb in "$default_wordbreaks" "${default_wordbreaks/:/}"; do
173 # Override built-in function
178 COMP_WORDBREAKS="$wb"
182 COMP_WORDS=( gnt-instance add -n )
184 if [[ "${COMPREPLY[*]}" != 'nodeA nodeA: nodeB nodeB: nodeC nodeC:' ]]; then
185 err 'Got wrong node list'
189 COMP_WORDS=( gnt-instance add -n nodeB )
191 if [[ "${COMPREPLY[*]}" != 'nodeB nodeB:' ]]; then
192 err 'Got wrong node list'
196 COMP_WORDS=( gnt-instance add -n nodeC: )
198 if [[ "$COMP_WORDBREAKS" == *:* ]]; then
199 expected='nodeA nodeB'
201 expected='nodeC:nodeA nodeC:nodeB'
203 if [[ "${COMPREPLY[*]}" != "$expected" ]]; then
204 err 'Got wrong node list'
209 # Need to test different settings for the extglob shell option
212 if [[ "$(shopt -p extglob)" != "shopt $opt extglob" ]]; then
213 err 'The "extglob" shell option has an unexpected value'
226 COMP_WORDS=( gnt-instance add --os-type busybox --no-n )
228 if [[ "${COMPREPLY[*]}" != '--no-name-check --no-nics' ]]; then
229 err "Completing '${COMP_WORDS[*]}' did not give correct result"