Skip to content

Commit

Permalink
fix(trait): camel to use a single properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jul 27, 2022
1 parent d108d03 commit dd3bff2
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 90 deletions.
151 changes: 103 additions & 48 deletions e2e/global/common/kamelet_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,91 @@ import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
)

func TestErrorHandler(t *testing.T) {
func TestKameletBinding(t *testing.T) {
WithNewTestNamespace(t, func(ns string) {
operatorID := "camel-k-kamelet-errorhandler"
operatorID := "camel-k-kameletbinding"
Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed())

Expect(createErrorProducerKamelet(ns, "my-own-error-producer-source")()).To(Succeed())
Expect(createLogKamelet(ns, "my-own-log-sink")()).To(Succeed())
from := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-error-producer-source",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

to := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-log-sink",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

errorHandler := map[string]interface{}{
"sink": map[string]interface{}{
"endpoint": map[string]interface{}{
"ref": map[string]string{
"kind": "Kamelet",
"apiVersion": v1alpha1.SchemeGroupVersion.String(),
"name": "my-own-log-sink",
},
"properties": map[string]string{
"loggerName": "kameletErrorHandler",
},
}}}

t.Run("throw error test", func(t *testing.T) {
// Error Handler testing
t.Run("test error handler", func(t *testing.T) {
RegisterTestingT(t)

Expect(BindKameletToWithErrorHandler(ns, "throw-error-binding", map[string]string{}, from, to, map[string]string{"message": "throw Error"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "throw-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).Should(ContainSubstring("kameletErrorHandler"))
Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("integrationLogger"))

Expect(createErrorProducerKamelet(ns, "my-own-error-producer-source")()).To(Succeed())
Expect(createLogKamelet(ns, "my-own-log-sink")()).To(Succeed())
from := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-error-producer-source",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

to := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-log-sink",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

errorHandler := map[string]interface{}{
"sink": map[string]interface{}{
"endpoint": map[string]interface{}{
"ref": map[string]string{
"kind": "Kamelet",
"apiVersion": v1alpha1.SchemeGroupVersion.String(),
"name": "my-own-log-sink",
},
"properties": map[string]string{
"loggerName": "kameletErrorHandler",
},
}}}

t.Run("throw error test", func(t *testing.T) {
RegisterTestingT(t)

Expect(BindKameletToWithErrorHandler(ns, "throw-error-binding", map[string]string{}, from, to, map[string]string{"message": "throw Error"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "throw-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).Should(ContainSubstring("kameletErrorHandler"))
Eventually(IntegrationLogs(ns, "throw-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("integrationLogger"))

})

t.Run("don't throw error test", func(t *testing.T) {
RegisterTestingT(t)

Expect(BindKameletToWithErrorHandler(ns, "no-error-binding", map[string]string{}, from, to, map[string]string{"message": "true"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "no-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("kameletErrorHandler"))
Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))

})

// Cleanup
Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})

t.Run("don't throw error test", func(t *testing.T) {
// Kamelet binding with traits testing
t.Run("test kamelet binding with trait", func(t *testing.T) {
RegisterTestingT(t)

Expect(BindKameletToWithErrorHandler(ns, "no-error-binding", map[string]string{}, from, to, map[string]string{"message": "true"}, map[string]string{"loggerName": "integrationLogger"}, errorHandler)()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "no-error-binding"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).ShouldNot(ContainSubstring("kameletErrorHandler"))
Eventually(IntegrationLogs(ns, "no-error-binding"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))

Expect(createTimerKamelet(ns, "my-own-timer-source")()).To(Succeed())
// Log sink kamelet exists from previous test

from := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-timer-source",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

to := corev1.ObjectReference{
Kind: "Kamelet",
Name: "my-own-log-sink",
APIVersion: v1alpha1.SchemeGroupVersion.String(),
}

Expect(BindKameletTo(ns, "kb-with-traits", map[string]string{"trait.camel.apache.org/camel.properties": "[\"camel.prop1=a\",\"camel.prop2=b\"]"}, from, to, map[string]string{"message": "hello from test"}, map[string]string{"loggerName": "integrationLogger"})()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "kb-with-traits"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationLogs(ns, "kb-with-traits"), TestTimeoutShort).Should(ContainSubstring("hello from test"))
Eventually(IntegrationLogs(ns, "kb-with-traits"), TestTimeoutShort).Should(ContainSubstring("integrationLogger"))
})

// Cleanup
Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})
}

Expand Down Expand Up @@ -143,3 +172,29 @@ func createErrorProducerKamelet(ns string, name string) func() error {

return CreateKamelet(ns, name, flow, props, nil)
}

func createTimerKamelet(ns string, name string) func() error {
props := map[string]v1alpha1.JSONSchemaProp{
"message": {
Type: "string",
},
}

flow := map[string]interface{}{
"from": map[string]interface{}{
"uri": "timer:tick",
"steps": []map[string]interface{}{
{
"set-body": map[string]interface{}{
"constant": "{{message}}",
},
},
{
"to": "kamelet:sink",
},
},
},
}

return CreateKamelet(ns, name, flow, props, nil)
}
50 changes: 8 additions & 42 deletions pkg/trait/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ func (t *camelTrait) Apply(e *Environment) error {
if e.IntegrationKitInPhase(v1.IntegrationKitPhaseReady) && e.IntegrationInRunningPhases() {
// Get all resources
maps := t.computeConfigMaps(e)
if t.Properties != nil {
// Only user.properties
maps = append(maps, t.computeUserProperties(e)...)
}
e.Resources.AddAll(maps)
}

Expand Down Expand Up @@ -179,6 +175,14 @@ func (t *camelTrait) computeConfigMaps(e *Environment) []ctrl.Object {
userProperties += fmt.Sprintf("%s=%s\n", prop.Name, prop.Value)
}

if t.Properties != nil {
// Merge with properties set in the trait
for _, prop := range t.Properties {
k, v := property.SplitPropertyFileEntry(prop)
userProperties += fmt.Sprintf("%s=%s\n", k, v)
}
}

if userProperties != "" {
maps = append(
maps,
Expand Down Expand Up @@ -283,41 +287,3 @@ func (t *camelTrait) computeConfigMaps(e *Environment) []ctrl.Object {

return maps
}

func (t *camelTrait) computeUserProperties(e *Environment) []ctrl.Object {
maps := make([]ctrl.Object, 0)

// combine properties of integration with kit, integration
// properties have the priority
userProperties := ""

for _, prop := range t.Properties {
k, v := property.SplitPropertyFileEntry(prop)
userProperties += fmt.Sprintf("%s=%s\n", k, v)
}

if userProperties != "" {
maps = append(
maps,
&corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: e.Integration.Name + "-user-properties",
Namespace: e.Integration.Namespace,
Labels: map[string]string{
v1.IntegrationLabel: e.Integration.Name,
"camel.apache.org/properties.type": "user",
},
},
Data: map[string]string{
"application.properties": userProperties,
},
},
)
}

return maps
}

0 comments on commit dd3bff2

Please sign in to comment.