|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -x |
| 5 | + |
| 6 | +# Setup: a cfbs project (without git) and a custom index in a temp directory |
| 7 | +tmpdir=$(mktemp -d) |
| 8 | +trap "rm -rf $tmpdir" EXIT |
| 9 | +cd "$tmpdir" |
| 10 | + |
| 11 | +cat > cfbs.json <<EOF |
| 12 | +{ |
| 13 | + "name": "Example project", |
| 14 | + "description": "Example description", |
| 15 | + "type": "policy-set", |
| 16 | + "git": false, |
| 17 | + "build": [] |
| 18 | +} |
| 19 | +EOF |
| 20 | + |
| 21 | +cat > custom.json <<EOF |
| 22 | +{ |
| 23 | + "name": "custom", |
| 24 | + "type": "index", |
| 25 | + "index": { |
| 26 | + "my-module": { |
| 27 | + "description": "Module only found in the custom index", |
| 28 | + "tags": ["experimental"], |
| 29 | + "repo": "https://github.com/cfengine/modules", |
| 30 | + "by": "https://github.com/cfengine", |
| 31 | + "version": "1.0.0", |
| 32 | + "commit": "e603b586e4028364ceea234f3b71c6e5d78b811e", |
| 33 | + "subdirectory": "management/autorun", |
| 34 | + "steps": ["json def.json def.json"] |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +EOF |
| 39 | + |
| 40 | +# --index is available for commands which look up modules in an index |
| 41 | +cfengine add -h | grep -- "--index" |
| 42 | +cfengine search -h | grep -- "--index" |
| 43 | +cfengine moduleinfo -h | grep -- "--index" |
| 44 | + |
| 45 | +# ...but not for commands which only operate on the project |
| 46 | +for command in update remove input; do |
| 47 | + if cfengine $command my-module --index ./custom.json; then |
| 48 | + echo "FAIL: expected '--index' to be rejected by 'cfengine $command'" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +done |
| 52 | + |
| 53 | +# Search and moduleinfo find the module in the custom index |
| 54 | +cfengine search my-module --index ./custom.json | grep "my-module - Module only found in the custom index" |
| 55 | +cfengine moduleinfo my-module --index ./custom.json | grep "Module only found in the custom index" |
| 56 | + |
| 57 | +# An invalid index string gives an error, not a backtrace |
| 58 | +output=$(cfengine search my-module --index custom.json 2>&1) && exit 1 |
| 59 | +echo "$output" | grep 'must be a URL (starting with https://) or relative path (starting with ./)' |
| 60 | +if echo "$output" | grep "Traceback"; then |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +# Adding from the custom index records the index on the module, |
| 65 | +# but does not change the index of the project |
| 66 | +cfengine add my-module --index ./custom.json |
| 67 | +grep '"name": "my-module"' cfbs.json |
| 68 | +grep '"index": "./custom.json"' cfbs.json |
| 69 | +grep '"added_by": "cfengine add"' cfbs.json |
| 70 | +[ "$(grep -c '"index"' cfbs.json)" = "1" ] |
| 71 | + |
| 72 | +# Remove works without an index (newline accepts the default "yes" in the prompt) |
| 73 | +echo | cfengine remove my-module |
| 74 | +if grep "my-module" cfbs.json; then |
| 75 | + echo "FAIL: expected my-module to be removed from cfbs.json" |
| 76 | + exit 1 |
| 77 | +fi |
0 commit comments