summaryrefslogtreecommitdiff
path: root/pkg/registrar/registrar_test.go
blob: 0c1ef312aef7fe1391bc5954dec6b95dfa106c11 (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
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
package registrar

import (
	"reflect"
	"testing"
)

func TestReserve(t *testing.T) {
	r := NewRegistrar()

	obj := "test1"
	if err := r.Reserve("test", obj); err != nil {
		t.Fatal(err)
	}

	if err := r.Reserve("test", obj); err != nil {
		t.Fatal(err)
	}

	obj2 := "test2"
	err := r.Reserve("test", obj2)
	if err == nil {
		t.Fatalf("expected error when reserving an already reserved name to another object")
	}
	if err != ErrNameReserved {
		t.Fatal("expected `ErrNameReserved` error when attempting to reserve an already reserved name")
	}
}

func TestRelease(t *testing.T) {
	r := NewRegistrar()
	obj := "testing"

	if err := r.Reserve("test", obj); err != nil {
		t.Fatal(err)
	}
	r.Release("test")
	r.Release("test") // Ensure there is no panic here

	if err := r.Reserve("test", obj); err != nil {
		t.Fatal(err)
	}
}

func TestGetNames(t *testing.T) {
	r := NewRegistrar()
	obj := "testing"
	names := []string{"test1", "test2"}

	for _, name := range names {
		if err := r.Reserve(name, obj); err != nil {
			t.Fatal(err)
		}
	}
	r.Reserve("test3", "other")

	names2, err := r.GetNames(obj)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(names, names2) {
		t.Fatalf("Exepected: %v, Got: %v", names, names2)
	}
}

func TestDelete(t *testing.T) {
	r := NewRegistrar()
	obj := "testing"
	names := []string{"test1", "test2"}
	for _, name := range names {
		if err := r.Reserve(name, obj); err != nil {
			t.Fatal(err)
		}
	}

	r.Reserve("test3", "other")
	r.Delete(obj)

	_, err := r.GetNames(obj)
	if err == nil {
		t.Fatal("expected error getting names for deleted key")
	}

	if err != ErrNoSuchKey {
		t.Fatal("expected `ErrNoSuchKey`")
	}
}

func TestGet(t *testing.T) {
	r := NewRegistrar()
	obj := "testing"
	name := "test"

	_, err := r.Get(name)
	if err == nil {
		t.Fatal("expected error when key does not exist")
	}
	if err != ErrNameNotReserved {
		t.Fatal(err)
	}

	if err := r.Reserve(name, obj); err != nil {
		t.Fatal(err)
	}

	if _, err = r.Get(name); err != nil {
		t.Fatal(err)
	}

	r.Delete(obj)
	_, err = r.Get(name)
	if err == nil {
		t.Fatal("expected error when key does not exist")
	}
	if err != ErrNameNotReserved {
		t.Fatal(err)
	}
}