Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#57 Run all tests in directory #58

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"regexp"
"strings"
"text/template"
Expand All @@ -41,6 +43,26 @@ import (
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

const FileSuffix = ".feature"

type testError struct {
errors map[string]error
}

func (e *testError) Error() string {
message := "Test suite failed with following errors:\n"
for k, v := range e.errors {
message += fmt.Sprintf("%s: %s\n", k, v)
}
return message
}

func newTestError(errs map[string]error) *testError {
var t testError
t.errors = errs
return &t
}

func newCmdTest(rootCmdOptions *RootCmdOptions) *cobra.Command {
options := testCmdOptions{
RootCmdOptions: rootCmdOptions,
Expand All @@ -51,7 +73,6 @@ func newCmdTest(rootCmdOptions *RootCmdOptions) *cobra.Command {
Use: "test [options] [test file to execute]",
Short: "Execute a test on Kubernetes",
Long: `Deploys and execute a pod on Kubernetes for running tests.`,
PreRunE: options.validateArgs,
RunE: options.run,
SilenceUsage: true,
}
Expand All @@ -72,14 +93,6 @@ type testCmdOptions struct {
env []string
}

func (o *testCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New(fmt.Sprintf("accepts exactly 1 test name to execute, received %d", len(args)))
}

return nil
}

func (o *testCmdOptions) run(_ *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
Expand All @@ -94,14 +107,53 @@ func (o *testCmdOptions) run(_ *cobra.Command, args []string) error {
o.dependencies = append(o.dependencies, additionalDep)
}

_, err = o.createTest(c, args)
return err
errors := make(map[string]error)
logError := func(source string, err error) {
if err == nil {
return
}
errors[source] = err
}

execute := func(name string) {
_, err := o.createTest(c, name)
logError(name, err)
}

for _, source := range args {
if isRemoteFile(source) {
execute(source)
continue
}

if info, err := os.Stat(source); err != nil {
logError(source, err)
} else {
if !info.IsDir() {
execute(source)
} else {
if files, err := ioutil.ReadDir(source); err != nil {
logError(source, err)
} else {
for _, f := range files {
if strings.HasSuffix(f.Name(), FileSuffix) {
execute(path.Join(info.Name(), f.Name()))
}
}
}
}
}
}

if len(errors) > 0 {
return newTestError(errors)
} else {
return nil
}
}

func (o *testCmdOptions) createTest(c client.Client, sources []string) (*v1alpha1.Test, error) {
func (o *testCmdOptions) createTest(c client.Client, rawName string) (*v1alpha1.Test, error) {
namespace := o.Namespace

rawName := sources[0]
fileName := kubernetes.SanitizeFileName(rawName)
name := kubernetes.SanitizeName(rawName)

Expand Down Expand Up @@ -275,11 +327,15 @@ func (o *testCmdOptions) printLogs(ctx context.Context, name string) error {
return nil
}

func isRemoteFile(fileName string) bool {
return strings.HasPrefix(fileName, "http://") || strings.HasPrefix(fileName, "https://")
}

func (*testCmdOptions) loadData(fileName string) (string, error) {
var content []byte
var err error

if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") {
if !isRemoteFile(fileName) {
content, err = ioutil.ReadFile(fileName)
if err != nil {
return "", err
Expand Down