github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/test/framework/ginkgo_wrapper_util_test.go (about)

     1  // Copyright (c) 2020, 2024, 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 framework
     5  
     6  import (
     7  	"os"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework/metrics"
    13  )
    14  
    15  // TestIsBodyFunc - test function for introspecting an interface value
    16  func TestIsBodyFunc(t *testing.T) {
    17  	type args struct {
    18  		body interface{}
    19  	}
    20  	tests := []struct {
    21  		name string
    22  		args args
    23  		want bool
    24  	}{
    25  		{
    26  			name: "Test using a function",
    27  			args: args{body: func() {}},
    28  			want: true,
    29  		},
    30  		{
    31  			name: "Test using a struct",
    32  			args: args{body: args{}},
    33  			want: false,
    34  		},
    35  	}
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			if got := isBodyFunc(tt.args.body); got != tt.want {
    39  				t.Errorf("isBodyFunc() = %v, want %v", got, tt.want)
    40  			}
    41  		})
    42  	}
    43  }
    44  
    45  func Test_createMetricsConfigFromEnv(t *testing.T) {
    46  	_, err := createMetricsConfigFromEnv("testname")
    47  	assert.Error(t, err)
    48  
    49  	defer func() {
    50  		getenvFunc = os.Getenv
    51  	}()
    52  
    53  	testURL := "https://some.pushgateway.url"
    54  	testUser := "myuser"
    55  	getenvFunc = promGetEnvTestFunc(testURL, testUser, "")
    56  	_, err = createMetricsConfigFromEnv("testname")
    57  
    58  	assert.Error(t, err, "expected error creating config when Prometheus push gateway password is not set")
    59  
    60  	testPass := "somepass"
    61  	getenvFunc = promGetEnvTestFunc(testURL, testUser, testPass)
    62  	cfg, err := createMetricsConfigFromEnv("testname")
    63  	assert.NoError(t, err)
    64  	assert.NotNil(t, cfg)
    65  	assert.Equal(t, "*metrics.PrometheusMetricsReceiverConfig", reflect.TypeOf(cfg).String())
    66  	promCfg := cfg.(*metrics.PrometheusMetricsReceiverConfig)
    67  	assert.Equal(t, testURL, promCfg.PushGatewayURL)
    68  	assert.Equal(t, testUser, promCfg.PushGatewayUser)
    69  	assert.Equal(t, testPass, promCfg.PushGatewayPassword)
    70  }
    71  
    72  func promGetEnvTestFunc(testURL string, testUser string, testPass string) func(key string) string {
    73  	return func(key string) string {
    74  		switch key {
    75  		case promPushURLEnvVarName:
    76  			return testURL
    77  		case promPushUserEnvVarName:
    78  			return testUser
    79  		case promPushPasswordEnvVarName:
    80  			return testPass
    81  		default:
    82  			return ""
    83  		}
    84  	}
    85  }