Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/prometheus/exporter/mssql/mssql_test.go
4096 views
1
package mssql
2
3
import (
4
"testing"
5
"time"
6
7
"github.com/grafana/agent/pkg/integrations/mssql"
8
"github.com/grafana/agent/pkg/river"
9
"github.com/grafana/agent/pkg/river/rivertypes"
10
config_util "github.com/prometheus/common/config"
11
"github.com/stretchr/testify/require"
12
)
13
14
func TestRiverUnmarshal(t *testing.T) {
15
riverConfig := `
16
connection_string = "sqlserver://user:pass@localhost:1433"
17
max_idle_connections = 3
18
max_open_connections = 3
19
timeout = "10s"
20
`
21
22
var args Arguments
23
err := river.Unmarshal([]byte(riverConfig), &args)
24
require.NoError(t, err)
25
26
expected := Arguments{
27
ConnectionString: rivertypes.Secret("sqlserver://user:pass@localhost:1433"),
28
MaxIdleConnections: 3,
29
MaxOpenConnections: 3,
30
Timeout: 10 * time.Second,
31
}
32
33
require.Equal(t, expected, args)
34
}
35
36
func TestUnmarshalInvalid(t *testing.T) {
37
invalidRiverConfig := `
38
connection_string = "sqlserver://user:pass@localhost:1433"
39
max_idle_connections = 1
40
max_open_connections = 1
41
timeout = "-1s"
42
`
43
44
var invalidArgs Arguments
45
err := river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs)
46
require.Error(t, err)
47
}
48
49
func TestArgumentsValidate(t *testing.T) {
50
tests := []struct {
51
name string
52
args Arguments
53
wantErr bool
54
}{
55
{
56
name: "invalid max open connections",
57
args: Arguments{
58
ConnectionString: rivertypes.Secret("test"),
59
MaxIdleConnections: 1,
60
MaxOpenConnections: 0,
61
Timeout: 10 * time.Second,
62
},
63
wantErr: true,
64
},
65
{
66
name: "invalid max idle connections",
67
args: Arguments{
68
ConnectionString: rivertypes.Secret("test"),
69
MaxIdleConnections: 0,
70
MaxOpenConnections: 1,
71
Timeout: 10 * time.Second,
72
},
73
wantErr: true,
74
},
75
{
76
name: "invalid timeout",
77
args: Arguments{
78
ConnectionString: rivertypes.Secret("test"),
79
MaxIdleConnections: 1,
80
MaxOpenConnections: 1,
81
Timeout: 0,
82
},
83
wantErr: true,
84
},
85
{
86
name: "valid",
87
args: Arguments{
88
ConnectionString: rivertypes.Secret("test"),
89
MaxIdleConnections: 1,
90
MaxOpenConnections: 1,
91
Timeout: 10 * time.Second,
92
},
93
wantErr: false,
94
},
95
}
96
97
for _, tt := range tests {
98
t.Run(tt.name, func(t *testing.T) {
99
err := tt.args.Validate()
100
if tt.wantErr {
101
require.Error(t, err)
102
} else {
103
require.NoError(t, err)
104
}
105
})
106
}
107
}
108
109
func TestConvert(t *testing.T) {
110
riverConfig := `
111
connection_string = "sqlserver://user:pass@localhost:1433"
112
`
113
var args Arguments
114
err := river.Unmarshal([]byte(riverConfig), &args)
115
require.NoError(t, err)
116
117
res := args.Convert()
118
119
expected := mssql.Config{
120
ConnectionString: config_util.Secret("sqlserver://user:pass@localhost:1433"),
121
MaxIdleConnections: DefaultArguments.MaxIdleConnections,
122
MaxOpenConnections: DefaultArguments.MaxOpenConnections,
123
Timeout: DefaultArguments.Timeout,
124
}
125
require.Equal(t, expected, *res)
126
}
127
128