Skip to content

Commit 0e22825

Browse files
authored
Merge pull request #277 from gibizer/volume-claim
[test]Handle PVC in StatefulSet and Deployment
2 parents f16ea22 + a41eea4 commit 0e22825

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

modules/test/helpers/deployment.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package helpers
1515

1616
import (
1717
"encoding/json"
18+
1819
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
1920
"github.com/onsi/gomega"
2021
corev1 "k8s.io/api/core/v1"
@@ -76,14 +77,22 @@ func (tc *TestHelper) SimulateDeploymentReplicaReady(name types.NamespacedName)
7677
// map[string][]string{manilaName.Namespace + "/internalapi": {"10.0.0.1"}},
7778
// )
7879
func (tc *TestHelper) SimulateDeploymentReadyWithPods(name types.NamespacedName, networkIPs map[string][]string) {
79-
ss := tc.GetDeployment(name)
80-
for i := 0; i < int(*ss.Spec.Replicas); i++ {
80+
depl := tc.GetDeployment(name)
81+
for i := 0; i < int(*depl.Spec.Replicas); i++ {
8182
pod := &corev1.Pod{
82-
ObjectMeta: ss.Spec.Template.ObjectMeta,
83-
Spec: ss.Spec.Template.Spec,
83+
ObjectMeta: depl.Spec.Template.ObjectMeta,
84+
Spec: depl.Spec.Template.Spec,
8485
}
8586
pod.ObjectMeta.Namespace = name.Namespace
8687
pod.ObjectMeta.GenerateName = name.Name
88+
// NOTE(gibi): If there is a mount that refers to a volume created via
89+
// persistent volume claim then that mount won't have a corresponding
90+
// volume created in EnvTest as we are not simulating the k8s volume
91+
// claim logic here at the moment. Therefore the Pod create would fail
92+
// with a missing volume. So to avoid that we remove every mount and
93+
// volume from the pod we create here.
94+
pod.Spec.Volumes = []corev1.Volume{}
95+
pod.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{}
8796

8897
var netStatus []networkv1.NetworkStatus
8998
for network, IPs := range networkIPs {
@@ -103,10 +112,10 @@ func (tc *TestHelper) SimulateDeploymentReadyWithPods(name types.NamespacedName,
103112
}
104113

105114
gomega.Eventually(func(g gomega.Gomega) {
106-
ss := tc.GetDeployment(name)
107-
ss.Status.Replicas = 1
108-
ss.Status.ReadyReplicas = 1
109-
g.Expect(tc.K8sClient.Status().Update(tc.Ctx, ss)).To(gomega.Succeed())
115+
depl := tc.GetDeployment(name)
116+
depl.Status.Replicas = 1
117+
depl.Status.ReadyReplicas = 1
118+
g.Expect(tc.K8sClient.Status().Update(tc.Ctx, depl)).To(gomega.Succeed())
110119

111120
}, tc.Timeout, tc.Interval).Should(gomega.Succeed())
112121

modules/test/helpers/statefulset.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package helpers
1515

1616
import (
1717
"encoding/json"
18+
1819
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
1920
t "github.com/onsi/gomega"
2021
appsv1 "k8s.io/api/apps/v1"
@@ -71,6 +72,15 @@ func (tc *TestHelper) SimulateStatefulSetReplicaReadyWithPods(name types.Namespa
7172
pod.ObjectMeta.Namespace = name.Namespace
7273
pod.ObjectMeta.GenerateName = name.Name
7374

75+
// NOTE(gibi): If there is a mount that refers to a volume created via
76+
// persistent volume claim then that mount won't have a corresponding
77+
// volume created in EnvTest as we are not simulating the k8s volume
78+
// claim logic here at the moment. Therefore the Pod create would fail
79+
// with a missing volume. So to avoid that we remove every mount and
80+
// volume from the pod we create here.
81+
pod.Spec.Volumes = []corev1.Volume{}
82+
pod.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{}
83+
7484
var netStatus []networkv1.NetworkStatus
7585
for network, IPs := range networkIPs {
7686
netStatus = append(

0 commit comments

Comments
 (0)