forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.sh
More file actions
116 lines (92 loc) · 5.28 KB
/
Copy pathoperations.sh
File metadata and controls
116 lines (92 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
test_get_operations() {
ensure_import_testimage
! lxc operation list --project nonexistent || false
lxc operation list --project default
proj1="op-proj1"
proj2="op-proj2"
(
set -e
lxc project create "${proj1}" -c features.profiles=false -c features.images=false
lxc project create "${proj2}" -c features.profiles=false -c features.images=false
lxc launch testimage c1 --project "${proj1}"
lxc launch testimage c2 --project "${proj2}"
# For each project, generate a single operation.
lxc exec -T --project="${proj1}" c1 true
lxc exec -T --project="${proj2}" c2 true
# Get the operations output json with recursion=1
proj1_full_ops_json=$(lxc query "/1.0/operations?project=${proj1}&recursion=1")
proj2_full_ops_json=$(lxc query "/1.0/operations?project=${proj2}&recursion=1")
all_full_ops_json=$(lxc query "/1.0/operations?all-projects=true&recursion=1")
# Verify that both individual project operations and the collective set of operations are queried correctly.
proj1_count=$(jq --exit-status '[.success[] | select(.description == "Executing command")] | length' <<< "${proj1_full_ops_json}")
test "${proj1_count}" -eq 1
proj2_count=$(jq --exit-status '[.success[] | select(.description == "Executing command")] | length' <<< "${proj2_full_ops_json}")
test "${proj2_count}" -eq 1
all_count=$(jq --exit-status '[.success[] | select(.description == "Executing command")] | length' <<< "${all_full_ops_json}")
test "${all_count}" -eq 2
proj1_op_id=$(jq --exit-status -r '.success[] | select(.description == "Executing command") | .id' <<< "${proj1_full_ops_json}")
proj2_op_id=$(jq --exit-status -r '.success[] | select(.description == "Executing command") | .id' <<< "${proj2_full_ops_json}")
proj1_ops_json=$(lxc query "/1.0/operations?project=${proj1}")
proj2_ops_json=$(lxc query "/1.0/operations?project=${proj2}")
all_ops_json=$(lxc query "/1.0/operations?all-projects=true")
# Assert that the operations with these IDs exist across all projects.
jq --exit-status --arg id "${proj1_op_id}" '.success | contains(["/1.0/operations/\($id)"])' <<< "${all_ops_json}"
jq --exit-status --arg id "${proj2_op_id}" '.success | contains(["/1.0/operations/\($id)"])' <<< "${all_ops_json}"
# Assert that the operations with these IDs exist within their respective projects.
jq --exit-status --arg id "${proj1_op_id}" '.success | contains(["/1.0/operations/\($id)"])' <<< "${proj1_ops_json}"
jq --exit-status --arg id "${proj2_op_id}" '.success | contains(["/1.0/operations/\($id)"])' <<< "${proj2_ops_json}"
lxc delete c1 --force --project "${proj1}"
lxc delete c2 --force --project "${proj2}"
)
}
test_bulk_operation_children() {
ensure_import_testimage
(
set -e
# Create two containers to generate a bulk operation with child operations.
lxc launch testimage c1
lxc launch testimage c2
# Trigger a bulk state change, which creates a parent operation with one child per instance.
bulk_op_id=$(lxc query -X PUT /1.0/instances -d '{"state": {"action": "stop"}}' | jq -r --exit-status '.id')
# Wait for the bulk operation to complete.
lxc query "/1.0/operations/${bulk_op_id}/wait?timeout=60" > /dev/null
# Assert the CHILDREN column in lxc operation list shows the correct child count.
child_count=$(lxc operation list --format json | jq --exit-status --arg id "${bulk_op_id}" '.[] | select(.id == $id) | .child_count')
[ "${child_count}" -eq 2 ]
# Assert child_count is accurate on a non-recursive single GET (no recursion=1 required).
api_child_count=$(lxc query "/1.0/operations/${bulk_op_id}" | jq --exit-status '.child_count')
[ "${api_child_count}" -eq 2 ]
# Assert lxc operation list-children shows the expected number of child operations.
list_children_count=$(lxc operation list-children "${bulk_op_id}" --format json | jq --exit-status 'length')
[ "${list_children_count}" -eq 2 ]
lxc delete c1 --force
lxc delete c2 --force
)
}
test_operations_conflict_reference() {
conflictRef="test-conflict-ref"
# Create two operations with the same conflict_reference. The second creation should fail.
# op_type 75 is "Wait" operation.
lxc query -X POST '/internal/testing/operation-wait' -d '{"duration": "5s", "op_class": 1, "op_type": 75, "conflict_reference": "'"${conflictRef}"'"}'
! lxc query -X POST '/internal/testing/operation-wait' -d '{"duration": "5s", "op_class": 1, "op_type": 75, "conflict_reference": "'"${conflictRef}"'"}' || false
}
test_operation_wait_failure() {
network_name="opwait$$"
(
set -e
lxc network create "${network_name}" ipv4.address=none ipv6.address=none
trap 'lxc network delete "${network_name}"' EXIT
op_id=$(lxc query -X POST /1.0/networks -d '{"name": "'"${network_name}"'", "config": {"ipv4.address": "none", "ipv6.address": "none"}}' | jq --exit-status --raw-output '.id')
wait_response=$(lxc query "/1.0/operations/${op_id}/wait?timeout=60")
jq --exit-status '
.status == "Failure" and
.status_code == 400 and
.err == "The network already exists" and
.err_code == 400
' <<< "${wait_response}"
if lxc network create "${network_name}" ipv4.address=none ipv6.address=none; then
echo "ERROR: Duplicate network creation succeeded"
exit 1
fi
)
}