Skip to content

Commit f2345a2

Browse files
committed
test: fix volumesnapshot unit test
Closes #2893 Signed-off-by: Armando Ruocco <[email protected]>
1 parent bd39be6 commit f2345a2

1 file changed

Lines changed: 38 additions & 43 deletions

File tree

‎pkg/reconciler/backup/volumesnapshot/reconciler_test.go‎

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package volumesnapshot
1818

1919
import (
20+
"fmt"
21+
2022
storagesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2123
v1 "k8s.io/api/core/v1"
2224
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/types"
2326
"k8s.io/client-go/tools/record"
2427
"k8s.io/utils/ptr"
2528
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -48,8 +51,10 @@ var _ = Describe("Volumesnapshot reconciler", func() {
4851
BeforeEach(func() {
4952
cluster = &apiv1.Cluster{
5053
ObjectMeta: metav1.ObjectMeta{
51-
Namespace: namespace,
52-
Name: clusterName,
54+
Namespace: namespace,
55+
Name: clusterName,
56+
Labels: map[string]string{},
57+
Annotations: map[string]string{},
5358
},
5459
Spec: apiv1.ClusterSpec{
5560
Backup: &apiv1.BackupConfiguration{
@@ -100,22 +105,20 @@ var _ = Describe("Volumesnapshot reconciler", func() {
100105

101106
result, err := executor.Execute(ctx, cluster, backup, targetPod, pvcs)
102107
Expect(err).ToNot(HaveOccurred())
103-
// we should have requested creation of snapshots, and so we'd return to
104-
// wait for them to be ready
105108
Expect(result).ToNot(BeNil())
106-
hasEvent := false
107-
var events string
108-
go func() {
109-
evts := <-fakeRecorder.Events
110-
hasEvent = true
111-
events = evts
112-
}()
113-
Eventually(func() bool {
114-
return hasEvent
115-
}).Should(BeTrue())
116-
Eventually(func() string {
117-
return events
118-
}).Should(ContainSubstring("Requesting fencing for Pod " + targetPod.Name))
109+
110+
var latestCluster apiv1.Cluster
111+
err = mockClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, &latestCluster)
112+
Expect(err).ToNot(HaveOccurred())
113+
114+
data, err := utils.GetFencedInstances(latestCluster.Annotations)
115+
Expect(err).ToNot(HaveOccurred())
116+
Expect(data.Len()).To(Equal(1))
117+
118+
var snapshotList storagesnapshotv1.VolumeSnapshotList
119+
err = mockClient.List(ctx, &snapshotList)
120+
Expect(err).ToNot(HaveOccurred())
121+
Expect(snapshotList.Items).NotTo(BeEmpty())
119122
})
120123

121124
It("should not fence the target pod when there are existing volumesnapshots", func(ctx SpecContext) {
@@ -158,19 +161,14 @@ var _ = Describe("Volumesnapshot reconciler", func() {
158161
// we should have found snapshots that are not ready, and so we'd return to
159162
// wait for them to be ready
160163
Expect(result).ToNot(BeNil())
161-
hasEvent := false
162-
var events string
163-
go func() {
164-
evts := <-fakeRecorder.Events
165-
hasEvent = true
166-
events = evts
167-
}()
168-
Consistently(func() bool {
169-
return hasEvent
170-
}).Should(BeFalse())
171-
Consistently(func() string {
172-
return events
173-
}).Should(BeEmpty())
164+
165+
var latestCluster apiv1.Cluster
166+
err = mockClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, &latestCluster)
167+
Expect(err).ToNot(HaveOccurred())
168+
169+
data, err := utils.GetFencedInstances(latestCluster.Annotations)
170+
Expect(err).ToNot(HaveOccurred())
171+
Expect(data.Len()).To(Equal(0))
174172
})
175173

176174
It("should unfence the target pod when the snapshots are ready", func(ctx SpecContext) {
@@ -205,6 +203,8 @@ var _ = Describe("Volumesnapshot reconciler", func() {
205203
},
206204
}
207205

206+
cluster.Annotations[utils.FencedInstanceAnnotation] = fmt.Sprintf(`["%s"]`, targetPod.Name)
207+
208208
mockClient := fake.NewClientBuilder().
209209
WithScheme(scheme.BuildWithAllKnownScheme()).
210210
WithObjects(cluster, targetPod).
@@ -221,18 +221,13 @@ var _ = Describe("Volumesnapshot reconciler", func() {
221221
// we should have found snapshots that are ready, and so the result
222222
// should be nil
223223
Expect(result).To(BeNil())
224-
hasEvent := false
225-
var events string
226-
go func() {
227-
evts := <-fakeRecorder.Events
228-
hasEvent = true
229-
events = evts
230-
}()
231-
Eventually(func() bool {
232-
return hasEvent
233-
}).Should(BeTrue())
234-
Eventually(func() string {
235-
return events
236-
}).Should(ContainSubstring("Un-fencing Pod " + targetPod.Name))
224+
225+
var latestCluster apiv1.Cluster
226+
err = mockClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, &latestCluster)
227+
Expect(err).ToNot(HaveOccurred())
228+
229+
data, err := utils.GetFencedInstances(latestCluster.Annotations)
230+
Expect(err).ToNot(HaveOccurred())
231+
Expect(data.Len()).To(Equal(0))
237232
})
238233
})

0 commit comments

Comments
 (0)