github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/printer/describe_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package printer
    21  
    22  import (
    23  	"bytes"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  
    30  	clitesting "github.com/1aal/kubeblocks/pkg/cli/testing"
    31  )
    32  
    33  func TestPrintAllWarningEvents(t *testing.T) {
    34  	eventList := &corev1.EventList{}
    35  	eventList.Items = []corev1.Event{{
    36  		Type:    corev1.EventTypeNormal,
    37  		Reason:  "EventSucceed",
    38  		Message: "event succeed",
    39  		TypeMeta: metav1.TypeMeta{
    40  			Kind: "Event",
    41  		}}}
    42  	out := &bytes.Buffer{}
    43  	PrintAllWarningEvents(eventList, out)
    44  	assert.Equal(t, "\nWarning Events: "+NoneString+"\n", out.String())
    45  
    46  	reason, message := "EventFailed", "event failed"
    47  	name := "pod-test-xkdsl1"
    48  	eventList.Items = append(eventList.Items, corev1.Event{
    49  		Type:    corev1.EventTypeWarning,
    50  		Reason:  reason,
    51  		Message: message,
    52  		InvolvedObject: corev1.ObjectReference{
    53  			Kind: "Pod",
    54  			Name: name,
    55  		},
    56  	})
    57  	PrintAllWarningEvents(eventList, out)
    58  	if !clitesting.ContainExpectStrings(out.String(), "TIME", "TYPE", "REASON", "OBJECT", "MESSAGE") {
    59  		t.Fatal(`Expect warning events output: "TIME	TYPE	REASON	OBJECT	MESSAGE"`)
    60  	}
    61  	object := "Instance/" + name
    62  	if !clitesting.ContainExpectStrings(out.String(), corev1.EventTypeWarning, reason, message, object) {
    63  		t.Fatalf(`Expect warning events output: "%s	%s	%s	%s"`,
    64  			corev1.EventTypeWarning, reason, message, object)
    65  	}
    66  }
    67  
    68  func TestPrintConditions(t *testing.T) {
    69  	conditionType, reason := "Created", "CreateResources"
    70  	message := "Failed to create resources"
    71  	conditions := []metav1.Condition{
    72  		{
    73  			Type:    "Initialize",
    74  			Reason:  "InitResources",
    75  			Status:  "True",
    76  			Message: "Start to init resources",
    77  		},
    78  		{
    79  			Type:    conditionType,
    80  			Reason:  reason,
    81  			Status:  metav1.ConditionFalse,
    82  			Message: message,
    83  		},
    84  	}
    85  	out := &bytes.Buffer{}
    86  	PrintConditions(conditions, out)
    87  	if !clitesting.ContainExpectStrings(out.String(), "LAST-TRANSITION-TIME", "TYPE", "REASON", "STATUS", "MESSAGE") {
    88  		t.Fatal(`Expect conditions output: "LAST-TRANSITION-TIME	TYPE	REASON	STATUS	MESSAGE"`)
    89  	}
    90  	if !clitesting.ContainExpectStrings(out.String(), conditionType, reason, string(metav1.ConditionFalse), message) {
    91  		t.Fatalf(`Expect conditions output: "%s	%s	%s	%s"`, conditionType, reason, metav1.ConditionFalse, message)
    92  	}
    93  }
    94  
    95  func TestPrintHelmValues(t *testing.T) {
    96  	mockHelmConfig := map[string]interface{}{
    97  		"updateStrategy": map[string]interface{}{
    98  			"rollingUpdate": map[string]interface{}{
    99  				"maxSurge":       1,
   100  				"maxUnavailable": "40%",
   101  			},
   102  			"type": "RollingUpdate",
   103  		},
   104  		"podDisruptionBudget": map[string]interface{}{
   105  			"minAvailable": 1,
   106  		},
   107  		"loggerSettings": map[string]interface{}{
   108  			"developmentMode": false,
   109  			"encoder":         "console",
   110  			"timeEncoding":    "iso8601",
   111  		},
   112  		"priorityClassName": nil,
   113  		"nameOverride":      "",
   114  		"fullnameOverride±": "",
   115  		"dnsPolicy":         "ClusterFirst",
   116  		"replicaCount":      1,
   117  		"hostNetwork":       false,
   118  		"keepAddons":        false,
   119  	}
   120  	out := &bytes.Buffer{}
   121  
   122  	PrintHelmValues(mockHelmConfig, JSON, out)
   123  }