1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-16 11:09:33 +02:00
sap-jenkins-library/cmd/batsExecuteTests_test.go
Mikalai Dzemidzenka b82ecb0ff7
convert batsExecuteTests to go implementation (#2737)
* convert batsExecuteTests to go implementation

* added additional test cases, added container definition to batsExecuteTests.yaml

* added influx, for junit added container definition

* added parameter envVars

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-04-29 16:50:23 +02:00

116 lines
3.3 KiB
Go

package cmd
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/go-git/go-git/v5"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type batsExecuteTestsMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func (b batsExecuteTestsMockUtils) CloneRepo(URL string) error {
if URL != "https://github.com/bats-core/bats-core.git" {
return git.ErrRepositoryNotExists
}
return nil
}
func newBatsExecuteTestsTestsUtils() batsExecuteTestsMockUtils {
utils := batsExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunBatsExecuteTests(t *testing.T) {
t.Parallel()
t.Run("success case", func(t *testing.T) {
t.Parallel()
config := &batsExecuteTestsOptions{
OutputFormat: "junit",
Repository: "https://github.com/bats-core/bats-core.git",
TestPackage: "piper-bats",
TestPath: "src/test",
}
mockUtils := newBatsExecuteTestsTestsUtils()
err := runBatsExecuteTests(config, nil, &mockUtils)
assert.NoError(t, err)
assert.True(t, mockUtils.HasFile("TEST-"+config.TestPackage+".tap"))
assert.True(t, mockUtils.HasFile("TEST-"+config.TestPackage+".xml"))
})
t.Run("output tap case", func(t *testing.T) {
t.Parallel()
config := &batsExecuteTestsOptions{
OutputFormat: "tap",
Repository: "https://github.com/bats-core/bats-core.git",
TestPackage: "piper-bats",
TestPath: "src/test",
}
mockUtils := newBatsExecuteTestsTestsUtils()
err := runBatsExecuteTests(config, nil, &mockUtils)
assert.NoError(t, err)
assert.True(t, mockUtils.HasFile("TEST-"+config.TestPackage+".tap"))
assert.False(t, mockUtils.HasFile("TEST-"+config.TestPackage+".xml"))
})
t.Run("output format failed case", func(t *testing.T) {
t.Parallel()
config := &batsExecuteTestsOptions{
OutputFormat: "fail",
Repository: "https://github.com/bats-core/bats-core.git",
TestPackage: "piper-bats",
TestPath: "src/test",
}
mockUtils := newBatsExecuteTestsTestsUtils()
err := runBatsExecuteTests(config, nil, &mockUtils)
assert.EqualError(t, err, "output format 'fail' is incorrect. Possible drivers: tap, junit")
})
t.Run("failed to clone repo case", func(t *testing.T) {
t.Parallel()
config := &batsExecuteTestsOptions{
OutputFormat: "junit",
Repository: "fail",
TestPackage: "piper-bats",
TestPath: "src/test",
}
mockUtils := newBatsExecuteTestsTestsUtils()
err := runBatsExecuteTests(config, nil, &mockUtils)
expectedError := fmt.Errorf("couldn't pull %s repository: %w", config.Repository, git.ErrRepositoryNotExists)
assert.EqualError(t, err, expectedError.Error())
})
t.Run("failed to run bats case", func(t *testing.T) {
t.Parallel()
config := &batsExecuteTestsOptions{
OutputFormat: "tap",
Repository: "https://github.com/bats-core/bats-core.git",
TestPackage: "piper-bats",
TestPath: "src/test",
}
mockUtils := batsExecuteTestsMockUtils{
ExecMockRunner: &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"bats-core/bin/bats": errors.New("error case")}},
FilesMock: &mock.FilesMock{},
}
runBatsExecuteTests(config, nil, &mockUtils)
assert.False(t, mockUtils.HasFile("TEST-"+config.TestPackage+".tap"))
assert.False(t, mockUtils.HasFile("TEST-"+config.TestPackage+".xml"))
})
}