]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/tests/functional/cli_root/zfs_program/zfs_program_json.ksh
Add JSON output support to channel programs
[mirror_zfs.git] / tests / zfs-tests / tests / functional / cli_root / zfs_program / zfs_program_json.ksh
1 #!/bin/ksh -p
2 #
3 # CDDL HEADER START
4 #
5 # This file and its contents are supplied under the terms of the
6 # Common Development and Distribution License ("CDDL"), version 1.0.
7 # You may only use this file in accordance with the terms of version
8 # 1.0 of the CDDL.
9 #
10 # A full copy of the text of the CDDL should have accompanied this
11 # source. A copy is of the CDDL is also available via the Internet
12 # at http://www.illumos.org/license/CDDL.
13 #
14 # CDDL HEADER END
15 #
16
17 #
18 # Copyright (c) 2018 Datto Inc.
19 #
20
21 . $STF_SUITE/include/libtest.shlib
22
23 #
24 # DESCRIPTION:
25 #
26 # STRATEGY:
27 # 1. Compare JSON output formatting for a channel program to template
28 # 2. Using bad command line option (-Z) gives correct error output
29 #
30
31 verify_runnable "both"
32
33 function cleanup
34 {
35 log_must zfs destroy $TESTDS
36 return 0
37 }
38 log_onexit cleanup
39
40 log_assert "Channel programs output valid JSON"
41
42 TESTDS="$TESTPOOL/zcp-json"
43 log_must zfs create $TESTDS
44
45 TESTZCP="/$TESTDS/zfs_rlist.zcp"
46 cat > "$TESTZCP" << EOF
47 succeeded = {}
48 failed = {}
49
50 function list_recursive(root, prop)
51 for child in zfs.list.children(root) do
52 list_recursive(child, prop)
53 end
54 val, src = zfs.get_prop(root, prop)
55 if (val == nil) then
56 failed[root] = val
57 else
58 succeeded[root] = val
59 end
60 end
61
62 args = ...
63
64 argv = args["argv"]
65
66 list_recursive(argv[1], argv[2])
67
68 results = {}
69 results["succeeded"] = succeeded
70 results["failed"] = failed
71 return results
72 EOF
73
74 # 1. Compare JSON output formatting for a channel program to template
75 typeset -a pos_cmds=("recordsize" "type")
76 typeset -a pos_cmds_out=(
77 "{
78 \"return\": {
79 \"failed\": {},
80 \"succeeded\": {
81 \"$TESTDS\": 131072
82 }
83 }
84 }"
85 "{
86 \"return\": {
87 \"failed\": {},
88 \"succeeded\": {
89 \"$TESTDS\": \"filesystem\"
90 }
91 }
92 }")
93 typeset -i cnt=0
94 typeset cmd
95 for cmd in ${pos_cmds[@]}; do
96 log_must zfs program $TESTPOOL $TESTZCP $TESTDS $cmd 2>&1
97 log_must zfs program $TESTPOOL -j $TESTZCP $TESTDS $cmd 2>&1
98 # json.tool is needed to guarantee consistent ordering of fields
99 # sed is needed to trim trailing space in CentOS 6's json.tool output
100 OUTPUT=$(zfs program $TESTPOOL -j $TESTZCP $TESTDS $cmd 2>&1 | python -m json.tool | sed 's/[[:space:]]*$//')
101 if [ "$OUTPUT" != "${pos_cmds_out[$cnt]}" ]; then
102 log_note "Got :$OUTPUT"
103 log_note "Expected:${pos_cmds_out[$cnt]}"
104 log_fail "Unexpected channel program output";
105 fi
106 cnt=$((cnt + 1))
107 done
108
109 # 2. Using bad command line option (-Z) gives correct error output
110 typeset -a neg_cmds=("-Z")
111 typeset -a neg_cmds_out=(
112 "invalid option 'Z'
113 usage:
114 program [-jn] [-t <instruction limit>] [-m <memory limit (b)>] <pool> <program file> [lua args...]
115
116 For the property list, run: zfs set|get
117
118 For the delegated permission list, run: zfs allow|unallow")
119 cnt=0
120 for cmd in ${neg_cmds[@]}; do
121 log_mustnot zfs program $TESTPOOL $TESTZCP $TESTDS $cmd 2>&1
122 log_mustnot zfs program $TESTPOOL -j $TESTZCP $TESTDS $cmd 2>&1
123 OUTPUT=$(zfs program $TESTPOOL -j $TESTZCP $TESTDS $cmd 2>&1)
124 if [ "$OUTPUT" != "${neg_cmds_out[$cnt]}" ]; then
125 log_note "Got :$OUTPUT"
126 log_note "Expected:${neg_cmds_out[$cnt]}"
127 log_fail "Unexpected channel program error output";
128 fi
129 cnt=$((cnt + 1))
130 done
131
132 log_pass "Channel programs output valid JSON"