github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/cluster/pods_test.go (about)

     1  // Copyright (c) 2021, 2024, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package cluster
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/log"
    10  	"github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/report"
    11  	corev1 "k8s.io/api/core/v1"
    12  )
    13  
    14  // TODO: Add more tests
    15  
    16  func TestPodConditionMessage(t *testing.T) {
    17  	ns := "test"
    18  	var tests = []struct {
    19  		name      string
    20  		condition corev1.PodCondition
    21  		message   string
    22  	}{
    23  		{
    24  			"pod-no-message-nor-reason",
    25  			corev1.PodCondition{
    26  				Type:   corev1.PodInitialized,
    27  				Status: corev1.ConditionFalse,
    28  			},
    29  			"Namespace test, Pod pod-no-message-nor-reason, ConditionType Initialized, Status False",
    30  		},
    31  		{
    32  			"pod-with-message-and-reason",
    33  			corev1.PodCondition{
    34  				Type:    corev1.ContainersReady,
    35  				Status:  corev1.ConditionTrue,
    36  				Message: "foo",
    37  				Reason:  "bar",
    38  			},
    39  			"Namespace test, Pod pod-with-message-and-reason, ConditionType ContainersReady, Status True, Reason bar, Message foo",
    40  		},
    41  	}
    42  
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			msg, err := podConditionMessage(tt.name, ns, tt.condition)
    46  			assert.NoError(t, err)
    47  			assert.Equal(t, tt.message, msg)
    48  		})
    49  	}
    50  }
    51  
    52  // Analyze Pod Issues with variety of cluster roots
    53  // Expect No Error for each analysis
    54  func TestAnalyzePodIssues(t *testing.T) {
    55  	logger := log.GetDebugEnabledLogger()
    56  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/problem-pods/cluster-snapshot"))
    57  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/pending-pods/cluster-snapshot"))
    58  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/problem-pods-install/cluster-snapshot"))
    59  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/insufficient-mem/cluster-snapshot"))
    60  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/pod-waiting-for-readiness-gates/cluster-snapshot"))
    61  }
    62  
    63  // TestPodReadinessGateIssue tests whether the relevant issue is reported when a pod does not have its readiness gates ready
    64  // GIVEN a call to analyze pod related issues in a cluster-snapshot
    65  // WHEN a valid input is provided that contains a pod whose readiness gates are not ready
    66  // THEN the function does not generate an error and adds the correct issue
    67  func TestPodReadinessGatesIssue(t *testing.T) {
    68  	report.ClearReports()
    69  	logger := log.GetDebugEnabledLogger()
    70  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/pod-waiting-for-readiness-gates/cluster-snapshot"))
    71  	reportedIssues := report.GetAllSourcesFilteredIssues(logger, true, 0, 0)
    72  	assert.True(t, len(reportedIssues) == 1)
    73  	assert.True(t, reportedIssues[0].Type == report.PodWaitingOnReadinessGates)
    74  	report.ClearReports()
    75  }
    76  
    77  // TestPodHangingOnDeletionIssue tests whether the relevant issue is reported when a pod has been in a state of deletion for an extended period of time
    78  // GIVEN a call to analyze pod related issues in a cluster-snapshot
    79  // WHEN a valid input is provided that has a pod has been terminating for a long time
    80  // THEN the function does not generate an error and adds the correct issue
    81  func TestPodHangingOnDeletionIssue(t *testing.T) {
    82  	report.ClearReports()
    83  	logger := log.GetDebugEnabledLogger()
    84  	assert.NoError(t, AnalyzePodIssues(logger, "../../test/cluster/pod-hanging-on-deletion/cluster-snapshot"))
    85  	reportedIssues := report.GetAllSourcesFilteredIssues(logger, true, 0, 0)
    86  	assert.True(t, len(reportedIssues) == 1)
    87  	assert.True(t, reportedIssues[0].Type == report.PodHangingOnDeletion)
    88  	report.ClearReports()
    89  }