123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- // Copyright (c) 2017 Arista Networks, Inc.
- // Use of this source code is governed by the Apache License 2.0
- // that can be found in the COPYING file.
- package main
- import (
- "testing"
- "notabug.org/themusicgod1/goarista/test"
- "github.com/openconfig/reference/rpc/openconfig"
- "github.com/prometheus/client_golang/prometheus"
- )
- func makeMetrics(cfg *Config, expValues map[source]float64) map[source]*labelledMetric {
- expMetrics := map[source]*labelledMetric{}
- for k, v := range expValues {
- desc, labels := cfg.getDescAndLabels(k)
- if desc == nil || labels == nil {
- panic("cfg.getDescAndLabels returned nil")
- }
- expMetrics[k] = &labelledMetric{
- metric: prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, labels...),
- labels: labels,
- }
- }
- return expMetrics
- }
- func makeResponse(notif *openconfig.Notification) *openconfig.SubscribeResponse {
- return &openconfig.SubscribeResponse{
- Response: &openconfig.SubscribeResponse_Update{Update: notif},
- }
- }
- func TestUpdate(t *testing.T) {
- config := []byte(`
- devicelabels:
- 10.1.1.1:
- lab1: val1
- lab2: val2
- '*':
- lab1: val3
- lab2: val4
- subscriptions:
- - /Sysdb/environment/cooling/status
- - /Sysdb/environment/power/status
- - /Sysdb/bridging/igmpsnooping/forwarding/forwarding/status
- metrics:
- - name: intfCounter
- path: /Sysdb/(lag|slice/phy/.+)/intfCounterDir/(?P<intf>.+)/intfCounter
- help: Per-Interface Bytes/Errors/Discards Counters
- - name: fanSpeed
- path: /Sysdb/environment/cooling/status/fan/speed/value
- help: Fan Speed
- - name: igmpSnoopingInf
- path: /Sysdb/igmpsnooping/vlanStatus/(?P<vlan>.+)/ethGroup/(?P<mac>.+)/intf/(?P<intf>.+)
- help: IGMP snooping status`)
- cfg, err := parseConfig(config)
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- coll := newCollector(cfg)
- notif := &openconfig.Notification{
- Prefix: &openconfig.Path{Element: []string{"Sysdb"}},
- Update: []*openconfig.Update{
- {
- Path: &openconfig.Path{
- Element: []string{"lag", "intfCounterDir", "Ethernet1", "intfCounter"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("42"),
- },
- },
- {
- Path: &openconfig.Path{
- Element: []string{"environment", "cooling", "status", "fan", "speed"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("{\"value\": 45}"),
- },
- },
- {
- Path: &openconfig.Path{
- Element: []string{"igmpsnooping", "vlanStatus", "2050", "ethGroup",
- "01:00:5e:01:01:01", "intf", "Cpu"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("true"),
- },
- },
- },
- }
- expValues := map[source]float64{
- {
- addr: "10.1.1.1",
- path: "/Sysdb/lag/intfCounterDir/Ethernet1/intfCounter",
- }: 42,
- {
- addr: "10.1.1.1",
- path: "/Sysdb/environment/cooling/status/fan/speed/value",
- }: 45,
- {
- addr: "10.1.1.1",
- path: "/Sysdb/igmpsnooping/vlanStatus/2050/ethGroup/01:00:5e:01:01:01/intf/Cpu",
- }: 1,
- }
- coll.update("10.1.1.1:6042", makeResponse(notif))
- expMetrics := makeMetrics(cfg, expValues)
- if !test.DeepEqual(expMetrics, coll.metrics) {
- t.Errorf("Mismatched metrics: %v", test.Diff(expMetrics, coll.metrics))
- }
- // Update one value, and one path which is not a metric
- notif = &openconfig.Notification{
- Prefix: &openconfig.Path{Element: []string{"Sysdb"}},
- Update: []*openconfig.Update{
- {
- Path: &openconfig.Path{
- Element: []string{"lag", "intfCounterDir", "Ethernet1", "intfCounter"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("52"),
- },
- },
- {
- Path: &openconfig.Path{
- Element: []string{"environment", "doesntexist", "status"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("{\"value\": 45}"),
- },
- },
- },
- }
- src := source{
- addr: "10.1.1.1",
- path: "/Sysdb/lag/intfCounterDir/Ethernet1/intfCounter",
- }
- expValues[src] = 52
- coll.update("10.1.1.1:6042", makeResponse(notif))
- expMetrics = makeMetrics(cfg, expValues)
- if !test.DeepEqual(expMetrics, coll.metrics) {
- t.Errorf("Mismatched metrics: %v", test.Diff(expMetrics, coll.metrics))
- }
- // Same path, different device
- notif = &openconfig.Notification{
- Prefix: &openconfig.Path{Element: []string{"Sysdb"}},
- Update: []*openconfig.Update{
- {
- Path: &openconfig.Path{
- Element: []string{"lag", "intfCounterDir", "Ethernet1", "intfCounter"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("42"),
- },
- },
- },
- }
- src.addr = "10.1.1.2"
- expValues[src] = 42
- coll.update("10.1.1.2:6042", makeResponse(notif))
- expMetrics = makeMetrics(cfg, expValues)
- if !test.DeepEqual(expMetrics, coll.metrics) {
- t.Errorf("Mismatched metrics: %v", test.Diff(expMetrics, coll.metrics))
- }
- // Delete a path
- notif = &openconfig.Notification{
- Prefix: &openconfig.Path{Element: []string{"Sysdb"}},
- Delete: []*openconfig.Path{
- {
- Element: []string{"lag", "intfCounterDir", "Ethernet1", "intfCounter"},
- },
- },
- }
- src.addr = "10.1.1.1"
- delete(expValues, src)
- coll.update("10.1.1.1:6042", makeResponse(notif))
- expMetrics = makeMetrics(cfg, expValues)
- if !test.DeepEqual(expMetrics, coll.metrics) {
- t.Errorf("Mismatched metrics: %v", test.Diff(expMetrics, coll.metrics))
- }
- // Non-numeric update
- notif = &openconfig.Notification{
- Prefix: &openconfig.Path{Element: []string{"Sysdb"}},
- Update: []*openconfig.Update{
- {
- Path: &openconfig.Path{
- Element: []string{"lag", "intfCounterDir", "Ethernet1", "intfCounter"},
- },
- Value: &openconfig.Value{
- Type: openconfig.Type_JSON,
- Value: []byte("\"test\""),
- },
- },
- },
- }
- coll.update("10.1.1.1:6042", makeResponse(notif))
- if !test.DeepEqual(expMetrics, coll.metrics) {
- t.Errorf("Mismatched metrics: %v", test.Diff(expMetrics, coll.metrics))
- }
- }
|