Skip to content

Commit

Permalink
Make e2e tests faster (#911)
Browse files Browse the repository at this point in the history
Instead of waiting for the secrets and configmap to propagate to the pod which could take ~2 mins restart the pod and force this update since the pod restart takes only a few seconds
  • Loading branch information
chitrangpatel committed Aug 31, 2023
1 parent 2a2fd11 commit 70c8c7d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 0 additions & 2 deletions test/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"io/ioutil"
"path/filepath"
"testing"
"time"

"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/sigstore/pkg/signature"
Expand Down Expand Up @@ -248,7 +247,6 @@ func setupSecret(ctx context.Context, t *testing.T, c kubernetes.Interface, opts
if _, err := c.CoreV1().Secrets(namespace).Update(ctx, &s, metav1.UpdateOptions{}); err != nil {
t.Fatal(err)
}
time.Sleep(time.Minute) // https://github.com/tektoncd/chains/issues/664

return secret{
cosignPriv: cosignPriv,
Expand Down
1 change: 0 additions & 1 deletion test/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func TestExamples(t *testing.T) {
ctx := context.Background()
c, ns, cleanup := setup(ctx, t, setupOpts{})
t.Cleanup(cleanup)

cleanUpInTotoFormatter := setConfigMap(ctx, t, c, test.cm)
runInTotoFormatterTests(ctx, t, ns, c, test)
cleanUpInTotoFormatter()
Expand Down
38 changes: 37 additions & 1 deletion test/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ import (
"github.com/tektoncd/chains/pkg/test/tekton"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
pipelineclientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)

func getTr(ctx context.Context, t *testing.T, c pipelineclientset.Interface, name, ns string) (tr *v1beta1.TaskRun) {
Expand Down Expand Up @@ -191,7 +194,10 @@ func setConfigMap(ctx context.Context, t *testing.T, c *clients, data map[string
if err != nil {
t.Fatal(err)
}
time.Sleep(30 * time.Second) // https://github.com/tektoncd/chains/issues/664
err = restartChainsControllerPod(ctx, c.KubeClient, 300*time.Second)
if err != nil {
t.Fatalf("Failed to restart the pod: %v", err)
}

return func() {
for k := range data {
Expand Down Expand Up @@ -281,3 +287,33 @@ func verifySignature(ctx context.Context, t *testing.T, c *clients, obj objects.
}
}
}

// restartChainsControllerPod restarts the pod running Chains
// it then waits for a given timeout for the pod to resume running state
func restartChainsControllerPod(ctx context.Context, c kubernetes.Interface, timeout time.Duration) error {
pods, err := c.CoreV1().Pods("tekton-chains").List(ctx, metav1.ListOptions{LabelSelector: "app.kubernetes.io/component=controller"})
if err != nil {
return err
}
pod := pods.Items[0]
podUid := pod.UID
gracePeriodSeconds := int64(0)
err = c.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{GracePeriodSeconds: &gracePeriodSeconds})
if err != nil {
return err
}

return wait.PollUntilContextTimeout(ctx, 2*time.Second, timeout, true, func(context.Context) (done bool, err error) {
pods, err := c.CoreV1().Pods("tekton-chains").List(context.Background(), metav1.ListOptions{LabelSelector: "app.kubernetes.io/component=controller"})
if err != nil {
return false, err
}
if len(pods.Items) > 0 {
pod := pods.Items[0]
if pod.UID != podUid {
return pod.Status.Phase == corev1.PodRunning && pod.Status.ContainerStatuses[0].Ready, nil
}
}
return false, nil
})
}

0 comments on commit 70c8c7d

Please sign in to comment.