github.com/verrazzano/verrazzano@v1.7.0/tools/psr/psrctl/cmd/stop/stop_test.go (about)

     1  // Copyright (c) 2022, 2023, 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 stop
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/base64"
     9  	"fmt"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	helmcli "github.com/verrazzano/verrazzano/pkg/helm"
    15  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    16  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    17  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/constants"
    18  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/manifest"
    19  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/scenario"
    20  	"github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    21  	corev1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/cli-runtime/pkg/genericclioptions"
    24  	k8sfake "k8s.io/client-go/kubernetes/fake"
    25  	corev1cli "k8s.io/client-go/kubernetes/typed/core/v1"
    26  )
    27  
    28  const psrRoot = "../../.."
    29  
    30  var ID = "ops-s1"
    31  
    32  // TestStopCmd tests the NewCmdStop and RunCmdStop functions
    33  //
    34  //	WHEN 'psrctl stop -s ops-s1 -n psr' is called
    35  //	THEN ensure the scenario is uninstalled
    36  func TestStopCmd(t *testing.T) {
    37  	manifest.Manifests = &manifest.PsrManifests{
    38  		RootTmpDir:        psrRoot,
    39  		WorkerChartAbsDir: psrRoot + "/manifests/charts/worker",
    40  		UseCasesAbsDir:    psrRoot + "/manifests/usecases",
    41  		ScenarioAbsDir:    psrRoot + "/manifests/scenarios",
    42  	}
    43  
    44  	defer manifest.ResetManifests()
    45  
    46  	// create scenario ConfigMap
    47  	cm := &corev1.ConfigMap{
    48  		ObjectMeta: metav1.ObjectMeta{
    49  			Name:      "psr-ops-s1",
    50  			Namespace: "psr",
    51  			Labels: map[string]string{
    52  				"psr.verrazzano.io/scenario":    "true",
    53  				"psr.verrazzano.io/scenario-id": "ops-s1",
    54  			},
    55  		},
    56  		Data: map[string]string{
    57  			"scenario": base64.StdEncoding.EncodeToString([]byte(`Description: "This is a scenario that writes logs to STDOUT and gets logs from OpenSearch
    58    at a moderated rate. \nThe purpose of the scenario is to test a moderate load on
    59    both Fluend and OpenSearch by logging records.\n"
    60  HelmReleases:
    61  - Description: write logs to STDOUT 10 times a second
    62    Name: psr-ops-s1-writelogs-0
    63    Namespace: psr
    64    OverrideFile: writelogs.yaml
    65    UsecasePath: opensearch/writelogs.yaml
    66  ID: ops-s1
    67  Name: opensearch-s1
    68  Namespace: default
    69  ScenarioUsecaseOverridesAbsDir: temp-dir
    70  Usecases:
    71  - Description: write logs to STDOUT 10 times a second
    72    OverrideFile: writelogs.yaml
    73    UsecasePath: opensearch/writelogs.yaml
    74  `)),
    75  		},
    76  	}
    77  
    78  	defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }()
    79  	k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) {
    80  		return k8sfake.NewSimpleClientset(cm).CoreV1(), nil
    81  	}
    82  
    83  	defer func() { scenario.UninstallFunc = helmcli.Uninstall }()
    84  	scenario.UninstallFunc = func(log vzlog.VerrazzanoLogger, releaseName string, namespace string, dryRun bool) (err error) {
    85  		assert.Equal(t, "psr-ops-s1-writelogs-0", releaseName)
    86  		assert.Equal(t, "psr", namespace)
    87  
    88  		return nil
    89  	}
    90  
    91  	// Send the command output to a byte buffer
    92  	buf := new(bytes.Buffer)
    93  	errBuf := new(bytes.Buffer)
    94  	rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
    95  
    96  	cmd := NewCmdStop(rc)
    97  	cmd.PersistentFlags().Set(constants.FlagScenario, "ops-s1")
    98  	cmd.PersistentFlags().Set(constants.FlagNamespace, "psr")
    99  	assert.NotNil(t, cmd)
   100  
   101  	// Run stop command, check for the expected status results to be displayed
   102  	err := cmd.Execute()
   103  	assert.NoError(t, err)
   104  	result := buf.String()
   105  
   106  	assert.Contains(t, result, fmt.Sprintf("Stopping scenario %s", ID))
   107  	assert.Contains(t, result, fmt.Sprintf("Scenario %s successfully stopped", ID))
   108  }
   109  
   110  // TestStopEmpty tests the NewCmdStop and RunCmdStop functions
   111  //
   112  //	WHEN 'psrctl stop -s ops-s1 -n psr' is called when the scenario is not running
   113  //	THEN ensure the output correctly tells the user their operation is invalid
   114  func TestStopEmpty(t *testing.T) {
   115  	manifest.Manifests = &manifest.PsrManifests{
   116  		RootTmpDir:        psrRoot,
   117  		WorkerChartAbsDir: psrRoot + "/manifests/charts/worker",
   118  		UseCasesAbsDir:    psrRoot + "/manifests/usecases",
   119  		ScenarioAbsDir:    psrRoot + "/manifests/scenarios",
   120  	}
   121  
   122  	defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }()
   123  	k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) {
   124  		return k8sfake.NewSimpleClientset().CoreV1(), nil
   125  	}
   126  
   127  	// Send the command output to a byte buffer
   128  	buf := new(bytes.Buffer)
   129  	errBuf := new(bytes.Buffer)
   130  	rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   131  
   132  	cmd := NewCmdStop(rc)
   133  	cmd.PersistentFlags().Set(constants.FlagScenario, "ops-s1")
   134  	cmd.PersistentFlags().Set(constants.FlagNamespace, "psr")
   135  	assert.NotNil(t, cmd)
   136  
   137  	// Run stop command, check for the expected status results to be displayed
   138  	err := cmd.Execute()
   139  	assert.Error(t, err)
   140  	result := err.Error()
   141  
   142  	assert.Contains(t, result, fmt.Sprintf("Failed to stop scenario psr/%s", ID))
   143  }