summaryrefslogtreecommitdiff
path: root/libvirt/utils_test.go
blob: f2cd9aafa7c47fd199a7e779a8c95de12069baec (plain)
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
package libvirt

import (
	"bytes"
	"errors"
	"log"
	"net"
	"os"
	"testing"
	"time"
)

func TestDiskLetterForIndex(t *testing.T) {

	diskNumbers := []int{0, 1, 2, 3, 4, 16, 24, 25, 26, 30, 300}
	names := []string{"a", "b", "c", "d", "e", "q", "y", "z", "aa", "ae", "ko"}

	for i, diskNumber := range diskNumbers {
		ret := DiskLetterForIndex(diskNumber)
		if ret != names[i] {
			t.Errorf("Expected %s, got %s for disk %d", names[i], ret, diskNumber)
		}
	}
}

func TestIPsRange(t *testing.T) {
	_, net, err := net.ParseCIDR("192.168.18.1/24")
	if err != nil {
		t.Errorf("When parsing network: %s", err)
	}

	start, end := NetworkRange(net)
	if start.String() != "192.168.18.0" {
		t.Errorf("unexpected range start for '%s': %s", net, start)
	}
	if end.String() != "192.168.18.255" {
		t.Errorf("unexpected range start for '%s': %s", net, start)
	}
}

func TestWaitForSuccessEverythingFine(t *testing.T) {
	waitSleep := WaitSleepInterval
	waitTimeout := WaitTimeout
	defer func() {
		WaitSleepInterval = waitSleep
		WaitTimeout = waitTimeout
	}()

	WaitTimeout = 1 * time.Second
	WaitSleepInterval = 1 * time.Nanosecond

	err := WaitForSuccess(
		"boom",
		func() error {
			return nil
		})

	if err != nil {
		t.Errorf("unexpected error %v", err)
	}
}

func TestWaitForSuccessBrokenFunction(t *testing.T) {
	waitSleep := WaitSleepInterval
	waitTimeout := WaitTimeout
	var b bytes.Buffer
	log.SetOutput(&b)
	defer func() {
		WaitSleepInterval = waitSleep
		WaitTimeout = waitTimeout
		log.SetOutput(os.Stderr)
	}()

	WaitTimeout = 1 * time.Second
	WaitSleepInterval = 1 * time.Nanosecond

	err := WaitForSuccess(
		"boom",
		func() error {
			return errors.New("something went wrong")
		})

	if err == nil {
		t.Error("expected error")
	}
}