aboutsummaryrefslogtreecommitdiff
path: root/libvirt/utils_test.go
blob: cdcf05b955f9cedb6922174689754829af811234 (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
package libvirt

import (
	"errors"
	"net"
	"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) {
	wait_sleep := WAIT_SLEEP_INTERVAL
	wait_timeout := WAIT_TIMEOUT
	defer func() {
		WAIT_SLEEP_INTERVAL = wait_sleep
		WAIT_TIMEOUT = wait_timeout
	}()

	WAIT_TIMEOUT = 1 * time.Second
	WAIT_SLEEP_INTERVAL = 1 * time.Nanosecond

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

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

func TestWaitForSuccessBrokenFunction(t *testing.T) {
	wait_sleep := WAIT_SLEEP_INTERVAL
	wait_timeout := WAIT_TIMEOUT
	defer func() {
		WAIT_SLEEP_INTERVAL = wait_sleep
		WAIT_TIMEOUT = wait_timeout
	}()

	WAIT_TIMEOUT = 1 * time.Second
	WAIT_SLEEP_INTERVAL = 1 * time.Nanosecond

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

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