|
| 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 | + "errors" |
| 16 | + "github.com/google/go-cmp/cmp" |
| 17 | + "io/fs" |
| 18 | + "maps" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestDefaultConfig(t *testing.T) { |
| 24 | + |
| 25 | + config := NewDefaultConfig() |
| 26 | + |
| 27 | + MustConnectOnStartupWant := true |
| 28 | + if config.MustConnectOnStartup != MustConnectOnStartupWant { |
| 29 | + t.Errorf("MustConnectOnStartup does not match. Want: %v, Got: %v", MustConnectOnStartupWant, config.MustConnectOnStartup) |
| 30 | + } |
| 31 | + |
| 32 | + MetricsPathWant := "/metrics" |
| 33 | + if config.MetricsPath != MetricsPathWant { |
| 34 | + t.Errorf("MustConnectOnStartup does not match. Want: %v, Got: %v", MetricsPathWant, config.MustConnectOnStartup) |
| 35 | + } |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func TestUnHappyFileConfig(t *testing.T) { |
| 40 | + |
| 41 | + var config *Config |
| 42 | + var err error |
| 43 | + |
| 44 | + config, err = NewConfigFromFile("") |
| 45 | + if config != nil || err != nil { |
| 46 | + t.Errorf("NewConfigFromFile should return nil for config and error if path is empty. Got: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + _, err = NewConfigFromFile("./testdata/i-do-not-exist.yaml") |
| 50 | + if errors.Is(err, fs.ErrNotExist) == false { |
| 51 | + t.Errorf("NewConfigFromFile should return fs.ErrNotExist error. Got: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + _, err = NewConfigFromFile("./testdata/parse_error.yaml") |
| 55 | + if err != nil && strings.Contains(err.Error(), "yaml: line") == false { |
| 56 | + t.Errorf("NewConfigFromFile should return yaml parse error. Got: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + _, err = NewConfigFromFile("./testdata/empty.yaml") |
| 60 | + if errors.Is(err, ErrNoPgbouncersConfigured) == false { |
| 61 | + t.Errorf("NewConfigFromFile should return ErrNoPgbouncersConfigured error. Got: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + _, err = NewConfigFromFile("./testdata/no-dsn.yaml") |
| 65 | + if errors.Is(err, ErrEmptyPgbouncersDSN) == false { |
| 66 | + t.Errorf("NewConfigFromFile should return ErrEmptyPgbouncersDSN error. Got: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +func TestFileConfig(t *testing.T) { |
| 72 | + |
| 73 | + var config *Config |
| 74 | + var err error |
| 75 | + |
| 76 | + config, err = NewConfigFromFile("./testdata/config.yaml") |
| 77 | + if err != nil { |
| 78 | + t.Errorf("NewConfigFromFile() should not throw an error: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + MustConnectOnStartupWant := false |
| 82 | + if config.MustConnectOnStartup != MustConnectOnStartupWant { |
| 83 | + t.Errorf("MustConnectOnStartup does not match. Want: %v, Got: %v", MustConnectOnStartupWant, config.MustConnectOnStartup) |
| 84 | + } |
| 85 | + |
| 86 | + MetricsPathWant := "/prom" |
| 87 | + if config.MetricsPath != MetricsPathWant { |
| 88 | + t.Errorf("MustConnectOnStartup does not match. Want: %v, Got: %v", MetricsPathWant, config.MustConnectOnStartup) |
| 89 | + } |
| 90 | + |
| 91 | + CommonExtraLabelsWant := map[string]string{"environment": "sandbox"} |
| 92 | + if maps.Equal(config.ExtraLabels, CommonExtraLabelsWant) == false { |
| 93 | + t.Errorf("ExtraLabels does not match. Want: %v, Got: %v", CommonExtraLabelsWant, config.ExtraLabels) |
| 94 | + } |
| 95 | + |
| 96 | + pgWants := []PgBouncerConfig{ |
| 97 | + { |
| 98 | + DSN: "postgres://postgres:@localhost:6543/pgbouncer?sslmode=disable", |
| 99 | + PidFile: "/var/run/pgbouncer1.pid", |
| 100 | + ExtraLabels: map[string]string{"pgbouncer_instance": "set1-0", "environment": "prod"}, |
| 101 | + }, |
| 102 | + { |
| 103 | + DSN: "postgres://postgres:@localhost:6544/pgbouncer?sslmode=disable", |
| 104 | + PidFile: "/var/run/pgbouncer2.pid", |
| 105 | + ExtraLabels: map[string]string{"pgbouncer_instance": "set1-1", "environment": "prod"}, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + for i := range pgWants { |
| 110 | + if cmp.Equal(config.PgBouncers[i], pgWants[i]) == false { |
| 111 | + t.Errorf("PGBouncer config %d does not match. Want: %v, Got: %v", i, pgWants[i], config.PgBouncers[i]) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + err = config.ValidateLabels() |
| 116 | + if err != nil { |
| 117 | + t.Errorf("ValidateLabels() throws an unexpected error: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +func TestNotUniqueLabels(t *testing.T) { |
| 123 | + |
| 124 | + config := NewDefaultConfig() |
| 125 | + |
| 126 | + config.AddPgbouncerConfig("", "", map[string]string{"pgbouncer_instance": "set1-0", "environment": "prod"}) |
| 127 | + config.AddPgbouncerConfig("", "", map[string]string{"pgbouncer_instance": "set1-0", "environment": "prod"}) |
| 128 | + |
| 129 | + err := config.ValidateLabels() |
| 130 | + if err == nil { |
| 131 | + t.Errorf("ValidateLabels() did not throw an error ") |
| 132 | + } |
| 133 | + errorWant := "Every pgbouncer instance must have unique label values, found the following label=value combination multiple times: 'environment=prod,pgbouncer_instance=set1-0'" |
| 134 | + if err.Error() != errorWant { |
| 135 | + t.Errorf("ValidateLabels() did not throw the expected error.\n- Want: %s\n- Got: %s", errorWant, err.Error()) |
| 136 | + } |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +func TestMissingLabels(t *testing.T) { |
| 141 | + |
| 142 | + config := NewDefaultConfig() |
| 143 | + |
| 144 | + config.AddPgbouncerConfig("", "", map[string]string{"pgbouncer_instance": "set1-0", "environment": "prod"}) |
| 145 | + config.AddPgbouncerConfig("", "", map[string]string{"pgbouncer_instance": "set1-0"}) |
| 146 | + |
| 147 | + err := config.ValidateLabels() |
| 148 | + if err == nil { |
| 149 | + t.Errorf("ValidateLabels() did not throw an error ") |
| 150 | + } |
| 151 | + errorWant := "Every pgbouncer instance must define the same extra labels, the label 'environment' is only found on 1 of the 2 instances" |
| 152 | + if err.Error() != errorWant { |
| 153 | + t.Errorf("ValidateLabels() did not throw the expected error.\n- Want: %s\n- Got: %s", errorWant, err.Error()) |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments