Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi committed Jun 17, 2023
1 parent b02bd18 commit 442ea10
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
37 changes: 30 additions & 7 deletions tests/helper/helper_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"regexp"
"strings"
"time"

"github.com/ActiveState/termtest/expect"
Expand Down Expand Up @@ -110,13 +111,14 @@ import (
*/

type DevSession struct {
session *gexec.Session
stopped bool
console *expect.Console
address string
StdOut string
ErrOut string
Endpoints map[string]string
session *gexec.Session
stopped bool
console *expect.Console
address string
StdOut string
ErrOut string
Endpoints map[string]string
APIServerEndpoint string
}

type DevSessionOpts struct {
Expand All @@ -128,6 +130,8 @@ type DevSessionOpts struct {
NoWatch bool
NoCommands bool
CustomAddress string
StartAPIServer bool
APIServerPort int
}

// StartDevMode starts a dev session with `odo dev`
Expand Down Expand Up @@ -156,6 +160,12 @@ func StartDevMode(options DevSessionOpts) (devSession DevSession, err error) {
if options.CustomAddress != "" {
args = append(args, "--address", options.CustomAddress)
}
if options.StartAPIServer {
args = append(args, "--api-server")
if options.APIServerPort != 0 {
args = append(args, "--api-server-port", fmt.Sprintf("%d", options.APIServerPort))
}
}
args = append(args, options.CmdlineArgs...)
cmd := Cmd("odo", args...)
cmd.Cmd.Stdin = c.Tty()
Expand Down Expand Up @@ -186,6 +196,10 @@ func StartDevMode(options DevSessionOpts) (devSession DevSession, err error) {
result.StdOut = string(outContents)
result.ErrOut = string(errContents)
result.Endpoints = getPorts(string(outContents), options.CustomAddress)
if options.StartAPIServer {
// errContents because the server message is still printed as a log/warning
result.APIServerEndpoint = getAPIServerPort(string(errContents))
}
return result, nil

}
Expand Down Expand Up @@ -358,3 +372,12 @@ func getPorts(s, address string) map[string]string {
}
return result
}

// getAPIServerPort returns the address at which api server is running
//
// `I0617 11:40:44.124391 49578 starterserver.go:36] API Server started at localhost:20000/api/v1`
func getAPIServerPort(s string) string {
re := regexp.MustCompile(`(API Server started at localhost:[0-9]+\/api\/v1)`)
matches := re.FindString(s)
return strings.Split(matches, "at ")[1]
}
73 changes: 73 additions & 0 deletions tests/integration/cmd_dev_api_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package integration

import (
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-developer/odo/tests/helper"
"net/http"
"path/filepath"
)

var _ = Describe("odo dev command with api server tests", func() {
var cmpName string
var commonVar helper.CommonVar

// This is run before every Spec (It)
var _ = BeforeEach(func() {
commonVar = helper.CommonBeforeEach()
cmpName = helper.RandString(6)
helper.Chdir(commonVar.Context)
Expect(helper.VerifyFileExists(".odo/env/env.yaml")).To(BeFalse())
})

// This is run after every Spec (It)
var _ = AfterEach(func() {
helper.CommonAfterEach(commonVar)
})
for _, podman := range []bool{false, true} {
podman := podman
for _, customPort := range []bool{false, true} {
customPort := customPort
When("the component is bootstrapped", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile.yaml"), filepath.Join(commonVar.Context, "devfile.yaml"), cmpName)
})
When(fmt.Sprintf("odo dev is run with --api-server flag (custom api server port=%v)", customPort), func() {
var (
devSession helper.DevSession
localPort = helper.GetCustomStartPort()
)
BeforeEach(func() {
opts := helper.DevSessionOpts{
RunOnPodman: podman,
StartAPIServer: true,
EnvVars: []string{"ODO_EXPERIMENTAL_MODE=true"},
}
if customPort {
opts.APIServerPort = localPort
}
var err error
devSession, err = helper.StartDevMode(opts)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
devSession.Stop()
devSession.WaitEnd()
})
It("should start the Dev server when --api-server flag is passed", func() {
if customPort {
Expect(devSession.APIServerEndpoint).To(ContainSubstring(fmt.Sprintf("%d", localPort)))
}
url := fmt.Sprintf("http://%s/instance", devSession.APIServerEndpoint)
resp, err := http.Get(url)
Expect(err).ToNot(HaveOccurred())
// TODO: Change this once it is implemented
Expect(resp.StatusCode).To(BeEquivalentTo(http.StatusNotImplemented))
})
})
})
}
}
})

0 comments on commit 442ea10

Please sign in to comment.