This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
domain_events_test.go
121 lines (106 loc) · 3.27 KB
/
domain_events_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
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (c) 2013 Alex Zorin
* Copyright (C) 2016 Red Hat, Inc.
*
*/
package libvirt
import (
"fmt"
"testing"
"time"
)
func TestDomainEventRegister(t *testing.T) {
callbackId := -1
conn := buildTestConnection()
defer func() {
if callbackId >= 0 {
if err := conn.DomainEventDeregister(callbackId); err != nil {
t.Errorf("got `%v` on DomainEventDeregister instead of nil", err)
}
}
if res, _ := conn.Close(); res != 0 {
t.Errorf("Close() == %d, expected 0", res)
}
}()
defName := time.Now().String()
nbEvents := 0
callback := func(c *Connect, d *Domain, event *DomainEventLifecycle) {
if event.Event == DOMAIN_EVENT_STARTED {
domName, _ := d.GetName()
if defName != domName {
t.Fatalf("Name was not '%s': %s", defName, domName)
}
}
eventString := fmt.Sprintf("%s", event)
expected := "Domain event=\"started\" detail=\"booted\""
if eventString != expected {
t.Errorf("event == %q, expected %q", eventString, expected)
}
nbEvents++
}
callbackId, err := conn.DomainEventLifecycleRegister(nil, callback)
if err != nil {
t.Error(err)
return
}
// Test a minimally valid xml
xml := `<domain type="test">
<name>` + defName + `</name>
<memory unit="KiB">8192</memory>
<os>
<type>hvm</type>
</os>
</domain>`
dom, err := conn.DomainCreateXML(xml, DOMAIN_NONE)
if err != nil {
t.Error(err)
return
}
// This is blocking as long as there is no message
EventRunDefaultImpl()
if nbEvents == 0 {
t.Fatal("At least one event was expected")
}
defer func() {
dom.Destroy()
dom.Free()
}()
// Check that the internal context entry was added, and that there only is
// one.
goCallbackLock.Lock()
if len(goCallbacks) != 1 {
t.Errorf("goCallbacks should hold one entry, got %+v", goCallbacks)
}
goCallbackLock.Unlock()
// Deregister the event
if err := conn.DomainEventDeregister(callbackId); err != nil {
t.Fatalf("Event deregistration failed with: %v", err)
}
callbackId = -1 // Don't deregister twice
// Check that the internal context entries was removed
goCallbackLock.Lock()
if len(goCallbacks) > 0 {
t.Errorf("goCallbacks entry wasn't removed: %+v", goCallbacks)
}
goCallbackLock.Unlock()
}