Skip to content

Commit

Permalink
Moved from direct curl invocation to tools.Curl; added DumpTestInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
hash-d committed Dec 2, 2022
1 parent 15463a5 commit 0ea7a9e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
55 changes: 51 additions & 4 deletions test/integration/examples/custom/helloworld/helloworld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,16 @@ func TestHelloWorldCLI(t *testing.T) {
}},
{Ctx: prv, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-frontend:8080",
Target: "http://hello-world-frontend:8080",
Silent: true,
ShowError: true,
Verbose: true,
},
&cli.CurlTester{
Target: "http://hello-world-backend:8080/api/hello",
Target: "http://hello-world-backend:8080/api/hello",
Silent: true,
ShowError: true,
Verbose: true,
},
}},
},
Expand Down Expand Up @@ -420,24 +426,36 @@ func TestHelloWorldCLI(t *testing.T) {
Tasks: []cli.SkupperTask{
{Ctx: prv, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
ExpectFail: true, // this is a standard k8s service, so before exposure it does not exist on pub
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: prv, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-frontend-k8s-service:8080",
ExpectFail: true, // this is a standard k8s service, so before exposure it does not exist on pub
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-frontend-k8s-service:8080",
Target: "http://hello-world-frontend-k8s-service:8080",
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
Expand Down Expand Up @@ -482,27 +500,39 @@ func TestHelloWorldCLI(t *testing.T) {
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: prv, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-frontend-k8s-service:8080",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
&cli.CurlTester{
Target: "http://hello-world-frontend-k8s-service:8080",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
},
Expand Down Expand Up @@ -549,6 +579,9 @@ func TestHelloWorldCLI(t *testing.T) {
Target: "http://hello-world-backend-k8s-service:8080/api/hello",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
Expand All @@ -557,6 +590,9 @@ func TestHelloWorldCLI(t *testing.T) {
ExpectFail: true, // this was a standard k8s service, so after unexpose it should not exist on pub
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: prv, Commands: []cli.SkupperCommandTester{
Expand All @@ -565,6 +601,9 @@ func TestHelloWorldCLI(t *testing.T) {
ExpectFail: true, // this was a standard k8s service, so after unexpose it should not exist on prv
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
{Ctx: pub, Commands: []cli.SkupperCommandTester{
Expand All @@ -574,6 +613,9 @@ func TestHelloWorldCLI(t *testing.T) {
Target: "http://hello-world-frontend-k8s-service:8080",
Interval: time.Second,
MaxRetries: 100,
Silent: true,
ShowError: true,
Verbose: true,
},
}},
},
Expand Down Expand Up @@ -613,6 +655,11 @@ func TestHelloWorldCLI(t *testing.T) {
// Running the scenarios
cli.RunScenarios(t, scenarios)

if t.Failed() {
pub.DumpTestInfo("hello_world")
prv.DumpTestInfo("hello_world")
}

}

// deployResources Deploys the hello-world-frontend and hello-world-backend
Expand Down
61 changes: 52 additions & 9 deletions test/utils/skupper/cli/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/skupperproject/skupper/pkg/utils"
"github.com/skupperproject/skupper/test/utils/base"
"github.com/skupperproject/skupper/test/utils/tools"
)

// CurlTester is not a true SkupperCommandTester, as it does not call Skupper.
Expand All @@ -17,39 +18,81 @@ import (
type CurlTester struct {
Target string
ExpectFail bool
Interval time.Duration
Silent bool
ShowError bool
Verbose bool
MaxRetries int
// Retry interval. Default is 1s
Interval time.Duration
// Individual curl invocation timeout in seconds. If not given, the default is 10
TimeOut int
// Possible improvements: search output, use CA, cert, key files
}

func (c *CurlTester) Command(cluster *base.ClusterContext) []string {
return []string{"exec -c router deployment/skupper-router -- curl -fsSvk ", c.Target}
// The curl command is created on tools.Curl()
return []string{}
}

// As this calls cluster.KubectlExec, stdout and stderr will be together in the stdout
// return value. stderr is always empty.
// As this uses util.Curl, headers, body and other info will be together in the stdout
// return value.
func (c *CurlTester) Run(cluster *base.ClusterContext) (stdout string, stderr string, err error) {
// TODO replace this by test/utils/tools/curl.go?
var out []byte
var resp *tools.CurlResponse
var count int
var works string
if c.ExpectFail {
works = "does not work"
} else {
works = "works"
}
utils.RetryError(c.Interval, c.MaxRetries, func() error {
var interval = c.Interval
if interval == 0 {
interval = time.Second
}
var timeout = c.TimeOut
if timeout == 0 {
timeout = 10
}
var cOpts = tools.CurlOpts{
Timeout: timeout,
ShowError: c.ShowError,
Silent: c.Silent,
Verbose: c.Verbose,
}
log.Printf("Running: curl %v", c)
// utils.RetryError does not allow its MaxRetries == 0, and it is actually
// the number of tries, not retries
var tries = c.MaxRetries + 1
utils.RetryError(interval, tries, func() error {
count++
log.Printf("Validating url %s %s - attempt %d", c.Target, works, count)
out, err = cluster.KubectlExec(fmt.Sprintf(strings.Join(c.Command(cluster), " ")))
resp, err = tools.Curl(cluster.VanClient.KubeClient, cluster.VanClient.RestConfig, cluster.VanClient.Namespace, "", c.Target, cOpts)
log.Printf("curl returned HTTP response %d (%v)", resp.StatusCode, err)
if c.ExpectFail {
if err != nil {
if err != nil || resp.StatusCode >= 400 {
err = nil
} else {
err = fmt.Errorf("expected error on curl operation, but it succeeded")
}
} else {
if resp.StatusCode >= 400 {
err = fmt.Errorf("HTTP error: %d", resp.StatusCode)
}
}
return err
})
return string(out), "", err
out := fmt.Sprintf(
"- HTTP Version: %s\n- Reason: %s\n- Headers:\n%s\n- Body\n%s",
resp.HttpVersion,
strings.TrimSpace(resp.ReasonPhrase),
resp.Headers,
resp.Body,
)
if base.IsVerboseCommandOutput() || err != nil {
fmt.Printf("RESULT:\n%v\n", out)
fmt.Printf("STDERR:\n%v\n", resp.Output)
fmt.Printf("Error: %v\n", err)
}
return string(out), resp.Output, err
}
18 changes: 13 additions & 5 deletions test/utils/tools/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (

// CurlOpts allows specifying arguments to run curl on a pod
type CurlOpts struct {
Silent bool
Insecure bool
Username string
Password string
Timeout int
Silent bool
Insecure bool
Username string
Password string
Timeout int
ShowError bool
Verbose bool
}

// ToParams returns curl options serialized as a string slice
Expand All @@ -32,6 +34,12 @@ func (c *CurlOpts) ToParams() []string {
if c.Silent {
params = append(params, "-s")
}
if c.ShowError {
params = append(params, "-S")
}
if c.Verbose {
params = append(params, "-v")
}
if c.Insecure {
params = append(params, "-k")
}
Expand Down

0 comments on commit 0ea7a9e

Please sign in to comment.