Skip to content

Commit 1d5e382

Browse files
committed
Add unit tests for sharding API
1 parent 7705144 commit 1d5e382

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
24+
)
25+
26+
var _ = Describe("#LabelShard", func() {
27+
It("should append the ControllerRing name", func() {
28+
Expect(LabelShard("foo")).To(Equal("shard.alpha.sharding.timebertt.dev/foo"))
29+
})
30+
})
31+
32+
var _ = Describe("#LabelDrain", func() {
33+
It("should append the ControllerRing name", func() {
34+
Expect(LabelDrain("foo")).To(Equal("drain.alpha.sharding.timebertt.dev/foo"))
35+
})
36+
})

pkg/apis/sharding/v1alpha1/types_controllerring.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c *ControllerRing) LabelDrain() string {
131131
return LabelDrain(c.Name)
132132
}
133133

134-
// RingResources returns the the list of resources that are distributed across shards in this ControllerRing.
134+
// RingResources returns the list of resources that are distributed across shards in this ControllerRing.
135135
func (c *ControllerRing) RingResources() []RingResource {
136136
return c.Spec.Resources
137137
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
24+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
25+
)
26+
27+
var _ = Describe("ControllerRing", func() {
28+
var ring *ControllerRing
29+
30+
BeforeEach(func() {
31+
ring = &ControllerRing{
32+
ObjectMeta: metav1.ObjectMeta{
33+
Name: "operator",
34+
},
35+
Spec: ControllerRingSpec{
36+
Resources: []RingResource{{
37+
GroupResource: metav1.GroupResource{
38+
Group: "",
39+
Resource: "configmaps",
40+
},
41+
ControlledResources: []metav1.GroupResource{{
42+
Group: "",
43+
Resource: "secrets",
44+
}},
45+
}},
46+
},
47+
}
48+
})
49+
50+
Describe("#LeaseSelector", func() {
51+
It("should return the correct lease selector", func() {
52+
Expect(ring.LeaseSelector().String()).To(Equal("alpha.sharding.timebertt.dev/controllerring=operator"))
53+
})
54+
})
55+
56+
Describe("#LabelShard", func() {
57+
It("should append the ControllerRing name", func() {
58+
Expect(ring.LabelShard()).To(Equal("shard.alpha.sharding.timebertt.dev/operator"))
59+
})
60+
})
61+
62+
Describe("#LabelDrain", func() {
63+
It("should append the ControllerRing name", func() {
64+
Expect(ring.LabelDrain()).To(Equal("drain.alpha.sharding.timebertt.dev/operator"))
65+
})
66+
})
67+
68+
Describe("#RingResources", func() {
69+
It("should return the specified resources", func() {
70+
Expect(ring.RingResources()).To(Equal(ring.Spec.Resources))
71+
})
72+
})
73+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestV1alpha1(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Sharding API V1alpha1 Suite")
29+
}

0 commit comments

Comments
 (0)