|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific langu |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "testing" |
| 16 | + |
| 17 | + "log/slog" |
| 18 | + |
| 19 | + "github.com/DATA-DOG/go-sqlmock" |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + dto "github.com/prometheus/client_model/go" |
| 22 | + "github.com/smartystreets/goconvey/convey" |
| 23 | +) |
| 24 | + |
| 25 | +type labelMap map[string]string |
| 26 | + |
| 27 | +type MetricResult struct { |
| 28 | + labels labelMap |
| 29 | + value float64 |
| 30 | + metricType dto.MetricType |
| 31 | +} |
| 32 | + |
| 33 | +func readMetric(m prometheus.Metric) MetricResult { |
| 34 | + pb := &dto.Metric{} |
| 35 | + m.Write(pb) |
| 36 | + labels := make(labelMap, len(pb.Label)) |
| 37 | + for _, v := range pb.Label { |
| 38 | + labels[v.GetName()] = v.GetValue() |
| 39 | + } |
| 40 | + if pb.Gauge != nil { |
| 41 | + return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE} |
| 42 | + } |
| 43 | + if pb.Counter != nil { |
| 44 | + return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER} |
| 45 | + } |
| 46 | + if pb.Untyped != nil { |
| 47 | + return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED} |
| 48 | + } |
| 49 | + panic("Unsupported metric type") |
| 50 | +} |
| 51 | + |
| 52 | +func TestQueryShowList(t *testing.T) { |
| 53 | + db, mock, err := sqlmock.New() |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 56 | + } |
| 57 | + defer db.Close() |
| 58 | + |
| 59 | + rows := sqlmock.NewRows([]string{"key", "value"}). |
| 60 | + AddRow("dns_queries", -1). |
| 61 | + AddRow("databases", 1). |
| 62 | + AddRow("pools", 0). |
| 63 | + AddRow("users", 2) |
| 64 | + |
| 65 | + mock.ExpectQuery("SHOW LISTS;").WillReturnRows(rows) |
| 66 | + logger := &slog.Logger{} |
| 67 | + |
| 68 | + ch := make(chan prometheus.Metric) |
| 69 | + go func() { |
| 70 | + defer close(ch) |
| 71 | + if err := queryShowLists(ch, db, logger); err != nil { |
| 72 | + t.Errorf("Error running queryShowList: %s", err) |
| 73 | + } |
| 74 | + }() |
| 75 | + |
| 76 | + expected := []MetricResult{ |
| 77 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: -1}, |
| 78 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 1}, |
| 79 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 80 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 2}, |
| 81 | + } |
| 82 | + |
| 83 | + convey.Convey("Metrics comparison", t, func() { |
| 84 | + for _, expect := range expected { |
| 85 | + m := readMetric(<-ch) |
| 86 | + convey.So(expect, convey.ShouldResemble, m) |
| 87 | + } |
| 88 | + }) |
| 89 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 90 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestQueryShowConfig(t *testing.T) { |
| 95 | + db, mock, err := sqlmock.New() |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 98 | + } |
| 99 | + defer db.Close() |
| 100 | + |
| 101 | + rows := sqlmock.NewRows([]string{"key", "value", "default", "changeable"}). |
| 102 | + AddRow("max_client_conn", 1900, 100, true). |
| 103 | + AddRow("max_user_connections", 100, 100, true). |
| 104 | + AddRow("auth_type", "md5", "md5", true). |
| 105 | + AddRow("client_tls_ciphers", "default", "default", "yes") |
| 106 | + |
| 107 | + mock.ExpectQuery("SHOW CONFIG;").WillReturnRows(rows) |
| 108 | + logger := &slog.Logger{} |
| 109 | + |
| 110 | + ch := make(chan prometheus.Metric) |
| 111 | + go func() { |
| 112 | + defer close(ch) |
| 113 | + if err := queryShowConfig(ch, db, logger); err != nil { |
| 114 | + t.Errorf("Error running queryShowConfig: %s", err) |
| 115 | + } |
| 116 | + }() |
| 117 | + |
| 118 | + expected := []MetricResult{ |
| 119 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 1900}, |
| 120 | + {labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 100}, |
| 121 | + } |
| 122 | + convey.Convey("Metrics comparison", t, func() { |
| 123 | + for _, expect := range expected { |
| 124 | + m := readMetric(<-ch) |
| 125 | + convey.So(expect, convey.ShouldResemble, m) |
| 126 | + } |
| 127 | + }) |
| 128 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 129 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 130 | + } |
| 131 | +} |
0 commit comments