Skip to content

Commit

Permalink
Merge pull request #50 from nicolaferraro/exit-code
Browse files Browse the repository at this point in the history
fix: return non-zero exit code when test fails
  • Loading branch information
christophd authored Feb 28, 2020
2 parents ce0df08 + d2eb9de commit ccb0c24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
3 changes: 0 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"context"
"fmt"
"math/rand"
"os"
"time"
Expand All @@ -46,8 +45,6 @@ func main() {

func exitOnError(err error) {
if err != nil {
fmt.Println("Error:", err)

os.Exit(1)
}
}
9 changes: 9 additions & 0 deletions pkg/apis/yaks/v1alpha1/test_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -89,6 +91,13 @@ const (
TestPhaseDeleting TestPhase = "Deleting"
)

func (phase TestPhase) AsError() error {
if phase != TestPhasePassed {
return fmt.Errorf("Test %s", string(phase))
}
return nil
}

type Language string

const (
Expand Down
26 changes: 16 additions & 10 deletions pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"context"
"fmt"
"io/ioutil"
"k8s.io/apimachinery/pkg/labels"
"net/http"
"regexp"
"strings"
"text/template"
"time"

"k8s.io/apimachinery/pkg/labels"

"github.com/fatih/color"
"github.com/jboss-fuse/yaks/pkg/apis/yaks/v1alpha1"
"github.com/jboss-fuse/yaks/pkg/client"
Expand All @@ -52,18 +53,19 @@ func newCmdTest(rootCmdOptions *RootCmdOptions) *cobra.Command {
Long: `Deploys and execute a pod on Kubernetes for running tests.`,
PreRunE: options.validateArgs,
RunE: options.run,
SilenceUsage: true,
}

cmd.Flags().StringVarP(&options.dependencies, "dependencies", "d","", "Adds runtime dependencies that get automatically loaded before the test is executed.")
cmd.Flags().StringVarP(&options.settings, "settings", "s","", "Path to runtime settings file. File content is added to the test runtime and can hold runtime dependency information for instance.")
cmd.Flags().StringVarP(&options.dependencies, "dependencies", "d", "", "Adds runtime dependencies that get automatically loaded before the test is executed.")
cmd.Flags().StringVarP(&options.settings, "settings", "s", "", "Path to runtime settings file. File content is added to the test runtime and can hold runtime dependency information for instance.")

return &cmd
}

type testCmdOptions struct {
*RootCmdOptions
dependencies string
settings string
settings string
}

func (o *testCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -166,29 +168,33 @@ func (o *testCmdOptions) createTest(c client.Client, sources []string) (*v1alpha
}

ctx, cancel := context.WithCancel(o.Context)
var status v1alpha1.TestPhase = "Unknown"
go func() {
status := "Unknown"
err = kubernetes.WaitCondition(o.Context, c, &test, func(obj interface{}) (bool, error) {
if val, ok := obj.(*v1alpha1.Test); ok {
if val.Status.Phase == v1alpha1.TestPhaseDeleting ||
val.Status.Phase == v1alpha1.TestPhaseError ||
val.Status.Phase == v1alpha1.TestPhasePassed ||
val.Status.Phase == v1alpha1.TestPhaseFailed {
status = string(val.Status.Phase)
status = val.Status.Phase
return true, nil
}
}
return false, nil
}, 10*time.Minute)

cancel()
fmt.Printf("Test result: %s\n", status)
}()

if err := o.printLogs(ctx, name); err != nil {
return nil, err
}

err = status.AsError()
if err != nil {
return nil, err
}
fmt.Printf("Test %s\n", string(status))
return &test, nil
}

Expand All @@ -197,7 +203,7 @@ func (o *testCmdOptions) newTestSettings() (*v1alpha1.SettingsSpec, error) {

if runtimeDependencies != "" {
settings := v1alpha1.SettingsSpec{
Content: runtimeDependencies,
Content: runtimeDependencies,
}
return &settings, nil
}
Expand All @@ -215,8 +221,8 @@ func (o *testCmdOptions) newTestSettings() (*v1alpha1.SettingsSpec, error) {
}

settings := v1alpha1.SettingsSpec{
Name: settingsFileName,
Content: configData,
Name: settingsFileName,
Content: configData,
}

return &settings, nil
Expand Down

0 comments on commit ccb0c24

Please sign in to comment.