github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/cmd/octsdb/config_test.go (about)

     1  // Copyright (c) 2016 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package main
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/aristanetworks/goarista/test"
    11  )
    12  
    13  func TestConfig(t *testing.T) {
    14  	cfg, err := loadConfig("/nonexistent.json")
    15  	if err == nil {
    16  		t.Fatal("Managed to load a nonexistent config!")
    17  	}
    18  	cfg, err = loadConfig("sampleconfig.json")
    19  	if err != nil {
    20  		t.Fatal("Failed to load config:", err)
    21  	}
    22  
    23  	testcases := []struct {
    24  		path           string
    25  		metric         string
    26  		tags           map[string]string
    27  		staticValueMap map[string]int64
    28  	}{{
    29  		path:           "/Sysdb/environment/cooling/status/fan/Fan1/1/speed/value",
    30  		metric:         "eos.fanspeed.environment.fan.speed",
    31  		tags:           map[string]string{"fan": "Fan1/1"},
    32  		staticValueMap: map[string]int64{},
    33  	}, {
    34  		path: "/Sysdb/environment/power/status/powerSupply/PowerSupply2/" +
    35  			"outputPower/value",
    36  		metric:         "eos.powersensor.environment.power.output",
    37  		tags:           map[string]string{"sensor": "PowerSupply2"},
    38  		staticValueMap: map[string]int64{},
    39  	}, {
    40  		path: "/Sysdb/environment/power/status/voltageSensor/VoltageSensor23/" +
    41  			"voltage/value",
    42  		metric:         "eos.voltagesensor.environment.voltage",
    43  		tags:           map[string]string{"sensor": "VoltageSensor23"},
    44  		staticValueMap: map[string]int64{},
    45  	}, {
    46  		path: "/Sysdb/environment/power/status/currentSensor/CurrentSensorP2/1/" +
    47  			"current/value",
    48  		metric:         "eos.currentsensor.environment.current",
    49  		tags:           map[string]string{"sensor": "CurrentSensorP2/1"},
    50  		staticValueMap: map[string]int64{},
    51  	}, {
    52  		path: "/Sysdb/environment/temperature/status/tempSensor/" +
    53  			"TempSensorP2/1/maxTemperature/value",
    54  		metric:         "eos.tempsensor.environment.maxtemperature",
    55  		tags:           map[string]string{"sensor": "TempSensorP2/1"},
    56  		staticValueMap: map[string]int64{},
    57  	}, {
    58  		path: "/Sysdb/interface/counter/eth/lag/intfCounterDir/" +
    59  			"Port-Channel201/intfCounter/current/statistics/outUcastPkts",
    60  		metric: "eos.intfpktcounter.interface.pkt",
    61  		tags: map[string]string{"intf": "Port-Channel201", "direction": "out",
    62  			"type": "Ucast"},
    63  		staticValueMap: map[string]int64{},
    64  	}, {
    65  		path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
    66  			"Ethernet42/intfCounter/current/statistics/inUcastPkts",
    67  		metric:         "eos.intfpktcounter.interface.pkt",
    68  		tags:           map[string]string{"intf": "Ethernet42", "direction": "in", "type": "Ucast"},
    69  		staticValueMap: map[string]int64{},
    70  	}, {
    71  		path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
    72  			"Ethernet42/intfCounter/lastClear/statistics/inErrors",
    73  		staticValueMap: map[string]int64{},
    74  	}, {
    75  		path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
    76  			"Ethernet42/intfCounter/current/ethStatistics/outPfcClassFrames",
    77  		metric:         "eos.intfpfcclasscounter.interface.pfcclassframes",
    78  		tags:           map[string]string{"intf": "Ethernet42", "direction": "out"},
    79  		staticValueMap: map[string]int64{},
    80  	}, {
    81  		path: "/Sysdb/interface/status/eth/phy/slice/1/intfStatus/" +
    82  			"Ethernet42/operStatus$",
    83  		metric:         "eos.operstatus",
    84  		tags:           map[string]string{"intf": "Ethernet42"},
    85  		staticValueMap: map[string]int64{"intfOperUp": 1, "intfOperDown": 0, "default": 0},
    86  	}}
    87  
    88  	for i, tcase := range testcases {
    89  		actualMetric, actualTags, staticValueMap := cfg.Match(tcase.path)
    90  		if actualMetric != tcase.metric {
    91  			t.Errorf("#%d expected metric %q but got %q", i, tcase.metric, actualMetric)
    92  		}
    93  		if d := test.Diff(tcase.tags, actualTags); actualMetric != "" && d != "" {
    94  			t.Errorf("#%d expected tags %q but got %q: %s", i, tcase.tags, actualTags, d)
    95  		}
    96  
    97  		if d := test.Diff(tcase.staticValueMap, staticValueMap); d != "" {
    98  			t.Errorf("#%d expected staticValueMap %q but got %q, %q", i, tcase.staticValueMap,
    99  				staticValueMap, d)
   100  		}
   101  
   102  	}
   103  }