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

Implement HTTP Server based on OpenAPI spec #6835

Merged
merged 7 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
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
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() {
feloy marked this conversation as resolved.
Show resolved Hide resolved
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))
})
})
})
}
}
})