Skip to content

Commit

Permalink
WIP: tests for sol termination
Browse files Browse the repository at this point in the history
not sure bmc/sol_test.go is getting compiled though?
  • Loading branch information
zevweiss committed Dec 20, 2023
1 parent 95ec654 commit e8689e8
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
117 changes: 117 additions & 0 deletions bmc/sol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package bmc

import (
"context"
"errors"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-multierror"
)

type solTermTester struct {
MakeNotOK bool
MakeErrorOut bool
}

func (r *solTermTester) TerminateSOL(ctx context.Context) (ok bool, err error) {
if r.MakeErrorOut {
return ok, errors.New("SOL termination failed")
}
if r.MakeNotOK {
return false, nil
}
return true, nil
}

func (r *solTermTester) Name() string {
return "test provider"
}

func TestTerminateSOL(t *testing.T) {
testCases := map[string]struct {
makeErrorOut bool
makeNotOk bool
want bool
err error
ctxTimeout time.Duration
}{
"success": {want: true},
"not ok return": {want: false, makeNotOk: true, err: &multierror.Error{Errors: []error{errors.New("provider: test provider, failed to terminate SOL session"), errors.New("failed to terminate SOL session")}}},
"error": {want: false, makeErrorOut: true, err: &multierror.Error{Errors: []error{errors.New("provider: test provider: SOL termination failed"), errors.New("failed to terminate SOL session")}}},
"error context timeout": {want: false, makeErrorOut: true, 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, MakeNotOK: tc.makeNotOk}
expectedResult := tc.want
if tc.ctxTimeout == 0 {
tc.ctxTimeout = time.Second * 3
}
ctx, cancel := context.WithTimeout(context.Background(), tc.ctxTimeout)
defer cancel()
result, _, err := terminateSOL(ctx, 0, []bmcProviders{{"test provider", &testImplementation}})

Check failure on line 55 in bmc/sol_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use []bmcProviders{…} (value of type []bmcProviders) as type []terminatorProvider in argument to terminateSOL

Check failure on line 55 in bmc/sol_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use &testImplementation (value of type *solTermTester) as type BMCResetter in struct literal:

Check failure on line 55 in bmc/sol_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &testImplementation (value of type *solTermTester) as BMCResetter value in struct literal: *solTermTester does not implement BMCResetter (missing method BmcReset) (typecheck)
if err != nil {
diff := cmp.Diff(err.Error(), tc.err.Error())
if diff != "" {
t.Fatal(diff)
}
} else {
diff := cmp.Diff(result, expectedResult)
if diff != "" {
t.Fatal(diff)
}
}
})
}
}

func TestTerminateSOLFromInterfaces(t *testing.T) {
testCases := map[string]struct {
err error
badImplementation bool
want bool
withName bool
}{
"success": {want: true},
"success with metadata": {want: true, withName: true},
"no implementations found": {want: false, badImplementation: true, err: &multierror.Error{Errors: []error{errors.New("not an SOLTerminator implementation: *struct {}"), errors.New("no SOLTerminator 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}
}
expectedResult := tc.want
result, metadata, err := TerminateSOLFromInterfaces(context.Background(), 0, generic)
if err != nil {
if tc.err != nil {
diff := cmp.Diff(err.Error(), tc.err.Error())
if diff != "" {
t.Fatal(diff)
}
} else {
t.Fatal(err)
}
} else {
diff := cmp.Diff(result, expectedResult)
if diff != "" {
t.Fatal(diff)
}
}
if tc.withName {
if diff := cmp.Diff(metadata.SuccessfulProvider, "test provider"); diff != "" {
t.Fatal(diff)
}
}
})
}
}
18 changes: 18 additions & 0 deletions providers/ipmitool/ipmitool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ func TestBMCReset(t *testing.T) {
t.Fatal()
}

func TestTerminateSOL(t *testing.T) {
t.Skip("need real ipmi server")
host := "127.0.0.1"
port := "623"
user := "ADMIN"
pass := "ADMIN"
i, err := New(host, user, pass, WithPort(port), WithLogger(logging.DefaultLogger()))
if err != nil {
t.Fatal(err)
}
state, err := i.TerminateSOL(context.Background())

Check failure on line 120 in providers/ipmitool/ipmitool_test.go

View workflow job for this annotation

GitHub Actions / test

assignment mismatch: 2 variables but i.TerminateSOL returns 1 value

Check failure on line 120 in providers/ipmitool/ipmitool_test.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 2 variables but i.TerminateSOL returns 1 value (typecheck)
if err != nil {
t.Fatal(err)
}
t.Log(state)
t.Fatal()
}

func TestSystemEventLogClear(t *testing.T) {
t.Skip("need real ipmi server")
host := "127.0.0.1"
Expand Down

0 comments on commit e8689e8

Please sign in to comment.