Skip to content

Commit

Permalink
test(mc): Address PR review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SRodi committed Jan 31, 2025
1 parent bc04232 commit f035b52
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion test/multicloud/test/example_aks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestAKSExample(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
TerraformDir: "../examples/aks",
TerraformDir: examplesPath + "aks",

Vars: map[string]interface{}{
"prefix": "test-mc",
Expand Down
2 changes: 1 addition & 1 deletion test/multicloud/test/example_gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestGKEExample(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
TerraformDir: "../examples/gke",
TerraformDir: examplesPath + "gke",

Vars: map[string]interface{}{
"prefix": "test",
Expand Down
2 changes: 1 addition & 1 deletion test/multicloud/test/example_kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestKindExample(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
TerraformDir: "../examples/kind",
TerraformDir: examplesPath + "kind",

Vars: map[string]interface{}{
"prefix": "test",
Expand Down
11 changes: 6 additions & 5 deletions test/multicloud/test/integration_prometheus_kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/terraform"
)
Expand All @@ -10,7 +11,7 @@ func TestPrometheusKindIntegration(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
TerraformDir: "../examples/integration/prometheus-kind",
TerraformDir: examplesPath + "integration/prometheus-kind",

Vars: map[string]interface{}{
"prefix": "test-integration",
Expand Down Expand Up @@ -39,21 +40,21 @@ func TestPrometheusKindIntegration(t *testing.T) {
// test the cluster is accessible
testClusterAccess(t, clientSet)

promPodSpec := PodSpec{
Name: "prometheus",
podSelector := PodSelector{
Namespace: "default",
LabelSelector: "app.kubernetes.io/instance=prometheus-kube-prometheus-prometheus",
ContainerName: "prometheus",
}

timeOut := time.Duration(60) * time.Second
// check the prometheus pods are running
result, err := arePodsRunning(clientSet, promPodSpec, 60)
result, err := arePodsRunning(clientSet, podSelector, timeOut)
if !result {
t.Fatalf("Prometheus pods did not start in time: %v\n", err)
}

// check the retina pods logs for errors
checkPodLogs(t, clientSet, promPodSpec)
checkPodLogs(t, clientSet, podSelector)

// TODO: add more tests here
}
11 changes: 6 additions & 5 deletions test/multicloud/test/integration_retina_kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/terraform"
)
Expand All @@ -10,7 +11,7 @@ func TestRetinaKindIntegration(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
TerraformDir: "../examples/integration/retina-kind",
TerraformDir: examplesPath + "integration/retina-kind",

Vars: map[string]interface{}{
"prefix": "test-integration",
Expand Down Expand Up @@ -40,21 +41,21 @@ func TestRetinaKindIntegration(t *testing.T) {
// test the cluster is accessible
testClusterAccess(t, clientSet)

retinaPodSpec := PodSpec{
Name: "retina",
retinaPodSelector := PodSelector{
Namespace: "kube-system",
LabelSelector: "k8s-app=retina",
ContainerName: "retina",
}

timeOut := time.Duration(90) * time.Second
// check the retina pods are running
result, err := arePodsRunning(clientSet, retinaPodSpec, 90)
result, err := arePodsRunning(clientSet, retinaPodSelector, timeOut)
if !result {
t.Fatalf("Retina pods did not start in time: %v\n", err)
}

// check the retina pods logs for errors
checkPodLogs(t, clientSet, retinaPodSpec)
checkPodLogs(t, clientSet, retinaPodSelector)

// TODO: add more tests here
}
12 changes: 0 additions & 12 deletions test/multicloud/test/testTypes.go

This file was deleted.

9 changes: 9 additions & 0 deletions test/multicloud/test/test_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test

const examplesPath = "../examples/"

type PodSelector struct {
Namespace string
LabelSelector string
ContainerName string
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ func checkLogsForErrors(logs io.ReadCloser) error {
return nil
}

func checkPodLogs(t *testing.T, clientset kubernetes.Interface, podSpec PodSpec) {
func checkPodLogs(t *testing.T, clientset kubernetes.Interface, podSelector PodSelector) {
// Get the logs for the retina pods
pods, err := clientset.CoreV1().Pods(podSpec.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: podSpec.LabelSelector,
pods, err := clientset.CoreV1().Pods(podSelector.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: podSelector.LabelSelector,
})
if err != nil {
t.Fatalf("Failed to list pods: %v", err)
}
// Stream the logs for each pod
for _, pod := range pods.Items {
// Get the logs for the pod
req := clientset.CoreV1().Pods(podSpec.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{
Container: podSpec.ContainerName,
req := clientset.CoreV1().Pods(podSelector.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{
Container: podSelector.ContainerName,
})
// Stream the logs
logs, err := req.Stream(context.Background())
Expand Down Expand Up @@ -134,15 +134,15 @@ func fetchSensitiveOutput(t *testing.T, options *terraform.Options, name string)
return terraform.Output(t, options, name)
}

func arePodsRunning(clientset kubernetes.Interface, podSpec PodSpec, timeoutSecounds int) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSecounds)*time.Minute)
func arePodsRunning(clientset kubernetes.Interface, podSelector PodSelector, timeout time.Duration) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

// Poll until the pods are running
err := wait.PollUntilContextTimeout(ctx, 3*time.Second, time.Duration(timeoutSecounds)*time.Second, true, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextTimeout(ctx, 3*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
// List the pods with the label selector
pods, err := clientset.CoreV1().Pods(podSpec.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: podSpec.LabelSelector,
pods, err := clientset.CoreV1().Pods(podSelector.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: podSelector.LabelSelector,
})
if err != nil {
return false, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/terraform"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -53,6 +54,15 @@ func TestCreateRESTConfigWithClientCert(t *testing.T) {
func TestTestClusterAccess(t *testing.T) {
clientset := fake.NewSimpleClientset()
testClusterAccess(t, clientset)

// Simulate a failure scenario
clientset = nil
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expected panic due to nil clientset, but code did not panic")
}
}()
testClusterAccess(t, clientset)
}

func TestCheckLogsForErrors(t *testing.T) {
Expand Down Expand Up @@ -109,12 +119,12 @@ func TestCheckPodLogs(t *testing.T) {
},
},
})
podSpec := PodSpec{
podSelector := PodSelector{
Namespace: "default",
LabelSelector: "app=test",
ContainerName: "test-container",
}
checkPodLogs(t, clientset, podSpec)
checkPodLogs(t, clientset, podSelector)
}

func TestDecodeBase64(t *testing.T) {
Expand Down Expand Up @@ -255,11 +265,11 @@ func TestCheckPodsRunning(t *testing.T) {
objects = append(objects, pod.DeepCopyObject())
}
clientset := fake.NewSimpleClientset(objects...)
podSpec := PodSpec{
podSelector := PodSelector{
Namespace: "default",
LabelSelector: "app=test",
}
result, err := arePodsRunning(clientset, podSpec, 1)
result, err := arePodsRunning(clientset, podSelector, time.Duration(1)*time.Second)
if result != tc.expected {
t.Fatalf("Expected %v, got %v with error: %v", tc.expected, result, err)
}
Expand Down

0 comments on commit f035b52

Please sign in to comment.