1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/hadolintExecute_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

96 lines
3.4 KiB
Go

//go:build unit
// +build unit
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).
On("WriteFile", "hadolintExecute_reports.json", mock.Anything, mock.Anything).Return(nil).
On("WriteFile", "hadolintExecute_links.json", mock.Anything, mock.Anything).Return(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).
On("WriteFile", "hadolintExecute_reports.json", mock.Anything, mock.Anything).Return(nil).
On("WriteFile", "hadolintExecute_links.json", mock.Anything, mock.Anything).Return(nil)
//m.On("Do", MatchedBy(func(req *http.Request) bool { return req.Host == "example.com" }))
// 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)
})
}