Skip to content

Commit 46be900

Browse files
authored
Merge pull request #125 from Dyanngg/conformance-setup-check
Change conformance test ready condition
2 parents fed6822 + fb53f86 commit 46be900

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

conformance/utils/kubernetes/helper.go

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"time"
1010

1111
"github.com/stretchr/testify/require"
12-
v1 "k8s.io/api/core/v1"
12+
appsv1 "k8s.io/api/apps/v1"
13+
"k8s.io/apimachinery/pkg/types"
1314
"k8s.io/apimachinery/pkg/util/wait"
1415
"k8s.io/kubernetes/test/e2e/framework"
1516
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
@@ -18,6 +19,10 @@ import (
1819
"sigs.k8s.io/network-policy-api/conformance/utils/config"
1920
)
2021

22+
var (
23+
numStatufulSetReplicas int32 = 2
24+
)
25+
2126
// PokeServer is a utility function that checks if the connection from the provided clientPod in clientNamespace towards the targetHost:targetPort
2227
// using the provided protocol can be established or not and returns the result based on if the expectation is shouldConnect or !shouldConnect
2328
func PokeServer(t *testing.T, clientNamespace, clientPod, protocol, targetHost string, targetPort int32, timeout time.Duration, shouldConnect bool) bool {
@@ -71,45 +76,28 @@ func PokeServer(t *testing.T, clientNamespace, clientPod, protocol, targetHost s
7176

7277
// NamespacesMustBeReady waits until all Pods are marked Ready. This will
7378
// cause the test to halt if the specified timeout is exceeded.
74-
func NamespacesMustBeReady(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, namespaces []string) {
79+
func NamespacesMustBeReady(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, namespaces []string, statefulSetNames []string) {
7580
t.Helper()
81+
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.NamespacesMustBeReady)
82+
defer cancel()
7683

7784
waitErr := wait.PollImmediate(1*time.Second, timeoutConfig.NamespacesMustBeReady, func() (bool, error) {
78-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
79-
defer cancel()
80-
81-
for _, ns := range namespaces {
82-
podList := &v1.PodList{}
83-
err := c.List(ctx, podList, client.InNamespace(ns))
84-
if err != nil {
85-
t.Errorf("Error listing Pods: %v", err)
85+
for i, ns := range namespaces {
86+
statefulSet := &appsv1.StatefulSet{}
87+
statefulSetKey := types.NamespacedName{
88+
Namespace: ns,
89+
Name: statefulSetNames[i],
8690
}
87-
for _, pod := range podList.Items {
88-
if !findPodConditionInList(t, pod.Status.Conditions, "Ready", "True") &&
89-
pod.Status.Phase != v1.PodSucceeded {
90-
t.Logf("%s/%s Pod not ready yet", ns, pod.Name)
91-
return false, nil
92-
}
91+
if err := c.Get(ctx, statefulSetKey, statefulSet); err != nil {
92+
t.Errorf("Error retrieving StatefulSet %s from namespace %s: %v", statefulSetNames[i], ns, err)
93+
}
94+
if statefulSet.Status.ReadyReplicas != numStatufulSetReplicas {
95+
t.Logf("StatefulSet replicas in namespace %s not rolled out yet. %d/%d replicas are available.", ns, statefulSet.Status.ReadyReplicas, numStatufulSetReplicas)
96+
return false, nil
9397
}
9498
}
95-
t.Logf("Namespaces and Pods in %s namespaces ready", strings.Join(namespaces, ", "))
99+
t.Logf("Namespaces and Pods in %s namespaces are ready", strings.Join(namespaces, ", "))
96100
return true, nil
97101
})
98102
require.NoErrorf(t, waitErr, "error waiting for %s namespaces to be ready", strings.Join(namespaces, ", "))
99103
}
100-
101-
func findPodConditionInList(t *testing.T, conditions []v1.PodCondition, condName, condValue string) bool {
102-
t.Helper()
103-
104-
for _, cond := range conditions {
105-
if cond.Type == v1.PodConditionType(condName) {
106-
if cond.Status == v1.ConditionStatus(condValue) {
107-
return true
108-
}
109-
t.Logf("%s condition set to %s, expected %s", condName, cond.Status, condValue)
110-
}
111-
}
112-
113-
t.Logf("%s was not in conditions list", condName)
114-
return false
115-
}

conformance/utils/suite/suite.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ func (suite *ConformanceTestSuite) Setup(t *testing.T) {
123123
"network-policy-conformance-hufflepuff",
124124
"network-policy-conformance-ravenclaw",
125125
}
126-
kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces)
126+
statefulSets := []string{
127+
"harry-potter",
128+
"draco-malfoy",
129+
"cedric-diggory",
130+
"luna-lovegood",
131+
}
132+
kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces, statefulSets)
127133
}
128134
}
129135

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ require (
66
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0
77
github.com/stretchr/testify v1.8.1
88
k8s.io/api v0.26.1
9+
k8s.io/apiextensions-apiserver v0.26.1
910
k8s.io/apimachinery v0.26.1
1011
k8s.io/client-go v0.26.1
1112
k8s.io/code-generator v0.26.1
12-
k8s.io/klog v0.2.0
1313
k8s.io/kubernetes v1.26.0
1414
sigs.k8s.io/controller-runtime v0.14.6
1515
sigs.k8s.io/controller-tools v0.11.1
16+
sigs.k8s.io/yaml v1.3.0
1617
)
1718

1819
require (
@@ -91,11 +92,11 @@ require (
9192
gopkg.in/inf.v0 v0.9.1 // indirect
9293
gopkg.in/yaml.v2 v2.4.0 // indirect
9394
gopkg.in/yaml.v3 v3.0.1 // indirect
94-
k8s.io/apiextensions-apiserver v0.26.1 // indirect
9595
k8s.io/apiserver v0.26.1 // indirect
9696
k8s.io/component-base v0.26.1 // indirect
9797
k8s.io/component-helpers v0.26.0 // indirect
9898
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
99+
k8s.io/klog v0.2.0 // indirect
99100
k8s.io/klog/v2 v2.90.1 // indirect
100101
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
101102
k8s.io/kubectl v0.26.0 // indirect
@@ -104,5 +105,4 @@ require (
104105
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
105106
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
106107
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
107-
sigs.k8s.io/yaml v1.3.0 // indirect
108108
)

0 commit comments

Comments
 (0)