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

feat(controller/cli): improve handling of invalid components & dependencies against Camel catalog #3640

Merged
merged 11 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 28 additions & 1 deletion e2e/global/common/integration_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,38 @@ func TestBadRouteIntegration(t *testing.T) {
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("run invalid dependency java route", func(t *testing.T) {
RegisterTestingT(t)
name := "invalid-dependency"
Expect(KamelRunWithID(operatorID, ns, "files/Java.java", "--name", name,
"-d", "camel:non-existent").Execute()).To(Succeed())
// Integration in error with Initialization Failed condition
Eventually(IntegrationPhase(ns, name), TestTimeoutLong).Should(Equal(v1.IntegrationPhaseError))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionFalse))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionInitializationFailedReason)),
WithTransform(IntegrationConditionMessage, HavePrefix("error during trait customization")),
))
// Kit shouldn't be created
Consistently(IntegrationKit(ns, name), 10*time.Second).Should(BeEmpty())

// Fixing the route should reconcile the Integration in Initialization Failed condition to Running
Expect(KamelRunWithID(operatorID, ns, "files/Java.java", "--name", name).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))

// Clean up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("run unresolvable component java route", func(t *testing.T) {
RegisterTestingT(t)
name := "unresolvable-route"
Expect(KamelRunWithID(operatorID, ns, "files/Unresolvable.java", "--name", name).Execute()).To(Succeed())
// Integration in error
// Integration in error with Initialization Failed condition
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionFalse))
Expand Down
40 changes: 40 additions & 0 deletions e2e/local/local_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"io"
"strings"
"sync"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -113,6 +114,45 @@ func TestLocalBuildWithTrait(t *testing.T) {
Eventually(DockerImages, TestTimeoutMedium).Should(ContainSubstring(image))
}

func TestLocalBuildWithInvalidDependency(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := testutil.MakeTempCopy(t, "files/yaml.yaml")
image := "test/test-" + strings.ToLower(util.RandomString(10))

kamelBuild := KamelWithContext(ctx, "local", "build", file, "--image", image,
"-d", "camel-xxx",
"-d", "mvn:org.apache.camel:camel-http:3.18.0",
"-d", "mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0")
kamelBuild.SetOut(pipew)
kamelBuild.SetErr(pipew)

warn1 := "Warning: dependency camel:xxx not found in Camel catalog"
warn2 := "Warning: do not use mvn:org.apache.camel:camel-http:3.18.0. Use camel:http instead"
warn3 := "Warning: do not use mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0. Use camel:netty instead"
logScanner := testutil.NewLogScanner(ctx, piper, warn1, warn2, warn3)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := kamelBuild.Execute()
assert.Error(t, err)
cancel()
}()

Eventually(logScanner.IsFound(warn1), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn2), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn3), TestTimeoutShort).Should(BeTrue())
wg.Wait()
}

func TestLocalBuildIntegrationDirectory(t *testing.T) {
RegisterTestingT(t)

Expand Down
108 changes: 108 additions & 0 deletions e2e/local/local_inspect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//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 (
"context"
"io"
"testing"

. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

. "github.com/apache/camel-k/e2e/support"
testutil "github.com/apache/camel-k/e2e/support/util"
)

func TestLocalInspect(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := testutil.MakeTempCopy(t, "files/yaml.yaml")

kamelInspect := KamelWithContext(ctx, "local", "inspect", file)
kamelInspect.SetOut(pipew)
kamelInspect.SetErr(pipew)

logScanner := testutil.NewLogScanner(ctx, piper,
"camel:log",
"camel:timer",
//"mvn:org.apache.camel.quarkus:camel-quarkus-yaml-dsl",
)

go func() {
err := kamelInspect.Execute()
assert.NoError(t, err)
cancel()
}()

Eventually(logScanner.IsFound("camel:log"), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound("camel:timer"), TestTimeoutShort).Should(BeTrue())
//Eventually(logScanner.IsFound("mvn:org.apache.camel.quarkus:camel-quarkus-yaml-dsl"), TestTimeoutShort).Should(BeTrue())
}

