github.com/Psiphon-Inc/goarista@v0.0.0-20160825065156-d002785f4c67/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  
    20  	testcases := []struct {
    21  		path   string
    22  		metric string
    23  		tags   map[string]string
    24  	}{{
    25  		path:   "/Sysdb/environment/cooling/status/fan/Fan1/1/speed/value",
    26  		metric: "eos.environment.fan.speed",
    27  		tags:   map[string]string{"fan": "Fan1/1"},
    28  	}, {
    29  		path:   "/Sysdb/environment/power/status/powerSupply/PowerSupply2/outputPower/value",
    30  		metric: "eos.environment.power.output",
    31  		tags:   map[string]string{"sensor": "PowerSupply2"},
    32  	}, {
    33  		path:   "/Sysdb/environment/power/status/voltageSensor/VoltageSensor23/voltage/value",
    34  		metric: "eos.environment.voltage",
    35  		tags:   map[string]string{"sensor": "VoltageSensor23"},
    36  	}, {
    37  		path:   "/Sysdb/environment/power/status/currentSensor/CurrentSensorP2/1/current/value",
    38  		metric: "eos.environment.current",
    39  		tags:   map[string]string{"sensor": "CurrentSensorP2/1"},
    40  	}, {
    41  		path: "/Sysdb/environment/temperature/status/tempSensor/" +
    42  			"TempSensorP2/1/maxTemperature/value",
    43  		metric: "eos.environment.maxtemperature",
    44  		tags:   map[string]string{"sensor": "TempSensorP2/1"},
    45  	}, {
    46  		path: "/Sysdb/interface/counter/eth/lag/intfCounterDir/" +
    47  			"Port-Channel201/intfCounter/current/statistics/outUcastPkts",
    48  		metric: "eos.interface.pkt",
    49  		tags:   map[string]string{"intf": "Port-Channel201", "direction": "out", "type": "Ucast"},
    50  	}, {
    51  		path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
    52  			"Ethernet42/intfCounter/current/statistics/inUcastPkts",
    53  		metric: "eos.interface.pkt",
    54  		tags:   map[string]string{"intf": "Ethernet42", "direction": "in", "type": "Ucast"},
    55  	}, {
    56  		path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
    57  			"Ethernet42/intfCounter/lastClear/statistics/inErrors",
    58  	}}
    59  	for i, tcase := range testcases {
    60  		actualMetric, actualTags := cfg.Match(tcase.path)
    61  		if actualMetric != tcase.metric {
    62  			t.Errorf("#%d expected metric %q but got %q", i, tcase.metric, actualMetric)
    63  		}
    64  		if d := test.Diff(tcase.tags, actualTags); actualMetric != "" && d != "" {
    65  			t.Errorf("#%d expected tags %q but got %q: %s", i, tcase.tags, actualTags, d)
    66  		}
    67  	}
    68  }