github.com/Jeffail/benthos/v3@v3.65.0/lib/metrics/constructor_test.go (about)

     1  package metrics_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/metrics"
     8  	"github.com/Jeffail/benthos/v3/lib/util/config"
     9  	yaml "gopkg.in/yaml.v3"
    10  
    11  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    12  )
    13  
    14  func TestSanitise(t *testing.T) {
    15  	exp := config.Sanitised{
    16  		"type": "http_server",
    17  		"http_server": map[string]interface{}{
    18  			"prefix":       "benthos",
    19  			"path_mapping": "",
    20  		},
    21  	}
    22  
    23  	conf := metrics.NewConfig()
    24  	conf.Type = "http_server"
    25  
    26  	act, err := metrics.SanitiseConfig(conf)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if !reflect.DeepEqual(act, exp) {
    31  		t.Errorf("Wrong sanitised output: %v != %v", act, exp)
    32  	}
    33  
    34  	exp = config.Sanitised{
    35  		"type": "statsd",
    36  		"statsd": map[string]interface{}{
    37  			"address":      "foo",
    38  			"prefix":       "benthos",
    39  			"path_mapping": "",
    40  			"flush_period": "100ms",
    41  			"network":      "udp",
    42  			"tag_format":   "legacy",
    43  		},
    44  	}
    45  
    46  	conf = metrics.NewConfig()
    47  	conf.Type = "statsd"
    48  	conf.Statsd.Address = "foo"
    49  
    50  	act, err = metrics.SanitiseConfig(conf)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if !reflect.DeepEqual(act, exp) {
    55  		t.Errorf("Wrong sanitised output: %v != %v", act, exp)
    56  	}
    57  }
    58  
    59  func TestConstructorConfigYAMLInference(t *testing.T) {
    60  	conf := []metrics.Config{}
    61  
    62  	if err := yaml.Unmarshal([]byte(`[
    63  		{
    64  			"http_server": {
    65  				"value": "foo"
    66  			},
    67  			"prometheus": {
    68  				"query": "foo"
    69  			}
    70  		}
    71  	]`), &conf); err == nil {
    72  		t.Error("Expected error from multi candidates")
    73  	}
    74  
    75  	if err := yaml.Unmarshal([]byte(`[
    76  		{
    77  			"prometheus": {
    78  				"push_interval": "foo"
    79  			}
    80  		}
    81  	]`), &conf); err != nil {
    82  		t.Error(err)
    83  	}
    84  
    85  	if exp, act := 1, len(conf); exp != act {
    86  		t.Errorf("Wrong number of config parts: %v != %v", act, exp)
    87  		return
    88  	}
    89  	if exp, act := metrics.TypePrometheus, conf[0].Type; exp != act {
    90  		t.Errorf("Wrong inferred type: %v != %v", act, exp)
    91  	}
    92  	if exp, act := "benthos_push", conf[0].Prometheus.PushJobName; exp != act {
    93  		t.Errorf("Wrong default operator: %v != %v", act, exp)
    94  	}
    95  	if exp, act := "foo", conf[0].Prometheus.PushInterval; exp != act {
    96  		t.Errorf("Wrong value: %v != %v", act, exp)
    97  	}
    98  }