Path: blob/main/pkg/integrations/postgres_exporter/postgres_exporter_test.go
5304 views
package postgres_exporter //nolint:golint12import (3"testing"45"github.com/stretchr/testify/require"6"gopkg.in/yaml.v2"7)89func Test_ParsePostgresURL(t *testing.T) {10dsn := "postgresql://linus:42secret@localhost:5432/postgres?sslmode=disable"11expected := map[string]string{12"dbname": "postgres",13"host": "localhost",14"password": "42secret",15"port": "5432",16"sslmode": "disable",17"user": "linus",18}1920actual, err := parsePostgresURL(dsn)21require.NoError(t, err)22require.Equal(t, actual, expected)23}2425func Test_getDataSourceNames(t *testing.T) {26tt := []struct {27name string28config string29env string30expect []string31}{32{33name: "env",34config: "{}",35env: "foo",36expect: []string{"foo"},37},38{39name: "multi-env",40config: "{}",41env: "foo,bar",42expect: []string{"foo", "bar"},43},44{45name: "config",46config: `{47"data_source_names": [48"foo"49]50}`,51env: "",52expect: []string{"foo"},53},54{55name: "config and env",56config: `{57"data_source_names": [58"foo"59]60}`,61env: "bar",62expect: []string{"foo"},63},64}6566for _, tc := range tt {67t.Run(tc.name, func(t *testing.T) {68t.Setenv("POSTGRES_EXPORTER_DATA_SOURCE_NAME", tc.env)6970var cfg Config71err := yaml.Unmarshal([]byte(tc.config), &cfg)72require.NoError(t, err)7374res, err := cfg.getDataSourceNames()75require.NoError(t, err)76require.Equal(t, tc.expect, res)77})78}79}808182