Skip to content

Commit

Permalink
fix: failure tests fail reliably now
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Bressi <[email protected]>
  • Loading branch information
puffitos committed Feb 3, 2024
1 parent 271ac12 commit c3407ac
Showing 1 changed file with 59 additions and 52 deletions.
111 changes: 59 additions & 52 deletions test/framework/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ func (f *Framework) cleanupSecrets(t testing.TB) {
}
}

// GetPods returns the pod(s) of the deployment. The fetch is done by label selector (app=<deployment name>)
// If the get request fails, the test will fail and the framework will be cleaned up
func (f *Framework) GetPods(t *testing.T, d appsv1.Deployment) *corev1.PodList {
pods, err := f.k8s.CoreV1().Pods("test-cases").List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", d.Name),
})
if err != nil {
f.Cleanup(t)
t.Fatal(err)
}
return pods
}

// CreateDeployment creates a deployment in the testing namespace
func (f *Framework) CreateDeployment(t testing.TB, d appsv1.Deployment) {
_, err := f.k8s.AppsV1().Deployments("test-cases").Create(context.Background(), &d, metav1.CreateOptions{})
Expand All @@ -126,6 +139,17 @@ func (f *Framework) CreateDeployment(t testing.TB, d appsv1.Deployment) {
}
}

// CreateSecret creates a secret in the testing namespace
func (f *Framework) CreateSecret(t *testing.T, secret corev1.Secret) {
t.Logf("creating secret %s", secret.Name)
s, err := f.k8s.CoreV1().Secrets("test-cases").Create(context.Background(), &secret, metav1.CreateOptions{})
if err != nil {
f.Cleanup(t)
t.Fatal(err)
}
t.Logf("created secret %s", s.Name)
}

// WaitForDeployment waits until the deployment is ready
func (f *Framework) WaitForDeployment(t *testing.T, d appsv1.Deployment) {
t.Logf("waiting for deployment %s to be ready", d.Name)
Expand Down Expand Up @@ -161,15 +185,33 @@ func (f *Framework) WaitForDeployment(t *testing.T, d appsv1.Deployment) {
}
}

// CreateSecret creates a secret in the testing namespace
func (f *Framework) CreateSecret(t *testing.T, secret corev1.Secret) {
t.Logf("creating secret %s", secret.Name)
s, err := f.k8s.CoreV1().Secrets("test-cases").Create(context.Background(), &secret, metav1.CreateOptions{})
// waitForReplicaSetCreation waits for the replicaset of the given deployment to be created
func (f *Framework) waitForReplicaSetCreation(t *testing.T, d appsv1.Deployment) (string, error) {
rs, err := f.k8s.AppsV1().ReplicaSets("test-cases").Watch(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", d.Name),
})
if err != nil {
f.Cleanup(t)
t.Fatal(err)
}
t.Logf("created secret %s", s.Name)

ctx, done := context.WithTimeout(context.Background(), 30*time.Second)
defer done()

for {
select {
case <-ctx.Done():
f.Cleanup(t)
t.Fatal("timeout reached while waiting for replicaset to be created")
case event := <-rs.ResultChan():
rs, ok := event.Object.(*appsv1.ReplicaSet)
if ok {
t.Logf("replicaset %s created", rs.Name)
return rs.Name, nil
}
time.Sleep(5 * time.Second)
}
}
}

// AssertDeploymentFailed asserts that the deployment cannot start
Expand All @@ -192,13 +234,15 @@ func (f *Framework) AssertDeploymentFailed(t *testing.T, d appsv1.Deployment) {
t.Fatal(err)
}

timeout := time.After(30 * time.Second)
for event := range w.ResultChan() {
ctx, done := context.WithTimeout(context.Background(), 30*time.Second)
defer done()

for {
select {
case <-timeout:
case <-ctx.Done():
f.Cleanup(t)
t.Fatal("timeout reached while waiting for deployment to fail")
default:
case event := <-w.ResultChan():
e, ok := event.Object.(*corev1.Event)
if !ok {
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -226,13 +270,15 @@ func (f *Framework) AssertEventForPod(t *testing.T, reason string, p corev1.Pod)
t.Fatal(err)
}

timeout := time.After(30 * time.Second)
for event := range w.ResultChan() {
ctx, done := context.WithTimeout(context.Background(), 30*time.Second)
defer done()

for {
select {
case <-timeout:
case <-ctx.Done():
f.Cleanup(t)
t.Fatal("timeout reached while waiting for podverified event")
default:
case event := <-w.ResultChan():
e, ok := event.Object.(*corev1.Event)
if !ok {
time.Sleep(5 * time.Second)
Expand All @@ -246,42 +292,3 @@ func (f *Framework) AssertEventForPod(t *testing.T, reason string, p corev1.Pod)
}
}
}

func (f *Framework) waitForReplicaSetCreation(t *testing.T, d appsv1.Deployment) (string, error) {
rs, err := f.k8s.AppsV1().ReplicaSets("test-cases").Watch(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", d.Name),
})
if err != nil {
f.Cleanup(t)
t.Fatal(err)
}

timeout := time.After(30 * time.Second)
for event := range rs.ResultChan() {
select {
case <-timeout:
return "", fmt.Errorf("timeout reached while waiting for replicaset to be created")
default:
rs, ok := event.Object.(*appsv1.ReplicaSet)
if ok {
t.Logf("replicaset %s created", rs.Name)
return rs.Name, nil
}
time.Sleep(5 * time.Second)
}
}
return "", fmt.Errorf("failed to wait for replicaset creation")
}

// GetPods returns the pod(s) of the deployment. The fetch is done by label selector (app=<deployment name>)
// If the get request fails, the test will fail and the framework will be cleaned up
func (f *Framework) GetPods(t *testing.T, d appsv1.Deployment) *corev1.PodList {
pods, err := f.k8s.CoreV1().Pods("test-cases").List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", d.Name),
})
if err != nil {
f.Cleanup(t)
t.Fatal(err)
}
return pods
}

0 comments on commit c3407ac

Please sign in to comment.