Path: blob/main/component/prometheus/exporter/mssql/mssql_test.go
4096 views
package mssql12import (3"testing"4"time"56"github.com/grafana/agent/pkg/integrations/mssql"7"github.com/grafana/agent/pkg/river"8"github.com/grafana/agent/pkg/river/rivertypes"9config_util "github.com/prometheus/common/config"10"github.com/stretchr/testify/require"11)1213func TestRiverUnmarshal(t *testing.T) {14riverConfig := `15connection_string = "sqlserver://user:pass@localhost:1433"16max_idle_connections = 317max_open_connections = 318timeout = "10s"19`2021var args Arguments22err := river.Unmarshal([]byte(riverConfig), &args)23require.NoError(t, err)2425expected := Arguments{26ConnectionString: rivertypes.Secret("sqlserver://user:pass@localhost:1433"),27MaxIdleConnections: 3,28MaxOpenConnections: 3,29Timeout: 10 * time.Second,30}3132require.Equal(t, expected, args)33}3435func TestUnmarshalInvalid(t *testing.T) {36invalidRiverConfig := `37connection_string = "sqlserver://user:pass@localhost:1433"38max_idle_connections = 139max_open_connections = 140timeout = "-1s"41`4243var invalidArgs Arguments44err := river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs)45require.Error(t, err)46}4748func TestArgumentsValidate(t *testing.T) {49tests := []struct {50name string51args Arguments52wantErr bool53}{54{55name: "invalid max open connections",56args: Arguments{57ConnectionString: rivertypes.Secret("test"),58MaxIdleConnections: 1,59MaxOpenConnections: 0,60Timeout: 10 * time.Second,61},62wantErr: true,63},64{65name: "invalid max idle connections",66args: Arguments{67ConnectionString: rivertypes.Secret("test"),68MaxIdleConnections: 0,69MaxOpenConnections: 1,70Timeout: 10 * time.Second,71},72wantErr: true,73},74{75name: "invalid timeout",76args: Arguments{77ConnectionString: rivertypes.Secret("test"),78MaxIdleConnections: 1,79MaxOpenConnections: 1,80Timeout: 0,81},82wantErr: true,83},84{85name: "valid",86args: Arguments{87ConnectionString: rivertypes.Secret("test"),88MaxIdleConnections: 1,89MaxOpenConnections: 1,90Timeout: 10 * time.Second,91},92wantErr: false,93},94}9596for _, tt := range tests {97t.Run(tt.name, func(t *testing.T) {98err := tt.args.Validate()99if tt.wantErr {100require.Error(t, err)101} else {102require.NoError(t, err)103}104})105}106}107108func TestConvert(t *testing.T) {109riverConfig := `110connection_string = "sqlserver://user:pass@localhost:1433"111`112var args Arguments113err := river.Unmarshal([]byte(riverConfig), &args)114require.NoError(t, err)115116res := args.Convert()117118expected := mssql.Config{119ConnectionString: config_util.Secret("sqlserver://user:pass@localhost:1433"),120MaxIdleConnections: DefaultArguments.MaxIdleConnections,121MaxOpenConnections: DefaultArguments.MaxOpenConnections,122Timeout: DefaultArguments.Timeout,123}124require.Equal(t, expected, *res)125}126127128