func TestLocalInspectWithDependencies(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := testutil.MakeTempCopy(t, "files/yaml.yaml")

kamelInspect := KamelWithContext(ctx, "local", "inspect", file,
"-d", "camel-amqp",
"-d", "camel-xxx",
"-d", "mvn:org.apache.camel:camel-http:3.18.0",
"-d", "mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0")
kamelInspect.SetOut(pipew)
kamelInspect.SetErr(pipew)

warn1 := "Warning: dependency camel:xxx not found in Camel catalog"
warn2 := "Warning: do not use mvn:org.apache.camel:camel-http:3.18.0. Use camel:http instead"
warn3 := "Warning: do not use mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0. Use camel:netty instead"
logScanner := testutil.NewLogScanner(ctx, piper,
warn1, warn2, warn3,
"camel:amqp",
"camel:log",
"camel:timer",
)

go func() {
err := kamelInspect.Execute()
assert.NoError(t, err)
cancel()
}()

Eventually(logScanner.IsFound(warn1), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn2), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn3), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound("camel:amqp"), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound("camel:log"), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound("camel:timer"), TestTimeoutShort).Should(BeTrue())
}
51 changes: 43 additions & 8 deletions e2e/local/local_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"io"
"strings"
"sync"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -52,8 +53,7 @@ func TestLocalRun(t *testing.T) {
logScanner := testutil.NewLogScanner(ctx, piper, "Magicstring!")

go func() {
err := kamelRun.Execute()
assert.NoError(t, err)
_ = kamelRun.Execute()
cancel()
}()

Expand All @@ -78,14 +78,51 @@ func TestLocalRunWithDependencies(t *testing.T) {
logScanner := testutil.NewLogScanner(ctx, piper, "Magicstring!")

go func() {
err := kamelRun.Execute()
assert.NoError(t, err)
_ = kamelRun.Execute()
cancel()
}()

Eventually(logScanner.IsFound("Magicstring!"), TestTimeoutMedium).Should(BeTrue())
}

func TestLocalRunWithInvalidDependency(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := testutil.MakeTempCopy(t, "files/yaml.yaml")

kamelRun := KamelWithContext(ctx, "local", "run", file,
"-d", "camel-xxx",
"-d", "mvn:org.apache.camel:camel-http:3.18.0",
"-d", "mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0")
kamelRun.SetOut(pipew)
kamelRun.SetErr(pipew)

warn1 := "Warning: dependency camel:xxx not found in Camel catalog"
warn2 := "Warning: do not use mvn:org.apache.camel:camel-http:3.18.0. Use camel:http instead"
warn3 := "Warning: do not use mvn:org.apache.camel.quarkus:camel-quarkus-netty:2.11.0. Use camel:netty instead"
logScanner := testutil.NewLogScanner(ctx, piper, warn1, warn2, warn3)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := kamelRun.Execute()
assert.Error(t, err)
cancel()
}()

Eventually(logScanner.IsFound(warn1), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn2), TestTimeoutShort).Should(BeTrue())
Eventually(logScanner.IsFound(warn3), TestTimeoutShort).Should(BeTrue())
wg.Wait()
}

