From 288790dba5b8c534126a8f109b3f35c0a93b3045 Mon Sep 17 00:00:00 2001 From: Zev Weiss Date: Wed, 20 Dec 2023 00:50:25 -0800 Subject: [PATCH] Add tests for SOL deactivation --- bmc/sol_test.go | 99 +++++++++++++++++++++++++++++ providers/ipmitool/ipmitool_test.go | 18 ++++++ 2 files changed, 117 insertions(+) create mode 100644 bmc/sol_test.go diff --git a/bmc/sol_test.go b/bmc/sol_test.go new file mode 100644 index 00000000..1623a027 --- /dev/null +++ b/bmc/sol_test.go @@ -0,0 +1,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) + } + } + }) + } +} diff --git a/providers/ipmitool/ipmitool_test.go b/providers/ipmitool/ipmitool_test.go index 241503f1..90f563e8 100644 --- a/providers/ipmitool/ipmitool_test.go +++ b/providers/ipmitool/ipmitool_test.go @@ -107,6 +107,24 @@ func TestBMCReset(t *testing.T) { t.Fatal() } +func TestDeactivateSOL(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) + } + err = i.DeactivateSOL(context.Background()) + if err != nil { + t.Fatal(err) + } + t.Log(err != nil) + t.Fatal() +} + func TestSystemEventLogClear(t *testing.T) { t.Skip("need real ipmi server") host := "127.0.0.1"