Path: blob/main/pkg/integrations/mssql/sql_exporter_test.go
5330 views
package mssql12import (3"os"4"testing"5"time"67"github.com/go-kit/log"8"github.com/stretchr/testify/require"9"gopkg.in/yaml.v2"10)1112func TestConfig_validate(t *testing.T) {13testCases := []struct {14name string15input Config16err string17}{18{19name: "valid config",20input: Config{21ConnectionString: "sqlserver://user:pass@localhost:1433",22MaxIdleConnections: 3,23MaxOpenConnections: 3,24Timeout: 10 * time.Second,25},26},27{28name: "incorrect connection_string scheme",29input: Config{30ConnectionString: "mysql://user:pass@localhost:1433",31MaxIdleConnections: 3,32MaxOpenConnections: 3,33Timeout: 10 * time.Second,34},35err: "scheme of provided connection_string URL must be sqlserver",36},37{38name: "invalid URL",39input: Config{40ConnectionString: "\u0001",41MaxIdleConnections: 3,42MaxOpenConnections: 3,43Timeout: 10 * time.Second,44},45err: "failed to parse connection_string",46},47{48name: "missing connection_string",49input: Config{50MaxIdleConnections: 3,51MaxOpenConnections: 3,52Timeout: 10 * time.Second,53},54err: "the connection_string parameter is required",55},56{57name: "max connections is less than 1",58input: Config{59ConnectionString: "sqlserver://user:pass@localhost:1433",60MaxIdleConnections: 3,61MaxOpenConnections: 0,62Timeout: 10 * time.Second,63},64err: "max_connections must be at least 1",65},66{67name: "max idle connections is less than 1",68input: Config{69ConnectionString: "sqlserver://user:pass@localhost:1433",70MaxIdleConnections: 0,71MaxOpenConnections: 3,72Timeout: 10 * time.Second,73},74err: "max_idle_connection must be at least 1",75},76{77name: "timeout is not positive",78input: Config{79ConnectionString: "sqlserver://user:pass@localhost:1433",80MaxIdleConnections: 3,81MaxOpenConnections: 3,82Timeout: 0,83},84err: "timeout must be positive",85},86}8788for _, tc := range testCases {89t.Run(tc.name, func(t *testing.T) {90err := tc.input.validate()91if tc.err == "" {92require.NoError(t, err)93} else {94require.Error(t, err)95require.ErrorContains(t, err, tc.err)96}97})98}99}100func TestConfig_UnmarshalYaml(t *testing.T) {101t.Run("only required values", func(t *testing.T) {102strConfig := `connection_string: "sqlserver://user:pass@localhost:1433"`103104var c Config105106require.NoError(t, yaml.UnmarshalStrict([]byte(strConfig), &c))107108require.Equal(t, Config{109ConnectionString: "sqlserver://user:pass@localhost:1433",110MaxIdleConnections: 3,111MaxOpenConnections: 3,112Timeout: 10 * time.Second,113}, c)114})115116t.Run("all values", func(t *testing.T) {117strConfig := `118connection_string: "sqlserver://user:pass@localhost:1433"119max_idle_connections: 5120max_open_connections: 6121timeout: 1m122`123124var c Config125126require.NoError(t, yaml.UnmarshalStrict([]byte(strConfig), &c))127128require.Equal(t, Config{129ConnectionString: "sqlserver://user:pass@localhost:1433",130MaxIdleConnections: 5,131MaxOpenConnections: 6,132Timeout: time.Minute,133}, c)134})135}136137func TestConfig_NewIntegration(t *testing.T) {138t.Run("integration with valid config", func(t *testing.T) {139c := &Config{140ConnectionString: "sqlserver://user:pass@localhost:1433",141MaxIdleConnections: 3,142MaxOpenConnections: 3,143Timeout: 10 * time.Second,144}145146i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout))147require.NoError(t, err)148require.NotNil(t, i)149})150151t.Run("integration with invalid config", func(t *testing.T) {152c := &Config{153ConnectionString: "mysql://user:pass@localhost:1433",154MaxIdleConnections: 3,155MaxOpenConnections: 3,156Timeout: 10 * time.Second,157}158159i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout))160require.Nil(t, i)161require.ErrorContains(t, err, "failed to validate config:")162})163}164165func TestConfig_AgentKey(t *testing.T) {166t.Run("valid url", func(t *testing.T) {167c := Config{168ConnectionString: "mssql://user:pass@localhost:1433",169MaxIdleConnections: 3,170MaxOpenConnections: 3,171Timeout: 10 * time.Second,172}173174ik, err := c.InstanceKey("agent-key")175176require.NoError(t, err)177require.Equal(t, "localhost:1433", ik)178})179180t.Run("invalid url", func(t *testing.T) {181c := Config{182ConnectionString: "\u0001",183MaxIdleConnections: 3,184MaxOpenConnections: 3,185Timeout: 10 * time.Second,186}187188_, err := c.InstanceKey("agent-key")189190require.Error(t, err)191require.ErrorContains(t, err, "failed to parse connection string URL")192})193}194195196