Skip to content

Commit

Permalink
Merge aa072da into d4e82cd
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy authored Aug 19, 2022
2 parents d4e82cd + aa072da commit cb3c2bb
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 247 deletions.
32 changes: 2 additions & 30 deletions tests/helper/helper_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ type DevSession struct {
func StartDevMode(envvars []string, opts ...string) (DevSession, []byte, []byte, map[string]string, error) {
args := []string{"dev", "--random-ports"}
args = append(args, opts...)
session := Cmd("odo", args...).AddEnv(envvars...).Runner().session
// session := Cmd("odo", args...).AddEnv(envvars...).Runner().session
session := CmdRunner("odo", args...)
WaitForOutputToContain("Press Ctrl+c to exit `odo dev` and delete resources from the cluster", 360, 10, session)
result := DevSession{
session: session,
Expand Down Expand Up @@ -205,35 +206,6 @@ func RunDevMode(additionalOpts []string, envvars []string, inside func(session *
return nil
}

// DevModeShouldFail runs `odo dev` with an intention to fail, and checks for a given substring
// `odo dev` runs in an infinite reconciliation loop, and hence running it with Cmd will not work for a lot of failing cases,
// this function is helpful in such cases.
// TODO(pvala): Modify StartDevMode to take substring arg into account, and replace this method with it.
func DevModeShouldFail(envvars []string, substring string, opts ...string) (DevSession, []byte, []byte, error) {
args := []string{"dev", "--random-ports"}
args = append(args, opts...)
session := Cmd("odo", args...).AddEnv(envvars...).Runner().session
WaitForOutputToContain(substring, 360, 10, session)
result := DevSession{
session: session,
}
defer func() {
result.Stop()
result.WaitEnd()
}()
outContents := session.Out.Contents()
errContents := session.Err.Contents()
err := session.Out.Clear()
if err != nil {
return DevSession{}, nil, nil, err
}
err = session.Err.Clear()
if err != nil {
return DevSession{}, nil, nil, err
}
return result, outContents, errContents, nil
}

// getPorts returns a map of ports redirected depending on the information in s
// `- Forwarding from 127.0.0.1:40001 -> 3000` will return { "3000": "127.0.0.1:40001" }
func getPorts(s string) map[string]string {
Expand Down
66 changes: 0 additions & 66 deletions tests/integration/cmd_dev_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,72 +122,6 @@ var _ = Describe("odo dev debug command tests", func() {
})
})
})
When("a composite apply command is used as debug command", func() {
deploymentName := "my-component"
var session helper.DevSession
var sessionOut []byte
var err error
var ports map[string]string
const (
DEVFILE_DEBUG_PORT = "5858"
)

BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile-composite-apply-commands.yaml"), filepath.Join(commonVar.Context, "devfile.yaml"))
session, sessionOut, _, ports, err = helper.StartDevMode([]string{"PODMAN_CMD=echo"}, "--debug")
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
session.Stop()
session.WaitEnd()
})
It("should execute the composite apply commands successfully", func() {
checkDeploymentExists := func() {
out := commonVar.CliRunner.Run("get", "deployments", deploymentName).Out.Contents()
Expect(out).To(ContainSubstring(deploymentName))
}
checkImageBuilt := func() {
Expect(string(sessionOut)).To(ContainSubstring("Building & Pushing Container"))
Expect(string(sessionOut)).To(ContainSubstring("build -t quay.io/unknown-account/myimage -f " + filepath.Join(commonVar.Context, "Dockerfile ") + commonVar.Context))
Expect(string(sessionOut)).To(ContainSubstring("push quay.io/unknown-account/myimage"))
}

checkWSConnection := func() {
// 400 response expected because the endpoint expects a websocket request and we are doing a HTTP GET
// We are just using this to validate if nodejs agent is listening on the other side
helper.HttpWaitForWithStatus("http://"+ports[DEVFILE_DEBUG_PORT], "WebSockets request was expected", 12, 5, 400)
}
By("expecting a ws connection when tried to connect on default debug port locally", func() {
checkWSConnection()
})

By("checking is the image was successfully built", func() {
checkImageBuilt()
})

By("checking the deployment was created successfully", func() {
checkDeploymentExists()
})

By("checking odo dev watches correctly", func() {
// making changes to the project again
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js Starter Application", "from the new Node.js Starter Application")
_, _, _, err = session.WaitSync()
Expect(err).ToNot(HaveOccurred())
checkDeploymentExists()
checkImageBuilt()
checkWSConnection()
})

By("cleaning up the resources on ending the session", func() {
session.Stop()
session.WaitEnd()
out := commonVar.CliRunner.Run("get", "deployments").Out.Contents()
Expect(out).ToNot(ContainSubstring(deploymentName))
})
})
})

When("running build and debug commands as composite in different containers and a shared volume", func() {
const devfileCmpName = "nodejs"
Expand Down
151 changes: 0 additions & 151 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -1194,155 +1192,6 @@ var _ = Describe("odo dev command tests", func() {
})
})

