domain_events_test.go 3.27 KB
Newer Older
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
/*
 * 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.
 *
 */

27 28 29
package libvirt

import (
30
	"fmt"
31 32 33 34 35 36 37 38 39 40 41
	"testing"
	"time"
)

func TestDomainEventRegister(t *testing.T) {

	callbackId := -1

	conn := buildTestConnection()
	defer func() {
		if callbackId >= 0 {
42 43
			if err := conn.DomainEventDeregister(callbackId); err != nil {
				t.Errorf("got `%v` on DomainEventDeregister instead of nil", err)
44
			}
45
		}
46 47
		if res, _ := conn.Close(); res != 0 {
			t.Errorf("Close() == %d, expected 0", res)
48
		}
49 50 51 52 53 54
	}()

	defName := time.Now().String()

	nbEvents := 0

55 56 57 58 59
	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)
60
			}
61 62 63 64 65 66 67 68
		}
		eventString := fmt.Sprintf("%s", event)
		expected := "Domain event=\"started\" detail=\"booted\""
		if eventString != expected {
			t.Errorf("event == %q, expected %q", eventString, expected)
		}
		nbEvents++
	}
69

70
	callbackId, err := conn.DomainEventLifecycleRegister(nil, callback)
71 72 73 74
	if err != nil {
		t.Error(err)
		return
	}
75 76 77 78 79 80 81 82 83

	// Test a minimally valid xml
	xml := `<domain type="test">
		<name>` + defName + `</name>
		<memory unit="KiB">8192</memory>
		<os>
			<type>hvm</type>
		</os>
	</domain>`
84
	dom, err := conn.DomainCreateXML(xml, DOMAIN_NONE)
85 86 87 88 89 90 91 92 93 94 95 96
	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() {
97 98
		dom.Destroy()
		dom.Free()
99 100
	}()

101 102 103 104
	// Check that the internal context entry was added, and that there only is
	// one.
	goCallbackLock.Lock()
	if len(goCallbacks) != 1 {
105
		t.Errorf("goCallbacks should hold one entry, got %+v", goCallbacks)
106 107 108
	}
	goCallbackLock.Unlock()

109
	// Deregister the event
110
	if err := conn.DomainEventDeregister(callbackId); err != nil {
111
		t.Fatalf("Event deregistration failed with: %v", err)
112 113
	}
	callbackId = -1 // Don't deregister twice
114 115 116 117

	// Check that the internal context entries was removed
	goCallbackLock.Lock()
	if len(goCallbacks) > 0 {
118
		t.Errorf("goCallbacks entry wasn't removed: %+v", goCallbacks)
119 120
	}
	goCallbackLock.Unlock()
121
}