github.com/verrazzano/verrazzano@v1.7.1/tools/psr/psrctl/cmd/update/update_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 update
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/base64"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	helmcli "github.com/verrazzano/verrazzano/pkg/helm"
    14  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    15  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    16  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/constants"
    17  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/manifest"
    18  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/scenario"
    19  	"github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    20  	"helm.sh/helm/v3/pkg/release"
    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  // TestUpdateCmd tests that the psrctl command works correctly
    31  func TestUpdateCmd(t *testing.T) {
    32  	manifest.Manifests = &manifest.PsrManifests{
    33  		RootTmpDir:        psrRoot,
    34  		WorkerChartAbsDir: psrRoot + "/manifests/charts/worker",
    35  		UseCasesAbsDir:    psrRoot + "/manifests/usecases",
    36  		ScenarioAbsDir:    psrRoot + "/manifests/scenarios",
    37  	}
    38  
    39  	defer manifest.ResetManifests()
    40  
    41  	// create scenario ConfigMap
    42  	cm := &corev1.ConfigMap{
    43  		ObjectMeta: metav1.ObjectMeta{
    44  			Name:      "psr-ops-s1",
    45  			Namespace: "psr",
    46  			Labels: map[string]string{
    47  				"psr.verrazzano.io/scenario":    "true",
    48  				"psr.verrazzano.io/scenario-id": "ops-s1",
    49  			},
    50  		},
    51  		Data: map[string]string{
    52  			"scenario": base64.StdEncoding.EncodeToString([]byte(`Description: "This is a scenario that writes logs to STDOUT and gets logs from OpenSearch
    53    at a moderated rate. \nThe purpose of the scenario is to test a moderate load on
    54    both Fluend and OpenSearch by logging records.\n"
    55  HelmReleases:
    56  - Description: write logs to STDOUT 10 times a second
    57    Name: psr-ops-s1-writelogs-0
    58    Namespace: psr
    59    OverrideFile: writelogs.yaml
    60    UsecasePath: opensearch/writelogs.yaml
    61  ID: ops-s1
    62  Name: opensearch-s1
    63  Namespace: default
    64  ScenarioUsecaseOverridesAbsDir: temp-dir
    65  Usecases:
    66  - Description: write logs to STDOUT 10 times a second
    67    OverrideFile: writelogs.yaml
    68    UsecasePath: opensearch/writelogs.yaml
    69  `)),
    70  		},
    71  	}
    72  
    73  	defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }()
    74  	k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) {
    75  		return k8sfake.NewSimpleClientset(cm).CoreV1(), nil
    76  	}
    77  
    78  	defer func() { scenario.UpdateGetValuesFunc = helmcli.GetValues }()
    79  	scenario.UpdateGetValuesFunc = func(log vzlog.VerrazzanoLogger, releaseName string, namespace string) ([]byte, error) {
    80  		assert.Equal(t, "psr-ops-s1-writelogs-0", releaseName)
    81  		assert.Equal(t, "psr", namespace)
    82  		return []byte("old-values"), nil
    83  	}
    84  
    85  	defer func() { scenario.UpdateUpgradeFunc = helmcli.Upgrade }()
    86  	scenario.UpdateUpgradeFunc = func(log vzlog.VerrazzanoLogger, releaseName string, namespace string, chartDir string, wait bool, dryRun bool, overrides []helmcli.HelmOverrides) (*release.Release, error) {
    87  		assert.Equal(t, 3, len(overrides))
    88  		assert.Equal(t, "psr-ops-s1-writelogs-0", releaseName)
    89  		assert.Equal(t, "psr", namespace)
    90  		assert.Contains(t, chartDir, "manifests/charts/worker")
    91  		return nil, nil
    92  	}
    93  
    94  	// Send the command output to a byte buffer
    95  	buf := new(bytes.Buffer)
    96  	errBuf := new(bytes.Buffer)
    97  	rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
    98  
    99  	cmd := NewCmdUpdate(rc)
   100  	assert.NotNil(t, cmd)
   101  
   102  	cmd.PersistentFlags().Set(constants.FlagScenario, "ops-s1")
   103  	cmd.PersistentFlags().Set(constants.FlagNamespace, "psr")
   104  	cmd.PersistentFlags().Set(constants.ImageNameKey, "worker-image")
   105  
   106  	err := cmd.Execute()
   107  	assert.NoError(t, err)
   108  	assert.Contains(t, buf.String(), "Updating scenario")
   109  	assert.Contains(t, buf.String(), "Updating use case")
   110  	assert.Contains(t, buf.String(), "successfully updated")
   111  }
   112  
   113  func TestUpdateNoConfigmap(t *testing.T) {
   114  	manifest.Manifests = &manifest.PsrManifests{
   115  		RootTmpDir:        psrRoot,
   116  		WorkerChartAbsDir: psrRoot + "/manifests/charts/worker",
   117  		UseCasesAbsDir:    psrRoot + "/manifests/usecases",
   118  		ScenarioAbsDir:    psrRoot + "/manifests/scenarios",
   119  	}
   120  
   121  	defer manifest.ResetManifests()
   122  
   123  	defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }()
   124  	k8sutil.GetCoreV1Func = func(log ...vzlog.VerrazzanoLogger) (corev1cli.CoreV1Interface, error) {
   125  		return k8sfake.NewSimpleClientset().CoreV1(), nil
   126  	}
   127  
   128  	// Send the command output to a byte buffer
   129  	buf := new(bytes.Buffer)
   130  	errBuf := new(bytes.Buffer)
   131  	rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
   132  
   133  	cmd := NewCmdUpdate(rc)
   134  	assert.NotNil(t, cmd)
   135  
   136  	cmd.PersistentFlags().Set(constants.FlagScenario, "ops-s1")
   137  	cmd.PersistentFlags().Set(constants.FlagNamespace, "psr")
   138  	cmd.PersistentFlags().Set(constants.ImageNameKey, "worker-image")
   139  
   140  	err := cmd.Execute()
   141  	assert.Error(t, err)
   142  }