Skip to content

Commit aeaadae

Browse files
committed
CP-311905: VM.update_secureboot_certificates_on_boot idempotent
Signed-off-by: Chunjie Zhu <chunjie.zhu@cloud.com>
1 parent df2db1c commit aeaadae

2 files changed

Lines changed: 19 additions & 54 deletions

File tree

ocaml/tests/test_vm.ml

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ module VMUpdateSecurebootCertificatesOnBoot = Generic.MakeStateful (struct
237237
module Io = struct
238238
type input_t = [`ok | `update_available | `update_on_boot] * bool
239239

240-
type output_t =
241-
([`ok | `update_available | `update_on_boot], string * string list) result
240+
type output_t = [`ok | `update_available | `update_on_boot]
242241

243242
let string_of_state = function
244243
| `ok ->
@@ -251,14 +250,7 @@ module VMUpdateSecurebootCertificatesOnBoot = Generic.MakeStateful (struct
251250
let string_of_input_t (state, mark) =
252251
Printf.sprintf "(%s, mark=%b)" (string_of_state state) mark
253252

254-
let string_of_error (code, args) =
255-
Printf.sprintf "%s(%s)" code (String.concat ", " args)
256-
257-
let string_of_output_t = function
258-
| Ok state ->
259-
Printf.sprintf "Ok %s" (string_of_state state)
260-
| Error err ->
261-
Printf.sprintf "Error %s" (string_of_error err)
253+
let string_of_output_t = string_of_state
262254
end
263255

264256
module State = Test_state.XapiDb
@@ -273,34 +265,21 @@ module VMUpdateSecurebootCertificatesOnBoot = Generic.MakeStateful (struct
273265
let self =
274266
List.nth (Db.VM.get_by_name_label ~__context ~label:name_label) 0
275267
in
276-
try
277-
Xapi_vm.update_secureboot_certificates_on_boot ~__context ~self ~mark ;
278-
Ok (Db.VM.get_secureboot_certificates_state ~__context ~self)
279-
with Api_errors.Server_error (code, args) -> Error (code, args)
268+
Xapi_vm.update_secureboot_certificates_on_boot ~__context ~self ~mark ;
269+
Db.VM.get_secureboot_certificates_state ~__context ~self
280270

281271
let tests =
282272
`QuickAndAutoDocumented
283273
[
284-
((`update_available, true), Ok `update_on_boot)
285-
; ((`update_on_boot, false), Ok `update_available)
286-
; ( (`ok, true)
287-
, Error
288-
( Api_errors.operation_not_allowed
289-
, [
290-
"Cannot set update_on_boot: VM.secureboot_certificates_state \
291-
is not update_available"
292-
]
293-
)
294-
)
295-
; ( (`ok, false)
296-
, Error
297-
( Api_errors.operation_not_allowed
298-
, [
299-
"Cannot clear update_on_boot: VM.secureboot_certificates_state \
300-
is not update_on_boot"
301-
]
302-
)
303-
)
274+
(* transitions *)
275+
((`update_available, true), `update_on_boot)
276+
; ((`update_on_boot, false), `update_available)
277+
(* idempotent: already in desired state *)
278+
; ((`update_on_boot, true), `update_on_boot)
279+
; ((`update_available, false), `update_available)
280+
(* irrelevant states: no-op, no exception *)
281+
; ((`ok, true), `ok)
282+
; ((`ok, false), `ok)
304283
]
305284
end)
306285

ocaml/xapi/xapi_vm.ml

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,26 +1763,12 @@ let update_secureboot_certificates_on_boot ~__context ~self ~mark =
17631763
| false, `update_on_boot ->
17641764
Db.VM.set_secureboot_certificates_state ~__context ~self
17651765
~value:`update_available
1766-
| true, _ ->
1767-
raise
1768-
(Api_errors.Server_error
1769-
( Api_errors.operation_not_allowed
1770-
, [
1771-
"Cannot set update_on_boot: VM.secureboot_certificates_state is \
1772-
not update_available"
1773-
]
1774-
)
1775-
)
1776-
| false, _ ->
1777-
raise
1778-
(Api_errors.Server_error
1779-
( Api_errors.operation_not_allowed
1780-
, [
1781-
"Cannot clear update_on_boot: VM.secureboot_certificates_state \
1782-
is not update_on_boot"
1783-
]
1784-
)
1785-
)
1766+
| true, `update_on_boot ->
1767+
() (* already marked for update on boot — idempotent *)
1768+
| false, `update_available ->
1769+
() (* already cleared — idempotent *)
1770+
| _, _ ->
1771+
() (* irrelevant state — no-op *)
17861772

17871773
let sysprep ~__context ~self ~unattend ~timeout =
17881774
let uuid = Db.VM.get_uuid ~__context ~self in

0 commit comments

Comments
 (0)