1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-16 11:09:33 +02:00
sap-jenkins-library/cmd/hadolintExecute_test.go
Christopher Fenner 81c8553d6a
refactor(hadolint): implement step in GO (#1169)
* initial commit of yaml file

* initial commit for HaDoLint in GO

* add helper function to load file from url

* load config file

* write report information to disk

* comment the code

* refactor groovy code

* remove download function from FileUtils

* use http.Downloader

* rename step files

* update generated files

* update generated files

* remove duplicate commands

* add credentials for config url

* add generated test file

* reuse piperExecuteBin functions

* correct step name

* update go step

* deactivate test

* fix import

* use differing go step name

* rename step

* correct result publishing

* correct command name

* expose tls insecure flag

* hand through error

* disable tls verification

* fix tls disabling

* use credentials

* mow

* reformat

* add qgate only if set

* correct report name

* remove old defaults

* add qgate to defaults

* handle report name

* restore default

* remove unused step config

* use piperExecuteBin

* remove obsolete type

* add test cases

* remove groovy tests

* move client parameter handling to run function

* use custom interfaces and mockery

* remove commented code

* correct struct names

* rename parameter dockerfile

* add further asserts

* cleanup

* change file permission to read/write

* remove tokenize

* add further comments

* init http client only if necessary

* add todo

* Revert "rename parameter dockerfile"

This reverts commit 2a570685b8.

* add alias for dockerfile parameter

* correct test case

* Apply suggestions from code review

Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>

* add comment about mock assertions

Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
2020-11-16 15:14:54 +01:00

87 lines
2.9 KiB
Go

package cmd
import (
"testing"
"github.com/SAP/jenkins-library/pkg/hadolint/mocks"
piperMocks "github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRunHadolintExecute(t *testing.T) {
t.Run("default", func(t *testing.T) {
// init
fileMock := &mocks.HadolintPiperFileUtils{}
clientMock := &mocks.HadolintClient{}
runnerMock := &piperMocks.ExecMockRunner{}
config := hadolintExecuteOptions{
DockerFile: "./Dockerfile", // default
ConfigurationFile: ".hadolint.yaml", // default
}
fileMock.
On("FileExists", config.ConfigurationFile).Return(false, nil)
// test
err := runHadolint(config, hadolintUtils{
HadolintPiperFileUtils: fileMock,
HadolintClient: clientMock,
hadolintRunner: runnerMock,
})
// assert
assert.NoError(t, err)
if assert.Len(t, runnerMock.Calls, 1) {
assert.Equal(t, "hadolint", runnerMock.Calls[0].Exec)
assert.Contains(t, runnerMock.Calls[0].Params, config.DockerFile)
assert.Contains(t, runnerMock.Calls[0].Params, "--format")
assert.Contains(t, runnerMock.Calls[0].Params, "checkstyle")
assert.NotContains(t, runnerMock.Calls[0].Params, "--config")
assert.NotContains(t, runnerMock.Calls[0].Params, config.ConfigurationFile)
}
// assert that mocks are called as previously defined
fileMock.AssertExpectations(t)
clientMock.AssertExpectations(t)
})
t.Run("with remote config", func(t *testing.T) {
// init
fileMock := &mocks.HadolintPiperFileUtils{}
clientMock := &mocks.HadolintClient{}
runnerMock := &piperMocks.ExecMockRunner{}
config := hadolintExecuteOptions{
DockerFile: "./Dockerfile", // default
ConfigurationFile: ".hadolint.yaml", // default
ConfigurationURL: "https://myconfig",
}
clientMock.
On("SetOptions", mock.Anything).
On("DownloadFile", config.ConfigurationURL, config.ConfigurationFile, mock.Anything, mock.Anything).Return(nil)
fileMock.
// checks if config exists before downloading
On("FileExists", config.ConfigurationFile).Return(false, nil).Once().
// checks again but config is now downloaded
On("FileExists", config.ConfigurationFile).Return(true, nil)
// test
err := runHadolint(config, hadolintUtils{
HadolintPiperFileUtils: fileMock,
HadolintClient: clientMock,
hadolintRunner: runnerMock,
})
// assert
assert.NoError(t, err)
if assert.Len(t, runnerMock.Calls, 1) {
assert.Equal(t, "hadolint", runnerMock.Calls[0].Exec)
assert.Contains(t, runnerMock.Calls[0].Params, config.DockerFile)
assert.Contains(t, runnerMock.Calls[0].Params, "--format")
assert.Contains(t, runnerMock.Calls[0].Params, "checkstyle")
assert.Contains(t, runnerMock.Calls[0].Params, "--config")
assert.Contains(t, runnerMock.Calls[0].Params, config.ConfigurationFile)
}
// assert that mocks are called as previously defined
fileMock.AssertExpectations(t)
clientMock.AssertExpectations(t)
})
}