|
| 1 | +// Copyright 2022 OnMetal authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package clientutils_test |
| 16 | + |
| 17 | +import ( |
| 18 | + . "github.com/onmetal/controller-utils/clientutils" |
| 19 | + . "github.com/onsi/ginkgo" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("ObjectKey", func() { |
| 25 | + Context("ObjectKeySet", func() { |
| 26 | + var ( |
| 27 | + k1, k2, k3, k4, k5, k6 client.ObjectKey |
| 28 | + ) |
| 29 | + BeforeEach(func() { |
| 30 | + k1 = client.ObjectKey{ |
| 31 | + Namespace: "n1", |
| 32 | + Name: "foo", |
| 33 | + } |
| 34 | + k2 = client.ObjectKey{ |
| 35 | + Namespace: "n1", |
| 36 | + Name: "bar", |
| 37 | + } |
| 38 | + k3 = client.ObjectKey{ |
| 39 | + Namespace: "n2", |
| 40 | + Name: "foo", |
| 41 | + } |
| 42 | + k4 = client.ObjectKey{ |
| 43 | + Namespace: "n2", |
| 44 | + Name: "bar", |
| 45 | + } |
| 46 | + k5 = client.ObjectKey{ |
| 47 | + Name: "cluster1", |
| 48 | + } |
| 49 | + k6 = client.ObjectKey{ |
| 50 | + Name: "cluster2", |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + Describe("New", func() { |
| 55 | + It("should initialize a new object key set with the given elements", func() { |
| 56 | + s := NewObjectKeySet(k1, k2, k3, k4, k5, k6) |
| 57 | + Expect(s).To(Equal(ObjectKeySet{ |
| 58 | + k1: struct{}{}, |
| 59 | + k2: struct{}{}, |
| 60 | + k3: struct{}{}, |
| 61 | + k4: struct{}{}, |
| 62 | + k5: struct{}{}, |
| 63 | + k6: struct{}{}, |
| 64 | + })) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + Describe("Insert", func() { |
| 69 | + It("should insert the given items", func() { |
| 70 | + s := NewObjectKeySet() |
| 71 | + s.Insert(k1, k2, k3) |
| 72 | + Expect(s).To(Equal(ObjectKeySet{ |
| 73 | + k1: struct{}{}, |
| 74 | + k2: struct{}{}, |
| 75 | + k3: struct{}{}, |
| 76 | + })) |
| 77 | + |
| 78 | + s.Insert(k4, k5, k6) |
| 79 | + Expect(s).To(Equal(ObjectKeySet{ |
| 80 | + k1: struct{}{}, |
| 81 | + k2: struct{}{}, |
| 82 | + k3: struct{}{}, |
| 83 | + k4: struct{}{}, |
| 84 | + k5: struct{}{}, |
| 85 | + k6: struct{}{}, |
| 86 | + })) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + Describe("Delete", func() { |
| 91 | + It("should delete the given items from the set", func() { |
| 92 | + s := NewObjectKeySet(k1, k2, k3, k4, k5, k6) |
| 93 | + |
| 94 | + s.Delete(k2, k4, k6) |
| 95 | + |
| 96 | + Expect(s).To(Equal(ObjectKeySet{ |
| 97 | + k1: struct{}{}, |
| 98 | + k3: struct{}{}, |
| 99 | + k5: struct{}{}, |
| 100 | + })) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + Describe("Has", func() { |
| 105 | + It("should return whether the given item is present in the set", func() { |
| 106 | + s := NewObjectKeySet(k1, k2, k3) |
| 107 | + |
| 108 | + Expect(s.Has(k1)).To(BeTrue(), "set should have key: set %#v, key %s", s, k1) |
| 109 | + Expect(s.Has(k2)).To(BeTrue(), "set should have key: set %#v, key %s", s, k2) |
| 110 | + Expect(s.Has(k3)).To(BeTrue(), "set should have key: set %#v, key %s", s, k3) |
| 111 | + Expect(s.Has(k4)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k4) |
| 112 | + Expect(s.Has(k5)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k5) |
| 113 | + Expect(s.Has(k6)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k6) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + Describe("Len", func() { |
| 118 | + It("should return the length of the set", func() { |
| 119 | + Expect(NewObjectKeySet(k1, k2, k3).Len()).To(Equal(3)) |
| 120 | + Expect((ObjectKeySet)(nil).Len()).To(Equal(0)) |
| 121 | + Expect(NewObjectKeySet().Len()).To(Equal(0)) |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments