|
| 1 | +/*This file is part of sisyphus. |
| 2 | + * |
| 3 | + * Copyright Datto, Inc. |
| 4 | + * Author: John Seekins <[email protected]> |
| 5 | + * |
| 6 | + * Licensed under the GNU General Public License Version 3 |
| 7 | + * Fedora-License-Identifier: GPLv3+ |
| 8 | + * SPDX-2.0-License-Identifier: GPL-3.0+ |
| 9 | + * SPDX-3.0-License-Identifier: GPL-3.0-or-later |
| 10 | + * |
| 11 | + * sisyphus is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * sisyphus is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License |
| 22 | + * along with sisyphus. If not, see <https://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | +package main |
| 25 | + |
| 26 | +import ( |
| 27 | + "testing" |
| 28 | +) |
| 29 | + |
| 30 | +/* |
| 31 | +Things we should check: |
| 32 | +1. metric where nothing changes |
| 33 | +2. metric with bad name |
| 34 | +3. metric with no fields |
| 35 | +4. normalized metrics (lower cased) |
| 36 | +5. metric names with "bad" characters |
| 37 | +6. metric tag names with "bad" characters |
| 38 | +7. metric field names with "bad" characters |
| 39 | +*/ |
| 40 | +func TestFilter(t *testing.T) { |
| 41 | + var err error |
| 42 | + var results InfluxMetric |
| 43 | + // nothing should change here |
| 44 | + msg := InfluxMetric{Name: "test_metric", Fields: map[string]interface{}{"field": float64(1)}, |
| 45 | + Tags: map[string]string{"tag": "Value"}, Timestamp: 1637090544726635243} |
| 46 | + results, err = filterMsg(1, msg, false) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("Failed to parse valid metric: %v -> %v", msg, err) |
| 49 | + } |
| 50 | + if results.Timestamp != 1637090544726635243 { |
| 51 | + t.Fatalf("Timestamp is invalid: %v -> should be '1637090544726635243'", results.Timestamp) |
| 52 | + } |
| 53 | + if results.Name != "test_metric" { |
| 54 | + t.Fatalf("Name is wrong: %v -> should be 'test_metric'", results.Name) |
| 55 | + } |
| 56 | + if results.Fields["field"].(float64) != 1 { |
| 57 | + t.Fatalf("field value is wrong: %v -> should be '1'", results.Fields["field"]) |
| 58 | + } |
| 59 | + if results.Tags["tag"] != "Value" { |
| 60 | + t.Fatalf("tag value is wrong: %v -> should be 'Value'", results.Tags["tag"]) |
| 61 | + } |
| 62 | + // check for bad metric name |
| 63 | + msg = InfluxMetric{Name: "_test_metric", Fields: map[string]interface{}{"field": 1}, |
| 64 | + Tags: map[string]string{"tag": "Value"}, Timestamp: 1637090544726635243} |
| 65 | + results, err = filterMsg(1, msg, false) |
| 66 | + if err == nil { |
| 67 | + t.Fatalf("Received results from a bad metric name! Impossible! %v", results) |
| 68 | + } |
| 69 | + // check for metric with no fields |
| 70 | + msg = InfluxMetric{Name: "test_metric", Fields: map[string]interface{}{}, |
| 71 | + Tags: map[string]string{"tag": "Value"}, Timestamp: 1637090544726635243} |
| 72 | + results, err = filterMsg(1, msg, false) |
| 73 | + if err == nil { |
| 74 | + t.Fatalf("Received results from empty field list! Impossible! %v", results) |
| 75 | + } |
| 76 | + // check for normalization |
| 77 | + msg = InfluxMetric{Name: "test_metric", Fields: map[string]interface{}{"field": 1}, |
| 78 | + Tags: map[string]string{"tag": "Value"}, Timestamp: 1637090544726635243} |
| 79 | + results, err = filterMsg(1, msg, true) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Failed to parse valid metric: %v -> %v", msg, err) |
| 82 | + } |
| 83 | + if results.Tags["tag"] != "value" { |
| 84 | + t.Fatalf("Normalization failed: %v", results) |
| 85 | + } |
| 86 | + // bad characters |
| 87 | + msg = InfluxMetric{Name: "test-metric", Fields: map[string]interface{}{"field.1": 1}, |
| 88 | + Tags: map[string]string{"tag 1": "Value"}, Timestamp: 1637090544726635243} |
| 89 | + results, err = filterMsg(1, msg, false) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Failed to parse valid metric: %v -> %v", msg, err) |
| 92 | + } |
| 93 | + if results.Name != "test_metric" { |
| 94 | + t.Fatalf("Failed to reformat bad metric name %v -> should be 'test_metric'", results.Name) |
| 95 | + } |
| 96 | + if _, ok := results.Fields["field_1"]; !ok { |
| 97 | + t.Fatalf("Failed to reformat bad field name %v -> should be 'field_1'", results.Fields) |
| 98 | + } |
| 99 | + if _, ok := results.Tags["tag_1"]; !ok { |
| 100 | + t.Fatalf("Failed to reformat bad tag name %v -> should be 'tag_1'", results.Tags) |
| 101 | + } |
| 102 | +} |
0 commit comments