Describe("devfile contains composite apply command", func() {
const (
deploymentName = "my-component"
DEVFILEPORT = "3000"
)
var session helper.DevSession
var sessionOut, sessionErr []byte
var err error
var ports map[string]string
BeforeEach(func() {
helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile-composite-apply-commands.yaml"), filepath.Join(commonVar.Context, "devfile.yaml"))
})
When("odo dev is running", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
session, sessionOut, sessionErr, ports, err = helper.StartDevMode([]string{"PODMAN_CMD=echo"})
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
session.Stop()
session.WaitEnd()
})
It("should execute the composite apply commands successfully", func() {
checkDeploymentExists := func() {
out := commonVar.CliRunner.Run("get", "deployments", deploymentName).Out.Contents()
Expect(out).To(ContainSubstring(deploymentName))
}
checkImageBuilt := func() {
Expect(string(sessionOut)).To(ContainSubstring("build -t quay.io/unknown-account/myimage -f " + filepath.Join(commonVar.Context, "Dockerfile ") + commonVar.Context))
Expect(string(sessionOut)).To(ContainSubstring("push quay.io/unknown-account/myimage"))
}
checkEndpointAccessible := func(message []string) {
url := fmt.Sprintf("http://%s", ports[DEVFILEPORT])
resp, e := http.Get(url)
Expect(e).ToNot(HaveOccurred())
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)
helper.MatchAllInOutput(string(body), message)
}
By("checking is the image was successfully built", func() {
checkImageBuilt()
})

By("checking the endpoint accessibility", func() {
checkEndpointAccessible([]string{"Hello from Node.js Starter Application!"})
})

By("checking the deployment was created successfully", func() {
checkDeploymentExists()
})
By("ensuring multiple deployments exist for selector error is not occurred", func() {
Expect(string(sessionErr)).ToNot(ContainSubstring("multiple Deployments exist for the selector"))
})
By("checking odo dev watches correctly", func() {
// making changes to the project again
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js Starter Application", "from the new Node.js Starter Application")
_, _, _, err = session.WaitSync()
Expect(err).ToNot(HaveOccurred())
checkDeploymentExists()
checkImageBuilt()
checkEndpointAccessible([]string{"Hello from the new Node.js Starter Application!"})
})

By("cleaning up the resources on ending the session", func() {
session.Stop()
session.WaitEnd()
out := commonVar.CliRunner.Run("get", "deployments").Out.Contents()
Expect(out).ToNot(ContainSubstring(deploymentName))
})
})
})

Context("the devfile contains an image component that uses a remote Dockerfile", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
})
for _, env := range [][]string{
{"PODMAN_CMD=echo"},
{
"PODMAN_CMD=a-command-not-found-for-podman-should-make-odo-fallback-to-docker",
"DOCKER_CMD=echo",
},
} {
env := env
When(fmt.Sprintf("%v remote server returns a valid file when odo dev is run", env), func() {
var buildRegexp string
var server *httptest.Server
var url string

BeforeEach(func() {
buildRegexp = regexp.QuoteMeta("build -t quay.io/unknown-account/myimage -f ") +
".*\\.dockerfile " + regexp.QuoteMeta(commonVar.Context)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `# Dockerfile
FROM node:8.11.1-alpine
COPY . /app
WORKDIR /app
RUN npm install
CMD ["npm", "start"]
`)
}))
url = server.URL

helper.ReplaceString(filepath.Join(commonVar.Context, "devfile.yaml"), "./Dockerfile", url)
session, sessionOut, _, ports, err = helper.StartDevMode(env)
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
session.Stop()
session.WaitEnd()
server.Close()
})

It("should build and push image when odo dev is run", func() {
lines, _ := helper.ExtractLines(string(sessionOut))
_, ok := helper.FindFirstElementIndexMatchingRegExp(lines, buildRegexp)
Expect(ok).To(BeTrue(), "build regexp not found in output: "+buildRegexp)
Expect(string(sessionOut)).To(ContainSubstring("push quay.io/unknown-account/myimage"))
})
})
When(fmt.Sprintf("%v remote server returns an error when odo dev is run", env), func() {
var server *httptest.Server
var url string
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
url = server.URL

helper.ReplaceString(filepath.Join(commonVar.Context, "devfile.yaml"), "./Dockerfile", url)
})

AfterEach(func() {
server.Close()
})

It("should not build images when odo dev is run", func() {
_, sessionOut, _, err := helper.DevModeShouldFail(env, "failed to retrieve "+url)
Expect(err).To(BeNil())
Expect(sessionOut).NotTo(ContainSubstring("build -t quay.io/unknown-account/myimage -f "))
Expect(sessionOut).NotTo(ContainSubstring("push quay.io/unknown-account/myimage"))
})
})
}
})
})

When("running odo dev and devfile with composite command", func() {
devfileCmpName := "nodejs"
var session helper.DevSession
Expand Down

0 comments on commit cb3c2bb

Please sign in to comment.