Skip to content

Commit 14dd9bd

Browse files
authored
Merge pull request #325 from luizbafilho/backend-tests
Improves Backend API Client tests
2 parents 50f61be + 45d2a58 commit 14dd9bd

File tree

2 files changed

+139
-33
lines changed

2 files changed

+139
-33
lines changed

lib/backend/backend_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package backend
1+
package backend_test
22

33
import (
44
. "github.com/onsi/ginkgo"

lib/backend/backend_test.go

Lines changed: 138 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ package backend_test
1717
import (
1818
. "github.com/onsi/ginkgo"
1919
. "github.com/onsi/gomega"
20-
"github.com/projectcalico/libcalico-go/lib/backend/model"
2120

2221
"github.com/projectcalico/libcalico-go/lib/backend/etcd"
22+
"github.com/projectcalico/libcalico-go/lib/backend/model"
2323
"github.com/projectcalico/libcalico-go/lib/testutils"
2424
)
2525

@@ -29,50 +29,156 @@ var etcdConfig = &etcd.EtcdConfig{
2929
}
3030

3131
var _ = Describe("Backend tests", func() {
32-
Describe("etcd GET/List Revision values", func() {
32+
etcdClient, _ := etcd.NewEtcdClient(etcdConfig)
33+
34+
var (
35+
block model.KVPair
36+
err error
37+
)
38+
39+
BeforeEach(func() {
3340
testutils.CleanEtcd()
3441

35-
etcdClient, _ := etcd.NewEtcdClient(etcdConfig)
42+
block = model.KVPair{
43+
Key: model.BlockKey{
44+
CIDR: testutils.MustParseCIDR("10.0.0.0/26"),
45+
},
46+
Value: model.AllocationBlock{
47+
CIDR: testutils.MustParseCIDR("10.0.0.0/26"),
48+
},
49+
}
50+
51+
_, err = etcdClient.Create(&block)
52+
Expect(err).NotTo(HaveOccurred())
53+
})
3654

37-
Context("CREATE a Block", func() {
38-
block := &model.KVPair{
39-
Key: model.BlockKey{
40-
CIDR: testutils.MustParseCIDR("10.0.0.0/26"),
41-
},
42-
Value: model.AllocationBlock{
43-
CIDR: testutils.MustParseCIDR("10.0.0.0/26"),
44-
},
55+
Describe("Create", func() {
56+
57+
It("persists a new kv pair", func() {
58+
kv, err := etcdClient.Get(block.Key)
59+
Expect(err).NotTo(HaveOccurred())
60+
61+
expectedCIDR := kv.Value.(*model.AllocationBlock).CIDR
62+
persitedCIRD := block.Value.(model.AllocationBlock).CIDR
63+
64+
Expect(expectedCIDR).To(Equal(persitedCIRD))
65+
})
66+
67+
It("sets revision field", func() {
68+
kv, err := etcdClient.Get(block.Key)
69+
70+
Expect(err).NotTo(HaveOccurred())
71+
Expect(kv.Revision).NotTo(BeNil())
72+
})
73+
74+
})
75+
76+
Describe("Update", func() {
77+
78+
It("updates a kv pair", func() {
79+
block.Value = model.AllocationBlock{
80+
CIDR: testutils.MustParseCIDR("192.168.0.0/26"),
4581
}
4682

47-
_, cErr := etcdClient.Create(block)
83+
kv, err := etcdClient.Update(&block)
84+
Expect(err).NotTo(HaveOccurred())
4885

49-
It("should succeed without an error", func() {
50-
Expect(cErr).NotTo(HaveOccurred())
51-
})
86+
expectedCIDR := kv.Value.(model.AllocationBlock).CIDR
87+
persitedCIRD := block.Value.(model.AllocationBlock).CIDR
88+
89+
Expect(expectedCIDR).To(Equal(persitedCIRD))
5290
})
5391

54-
Context("GET BlockKey", func() {
55-
key := model.BlockKey{
56-
CIDR: testutils.MustParseCIDR("10.0.0.0/26"),
92+
It("sets revision field", func() {
93+
kv, err := etcdClient.Update(&block)
94+
95+
Expect(err).NotTo(HaveOccurred())
96+
Expect(kv.Revision).NotTo(BeNil())
97+
})
98+
99+
It("validates revision field", func() {
100+
block.Revision = uint64(1000)
101+
_, err := etcdClient.Update(&block)
102+
Expect(err).To(HaveOccurred())
103+
})
104+
105+
})
106+
107+
Describe("Apply", func() {
108+
109+
It("updates a kv pair", func() {
110+
block.Value = model.AllocationBlock{
111+
CIDR: testutils.MustParseCIDR("192.168.0.0/26"),
57112
}
58-
b, bErr := etcdClient.Get(key)
59-
It("Revision field should not be nil", func() {
60-
Expect(bErr).NotTo(HaveOccurred())
61-
Expect(b.Revision).NotTo(BeNil())
62-
})
113+
114+
kv, err := etcdClient.Apply(&block)
115+
Expect(err).NotTo(HaveOccurred())
116+
117+
expectedCIDR := kv.Value.(model.AllocationBlock).CIDR
118+
persitedCIRD := block.Value.(model.AllocationBlock).CIDR
119+
120+
Expect(expectedCIDR).To(Equal(persitedCIRD))
63121
})
64122

65-
Context("LIST BlockKey", func() {
66-
blockListOpt := model.BlockListOptions{
67-
IPVersion: 4,
123+
It("creates a kv pair", func() {
124+
block.Key = model.BlockKey{
125+
CIDR: testutils.MustParseCIDR("192.168.0.0/26"),
68126
}
69-
bl, blErr := etcdClient.List(blockListOpt)
70-
for _, blv := range bl {
71-
It("Revision field should not be nil", func() {
72-
Expect(blErr).NotTo(HaveOccurred())
73-
Expect(blv.Revision).NotTo(BeNil())
74-
})
127+
128+
kv, err := etcdClient.Apply(&block)
129+
Expect(err).NotTo(HaveOccurred())
130+
131+
expectedCIDR := kv.Value.(model.AllocationBlock).CIDR
132+
persitedCIRD := block.Value.(model.AllocationBlock).CIDR
133+
134+
Expect(expectedCIDR).To(Equal(persitedCIRD))
135+
})
136+
137+
It("sets revision field", func() {
138+
block.Value = model.AllocationBlock{
139+
CIDR: testutils.MustParseCIDR("192.168.0.0/26"),
75140
}
141+
142+
kv, err := etcdClient.Apply(&block)
143+
144+
Expect(err).NotTo(HaveOccurred())
145+
Expect(kv.Revision).NotTo(BeNil())
146+
})
147+
148+
It("validates revision field", func() {
149+
block.Revision = uint64(1000)
150+
_, err := etcdClient.Apply(&block)
151+
Expect(err).NotTo(HaveOccurred())
76152
})
153+
154+
})
155+
156+
Describe("Delete", func() {
157+
158+
It("deletes the kv pair", func() {
159+
err := etcdClient.Delete(&block)
160+
Expect(err).NotTo(HaveOccurred())
161+
162+
_, err = etcdClient.Get(block.Key)
163+
Expect(err).To(HaveOccurred())
164+
})
165+
77166
})
167+
168+
Describe("List", func() {
169+
blockListOpt := model.BlockListOptions{
170+
IPVersion: 4,
171+
}
172+
173+
bl, err := etcdClient.List(blockListOpt)
174+
175+
for _, blv := range bl {
176+
It("sets revision field", func() {
177+
Expect(err).NotTo(HaveOccurred())
178+
Expect(blv.Revision).NotTo(BeNil())
179+
})
180+
}
181+
182+
})
183+
78184
})

0 commit comments

Comments
 (0)