| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "errors" |
| "testing" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| "go.chromium.org/luci/luciexe/invoke" |
| ) |
| |
| type successfulStepRunner struct{} |
| |
| func (sr *successfulStepRunner) runStep(ctx context.Context, name string, fn stepFunc) error { |
| return nil |
| } |
| |
| func (sr *successfulStepRunner) invoke(ctx context.Context, exeArgs []string, opts *invoke.Options) error { |
| return nil |
| } |
| |
| type failingStepRunner struct{} |
| |
| func (sr *failingStepRunner) runStep(ctx context.Context, name string, fn stepFunc) error { |
| return errors.New("step failed") |
| } |
| |
| func (sr *failingStepRunner) invoke(ctx context.Context, exeArgs []string, opts *invoke.Options) error { |
| return errors.New("invoke failed") |
| } |
| |
| func mockStepFunc(context.Context, *buildbucketpb.Build) error { |
| return nil |
| } |
| |
| func TestRunStep(t *testing.T) { |
| t.Parallel() |
| |
| var tests = []struct { |
| name string |
| fn stepFunc |
| sr stepRunner |
| expectedRunStepErr bool |
| expectedInvokeErr bool |
| }{ |
| { |
| "successful step", |
| mockStepFunc, |
| &successfulStepRunner{}, |
| false, |
| false, |
| }, |
| { |
| "failing step", |
| mockStepFunc, |
| &failingStepRunner{}, |
| true, |
| true, |
| }, |
| } |
| |
| for _, test := range tests { |
| ctx := context.Background() |
| err := test.sr.runStep(ctx, test.name, test.fn) |
| if err == nil { |
| if test.expectedRunStepErr { |
| t.Fatalf("expected error, got nil") |
| } |
| } else if !test.expectedRunStepErr { |
| t.Fatalf("got unexpected error %v", err) |
| } |
| err = test.sr.invoke(ctx, []string{}, &invoke.Options{}) |
| if err == nil { |
| if test.expectedInvokeErr { |
| t.Fatalf("expected error, got nil") |
| } |
| } else if !test.expectedInvokeErr { |
| t.Fatalf("got unexpected error %v", err) |
| } |
| } |
| } |