github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/build/strategies/sti/usage_test.go (about)

     1  package sti
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/openshift/source-to-image/pkg/api"
     9  	"github.com/openshift/source-to-image/pkg/api/constants"
    10  )
    11  
    12  type FakeUsageHandler struct {
    13  	cleanupCalled  bool
    14  	setupRequired  []string
    15  	setupOptional  []string
    16  	setupError     error
    17  	executeCommand string
    18  	executeUser    string
    19  	executeError   error
    20  }
    21  
    22  type FakeCleaner struct {
    23  	cleanupCalled bool
    24  }
    25  
    26  func (f *FakeCleaner) Cleanup(*api.Config) {
    27  	f.cleanupCalled = true
    28  }
    29  
    30  func (f *FakeUsageHandler) Prepare(*api.Config) error {
    31  	return f.setupError
    32  }
    33  
    34  func (f *FakeUsageHandler) SetScripts(r, o []string) {
    35  	f.setupRequired = r
    36  	f.setupOptional = o
    37  }
    38  
    39  func (f *FakeUsageHandler) Execute(command string, user string, r *api.Config) error {
    40  	f.executeCommand = command
    41  	f.executeUser = user
    42  	return f.executeError
    43  }
    44  
    45  func (f *FakeUsageHandler) Download(*api.Config) error {
    46  	return nil
    47  }
    48  
    49  func newTestUsage() *Usage {
    50  	return &Usage{
    51  		handler: &FakeUsageHandler{},
    52  	}
    53  }
    54  
    55  func TestUsage(t *testing.T) {
    56  	u := newTestUsage()
    57  	g := &FakeCleaner{}
    58  	u.garbage = g
    59  	fh := u.handler.(*FakeUsageHandler)
    60  	err := u.Show()
    61  	if err != nil {
    62  		t.Errorf("Unexpected error returned from Usage: %v", err)
    63  	}
    64  	if !reflect.DeepEqual(fh.setupOptional, []string{}) {
    65  		t.Errorf("setup called with unexpected optional scripts: %#v", fh.setupOptional)
    66  	}
    67  	if !reflect.DeepEqual(fh.setupRequired, []string{constants.Usage}) {
    68  		t.Errorf("setup called with unexpected required scripts: %#v", fh.setupRequired)
    69  	}
    70  	if fh.executeCommand != "usage" {
    71  		t.Errorf("execute called with unexpected command: %#v", fh.executeCommand)
    72  	}
    73  	if !g.cleanupCalled {
    74  		t.Errorf("cleanup was not called from usage.")
    75  	}
    76  }
    77  
    78  func TestUsageSetupError(t *testing.T) {
    79  	u := newTestUsage()
    80  	u.garbage = &FakeCleaner{}
    81  	fh := u.handler.(*FakeUsageHandler)
    82  	fh.setupError = fmt.Errorf("setup error")
    83  	err := u.Show()
    84  	if err != fh.setupError {
    85  		t.Errorf("Unexpected error returned from Usage: %v", err)
    86  	}
    87  	if fh.executeCommand != "" {
    88  		t.Errorf("Execute called when there was a setup error.")
    89  	}
    90  }
    91  
    92  func TestUsageExecuteError(t *testing.T) {
    93  	u := newTestUsage()
    94  	u.garbage = &FakeCleaner{}
    95  	fh := u.handler.(*FakeUsageHandler)
    96  	fh.executeError = fmt.Errorf("execute error")
    97  	err := u.Show()
    98  	if err != fh.executeError {
    99  		t.Errorf("Unexpected error returned from Usage: %v", err)
   100  	}
   101  }