github.com/verrazzano/verrazzano@v1.7.1/tools/psr/psrctl/cmd/list/list_test.go (about) 1 // Copyright (c) 2022, 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 4 package list 5 6 import ( 7 "bytes" 8 "encoding/base64" 9 "os" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/verrazzano/verrazzano/pkg/k8sutil" 14 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 15 "github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/constants" 16 "github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/manifest" 17 "github.com/verrazzano/verrazzano/tools/vz/test/helpers" 18 corev1 "k8s.io/api/core/v1" 19 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 20 "k8s.io/cli-runtime/pkg/genericclioptions" 21 k8sfake "k8s.io/client-go/kubernetes/fake" 22 corev1cli "k8s.io/client-go/kubernetes/typed/core/v1" 23 ) 24 25 var ( 26 expectedID = "ops-s1" 27 expectedDescription = "This is a scenario that writes logs to STDOUT and gets logs from OpenSearch at a moderated rate." 28 expectedRelease = "psr-ops-s1-writelogs-0" 29 ) 30 31 const psrRoot = "../../.." 32 33 // TestList tests the NewCmdList and RunCmdList functions 34 // 35 // WHEN 'psrctl list -n psr' is called 36 // THEN ensure the output correctly shows running scenarios 37 func TestList(t *testing.T) { 38 manifest.Manifests = &manifest.PsrManifests{ 39 RootTmpDir: psrRoot, 40 WorkerChartAbsDir: psrRoot + "/manifests/charts/worker", 41 UseCasesAbsDir: psrRoot + "/manifests/usecases", 42 ScenarioAbsDir: psrRoot + "/manifests/scenarios", 43 } 44 45 defer manifest.ResetManifests() 46 47 // create scenario ConfigMap 48 cm := &corev1.ConfigMap{ 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: "psr-ops-s1", 51 Namespace: "psr", 52 Labels: map[string]string{ 53 "psr.verrazzano.io/scenario": "true", 54 "psr.verrazzano.io/scenario-id": "ops-s1", 55 }, 56 }, 57 Data: map[string]string{ 58 "scenario": base64.StdEncoding.EncodeToString([]byte(`Description: "This is a scenario that writes logs to STDOUT and gets logs from OpenSearch 59 at a moderated rate. \nThe purpose of the scenario is to test a moderate load on 60 both Fluend and OpenSearch by logging records.\n" 61 HelmReleases: 62 - Description: write logs to STDOUT 10 times a second 63 Name: psr-ops-s1-writelogs-0 64 Namespace: psr 65 OverrideFile: writelogs.yaml 66 UsecasePath: opensearch/writelogs.yaml 67 ID: ops-s1 68 Name: opensearch-s1 69 Namespace: default 70 ScenarioUsecaseOverridesAbsDir: temp-dir 71 Usecases: 72 - Description: write logs to STDOUT 10 times a second 73 OverrideFile: writelogs.yaml 74 UsecasePath: opensearch/writelogs.yaml 75 `)), 76 }, 77 } 78 79 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 80 k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) { 81 return k8sfake.NewSimpleClientset(cm).CoreV1(), nil 82 } 83 84 // Send the command output to a byte buffer 85 buf := new(bytes.Buffer) 86 errBuf := new(bytes.Buffer) 87 rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 88 89 cmd := NewCmdList(rc) 90 cmd.PersistentFlags().Set(constants.FlagNamespace, "psr") 91 assert.NotNil(t, cmd) 92 93 err := cmd.Execute() 94 assert.NoError(t, err) 95 result := buf.String() 96 assert.Contains(t, result, "Scenarios running in namespace psr") 97 assert.Contains(t, result, expectedID) 98 assert.Contains(t, result, expectedDescription) 99 assert.Contains(t, result, expectedRelease) 100 } 101 102 // TestEmptyListDefault tests the NewCmdList and RunCmdList functions 103 // 104 // WHEN 'psrctl list' is called 105 // THEN ensure the output correctly shows no running scenarios 106 func TestEmptyListDefault(t *testing.T) { 107 manifest.Manifests = &manifest.PsrManifests{ 108 RootTmpDir: psrRoot, 109 WorkerChartAbsDir: psrRoot + "/manifests/charts/worker", 110 UseCasesAbsDir: psrRoot + "/manifests/usecases", 111 ScenarioAbsDir: psrRoot + "/manifests/scenarios", 112 } 113 114 defer manifest.ResetManifests() 115 116 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 117 k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) { 118 return k8sfake.NewSimpleClientset().CoreV1(), nil 119 } 120 121 // Send the command output to a byte buffer 122 buf := new(bytes.Buffer) 123 errBuf := new(bytes.Buffer) 124 rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 125 126 cmd := NewCmdList(rc) 127 assert.NotNil(t, cmd) 128 129 // Run list command, check for no scenarios to be running 130 err := cmd.Execute() 131 assert.NoError(t, err) 132 result := buf.String() 133 assert.Contains(t, result, "There are no scenarios running in namespace default") 134 } 135 136 // TestEmptyListCluster tests the NewCmdList and RunCmdList functions 137 // 138 // WHEN 'psrctl list -A' is called 139 // THEN ensure the output correctly shows no running scenarios 140 func TestEmptyListCluster(t *testing.T) { 141 manifest.Manifests = &manifest.PsrManifests{ 142 RootTmpDir: psrRoot, 143 WorkerChartAbsDir: psrRoot + "/manifests/charts/worker", 144 UseCasesAbsDir: psrRoot + "/manifests/usecases", 145 ScenarioAbsDir: psrRoot + "/manifests/scenarios", 146 } 147 148 defer manifest.ResetManifests() 149 150 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 151 k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) { 152 return k8sfake.NewSimpleClientset().CoreV1(), nil 153 } 154 155 // Send the command output to a byte buffer 156 buf := new(bytes.Buffer) 157 errBuf := new(bytes.Buffer) 158 rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 159 160 cmd := NewCmdList(rc) 161 cmd.PersistentFlags().Set(constants.FlagAll, "true") 162 assert.NotNil(t, cmd) 163 164 // Run list command, check for no scenarios to be running 165 err := cmd.Execute() 166 assert.NoError(t, err) 167 result := buf.String() 168 assert.Contains(t, result, "There are no scenarios running in the cluster") 169 }