This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 287dce0  Fix #2441: parse parameters in YAML DSL
287dce0 is described below

commit 287dce0c14a9293dee0a8052a75927182dfe38fa
Author: nicolaferraro <ni.ferr...@gmail.com>
AuthorDate: Thu Jun 24 11:42:44 2021 +0200

    Fix #2441: parse parameters in YAML DSL
---
 e2e/common/cron_test.go                |  8 ++++
 e2e/common/files/cron-yaml.yaml        | 31 ++++++++++++++
 pkg/util/source/inspector_yaml.go      | 16 +++++++-
 pkg/util/source/inspector_yaml_test.go | 74 +++++++++++++++++++++++++++++++++-
 4 files changed, 127 insertions(+), 2 deletions(-)

diff --git a/e2e/common/cron_test.go b/e2e/common/cron_test.go
index 9759334..b04d7ee 100644
--- a/e2e/common/cron_test.go
+++ b/e2e/common/cron_test.go
@@ -44,6 +44,14 @@ func TestRunCronExample(t *testing.T) {
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
                })
 
+               t.Run("cron-yaml", func(t *testing.T) {
+                       Expect(Kamel("run", "-n", ns, 
"files/cron-yaml.yaml").Execute()).To(Succeed())
+                       Eventually(IntegrationCronJob(ns, "cron-yaml"), 
TestTimeoutMedium).ShouldNot(BeNil())
+                       Eventually(IntegrationCondition(ns, "cron-yaml", 
camelv1.IntegrationConditionReady), 
TestTimeoutMedium).Should(Equal(v1.ConditionTrue))
+                       Eventually(IntegrationLogs(ns, "cron-yaml"), 
TestTimeoutMedium).Should(ContainSubstring("Magicstring!"))
+                       Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+               })
+
                t.Run("cron-timer", func(t *testing.T) {
                        Expect(Kamel("run", "-n", ns, 
"files/cron-timer.groovy").Execute()).To(Succeed())
                        Eventually(IntegrationCronJob(ns, "cron-timer"), 
TestTimeoutMedium).ShouldNot(BeNil())
diff --git a/e2e/common/files/cron-yaml.yaml b/e2e/common/files/cron-yaml.yaml
new file mode 100644
index 0000000..e017285
--- /dev/null
+++ b/e2e/common/files/cron-yaml.yaml
@@ -0,0 +1,31 @@
+# ---------------------------------------------------------------------------
+# 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.
+# ---------------------------------------------------------------------------
+
+- from:
+    uri: "cron:tab"
+    parameters:
+      schedule: "* * * * ?"
+    steps:
+      - set-header:
+          name: "m"
+          constant: "string!"
+      - set-body:
+          simple: "Magic${header.m}"
+      - to:
+          uri: "log:info"
+          parameters:
+            show-all: "false"
diff --git a/pkg/util/source/inspector_yaml.go 
b/pkg/util/source/inspector_yaml.go
index b5f9846..9fe8b11 100644
--- a/pkg/util/source/inspector_yaml.go
+++ b/pkg/util/source/inspector_yaml.go
@@ -22,6 +22,7 @@ import (
        "strings"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/pkg/util/uri"
        yaml2 "gopkg.in/yaml.v2"
 )
 
@@ -117,7 +118,20 @@ func (i YAMLInspector) parseStep(key string, content 
interface{}, meta *Metadata
 
                if u, ok := t["uri"]; ok {
                        if v, isString := u.(string); isString {
-                               maybeURI = v
+                               builtURI := v
+                               // Inject parameters into URIs to allow other 
parts of the operator to inspect them
+                               if params, pok := t["parameters"]; pok {
+                                       if paramMap, pmok := 
params.(map[interface{}]interface{}); pmok {
+                                               params := 
make(map[string]string, len(paramMap))
+                                               for k, v := range paramMap {
+                                                       ks := fmt.Sprintf("%v", 
k)
+                                                       vs := fmt.Sprintf("%v", 
v)
+                                                       params[ks] = vs
+                                               }
+                                               builtURI = 
uri.AppendParameters(builtURI, params)
+                                       }
+                               }
+                               maybeURI = builtURI
                        }
                }
 
diff --git a/pkg/util/source/inspector_yaml_test.go 
b/pkg/util/source/inspector_yaml_test.go
index 69ed703..692d4ed 100644
--- a/pkg/util/source/inspector_yaml_test.go
+++ b/pkg/util/source/inspector_yaml_test.go
@@ -242,7 +242,7 @@ func TestYAMLRouteAndFromEquivalence(t *testing.T) {
                        inspector := NewtestYAMLInspector(t)
                        err := inspector.Extract(code, &meta)
                        assert.Nil(t, err)
-                       assert.Equal(t, meta.FromURIs, []string{"timer:tick"})
+                       assert.Equal(t, meta.FromURIs, 
[]string{"timer:tick?period=5000"})
                        assert.Equal(t, meta.ToURIs, []string{"log:info"})
                        assert.True(t, meta.Dependencies.Has("camel:log"))
                        assert.True(t, meta.Dependencies.Has("camel:timer"))
@@ -419,3 +419,75 @@ func TestYAMLKamelet(t *testing.T) {
                })
        }
 }
+
+const YAMLKameletExplicitParams = `
+- from:
+    uri: cron:tab
+    parameters:
+      schedule: "* * * * ?"
+    steps:
+    - to:
+        uri: knative:channel/a
+        parameters:
+          cloudEventsSpecVersion: "1.0"
+`
+
+const YAMLKameletExplicitNumericParams = `
+- from:
+    uri: timer:tick
+    parameters:
+      period: 1000
+    steps:
+    - log: "hello"
+`
+
+func TestYAMLExplicitParameters(t *testing.T) {
+       tc := []struct {
+               source   string
+               fromURIs []string
+               toURIs   []string
+       }{
+               {
+                       source:   YAMLKameletExplicitParams,
+                       fromURIs: 
[]string{"cron:tab?schedule=%2A+%2A+%2A+%2A+%3F"},
+                       toURIs:   
[]string{"knative:channel/a?cloudEventsSpecVersion=1.0"},
+               },
+               {
+                       source:   YAMLKameletExplicitNumericParams,
+                       fromURIs: []string{"timer:tick?period=1000"},
+               },
+       }
+
+       for i, test := range tc {
+               t.Run(fmt.Sprintf("TestYAMLExplicitParameters-%d", i), func(t 
*testing.T) {
+                       code := v1.SourceSpec{
+                               DataSpec: v1.DataSpec{
+                                       Content: test.source,
+                               },
+                       }
+
+                       catalog, err := camel.DefaultCatalog()
+                       assert.Nil(t, err)
+
+                       meta := NewMetadata()
+                       inspector := YAMLInspector{
+                               baseInspector: baseInspector{
+                                       catalog: catalog,
+                               },
+                       }
+
+                       err = inspector.Extract(code, &meta)
+                       assert.Nil(t, err)
+                       assert.True(t, meta.RequiredCapabilities.IsEmpty())
+
+                       assert.Len(t, meta.FromURIs, len(test.fromURIs))
+                       for _, k := range test.fromURIs {
+                               assert.Contains(t, meta.FromURIs, k)
+                       }
+                       assert.Len(t, meta.ToURIs, len(test.toURIs))
+                       for _, k := range test.toURIs {
+                               assert.Contains(t, meta.ToURIs, k)
+                       }
+               })
+       }
+}

Reply via email to