github.com/verrazzano/verrazzano@v1.7.1/tools/psr/backend/config/config_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 config
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    12  	"github.com/verrazzano/verrazzano/tools/psr/backend/osenv"
    13  )
    14  
    15  type fakeEnv struct {
    16  	data map[string]string
    17  }
    18  
    19  // TestWorkerType tests the Config interface
    20  // GIVEN a config map with environment vars related to worker type
    21  //
    22  //	WHEN the GetCommonConfig is called
    23  //	THEN ensure that the resulting configuration is correct
    24  func TestWorkerType(t *testing.T) {
    25  	var tests = []struct {
    26  		name       string
    27  		envMap     map[string]string
    28  		envKey     string
    29  		workerType string
    30  		expectErr  bool
    31  	}{
    32  		{name: "ExampleWorker",
    33  			workerType: WorkerTypeExample,
    34  			expectErr:  false,
    35  			envMap: map[string]string{
    36  				PsrWorkerType: WorkerTypeExample,
    37  			},
    38  		},
    39  		{name: "GetLogs",
    40  			workerType: WorkerTypeOpsGetLogs,
    41  			expectErr:  false,
    42  			envMap: map[string]string{
    43  				PsrWorkerType: WorkerTypeOpsGetLogs,
    44  			},
    45  		},
    46  		{name: "ExampleWorkerType",
    47  			workerType: WorkerTypeOpsWriteLogs,
    48  			expectErr:  false,
    49  			envMap: map[string]string{
    50  				PsrWorkerType: WorkerTypeOpsWriteLogs,
    51  			},
    52  		},
    53  		{name: "MissingWorkerType",
    54  			workerType: WorkerTypeOpsWriteLogs,
    55  			expectErr:  true,
    56  			envMap:     map[string]string{},
    57  		},
    58  	}
    59  	for _, test := range tests {
    60  		t.Run(test.name, func(t *testing.T) {
    61  			// Load the fake env
    62  			f := fakeEnv{data: test.envMap}
    63  			saveEnv := osenv.GetEnvFunc
    64  			osenv.GetEnvFunc = f.GetEnv
    65  			defer func() {
    66  				osenv.GetEnvFunc = saveEnv
    67  			}()
    68  
    69  			cc, err := GetCommonConfig(vzlog.DefaultLogger())
    70  			if test.expectErr {
    71  				assert.Error(t, err)
    72  			} else {
    73  				assert.NoError(t, err)
    74  				assert.Equal(t, test.workerType, cc.WorkerType)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  // TestDuration tests the Config interface
    81  // GIVEN a config map with environment vars PsrDuration
    82  //
    83  //	WHEN the GetCommonConfig is called
    84  //	THEN ensure that the resulting configuration is correct
    85  func TestDuration(t *testing.T) {
    86  	var tests = []struct {
    87  		name      string
    88  		envMap    map[string]string
    89  		envKey    string
    90  		duration  time.Duration
    91  		expectErr bool
    92  	}{
    93  		{name: "DefaultDuration",
    94  			duration:  UnlimitedWorkerDuration,
    95  			expectErr: false,
    96  			envMap: map[string]string{
    97  				PsrWorkerType: WorkerTypeOpsWriteLogs,
    98  			},
    99  		},
   100  		{name: "OneSecDuration",
   101  			duration:  1 * time.Second,
   102  			expectErr: false,
   103  			envMap: map[string]string{
   104  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   105  				PsrDuration:   "1s",
   106  			},
   107  		},
   108  		{name: "OneMinDuration",
   109  			duration:  1 * time.Minute,
   110  			expectErr: false,
   111  			envMap: map[string]string{
   112  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   113  				PsrDuration:   "1m",
   114  			},
   115  		},
   116  		{name: "NegativeDuration",
   117  			duration:  UnlimitedWorkerDuration,
   118  			expectErr: false,
   119  			envMap: map[string]string{
   120  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   121  				PsrDuration:   "-3s",
   122  			},
   123  		},
   124  		{name: "BadNumericStringFormat",
   125  			expectErr: true,
   126  			envMap: map[string]string{
   127  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   128  				PsrDuration:   "10xyz",
   129  			},
   130  		},
   131  	}
   132  	for _, test := range tests {
   133  		t.Run(test.name, func(t *testing.T) {
   134  			// Load the fake env
   135  			f := fakeEnv{data: test.envMap}
   136  			saveEnv := osenv.GetEnvFunc
   137  			osenv.GetEnvFunc = f.GetEnv
   138  			defer func() {
   139  				osenv.GetEnvFunc = saveEnv
   140  			}()
   141  
   142  			cc, err := GetCommonConfig(vzlog.DefaultLogger())
   143  			if test.expectErr {
   144  				assert.Error(t, err)
   145  			} else {
   146  				assert.NoError(t, err)
   147  				assert.Equal(t, test.duration, cc.PsrDuration)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  // TestLoopSleep tests the Config interface
   154  // GIVEN a config map with environment vars loop sleep
   155  //
   156  //	WHEN the GetCommonConfig is called
   157  //	THEN ensure that the resulting configuration is correct
   158  func TestLoopSleep(t *testing.T) {
   159  	var tests = []struct {
   160  		name      string
   161  		envMap    map[string]string
   162  		envKey    string
   163  		loopSleep time.Duration
   164  		expectErr bool
   165  	}{
   166  		{name: "DefaultSleep",
   167  			loopSleep: time.Second,
   168  			expectErr: false,
   169  			envMap: map[string]string{
   170  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   171  			},
   172  		},
   173  		{name: "TenMilliSleep",
   174  			loopSleep: 10 * time.Millisecond,
   175  			expectErr: false,
   176  			envMap: map[string]string{
   177  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   178  				PsrLoopSleep:  "10ms",
   179  			},
   180  		},
   181  		{name: "TenNanoSleep",
   182  			loopSleep: 10 * time.Nanosecond,
   183  			expectErr: false,
   184  			envMap: map[string]string{
   185  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   186  				PsrLoopSleep:  "10ns",
   187  			},
   188  		},
   189  		// Test min sleep of 10ns
   190  		{name: "TenNanoSleepMin",
   191  			loopSleep: 10 * time.Nanosecond,
   192  			expectErr: false,
   193  			envMap: map[string]string{
   194  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   195  				PsrLoopSleep:  "1ns",
   196  			},
   197  		},
   198  		{name: "BadNumericStringFormat",
   199  			expectErr: true,
   200  			envMap: map[string]string{
   201  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   202  				PsrLoopSleep:  "10xyz",
   203  			},
   204  		},
   205  	}
   206  	for _, test := range tests {
   207  		t.Run(test.name, func(t *testing.T) {
   208  			// Load the fake env
   209  			f := fakeEnv{data: test.envMap}
   210  			saveEnv := osenv.GetEnvFunc
   211  			osenv.GetEnvFunc = f.GetEnv
   212  			defer func() {
   213  				osenv.GetEnvFunc = saveEnv
   214  			}()
   215  
   216  			cc, err := GetCommonConfig(vzlog.DefaultLogger())
   217  			if test.expectErr {
   218  				assert.Error(t, err)
   219  			} else {
   220  				assert.NoError(t, err)
   221  				assert.Equal(t, test.loopSleep, cc.LoopSleepNanos)
   222  			}
   223  		})
   224  	}
   225  }
   226  
   227  // TestThreadCount tests the Config interface
   228  // GIVEN a config map with environment vars related to thread count
   229  //
   230  //	WHEN the GetCommonConfig is called
   231  //	THEN ensure that the resulting configuration is correct
   232  func TestThreadCount(t *testing.T) {
   233  	var tests = []struct {
   234  		name          string
   235  		envMap        map[string]string
   236  		envKey        string
   237  		workerThreads int
   238  		expectErr     bool
   239  	}{
   240  		{name: "DefaultWorkerThreads",
   241  			workerThreads: 1,
   242  			expectErr:     false,
   243  			envMap: map[string]string{
   244  				PsrWorkerType: WorkerTypeOpsWriteLogs,
   245  			},
   246  		},
   247  		{name: "MultipleWorkerThreads",
   248  			workerThreads: 50,
   249  			expectErr:     false,
   250  			envMap: map[string]string{
   251  				PsrWorkerType:        WorkerTypeOpsWriteLogs,
   252  				PsrWorkerThreadCount: "50",
   253  			},
   254  		},
   255  		// Test max threads 100
   256  		{name: "MaxWorkerThreads",
   257  			workerThreads: 100,
   258  			expectErr:     false,
   259  			envMap: map[string]string{
   260  				PsrWorkerType:        WorkerTypeOpsWriteLogs,
   261  				PsrWorkerThreadCount: "1000",
   262  			},
   263  		},
   264  		{name: "BadThreadCountFormat",
   265  			expectErr: true,
   266  			envMap: map[string]string{
   267  				PsrWorkerType:        WorkerTypeOpsWriteLogs,
   268  				PsrWorkerThreadCount: "100n",
   269  			},
   270  		},
   271  	}
   272  	for _, test := range tests {
   273  		t.Run(test.name, func(t *testing.T) {
   274  			// Load the fake env
   275  			f := fakeEnv{data: test.envMap}
   276  			saveEnv := osenv.GetEnvFunc
   277  			osenv.GetEnvFunc = f.GetEnv
   278  			defer func() {
   279  				osenv.GetEnvFunc = saveEnv
   280  			}()
   281  
   282  			cc, err := GetCommonConfig(vzlog.DefaultLogger())
   283  			if test.expectErr {
   284  				assert.Error(t, err)
   285  			} else {
   286  				assert.NoError(t, err)
   287  				assert.Equal(t, test.workerThreads, cc.WorkerThreadCount)
   288  			}
   289  		})
   290  	}
   291  }
   292  
   293  func (f *fakeEnv) GetEnv(key string) string {
   294  	return f.data[key]
   295  }