2021-01-14 15:17:46 +00:00
|
|
|
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package library
|
|
|
|
|
|
|
|
import (
|
2021-01-12 20:55:31 +00:00
|
|
|
"context"
|
2021-02-25 22:40:02 +00:00
|
|
|
"errors"
|
2021-01-12 20:55:31 +00:00
|
|
|
"fmt"
|
2021-01-14 15:17:46 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-12 20:55:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-01-14 15:17:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-12 20:55:31 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-01-14 15:17:46 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2021-03-17 16:46:55 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2021-01-14 15:17:46 +00:00
|
|
|
)
|
|
|
|
|
2021-02-25 22:40:02 +00:00
|
|
|
// RequireEventuallyWithoutError is similar to require.Eventually() except that it also allows the caller to
|
2021-01-14 15:17:46 +00:00
|
|
|
// return an error from the condition function. If the condition function returns an error at any
|
|
|
|
// point, the assertion will immediately fail.
|
|
|
|
func RequireEventuallyWithoutError(
|
|
|
|
t *testing.T,
|
|
|
|
f func() (bool, error),
|
|
|
|
waitFor time.Duration,
|
|
|
|
tick time.Duration,
|
|
|
|
msgAndArgs ...interface{},
|
|
|
|
) {
|
|
|
|
t.Helper()
|
|
|
|
require.NoError(t, wait.PollImmediate(tick, waitFor, f), msgAndArgs...)
|
|
|
|
}
|
2021-01-12 20:55:31 +00:00
|
|
|
|
2021-02-25 22:40:02 +00:00
|
|
|
// RequireNeverWithoutError is similar to require.Never() except that it also allows the caller to
|
|
|
|
// return an error from the condition function. If the condition function returns an error at any
|
|
|
|
// point, the assertion will immediately fail.
|
|
|
|
func RequireNeverWithoutError(
|
|
|
|
t *testing.T,
|
|
|
|
f func() (bool, error),
|
|
|
|
waitFor time.Duration,
|
|
|
|
tick time.Duration,
|
|
|
|
msgAndArgs ...interface{},
|
|
|
|
) {
|
|
|
|
t.Helper()
|
|
|
|
err := wait.PollImmediate(tick, waitFor, f)
|
|
|
|
isWaitTimeout := errors.Is(err, wait.ErrWaitTimeout)
|
|
|
|
if err != nil && !isWaitTimeout {
|
|
|
|
require.NoError(t, err, msgAndArgs...) // this will fail and throw the right error message
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
// This prints the same error message that require.Never would print in this case.
|
|
|
|
require.Fail(t, "Condition satisfied", msgAndArgs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 16:08:01 +00:00
|
|
|
// assertNoRestartsDuringTest allows a caller to assert that there were no restarts for a Pod in the
|
2021-01-12 20:55:31 +00:00
|
|
|
// provided namespace with the provided labelSelector during the lifetime of a test.
|
2021-03-17 16:08:01 +00:00
|
|
|
func assertNoRestartsDuringTest(t *testing.T, namespace, labelSelector string) {
|
2021-01-12 20:55:31 +00:00
|
|
|
t.Helper()
|
2021-03-17 16:46:55 +00:00
|
|
|
kubeClient := NewKubernetesClientset(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer cancel()
|
2021-01-12 20:55:31 +00:00
|
|
|
|
2021-03-17 16:46:55 +00:00
|
|
|
previousRestartCounts := getRestartCounts(ctx, t, kubeClient, namespace, labelSelector)
|
2021-01-12 20:55:31 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2021-03-17 16:46:55 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
currentRestartCounts := getRestartCounts(ctx, t, kubeClient, namespace, labelSelector)
|
2021-01-12 20:55:31 +00:00
|
|
|
|
|
|
|
for key, previousRestartCount := range previousRestartCounts {
|
|
|
|
currentRestartCount, ok := currentRestartCounts[key]
|
2021-03-17 16:46:55 +00:00
|
|
|
|
|
|
|
// If the container no longer exists, that's a test failure.
|
|
|
|
if !assert.Truef(
|
2021-01-12 20:55:31 +00:00
|
|
|
t,
|
|
|
|
ok,
|
2021-03-17 16:46:55 +00:00
|
|
|
"container %s existed at beginning of the test, but not the end",
|
|
|
|
key.String(),
|
2021-01-12 20:55:31 +00:00
|
|
|
) {
|
2021-03-17 16:46:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect the restart count to be the same as it was before the test.
|
|
|
|
if !assert.Equal(
|
|
|
|
t,
|
|
|
|
previousRestartCount,
|
|
|
|
currentRestartCount,
|
|
|
|
"container %s has restarted %d times (original count was %d)",
|
|
|
|
key.String(),
|
|
|
|
currentRestartCount,
|
|
|
|
previousRestartCount,
|
2021-01-12 20:55:31 +00:00
|
|
|
) {
|
2021-03-17 16:46:55 +00:00
|
|
|
// Attempt to dump the logs from the previous container that crashed.
|
|
|
|
dumpContainerLogs(ctx, t, kubeClient, key.namespace, key.pod, key.container, true)
|
2021-01-12 20:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-17 16:46:55 +00:00
|
|
|
type containerRestartKey struct {
|
|
|
|
namespace string
|
|
|
|
pod string
|
|
|
|
container string
|
|
|
|
}
|
2021-01-12 20:55:31 +00:00
|
|
|
|
2021-03-17 16:46:55 +00:00
|
|
|
func (k containerRestartKey) String() string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s", k.namespace, k.pod, k.container)
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerRestartMap map[containerRestartKey]int32
|
|
|
|
|
|
|
|
func getRestartCounts(ctx context.Context, t *testing.T, kubeClient kubernetes.Interface, namespace, labelSelector string) containerRestartMap {
|
|
|
|
t.Helper()
|
2021-01-12 20:55:31 +00:00
|
|
|
|
|
|
|
pods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-17 16:46:55 +00:00
|
|
|
restartCounts := make(containerRestartMap)
|
2021-01-12 20:55:31 +00:00
|
|
|
for _, pod := range pods.Items {
|
|
|
|
for _, container := range pod.Status.ContainerStatuses {
|
2021-03-17 16:46:55 +00:00
|
|
|
key := containerRestartKey{
|
|
|
|
namespace: pod.Namespace,
|
|
|
|
pod: pod.Name,
|
|
|
|
container: container.Name,
|
|
|
|
}
|
2021-01-12 20:55:31 +00:00
|
|
|
restartCounts[key] = container.RestartCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return restartCounts
|
|
|
|
}
|