Skip to content

Commit

Permalink
Unit test - CP-47655: [Toolstack] - Associate/disassociate VM to/from…
Browse files Browse the repository at this point in the history
… anti-affinity group

Unit test - CP-47304: [Toolstack] - Add data model for anti-affinity group

Signed-off-by: Bengang Yuan <[email protected]>
  • Loading branch information
BengangY committed Apr 10, 2024
1 parent 3d6e9ab commit 5789cea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ocaml/tests/common/test_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,10 @@ let make_observer ~__context ?(ref = Ref.make ()) ?(uuid = make_uuid ())
Db.Observer.create ~__context ~ref ~uuid ~name_label ~name_description ~hosts
~attributes ~endpoints ~components ~enabled ;
ref

let make_vm_group ~__context ?(ref = Ref.make ()) ?(uuid = make_uuid ())
?(name_label = "vm_group") ?(name_description = "") ?(placement = `normal)
() =
Db.VM_group.create ~__context ~ref ~uuid ~name_label ~name_description
~placement ;
ref
1 change: 1 addition & 0 deletions ocaml/tests/suite_alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ let () =
; ("Test_storage_migrate_state", Test_storage_migrate_state.test)
; ("Test_bios_strings", Test_bios_strings.test)
; ("Test_certificates", Test_certificates.test)
; ("Test_vm_group", Test_vm_group.test)
]
@ Test_guest_agent.tests
@ Test_nm.tests
Expand Down
58 changes: 58 additions & 0 deletions ocaml/tests/test_vm_group.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(*
* Copyright (C) Cloud Software Group Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

module T = Test_common

let test_associate_vm_with_vm_group () =
let __context = T.make_test_database () in
let rpc, session_id = Test_common.make_client_params ~__context in
let vm1 = T.make_vm ~__context () in
let vm2 = T.make_vm ~__context () in
let vm3 = T.make_vm ~__context () in
let vm_group = T.make_vm_group ~__context ~placement:`anti_affinity () in
Client.Client.VM.set_groups ~rpc ~session_id ~self:vm1 ~value:[vm_group] ;
Client.Client.VM.set_groups ~rpc ~session_id ~self:vm2 ~value:[vm_group] ;
Client.Client.VM.set_groups ~rpc ~session_id ~self:vm3 ~value:[vm_group] ;
let vms = Db.VM_group.get_VMs ~__context ~self:vm_group in
let extract_vm_strings vms =
List.sort String.compare (List.map Ref.string_of vms)
in
Alcotest.(check (slist string String.compare))
"check VMs are in the group" (extract_vm_strings vms)
(extract_vm_strings [vm1; vm2; vm3])

let test_vm_can_only_belong_to_one_anti_affinity_group () =
let __context = T.make_test_database () in
let rpc, session_id = Test_common.make_client_params ~__context in
let vm = T.make_vm ~__context () in
let vm_group1 = T.make_vm_group ~__context ~placement:`anti_affinity () in
let vm_group2 = T.make_vm_group ~__context ~placement:`anti_affinity () in
Alcotest.check_raises "should fail"
(Api_errors.Server_error
(Api_errors.vm_can_only_belong_to_one_anti_affinity_group, [])
)
(fun () ->
Client.Client.VM.set_groups ~rpc ~session_id ~self:vm
~value:[vm_group1; vm_group2]
) ;
()

let test =
[
("test_associate_vm_with_vm_group", `Quick, test_associate_vm_with_vm_group)
; ( "test_vm_can_only_belong_to_one_anti_affinity_group"
, `Quick
, test_vm_can_only_belong_to_one_anti_affinity_group
)
]

0 comments on commit 5789cea

Please sign in to comment.