-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_test.go
More file actions
84 lines (74 loc) · 1.95 KB
/
timeout_test.go
File metadata and controls
84 lines (74 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"context"
"testing"
"time"
hclog "github.com/hashicorp/go-hclog"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInitialize_Timeout(t *testing.T) {
tests := []struct {
name string
config map[string]interface{}
expectedTimeout time.Duration
expectError bool
}{
{
name: "default timeout",
config: map[string]interface{}{},
expectedTimeout: 20 * time.Second,
},
{
name: "custom timeout string",
config: map[string]interface{}{
"timeout": "5s",
},
expectedTimeout: 5 * time.Second,
},
{
name: "invalid timeout too small",
config: map[string]interface{}{
"timeout": "500ms",
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newCmd()
req := dbplugin.InitializeRequest{
Config: tt.config,
}
_, err := c.Initialize(context.Background(), req)
if tt.expectError {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expectedTimeout, c.Timeout)
}
})
}
}
func TestExecuteScript_Timeout(t *testing.T) {
// Skip on windows as sleep command might differ or shell might differ
// But getShell handles it. "timeout" command is not standard on windows.
// We'll use "sleep" which exists on unix.
c := newCmd()
c.Logger = hclog.NewNullLogger()
c.Timeout = 100 * time.Millisecond
// Case 1: Command takes longer than timeout
// sleep 1 should take 1s, which is > 100ms
ctx := context.Background()
script := "sleep 1"
params := map[string]string{}
err := c.executeScript(ctx, script, params)
require.Error(t, err)
assert.Contains(t, err.Error(), "script execution timed out")
// Case 2: Command finishes within timeout
c.Timeout = 2 * time.Second
script = "echo hello"
err = c.executeScript(ctx, script, params)
require.NoError(t, err)
}