Skip to content

Commit

Permalink
Merge pull request #125 from Dyanngg/conformance-setup-check
Browse files Browse the repository at this point in the history
Change conformance test ready condition
  • Loading branch information
Dyanngg authored Jul 18, 2023
2 parents fed6822 + fb53f86 commit 46be900
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
54 changes: 21 additions & 33 deletions conformance/utils/kubernetes/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"time"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/test/e2e/framework"
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
Expand All @@ -18,6 +19,10 @@ import (
"sigs.k8s.io/network-policy-api/conformance/utils/config"
)

var (
numStatufulSetReplicas int32 = 2
)

// PokeServer is a utility function that checks if the connection from the provided clientPod in clientNamespace towards the targetHost:targetPort
// using the provided protocol can be established or not and returns the result based on if the expectation is shouldConnect or !shouldConnect
func PokeServer(t *testing.T, clientNamespace, clientPod, protocol, targetHost string, targetPort int32, timeout time.Duration, shouldConnect bool) bool {
Expand Down Expand Up @@ -71,45 +76,28 @@ func PokeServer(t *testing.T, clientNamespace, clientPod, protocol, targetHost s

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

waitErr := wait.PollImmediate(1*time.Second, timeoutConfig.NamespacesMustBeReady, func() (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

for _, ns := range namespaces {
podList := &v1.PodList{}
err := c.List(ctx, podList, client.InNamespace(ns))
if err != nil {
t.Errorf("Error listing Pods: %v", err)
for i, ns := range namespaces {
statefulSet := &appsv1.StatefulSet{}
statefulSetKey := types.NamespacedName{
Namespace: ns,
Name: statefulSetNames[i],
}
for _, pod := range podList.Items {
if !findPodConditionInList(t, pod.Status.Conditions, "Ready", "True") &&
pod.Status.Phase != v1.PodSucceeded {
t.Logf("%s/%s Pod not ready yet", ns, pod.Name)
return false, nil
}
if err := c.Get(ctx, statefulSetKey, statefulSet); err != nil {
t.Errorf("Error retrieving StatefulSet %s from namespace %s: %v", statefulSetNames[i], ns, err)
}
if statefulSet.Status.ReadyReplicas != numStatufulSetReplicas {
t.Logf("StatefulSet replicas in namespace %s not rolled out yet. %d/%d replicas are available.", ns, statefulSet.Status.ReadyReplicas, numStatufulSetReplicas)
return false, nil
}
}
t.Logf("Namespaces and Pods in %s namespaces ready", strings.Join(namespaces, ", "))
t.Logf("Namespaces and Pods in %s namespaces are ready", strings.Join(namespaces, ", "))
return true, nil
})
require.NoErrorf(t, waitErr, "error waiting for %s namespaces to be ready", strings.Join(namespaces, ", "))
}

func findPodConditionInList(t *testing.T, conditions []v1.PodCondition, condName, condValue string) bool {
t.Helper()

for _, cond := range conditions {
if cond.Type == v1.PodConditionType(condName) {
if cond.Status == v1.ConditionStatus(condValue) {
return true
}
t.Logf("%s condition set to %s, expected %s", condName, cond.Status, condValue)
}
}

t.Logf("%s was not in conditions list", condName)
return false
}
8 changes: 7 additions & 1 deletion conformance/utils/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ func (suite *ConformanceTestSuite) Setup(t *testing.T) {
"network-policy-conformance-hufflepuff",
"network-policy-conformance-ravenclaw",
}
kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces)
statefulSets := []string{
"harry-potter",
"draco-malfoy",
"cedric-diggory",
"luna-lovegood",
}
kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces, statefulSets)
}
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ require (
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0
github.com/stretchr/testify v1.8.1
k8s.io/api v0.26.1
k8s.io/apiextensions-apiserver v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
k8s.io/code-generator v0.26.1
k8s.io/klog v0.2.0
k8s.io/kubernetes v1.26.0
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/controller-tools v0.11.1
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -91,11 +92,11 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/component-helpers v0.26.0 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/klog v0.2.0 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/kubectl v0.26.0 // indirect
Expand All @@ -104,5 +105,4 @@ require (
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

0 comments on commit 46be900

Please sign in to comment.