github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/system/000-TEMPLATE (about) 1 #!/usr/bin/env bats -*- bats -*- 2 # 3 # FIXME: short description of the purpose of this module 4 # 5 # FIXME: copy this file to 'NNN-yourtestname.bats' and edit as needed. 6 # 7 8 load helpers 9 10 @test "podman subcmd - description of this particular test" { 11 args="some sort of argument list" 12 run_podman subcmd $args 13 is "$output" "what we expect" "output from 'podman subcmd $args'" 14 } 15 16 # vim: filetype=sh 17 18 ############################################################################### 19 # 20 # FIXME FIXME FIXME: Most of the time you can cut from here on down. 21 # FIXME FIXME FIXME: The above template is probably enough for many tests. 22 # FIXME FIXME FIXME: 23 # FIXME FIXME FIXME: If you need anything more complicated, read on. 24 # 25 # FIXME: This is a bloated test template. It provides mostly stuff for you 26 # FIXME: to remove, plus stuff for you to base your tests on. 27 # FIXME: 28 # FIXME: copy this file to 'NNN-yourtestname.bats' and edit as needed. 29 # FIXME: Read all FIXMEs, act on them as needed, then remove them. 30 # FIXME: test w/ $ PODMAN=./bin/podman bats test/system/NNN-yourtestname.bats 31 # 32 33 load helpers 34 35 # FIXME: DELETE THESE LINES UNLESS YOU ABSOLUTELY NEED THEM. 36 # FIXME: Most tests will not need a custom setup/teardown: they are 37 # FIXME: provided by helpers.bash. 38 # FIXME: But if you have to do anything special, these give you the 39 # FIXME: names of the standard setup/teardown so you can call them 40 # FIXME: before or after your own additions. 41 function setup() { 42 basic_setup 43 # FIXME: you almost certainly want to do your own setup _after_ basic. 44 } 45 function teardown() { 46 # FIXME: you almost certainly want to do your own teardown _before_ basic. 47 basic_teardown 48 } 49 50 51 # FIXME: very basic one-pass example 52 @test "podman FOO - description of test" { 53 # FIXME: please try to remove this line; that is, try to write tests 54 # that will pass as both root and rootless. 55 skip_if_rootless "Short explanation of why this doesn't work rootless" 56 57 # FIXME: template for run commands. Always use 'run_podman'! 58 # FIXME: The '?' means 'ignore exit status'; use a number if you 59 # FIXME: expect a precise nonzero code, or omit for 0 (usual case). 60 # FIXME: NEVER EVER RUN 'podman' DIRECTLY. See helpers.bash for why. 61 run_podman '?' run -d $IMAGE sh -c 'prep..; echo READY' 62 cid="$output" 63 wait_for_ready $cid 64 65 run_podman logs $cid 66 # FIXME: example of dprint. This will trigger if PODMAN_TEST_DEBUG=FOO 67 # FIXME: ...or anything that matches the name assigned in the @test line. 68 dprint "podman logs $cid -> '$output'" 69 is "$output" "what are we expecting?" "description of this check" 70 71 # Clean up 72 run_podman rm $cid 73 } 74 75 76 # FIXME: another example, this time with a test table loop 77 @test "podman FOO - json - template for playing with json output" { 78 # FIXME: Define a multiline string in tabular form, using '|' as separator. 79 # FIXME: Each row defines one test. Each column (there may be as many as 80 # FIXME: you want) is one field. In the case below we have two, a 81 # FIXME: json field descriptor and an expected value. 82 tests=" 83 id | [0-9a-f]\\\{64\\\} 84 created | [0-9-]\\\+T[0-9:]\\\+\\\.[0-9]\\\+Z 85 size | -\\\?[0-9]\\\+ 86 " 87 88 # FIXME: Run a basic podman command. We'll check $output multiple times 89 # FIXME: in the while loop below. 90 run_podman history --format json $IMAGE 91 92 # FIXME: parse_table is what does all the work, giving us test cases. 93 parse_table "$tests" | while read field expect; do 94 # FIXME: this shows a drawback of BATS and bash: we can't include '|' 95 # FIXME: in the table, but we need to because some images don't 96 # FIXME: have a CID. So, yeah, this is ugly -- but rare. 97 if [ "$field" = "id" ]; then expect="$expect\|<missing>";fi 98 99 # output is an array of dicts; check each one 100 count=$(echo "$output" | jq '. | length') 101 i=0 102 while [ $i -lt $count ]; do 103 actual=$(echo "$output" | jq -r ".[$i].$field") 104 # FIXME: please be sure to note the third field! 105 # FIXME: that's the test name. Make it something useful! Include 106 # FIXME: loop variables whenever possible. Don't just say "my test" 107 is "$actual" "$expect\$" "jq .[$i].$field" 108 i=$(expr $i + 1) 109 done 110 done 111 } 112 113 114 # vim: filetype=sh