-
Notifications
You must be signed in to change notification settings - Fork 20
/
affinitygroup_test.go
182 lines (153 loc) · 4.89 KB
/
affinitygroup_test.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package ovirtclient_test
import (
"fmt"
"testing"
ovirtclient "github.com/ovirt/go-ovirt-client/v3"
)
func assertCanCreateAffinityGroup(t *testing.T, helper ovirtclient.TestHelper, params ovirtclient.CreateAffinityGroupOptionalParams) ovirtclient.AffinityGroup {
client := helper.GetClient()
testClusterID := helper.GetClusterID()
affinityGroup, err := client.CreateAffinityGroup(
testClusterID,
fmt.Sprintf("test_ag_%s", helper.GenerateRandomID(5)),
params,
)
if err != nil {
t.Fatalf("Failed to create affinity group (%v)", err)
}
t.Cleanup(
func() {
if err := affinityGroup.Remove(); err != nil {
if !ovirtclient.HasErrorCode(err, ovirtclient.ENotFound) {
t.Fatalf("Failed to clean up affinity group %s (%v)", affinityGroup.ID(), err)
}
}
},
)
return affinityGroup
}
func assertCanRemoveAffinityGroup(
t *testing.T,
helper ovirtclient.TestHelper,
clusterID ovirtclient.ClusterID,
id ovirtclient.AffinityGroupID,
) {
client := helper.GetClient()
if err := client.RemoveAffinityGroup(clusterID, id); err != nil {
t.Fatalf("Failed to remove affinity group %s from cluster %s (%v)", id, clusterID, err)
}
}
func assertCanGetAffinityGroup(
t *testing.T,
helper ovirtclient.TestHelper,
clusterID ovirtclient.ClusterID,
id ovirtclient.AffinityGroupID,
) ovirtclient.AffinityGroup {
ag, err := helper.GetClient().GetAffinityGroup(clusterID, id)
if err != nil {
t.Fatalf("Failed to fetch affinity group %s from cluster %s (%v)", id, clusterID, err)
}
return ag
}
func assertAffinityGroupListHasAffinityGroup(
t *testing.T,
helper ovirtclient.TestHelper,
clusterID ovirtclient.ClusterID,
id ovirtclient.AffinityGroupID,
) {
agList, err := helper.GetClient().ListAffinityGroups(clusterID)
if err != nil {
t.Fatalf("Failed to list affinity groups in cluster %s (%v)", clusterID, err)
}
for _, ag := range agList {
if ag.ID() == id {
return
}
}
t.Fatalf("Failed to find affinity group %s in cluster %s", id, clusterID)
}
func TestAffinityGroupCreation(t *testing.T) {
t.Parallel()
helper := getHelper(t)
ag := assertCanCreateAffinityGroup(t, helper, nil)
if ag.ID() == "" {
t.Fatalf("Returned affinity group ID is empty.")
}
if ag.Name() == "" {
t.Fatalf("Returned affinity group name is empty.")
}
if ag.Enforcing() == true {
t.Fatalf("Returned affinity group enforcing flag is true.")
}
if ag.Priority() != 1 {
t.Fatalf("Returned affinity group priority is %f, not 1.", ag.Priority())
}
if ag.HostsRule().Enabled() != false {
t.Fatalf("Hosts rule is enabled on returned affinity group.")
}
if ag.VMsRule().Enabled() != false {
t.Fatalf("VMs rule is enabled on returned affinity group.")
}
secondAG := assertCanGetAffinityGroup(t, helper, ag.ClusterID(), ag.ID())
if ag.ID() != secondAG.ID() {
t.Fatalf("Affinity group IDs don't match (%s != %s)", ag.ID(), secondAG.ID())
}
if ag.Name() != secondAG.Name() {
t.Fatalf("Affinity group names don't match (%s != %s)", ag.Name(), secondAG.Name())
}
assertAffinityGroupListHasAffinityGroup(t, helper, ag.ClusterID(), ag.ID())
assertCanRemoveAffinityGroup(t, helper, ag.ClusterID(), ag.ID())
}
func TestAffinityGroupCreationWithDescription(t *testing.T) {
t.Parallel()
helper := getHelper(t)
params := ovirtclient.CreateAffinityGroupParams()
params = params.MustWithDescription("test")
ag := assertCanCreateAffinityGroup(t, helper, params)
if ag.Description() != "test" {
t.Fatalf("Incorrect affinity group description: %s", ag.Description())
}
}
func TestNegativeVMAffinityShouldResultInDifferentHosts(t *testing.T) {
t.Parallel()
helper := getHelper(t)
ag := assertCanCreateAffinityGroup(
t,
helper,
ovirtclient.
CreateAffinityGroupParams().
MustWithVMsRuleParameters(true, ovirtclient.AffinityNegative, true),
)
vm1 := assertCanCreateBootableVM(t, helper)
vm2 := assertCanCreateBootableVM(t, helper)
assertCanAddVMToAffinityGroup(t, vm1, ag)
assertCanAddVMToAffinityGroup(t, vm2, ag)
assertCanStartVM(t, helper, vm1)
vm1 = assertVMWillStart(t, vm1)
if err := vm2.Start(); err != nil {
t.Logf("VM 2 failed to start, we assume there are not enough hosts available. (%v)", err)
return
}
t.Cleanup(
func() {
if err := vm2.Stop(true); err != nil {
t.Fatalf("Failed to stop VM %s (%v)", vm2.ID(), err)
}
if _, err := vm2.WaitForStatus(ovirtclient.VMStatusDown); err != nil {
t.Fatalf("Failed to wait for VM %s to stop (%v)", vm2.ID(), err)
}
})
vm2, err := vm2.WaitForStatus(ovirtclient.VMStatusUp)
if err != nil {
t.Logf("VM 2 failed to start, we assume there are not enough hosts available. (%v)", err)
return
}
if *vm1.HostID() == *vm2.HostID() {
t.Fatalf("Identical hosts for both VMs despite affinity group.")
}
}
func assertCanAddVMToAffinityGroup(t *testing.T, vm1 ovirtclient.VM, ag ovirtclient.AffinityGroup) {
if err := ag.AddVM(vm1.ID()); err != nil {
t.Fatalf("Failed to add VM %s to affinity group %s (%v)", vm1.ID(), ag.ID(), err)
}
}