github.com/jaylevin/jenkins-library@v1.230.4/cmd/shellExecute_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  )
    12  
    13  type shellExecuteMockUtils struct {
    14  	t      *testing.T
    15  	config *shellExecuteOptions
    16  	*mock.ExecMockRunner
    17  	*mock.FilesMock
    18  	*mock.HttpClientMock
    19  	downloadError error
    20  	filename      string
    21  	header        http.Header
    22  	url           string
    23  }
    24  
    25  type shellExecuteFileMock struct {
    26  	*mock.FilesMock
    27  	fileReadContent map[string]string
    28  	fileReadErr     map[string]error
    29  }
    30  
    31  func (f *shellExecuteFileMock) FileRead(path string) ([]byte, error) {
    32  	if f.fileReadErr[path] != nil {
    33  		return []byte{}, f.fileReadErr[path]
    34  	}
    35  	return []byte(f.fileReadContent[path]), nil
    36  }
    37  
    38  func (f *shellExecuteFileMock) FileExists(path string) (bool, error) {
    39  	return strings.EqualFold(path, "path/to/script/script.sh"), nil
    40  }
    41  
    42  func (f *shellExecuteMockUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
    43  	if f.downloadError != nil {
    44  		return f.downloadError
    45  	}
    46  	f.url = url
    47  	f.filename = filename
    48  	f.header = header
    49  	return nil
    50  }
    51  
    52  func newShellExecuteTestsUtils() *shellExecuteMockUtils {
    53  	utils := shellExecuteMockUtils{
    54  		ExecMockRunner: &mock.ExecMockRunner{},
    55  		FilesMock:      &mock.FilesMock{},
    56  	}
    57  	return &utils
    58  }
    59  
    60  func (v *shellExecuteMockUtils) GetConfig() *shellExecuteOptions {
    61  	return v.config
    62  }
    63  
    64  func TestRunShellExecute(t *testing.T) {
    65  
    66  	t.Run("negative case - script isn't present", func(t *testing.T) {
    67  		c := &shellExecuteOptions{
    68  			Sources: []string{"path/to/script.sh"},
    69  		}
    70  		u := newShellExecuteTestsUtils()
    71  
    72  		err := runShellExecute(c, nil, u)
    73  		assert.EqualError(t, err, "the script 'path/to/script.sh' could not be found")
    74  	})
    75  
    76  	t.Run("success case - script run successfully", func(t *testing.T) {
    77  		o := &shellExecuteOptions{
    78  			Sources: []string{"path/script.sh"},
    79  		}
    80  
    81  		u := newShellExecuteTestsUtils()
    82  		u.AddFile("path/script.sh", []byte(`echo dummy`))
    83  
    84  		err := runShellExecute(o, nil, u)
    85  		assert.Equal(t, "path/script.sh", u.ExecMockRunner.Calls[0].Exec)
    86  		assert.Equal(t, []string{}, u.ExecMockRunner.Calls[0].Params)
    87  		assert.NoError(t, err)
    88  	})
    89  
    90  	t.Run("success case - download script header gets added", func(t *testing.T) {
    91  		o := &shellExecuteOptions{
    92  			Sources:     []string{"https://myScriptLocation/myScript.sh"},
    93  			GithubToken: "dummy@12345",
    94  		}
    95  		u := newShellExecuteTestsUtils()
    96  
    97  		runShellExecute(o, nil, u)
    98  
    99  		assert.Equal(t, http.Header{"Accept": []string{"application/vnd.github.v3.raw"}, "Authorization": []string{"Token dummy@12345"}}, u.header)
   100  	})
   101  
   102  	t.Run("success case - single positional script arguments gets added to the correct script", func(t *testing.T) {
   103  		o := &shellExecuteOptions{
   104  			Sources:         []string{"path1/script1.sh", "path2/script2.sh"},
   105  			ScriptArguments: []string{"arg1", "arg2"},
   106  		}
   107  
   108  		u := newShellExecuteTestsUtils()
   109  		u.AddFile("path1/script1.sh", []byte(`echo dummy1`))
   110  		u.AddFile("path2/script2.sh", []byte(`echo dummy2`))
   111  
   112  		err := runShellExecute(o, nil, u)
   113  
   114  		assert.Equal(t, "path1/script1.sh", u.ExecMockRunner.Calls[0].Exec)
   115  		assert.Equal(t, []string{"arg1"}, u.ExecMockRunner.Calls[0].Params)
   116  		assert.Equal(t, "path2/script2.sh", u.ExecMockRunner.Calls[1].Exec)
   117  		assert.Equal(t, []string{"arg2"}, u.ExecMockRunner.Calls[1].Params)
   118  		assert.NoError(t, err)
   119  	})
   120  
   121  	t.Run("success case - multiple positional script arguments gets added to the correct script", func(t *testing.T) {
   122  		o := &shellExecuteOptions{
   123  			Sources:         []string{"path1/script1.sh", "path2/script2.sh"},
   124  			ScriptArguments: []string{"arg1 arg2", "arg3 arg4"},
   125  		}
   126  
   127  		u := newShellExecuteTestsUtils()
   128  		u.AddFile("path1/script1.sh", []byte(`echo dummy1`))
   129  		u.AddFile("path2/script2.sh", []byte(`echo dummy2`))
   130  
   131  		err := runShellExecute(o, nil, u)
   132  
   133  		assert.Equal(t, "path1/script1.sh", u.ExecMockRunner.Calls[0].Exec)
   134  		assert.Equal(t, []string{"arg1", "arg2"}, u.ExecMockRunner.Calls[0].Params)
   135  		assert.Equal(t, "path2/script2.sh", u.ExecMockRunner.Calls[1].Exec)
   136  		assert.Equal(t, []string{"arg3", "arg4"}, u.ExecMockRunner.Calls[1].Params)
   137  		assert.NoError(t, err)
   138  	})
   139  }