github.com/verrazzano/verrazzano@v1.7.0/tools/psr/backend/workers/weblogic/scale/scale_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 scale
     5  
     6  import (
     7  	"github.com/verrazzano/verrazzano/tools/psr/backend/pkg/k8sclient"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/verrazzano/verrazzano/tools/psr/backend/config"
    13  	"github.com/verrazzano/verrazzano/tools/psr/backend/osenv"
    14  )
    15  
    16  type fakeEnv struct {
    17  	data map[string]string
    18  }
    19  
    20  type fakePsrClient struct {
    21  	psrClient *k8sclient.PsrClient
    22  }
    23  
    24  // TestGetters tests the worker getters
    25  // GIVEN a worker
    26  //
    27  //	WHEN the getter methods are calls
    28  //	THEN ensure that the correct results are returned
    29  func TestGetters(t *testing.T) {
    30  	envMap := map[string]string{
    31  		DomainUID:       "test-domain",
    32  		DomainNamespace: "test-namespace",
    33  		MinReplicaCount: "1",
    34  		MaxReplicaCount: "2",
    35  	}
    36  	f := fakeEnv{data: envMap}
    37  	saveEnv := osenv.GetEnvFunc
    38  	osenv.GetEnvFunc = f.GetEnv
    39  	defer func() {
    40  		osenv.GetEnvFunc = saveEnv
    41  	}()
    42  
    43  	w, err := NewScaleWorker()
    44  	assert.NoError(t, err)
    45  
    46  	wd := w.GetWorkerDesc()
    47  	assert.Equal(t, config.WorkerTypeWlsScale, wd.WorkerType)
    48  	assert.Equal(t, "The scale domain worker scales up and scales down the domain", wd.Description)
    49  	assert.Equal(t, metricsPrefix, wd.MetricsPrefix)
    50  
    51  	logged := w.WantLoopInfoLogged()
    52  	assert.False(t, logged)
    53  }
    54  
    55  // TestGetEnvDescList tests the GetEnvDescList method
    56  // GIVEN a worker
    57  //
    58  //	WHEN the GetEnvDescList methods is called
    59  //	THEN ensure that the correct results are returned
    60  func TestGetEnvDescList(t *testing.T) {
    61  	envMap := map[string]string{
    62  		DomainUID:       "test-domain",
    63  		DomainNamespace: "test-namespace",
    64  		MinReplicaCount: "1",
    65  		MaxReplicaCount: "2",
    66  	}
    67  	f := fakeEnv{data: envMap}
    68  	saveEnv := osenv.GetEnvFunc
    69  	osenv.GetEnvFunc = f.GetEnv
    70  	defer func() {
    71  		osenv.GetEnvFunc = saveEnv
    72  	}()
    73  
    74  	tests := []struct {
    75  		name     string
    76  		key      string
    77  		defval   string
    78  		required bool
    79  	}{
    80  		{name: "1",
    81  			key:      DomainUID,
    82  			defval:   "",
    83  			required: true,
    84  		},
    85  		{name: "2",
    86  			key:      DomainNamespace,
    87  			defval:   "",
    88  			required: true,
    89  		},
    90  		{name: "3",
    91  			key:      MinReplicaCount,
    92  			defval:   "2",
    93  			required: true,
    94  		},
    95  		{name: "4",
    96  			key:      MaxReplicaCount,
    97  			defval:   "4",
    98  			required: true,
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		t.Run(test.name, func(t *testing.T) {
   104  			w, err := NewScaleWorker()
   105  			assert.NoError(t, err)
   106  			el := w.GetEnvDescList()
   107  			for _, e := range el {
   108  				if e.Key == test.key {
   109  					assert.Equal(t, test.defval, e.DefaultVal)
   110  					assert.Equal(t, test.required, e.Required)
   111  				}
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  // TestGetMetricDescList tests the GetEnvDescList method
   118  // GIVEN a worker
   119  //
   120  //	WHEN the GetEnvDescList methods is called
   121  //	THEN ensure that the correct results are returned
   122  func TestGetMetricDescList(t *testing.T) {
   123  	envMap := map[string]string{
   124  		DomainUID:       "test-domain",
   125  		DomainNamespace: "test-namespace",
   126  		MinReplicaCount: "2",
   127  		MaxReplicaCount: "4",
   128  	}
   129  	f := fakeEnv{data: envMap}
   130  	saveEnv := osenv.GetEnvFunc
   131  	osenv.GetEnvFunc = f.GetEnv
   132  	defer func() {
   133  		osenv.GetEnvFunc = saveEnv
   134  	}()
   135  
   136  	tests := []struct {
   137  		name   string
   138  		fqName string
   139  		help   string
   140  	}{
   141  		{name: "1", fqName: "scale_up_domain_count_total", help: "The total number of successful scale up domain requests"},
   142  		{name: "2", fqName: "scale_down_domain_count_total", help: "The total number of failed scale down domain requests"},
   143  		{name: "3", fqName: "scale_up_seconds", help: "The total number of seconds elapsed to scale up the domain"},
   144  		{name: "4", fqName: "scale_down_seconds", help: "The total number of seconds elapsed to scale down the domain"},
   145  	}
   146  	for _, test := range tests {
   147  		t.Run(test.name, func(t *testing.T) {
   148  
   149  			wi, err := NewScaleWorker()
   150  			w := wi.(worker)
   151  			assert.NoError(t, err)
   152  			dl := w.GetMetricDescList()
   153  			var found int
   154  			for _, d := range dl {
   155  				s := d.String()
   156  				if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) {
   157  					found++
   158  				}
   159  			}
   160  			assert.Equal(t, 1, found)
   161  		})
   162  	}
   163  }
   164  
   165  // TestGetMetricList tests the GetMetricList method
   166  // GIVEN a worker
   167  //
   168  //	WHEN the GetMetricList methods is called
   169  //	THEN ensure that the correct results are returned
   170  func TestGetMetricList(t *testing.T) {
   171  	origFunc := overridePsrClient()
   172  	defer func() {
   173  		funcNewPsrClient = origFunc
   174  	}()
   175  
   176  	envMap := map[string]string{
   177  		DomainUID:       "test-domain",
   178  		DomainNamespace: "test-namespace",
   179  		MinReplicaCount: "2",
   180  		MaxReplicaCount: "4",
   181  	}
   182  	f := fakeEnv{data: envMap}
   183  	saveEnv := osenv.GetEnvFunc
   184  	osenv.GetEnvFunc = f.GetEnv
   185  	defer func() {
   186  		osenv.GetEnvFunc = saveEnv
   187  	}()
   188  
   189  	tests := []struct {
   190  		name   string
   191  		fqName string
   192  		help   string
   193  	}{
   194  		{name: "1", fqName: "scale_up_domain_count_total", help: "The total number of successful scale up domain requests"},
   195  		{name: "2", fqName: "scale_down_domain_count_total", help: "The total number of failed scale down domain requests"},
   196  		{name: "3", fqName: "scale_up_seconds", help: "The total number of seconds elapsed to scale up the domain"},
   197  		{name: "4", fqName: "scale_down_seconds", help: "The total number of seconds elapsed to scale down the domain"},
   198  	}
   199  	for _, test := range tests {
   200  		t.Run(test.name, func(t *testing.T) {
   201  			wi, err := NewScaleWorker()
   202  			w := wi.(worker)
   203  			assert.NoError(t, err)
   204  			ml := w.GetMetricList()
   205  			var found int
   206  			for _, m := range ml {
   207  				s := m.Desc().String()
   208  				if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) {
   209  					found++
   210  				}
   211  			}
   212  			assert.Equal(t, 1, found)
   213  		})
   214  	}
   215  }
   216  
   217  // TestDoWork tests the DoWork method
   218  // GIVEN a worker
   219  //
   220  //	WHEN the DoWork methods is called
   221  //	THEN ensure that the correct results are returned
   222  /* func TestDoWork(t *testing.T) {
   223  	envMap := map[string]string{
   224  		DomainUID:       "test-domain",
   225  		DomainNamespace: "test-namespace",
   226  		MinReplicaCount: "2",
   227  		MaxReplicaCount: "4",
   228  	}
   229  	f := fakeEnv{data: envMap}
   230  	saveEnv := osenv.GetEnvFunc
   231  	osenv.GetEnvFunc = f.GetEnv
   232  	defer func() {
   233  		osenv.GetEnvFunc = saveEnv
   234  	}()
   235  
   236  	tests := []struct {
   237  		name            string
   238  		currentReplicas string
   239  		readyReplicas   string
   240  		minReplicas     string
   241  		maxReplicas     string
   242  		expectError     bool
   243  	}{
   244  		{
   245  			name:            "scaleup",
   246  			currentReplicas: "2",
   247  			readyReplicas:   "4",
   248  			minReplicas:     "2",
   249  			maxReplicas:     "4",
   250  			expectError:     false,
   251  		},
   252  		{
   253  			name:            "scaledown",
   254  			currentReplicas: "4",
   255  			readyReplicas:   "2",
   256  			minReplicas:     "2",
   257  			maxReplicas:     "4",
   258  			expectError:     false,
   259  		},
   260  		{
   261  			name:            "scaleupfailed",
   262  			currentReplicas: "2",
   263  			readyReplicas:   "2",
   264  			minReplicas:     "2",
   265  			maxReplicas:     "4",
   266  			expectError:     true,
   267  		},
   268  		{
   269  			name:            "scaledownfailed",
   270  			currentReplicas: "4",
   271  			readyReplicas:   "4",
   272  			minReplicas:     "2",
   273  			maxReplicas:     "4",
   274  			expectError:     true,
   275  		},
   276  	}
   277  	for _, test := range tests {
   278  		t.Run(test.name, func(t *testing.T) {
   279  			envMap := map[string]string{
   280  				DomainUID:       "test-domain",
   281  				DomainNamespace: "test-namespace",
   282  				MinReplicaCount: "2",
   283  				MaxReplicaCount: "4",
   284  			}
   285  			f := fakeEnv{data: envMap}
   286  			saveEnv := osenv.GetEnvFunc
   287  			osenv.GetEnvFunc = f.GetEnv
   288  			defer func() {
   289  				osenv.GetEnvFunc = saveEnv
   290  			}()
   291  
   292  			// Setup fake VZ client
   293  			cr := initFakeVzCr(v1alpha1.VzStateReady)
   294  			vzclient := vpoFakeClient.NewSimpleClientset(cr)
   295  
   296  			// Setup fake K8s client
   297  			scheme := runtime.NewScheme()
   298  			_ = corev1.AddToScheme(scheme)
   299  			_ = k8sapiext.AddToScheme(scheme)
   300  			_ = v1alpha1.AddToScheme(scheme)
   301  			builder := crtFakeClient.NewClientBuilder().WithScheme(scheme)
   302  
   303  			crtClient := builder.Build()
   304  			// Load the PsrClient with fake clients
   305  			psrClient := fakePsrClient{
   306  				psrClient: &k8sclient.PsrClient{
   307  					CrtlRuntime: crtClient,
   308  					VzInstall:   vzclient,
   309  					DynClient:   dynamicfake.NewSimpleDynamicClient(runtime.NewScheme()),
   310  				},
   311  			}
   312  			origFc := funcNewPsrClient
   313  			defer func() {
   314  				funcNewPsrClient = origFc
   315  			}()
   316  			funcNewPsrClient = psrClient.NewPsrClient
   317  
   318  			// Create worker and call dowork
   319  			wi, err := NewScaleWorker()
   320  			assert.NoError(t, err)
   321  			w := wi.(worker)
   322  			err = config.PsrEnv.LoadFromEnv(w.GetEnvDescList())
   323  			assert.NoError(t, err)
   324  
   325  			err = w.DoWork(config.CommonConfig{
   326  				WorkerType: "scale",
   327  			}, vzlog.DefaultLogger())
   328  			if test.expectError {
   329  				assert.Error(t, err)
   330  			} else {
   331  				assert.NoError(t, err)
   332  			}
   333  		})
   334  	}
   335  } */
   336  
   337  func (f *fakeEnv) GetEnv(key string) string {
   338  	return f.data[key]
   339  }
   340  
   341  func (f *fakePsrClient) NewPsrClient() (k8sclient.PsrClient, error) {
   342  	return *f.psrClient, nil
   343  }
   344  
   345  func overridePsrClient() func() (k8sclient.PsrClient, error) {
   346  	f := fakePsrClient{
   347  		psrClient: &k8sclient.PsrClient{},
   348  	}
   349  	origFc := funcNewPsrClient
   350  	funcNewPsrClient = f.NewPsrClient
   351  	return origFc
   352  }