This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit d38a3f004106f8b883bb4fe8dc6dd052313c87ff Author: Tadayoshi Sato <[email protected]> AuthorDate: Tue Mar 8 15:18:01 2022 +0900 test(e2e): improve local e2e tests --- e2e/local/local_build_test.go | 14 ++---------- e2e/local/local_run_test.go | 23 ++++++++++++-------- e2e/local/util.go | 50 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/e2e/local/local_build_test.go b/e2e/local/local_build_test.go index 9e8bbed..8388d83 100644 --- a/e2e/local/local_build_test.go +++ b/e2e/local/local_build_test.go @@ -24,7 +24,6 @@ import ( "context" "fmt" "io" - "os/exec" "strings" "testing" @@ -40,15 +39,6 @@ import ( // TODO: read version for the Camel catalog var camelVersion = "3.14.1" -func getDockerImages() []byte { - cmd := exec.CommandContext(TestContext, "docker", "images") - out, err := cmd.Output() - if err != nil { - panic(err) - } - return out -} - func TestLocalBuild(t *testing.T) { RegisterTestingT(t) @@ -75,7 +65,7 @@ func TestLocalBuild(t *testing.T) { Eventually(logScanner.IsFound(msgTagged), TestTimeoutMedium).Should(BeTrue()) Eventually(logScanner.IsFound(image), TestTimeoutMedium).Should(BeTrue()) - Eventually(getDockerImages, TestTimeoutShort).Should(ContainSubstring(image)) + Eventually(DockerImages, TestTimeoutShort).Should(ContainSubstring(image)) } func TestLocalBuildWithTrait(t *testing.T) { @@ -106,7 +96,7 @@ func TestLocalBuildWithTrait(t *testing.T) { Eventually(logScanner.IsFound(msgWarning), TestTimeoutMedium).Should(BeTrue()) Eventually(logScanner.IsFound(msgTagged), TestTimeoutMedium).Should(BeTrue()) Eventually(logScanner.IsFound(image), TestTimeoutMedium).Should(BeTrue()) - Eventually(getDockerImages, TestTimeoutMedium).Should(ContainSubstring(image)) + Eventually(DockerImages, TestTimeoutMedium).Should(ContainSubstring(image)) } func dependency(dir string, jar string, params ...interface{}) string { diff --git a/e2e/local/local_run_test.go b/e2e/local/local_run_test.go index 4a8b1a0..86d7391 100644 --- a/e2e/local/local_run_test.go +++ b/e2e/local/local_run_test.go @@ -23,13 +23,15 @@ package local import ( "context" "io" + "strings" "testing" . "github.com/onsi/gomega" "github.com/stretchr/testify/assert" . "github.com/apache/camel-k/e2e/support" - "github.com/apache/camel-k/e2e/support/util" + testutil "github.com/apache/camel-k/e2e/support/util" + "github.com/apache/camel-k/pkg/util" ) func TestLocalRun(t *testing.T) { @@ -41,12 +43,12 @@ func TestLocalRun(t *testing.T) { defer pipew.Close() defer piper.Close() - file := util.MakeTempCopy(t, "files/yaml.yaml") + file := testutil.MakeTempCopy(t, "files/yaml.yaml") kamelRun := KamelWithContext(ctx, "local", "run", file) kamelRun.SetOut(pipew) - logScanner := util.NewLogScanner(ctx, piper, "Magicstring!") + logScanner := testutil.NewLogScanner(ctx, piper, "Magicstring!") go func() { err := kamelRun.Execute() @@ -66,13 +68,15 @@ func TestLocalRunContainerize(t *testing.T) { defer pipew.Close() defer piper.Close() - file := util.MakeTempCopy(t, "files/yaml.yaml") + file := testutil.MakeTempCopy(t, "files/yaml.yaml") + image := "test/test-" + strings.ToLower(util.RandomString(10)) - kamelRun := KamelWithContext(ctx, "local", "run", file, "--image", "test/test", "--containerize") + kamelRun := KamelWithContext(ctx, "local", "run", file, "--image", image, "--containerize") kamelRun.SetOut(pipew) - logScanner := util.NewLogScanner(ctx, piper, "Magicstring!") + logScanner := testutil.NewLogScanner(ctx, piper, "Magicstring!") + defer StopDockerContainers() go func() { err := kamelRun.Execute() assert.NoError(t, err) @@ -80,6 +84,7 @@ func TestLocalRunContainerize(t *testing.T) { }() Eventually(logScanner.IsFound("Magicstring!"), TestTimeoutMedium).Should(BeTrue()) + Eventually(DockerImages, TestTimeoutShort).Should(ContainSubstring(image)) } func TestLocalRunIntegrationDirectory(t *testing.T) { @@ -88,8 +93,8 @@ func TestLocalRunIntegrationDirectory(t *testing.T) { ctx1, cancel1 := context.WithCancel(TestContext) defer cancel1() - file := util.MakeTempCopy(t, "files/yaml.yaml") - dir := util.MakeTempDir(t) + file := testutil.MakeTempCopy(t, "files/yaml.yaml") + dir := testutil.MakeTempDir(t) kamelBuild := KamelWithContext(ctx1, "local", "build", file, "--integration-directory", dir) @@ -112,7 +117,7 @@ func TestLocalRunIntegrationDirectory(t *testing.T) { kamelRun := KamelWithContext(ctx2, "local", "run", "--integration-directory", dir) kamelRun.SetOut(pipew) - logScanner := util.NewLogScanner(ctx2, piper, "Magicstring!") + logScanner := testutil.NewLogScanner(ctx2, piper, "Magicstring!") go func() { err := kamelRun.Execute() diff --git a/e2e/local/util.go b/e2e/local/util.go new file mode 100644 index 0000000..1cbd7d0 --- /dev/null +++ b/e2e/local/util.go @@ -0,0 +1,50 @@ +//go:build integration +// +build integration + +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package local + +import ( + "os/exec" + "strings" + + . "github.com/apache/camel-k/e2e/support" +) + +func Docker(args ...string) string { + cmd := exec.CommandContext(TestContext, "docker", args...) + out, err := cmd.Output() + if err != nil { + panic(err) + } + return string(out) +} + +func DockerImages() string { + return Docker("images") +} + +func StopDockerContainers() { + output := Docker("container", "list", "--quiet") + containers := strings.Fields(output) + if len(containers) > 0 { + args := append([]string{"stop"}, containers...) + Docker(args...) + } +}