func TestLocalRunContainerize(t *testing.T) {
RegisterTestingT(t)

Expand All @@ -106,8 +143,7 @@ func TestLocalRunContainerize(t *testing.T) {

defer StopDockerContainers()
go func() {
err := kamelRun.Execute()
assert.NoError(t, err)
_ = kamelRun.Execute()
cancel()
}()

Expand Down Expand Up @@ -149,8 +185,7 @@ func TestLocalRunIntegrationDirectory(t *testing.T) {
logScanner := testutil.NewLogScanner(ctx2, piper, "Magicstring!")

go func() {
err := kamelRun.Execute()
assert.NoError(t, err)
_ = kamelRun.Execute()
cancel2()
}()

Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/camel/v1/camelcatalog_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func (c *CamelCatalogSpec) GetRuntimeVersion() string {
return c.Runtime.Version
}

// GetCamelVersion returns the Camel version the runtime is based on.
func (c *CamelCatalogSpec) GetCamelVersion() string {
return c.Runtime.Metadata["camel.version"]
}

// GetCamelQuarkusVersion returns the Camel Quarkus version the runtime is based on.
func (c *CamelCatalogSpec) GetCamelQuarkusVersion() string {
return c.Runtime.Metadata["camel-quarkus.version"]
}

// GetQuarkusVersion returns the Quarkus version the runtime is based on.
func (c *CamelCatalogSpec) GetQuarkusVersion() string {
return c.Runtime.Metadata["quarkus.version"]
}

// HasCapability checks if the given capability is present in the catalog.
func (c *CamelCatalogSpec) HasCapability(capability string) bool {
_, ok := c.Runtime.Capabilities[capability]
Expand Down
21 changes: 2 additions & 19 deletions pkg/apis/camel/v1/integration_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,12 @@ func (in *IntegrationSpec) AddDependency(dependency string) {
if in.Dependencies == nil {
in.Dependencies = make([]string, 0)
}
newDep := NormalizeDependency(dependency)
for _, d := range in.Dependencies {
if d == newDep {
if d == dependency {
return
}
}
in.Dependencies = append(in.Dependencies, newDep)
}

// NormalizeDependency converts different forms of camel dependencies
// -- `camel-xxx`, `camel-quarkus-xxx`, and `camel-quarkus:xxx` --
// into the unified form `camel:xxx`.
func NormalizeDependency(dependency string) string {
newDep := dependency
switch {
case strings.HasPrefix(newDep, "camel-quarkus-"):
newDep = "camel:" + strings.TrimPrefix(dependency, "camel-quarkus-")
case strings.HasPrefix(newDep, "camel-quarkus:"):
newDep = "camel:" + strings.TrimPrefix(dependency, "camel-quarkus:")
case strings.HasPrefix(newDep, "camel-"):
newDep = "camel:" + strings.TrimPrefix(dependency, "camel-")
}
return newDep
in.Dependencies = append(in.Dependencies, dependency)
}

// GetConfigurationProperty returns a configuration property
Expand Down
12 changes: 4 additions & 8 deletions pkg/apis/camel/v1/integration_types_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func TestLanguageAlreadySet(t *testing.T) {

func TestAddDependency(t *testing.T) {
integration := IntegrationSpec{}
integration.AddDependency("camel-file")
integration.AddDependency("camel:file")
assert.Equal(t, integration.Dependencies, []string{"camel:file"})
// adding the same dependency twice won't duplicate it in the list
integration.AddDependency("camel:file")
assert.Equal(t, integration.Dependencies, []string{"camel:file"})

integration = IntegrationSpec{}
Expand All @@ -74,13 +77,6 @@ func TestAddDependency(t *testing.T) {
assert.Equal(t, integration.Dependencies, []string{"file:dep"})
}

func TestNormalizeDependency(t *testing.T) {
assert.Equal(t, "camel:file", NormalizeDependency("camel-file"))
assert.Equal(t, "camel:file", NormalizeDependency("camel:file"))
assert.Equal(t, "camel:file", NormalizeDependency("camel-quarkus-file"))
assert.Equal(t, "camel:file", NormalizeDependency("camel-quarkus:file"))
}

func TestGetConfigurationProperty(t *testing.T) {
integration := IntegrationSpec{}
integration.AddConfiguration("property", "key1=value1")
Expand Down
5 changes: 4 additions & 1 deletion pkg/builder/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func loadCamelQuarkusCatalog(ctx *builderContext) error {
}

func generateQuarkusProject(ctx *builderContext) error {
p := GenerateQuarkusProjectCommon(ctx.Build.Runtime.Metadata["camel-quarkus.version"], ctx.Build.Runtime.Version, ctx.Build.Runtime.Metadata["quarkus.version"])
p := GenerateQuarkusProjectCommon(
ctx.Build.Runtime.Metadata["camel-quarkus.version"],
ctx.Build.Runtime.Version,
ctx.Build.Runtime.Metadata["quarkus.version"])

// Add all the properties from the build configuration
p.Properties.AddAll(ctx.Build.Maven.Properties)
Expand Down
Loading