forked from bmc-toolbox/bmclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol_test.go
99 lines (90 loc) · 2.73 KB
/
sol_test.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package bmc
import (
"context"
"errors"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-multierror"
)
type solTermTester struct {
MakeErrorOut bool
}
func (r *solTermTester) DeactivateSOL(ctx context.Context) (err error) {
if r.MakeErrorOut {
return errors.New("SOL deactivation failed")
}
return nil
}
func (r *solTermTester) Name() string {
return "test provider"
}
func TestDeactivateSOL(t *testing.T) {
testCases := map[string]struct {
makeErrorOut bool
err error
ctxTimeout time.Duration
}{
"success": {makeErrorOut: false},
"error": {makeErrorOut: true, err: &multierror.Error{Errors: []error{errors.New("provider: test provider: SOL deactivation failed"), errors.New("failed to deactivate SOL session")}}},
"error context timeout": {makeErrorOut: false, err: &multierror.Error{Errors: []error{errors.New("context deadline exceeded")}}, ctxTimeout: time.Nanosecond * 1},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
testImplementation := solTermTester{MakeErrorOut: tc.makeErrorOut}
if tc.ctxTimeout == 0 {
tc.ctxTimeout = time.Second * 3
}
ctx, cancel := context.WithTimeout(context.Background(), tc.ctxTimeout)
defer cancel()
_, err := deactivateSOL(ctx, 0, []deactivatorProvider{{"test provider", &testImplementation}})
var diff string
if err != nil && tc.err != nil {
diff = cmp.Diff(err.Error(), tc.err.Error())
} else {
diff = cmp.Diff(err, tc.err)
}
if diff != "" {
t.Fatal(diff)
}
})
}
}
func TestDeactivateSOLFromInterfaces(t *testing.T) {
testCases := map[string]struct {
err error
badImplementation bool
withName bool
}{
"success": {},
"success with metadata": {withName: true},
"no implementations found": {badImplementation: true, err: &multierror.Error{Errors: []error{errors.New("not an SOLDeactivator implementation: *struct {}"), errors.New("no SOLDeactivator implementations found")}}},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
var generic []interface{}
if tc.badImplementation {
badImplementation := struct{}{}
generic = []interface{}{&badImplementation}
} else {
testImplementation := solTermTester{}
generic = []interface{}{&testImplementation}
}
metadata, err := DeactivateSOLFromInterfaces(context.Background(), 0, generic)
var diff string
if err != nil && tc.err != nil {
diff = cmp.Diff(err.Error(), tc.err.Error())
} else {
diff = cmp.Diff(err, tc.err)
}
if diff != "" {
t.Fatal(diff)
}
if tc.withName {
if diff := cmp.Diff(metadata.SuccessfulProvider, "test provider"); diff != "" {
t.Fatal(diff)
}
}
})
}
}