github.com/verrazzano/verrazzano@v1.7.0/tools/psr/backend/workers/opensearch/writelogs/writelogs_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 writelogs
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
     9  	"github.com/verrazzano/verrazzano/tools/psr/backend/config"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  // TestGetters tests the worker getters
    15  // GIVEN a worker
    16  //
    17  //	WHEN the getter methods are calls
    18  //	THEN ensure that the correct results are returned
    19  func TestGetters(t *testing.T) {
    20  	w, err := NewWriteLogsWorker()
    21  	assert.NoError(t, err)
    22  
    23  	wd := w.GetWorkerDesc()
    24  	assert.Equal(t, config.WorkerTypeOpsWriteLogs, wd.WorkerType)
    25  	assert.Equal(t, "The writelogs worker writes logs to STDOUT, putting a load on OpenSearch", wd.Description)
    26  	assert.Equal(t, metricsPrefix, wd.MetricsPrefix)
    27  
    28  	el := w.GetEnvDescList()
    29  	assert.Len(t, el, 0)
    30  
    31  	logged := w.WantLoopInfoLogged()
    32  	assert.False(t, logged)
    33  }
    34  
    35  func TestGetMetricDescList(t *testing.T) {
    36  	tests := []struct {
    37  		name   string
    38  		fqName string
    39  		help   string
    40  	}{
    41  		{name: "1", fqName: metricsPrefix + "_logged_lines_count_total", help: "The total number of lines logged"},
    42  		{name: "2", fqName: metricsPrefix + "_logged_chars_total", help: "The total number of characters logged"},
    43  	}
    44  	for _, test := range tests {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			wi, err := NewWriteLogsWorker()
    47  			w := wi.(worker)
    48  			assert.NoError(t, err)
    49  			dl := w.GetMetricDescList()
    50  			var found int
    51  			for _, d := range dl {
    52  				s := d.String()
    53  				if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) {
    54  					found++
    55  				}
    56  			}
    57  			assert.Equal(t, 1, found)
    58  		})
    59  	}
    60  }
    61  
    62  func TestGetMetricList(t *testing.T) {
    63  	tests := []struct {
    64  		name   string
    65  		fqName string
    66  		help   string
    67  	}{
    68  		{name: "1", fqName: metricsPrefix + "_logged_lines_count_total", help: "The total number of lines logged"},
    69  		{name: "2", fqName: metricsPrefix + "_logged_chars_total", help: "The total number of characters logged"},
    70  	}
    71  	for _, test := range tests {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			wi, err := NewWriteLogsWorker()
    74  			w := wi.(worker)
    75  			assert.NoError(t, err)
    76  			ml := w.GetMetricList()
    77  			var found int
    78  			for _, m := range ml {
    79  				s := m.Desc().String()
    80  				if strings.Contains(s, test.fqName) && strings.Contains(s, test.help) {
    81  					found++
    82  				}
    83  			}
    84  			assert.Equal(t, 1, found)
    85  		})
    86  	}
    87  }
    88  
    89  // TestDoWork tests the DoWork method
    90  // GIVEN a worker
    91  //
    92  //	WHEN the DoWork methods is called
    93  //	THEN ensure that the correct results are returned
    94  func TestDoWork(t *testing.T) {
    95  	wi, err := NewWriteLogsWorker()
    96  	assert.NoError(t, err)
    97  	w := wi.(worker)
    98  	err = w.DoWork(config.CommonConfig{
    99  		WorkerType: "Fake",
   100  	}, vzlog.DefaultLogger())
   101  	assert.NoError(t, err)
   102  	assert.Equal(t, int64(31), w.loggedCharsCountTotal.Val)
   103  	assert.Equal(t, int64(1), w.loggedLinesCountTotal.Val)
   104  }