bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/prometheus_test.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestLoadPrometheus(t *testing.T) {
    12  
    13  	tests := []struct {
    14  		name           string
    15  		Input          *Config
    16  		expectedResult string
    17  		err            string
    18  	}{
    19  		{
    20  			name: "basic valid configuration",
    21  			Input: &Config{
    22  				Prometheus: &PrometheusCfg{
    23  					Enabled:    true,
    24  					Level:      "full",
    25  					ListenAddr: "127.0.0.1",
    26  					ListenPort: 6060,
    27  				},
    28  				Cscli: &CscliCfg{},
    29  			},
    30  			expectedResult: "http://127.0.0.1:6060",
    31  		},
    32  	}
    33  
    34  	for idx, test := range tests {
    35  		err := test.Input.LoadPrometheus()
    36  		if err == nil && test.err != "" {
    37  			fmt.Printf("TEST '%s': NOK\n", test.name)
    38  			t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
    39  		} else if test.err != "" {
    40  			if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
    41  				fmt.Printf("TEST '%s': NOK\n", test.name)
    42  				t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
    43  					test.err,
    44  					fmt.Sprintf("%s", err))
    45  			}
    46  		}
    47  
    48  		isOk := assert.Equal(t, test.expectedResult, test.Input.Cscli.PrometheusUrl)
    49  		if !isOk {
    50  			t.Fatalf("test '%s' failed\n", test.name)
    51  		} else {
    52  			fmt.Printf("TEST '%s': OK\n", test.name)
    53  		}
    54  	}
    55  }