summaryrefslogtreecommitdiff
path: root/pkg/registrar/registrar_test.go
blob: 3a9b3af87876d066b1fbd747a24ee0d8bbe46ab6 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package registrar_test

import (
	"testing"

	"github.com/containers/libpod/v2/pkg/registrar"
	. "github.com/containers/libpod/v2/test/framework"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

// TestRegistrar runs the created specs
func TestRegistrar(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Registrar")
}

// nolint: gochecknoglobals
var t *TestFramework

var _ = BeforeSuite(func() {
	t = NewTestFramework(NilFunc, NilFunc)
	t.Setup()
})

var _ = AfterSuite(func() {
	t.Teardown()
})

// The actual test suite
var _ = t.Describe("Registrar", func() {
	// Constant test data needed by some tests
	const (
		testKey    = "testKey"
		testName   = "testName"
		anotherKey = "anotherKey"
	)

	// The system under test
	var sut *registrar.Registrar

	// Prepare the system under test and register a test name and key before
	// each test
	BeforeEach(func() {
		sut = registrar.NewRegistrar()
		Expect(sut.Reserve(testName, testKey)).To(BeNil())
	})

	t.Describe("Reserve", func() {
		It("should succeed to reserve a new registrar", func() {
			// Given
			// When
			err := sut.Reserve("name", "key")

			// Then
			Expect(err).To(BeNil())
		})

		It("should succeed to reserve a registrar twice", func() {
			// Given
			// When
			err := sut.Reserve(testName, testKey)

			// Then
			Expect(err).To(BeNil())
		})

		It("should fail to reserve an already reserved registrar", func() {
			// Given
			// When
			err := sut.Reserve(testName, anotherKey)

			// Then
			Expect(err).NotTo(BeNil())
			Expect(err).To(Equal(registrar.ErrNameReserved))
		})
	})

	t.Describe("Release", func() {
		It("should succeed to release a registered registrar multiple times", func() {
			// Given
			// When
			// Then
			sut.Release(testName)
			sut.Release(testName)
		})

		It("should succeed to release a unknown registrar multiple times", func() {
			// Given
			// When
			// Then
			sut.Release(anotherKey)
			sut.Release(anotherKey)
		})

		It("should succeed to release and re-register a registrar", func() {
			// Given
			// When
			sut.Release(testName)
			err := sut.Reserve(testName, testKey)

			// Then
			Expect(err).To(BeNil())
		})
	})

	t.Describe("GetNames", func() {
		It("should succeed to retrieve a single name for a registrar", func() {
			// Given
			// When
			names, err := sut.GetNames(testKey)

			// Then
			Expect(err).To(BeNil())
			Expect(len(names)).To(Equal(1))
			Expect(names[0]).To(Equal(testName))
		})

		It("should succeed to retrieve all names for a registrar", func() {
			// Given
			testNames := []string{"test1", "test2"}
			for _, name := range testNames {
				Expect(sut.Reserve(name, anotherKey)).To(BeNil())
			}

			// When
			names, err := sut.GetNames(anotherKey)

			// Then
			Expect(err).To(BeNil())
			Expect(len(names)).To(Equal(2))
			Expect(names).To(Equal(testNames))
		})
	})

	t.Describe("GetNames", func() {
		It("should succeed to retrieve a single name for a registrar", func() {
			// Given
			// When
			names, err := sut.GetNames(testKey)

			// Then
			Expect(err).To(BeNil())
			Expect(len(names)).To(Equal(1))
			Expect(names[0]).To(Equal(testName))
		})

		It("should succeed to retrieve all names for a registrar", func() {
			// Given
			anotherKey := "anotherKey"
			testNames := []string{"test1", "test2"}
			for _, name := range testNames {
				Expect(sut.Reserve(name, anotherKey)).To(BeNil())
			}

			// When
			names, err := sut.GetNames(anotherKey)

			// Then
			Expect(err).To(BeNil())
			Expect(len(names)).To(Equal(2))
			Expect(names).To(Equal(testNames))
		})
	})

	t.Describe("Delete", func() {
		It("should succeed to delete a registrar", func() {
			// Given
			// When
			sut.Delete(testKey)

			// Then
			names, err := sut.GetNames(testKey)
			Expect(len(names)).To(BeZero())
			Expect(err).To(Equal(registrar.ErrNoSuchKey))
		})
	})

	t.Describe("Get", func() {
		It("should succeed to get a key for a registrar", func() {
			// Given
			// When
			key, err := sut.Get(testName)

			// Then
			Expect(err).To(BeNil())
			Expect(key).To(Equal(testKey))
		})

		It("should fail to get a key for a not existing registrar", func() {
			// Given
			// When
			key, err := sut.Get("notExistingName")

			// Then
			Expect(key).To(BeEmpty())
			Expect(err).To(Equal(registrar.ErrNameNotReserved))
		})
	})

	t.Describe("GetAll", func() {
		It("should succeed to get all names", func() {
			// Given
			// When
			names := sut.GetAll()

			// Then
			Expect(len(names)).To(Equal(1))
			Expect(len(names[testKey])).To(Equal(1))
			Expect(names[testKey][0]).To(Equal(testName))
		})
	})
})