You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2021/05/17 09:57:03 UTC

[camel-k] 02/05: chore(e2e): resource test refactory

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

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

commit 434e08c42e93af7a8d3f103d43211b546b3a216f
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Fri May 14 14:33:23 2021 +0200

    chore(e2e): resource test refactory
    
    * Changed Java test with Groovy test
    * Covered all resources cases (base encoded)
    
    Ref #2003
---
 .gitignore                                         |   1 +
 .../files/resources-base64-encoded-route.groovy}   |  32 +++------
 .../files/resources-binary-route.groovy}           |  20 +++---
 e2e/{resources => config}/files/resources-data.txt |   0
 e2e/{resources => config}/files/resources-data.zip | Bin
 .../files/resources-route.groovy}                  |  21 +++---
 e2e/config/resources_test.go                       |  73 +++++++++++++++++++++
 e2e/resources/resources_test.go                    |  56 ----------------
 pkg/cmd/util.go                                    |   9 +--
 pkg/resources/resources.go                         |   4 +-
 script/Makefile                                    |   4 +-
 11 files changed, 110 insertions(+), 110 deletions(-)

diff --git a/.gitignore b/.gitignore
index 4698207..94ad8cb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@
 build/_output
 build/_test
 build/_maven_output
+build/_maven_overlay
 build/_kamelets
 /api_*
 
diff --git a/e2e/resources/files/ResourcesBinary.java b/e2e/config/files/resources-base64-encoded-route.groovy
similarity index 56%
rename from e2e/resources/files/ResourcesBinary.java
rename to e2e/config/files/resources-base64-encoded-route.groovy
index e1dbb55..02559f0 100644
--- a/e2e/resources/files/ResourcesBinary.java
+++ b/e2e/config/files/resources-base64-encoded-route.groovy
@@ -1,3 +1,4 @@
+// camel-k: language=groovy
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -14,27 +15,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 
-public class ResourcesBinary extends org.apache.camel.builder.RouteBuilder {
+//
+// To run this integrations use:
+// kamel run --resource resources-data.txt --compression=true resources-base64-encoded-route.groovy --dev
+//
 
-    @Override
-    public void configure() throws Exception {
-        from("file:/etc/camel/resources/i-resource-000/?noop=true")
-                .unmarshal().zipFile()
-                .convertBodyTo(String.class)
-                .process(new ZipEntryProcessor());
-    }
-
-}
-
-class ZipEntryProcessor implements Processor {
-
-    @Override
-    public void process(Exchange exchange) throws Exception {
-        System.out.println(exchange.getIn().getBody().toString());
-    }
-
-}
\ No newline at end of file
+from('timer:resources-bas64')
+    .routeId('resources-base64')
+    .setBody()
+        .simple("resource:classpath:resources-data.txt")
+    .log('resource file base64 content is: ${body}')
diff --git a/e2e/resources/files/ResourcesText.java b/e2e/config/files/resources-binary-route.groovy
similarity index 68%
copy from e2e/resources/files/ResourcesText.java
copy to e2e/config/files/resources-binary-route.groovy
index 8ae314f..8b0878b 100644
--- a/e2e/resources/files/ResourcesText.java
+++ b/e2e/config/files/resources-binary-route.groovy
@@ -1,3 +1,4 @@
+// camel-k: language=groovy
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -14,16 +15,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 
-public class ResourcesText extends org.apache.camel.builder.RouteBuilder {
+//
+// To run this integrations use:
+// kamel run --resource resources-data.zip resources-binary-route.groovy -d camel-zipfile --dev
+//
 
-    @Override
-    public void configure() throws Exception {
-        from("file:/etc/camel/resources/i-resource-000/?noop=true")
-                .log("${body}");
-    }
-
-}
\ No newline at end of file
+from('file:/etc/camel/resources/?fileName=resources-data.zip&noop=true&idempotent=false')
+    .routeId('resources-zip')
+    .unmarshal().zipFile()
+    .log('resource file unzipped content is: ${body}')
diff --git a/e2e/resources/files/resources-data.txt b/e2e/config/files/resources-data.txt
similarity index 100%
rename from e2e/resources/files/resources-data.txt
rename to e2e/config/files/resources-data.txt
diff --git a/e2e/resources/files/resources-data.zip b/e2e/config/files/resources-data.zip
similarity index 100%
rename from e2e/resources/files/resources-data.zip
rename to e2e/config/files/resources-data.zip
diff --git a/e2e/resources/files/ResourcesText.java b/e2e/config/files/resources-route.groovy
similarity index 71%
rename from e2e/resources/files/ResourcesText.java
rename to e2e/config/files/resources-route.groovy
index 8ae314f..6f57285 100644
--- a/e2e/resources/files/ResourcesText.java
+++ b/e2e/config/files/resources-route.groovy
@@ -1,3 +1,4 @@
+// camel-k: language=groovy
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -14,16 +15,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 
-public class ResourcesText extends org.apache.camel.builder.RouteBuilder {
+//
+// To run this integrations use:
+// kamel run --resource resources-data.txt resources-route.groovy --dev
+//
 
-    @Override
-    public void configure() throws Exception {
-        from("file:/etc/camel/resources/i-resource-000/?noop=true")
-                .log("${body}");
-    }
-
-}
\ No newline at end of file
+from('timer:resources')
+    .routeId('resources')
+    .setBody()
+        .simple("resource:classpath:resources-data.txt")
+    .log('resource file content is: ${body}')
diff --git a/e2e/config/resources_test.go b/e2e/config/resources_test.go
new file mode 100644
index 0000000..404b7a2
--- /dev/null
+++ b/e2e/config/resources_test.go
@@ -0,0 +1,73 @@
+// +build integration
+
+// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "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 resources
+
+import (
+	"testing"
+
+	. "github.com/onsi/gomega"
+	"github.com/stretchr/testify/assert"
+
+	v1 "k8s.io/api/core/v1"
+
+	"io/ioutil"
+
+	. "github.com/apache/camel-k/e2e/support"
+	camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/gzip"
+)
+
+func TestRunResourceExamples(t *testing.T) {
+	WithNewTestNamespace(t, func(ns string) {
+		Expect(Kamel("install", "-n", ns).Execute()).To(Succeed())
+
+		t.Run("Plain text resource file", func(t *testing.T) {
+			Expect(Kamel("run", "-n", ns, "./files/resources-route.groovy", "--resource", "./files/resources-data.txt").Execute()).To(Succeed())
+			Eventually(IntegrationPodPhase(ns, "resources-route"), TestTimeoutMedium).Should(Equal(v1.PodRunning))
+			Eventually(IntegrationCondition(ns, "resources-route", camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
+			Eventually(IntegrationLogs(ns, "resources-route"), TestTimeoutShort).Should(ContainSubstring("the file body"))
+			Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
+		})
+
+		t.Run("Binary (zip) resource file", func(t *testing.T) {
+			Expect(Kamel("run", "-n", ns, "./files/resources-binary-route.groovy", "--resource", "./files/resources-data.zip", "-d", "camel-zipfile").Execute()).To(Succeed())
+			Eventually(IntegrationPodPhase(ns, "resources-binary-route"), TestTimeoutMedium).Should(Equal(v1.PodRunning))
+			Eventually(IntegrationCondition(ns, "resources-binary-route", camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
+			Eventually(IntegrationLogs(ns, "resources-binary-route"), TestTimeoutShort).Should(ContainSubstring("the file body"))
+			Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
+		})
+
+		t.Run("Base64 compressed binary resource file", func(t *testing.T) {
+			// We calculate the expected content
+			source, err := ioutil.ReadFile("./files/resources-data.txt")
+			assert.Nil(t, err)
+			expectedBytes, err := gzip.CompressBase64([]byte(source))
+			assert.Nil(t, err)
+
+			Expect(Kamel("run", "-n", ns, "./files/resources-base64-encoded-route.groovy", "--resource", "./files/resources-data.txt", "--compression=true").Execute()).To(Succeed())
+			Eventually(IntegrationPodPhase(ns, "resources-base64-encoded-route"), TestTimeoutMedium).Should(Equal(v1.PodRunning))
+			Eventually(IntegrationCondition(ns, "resources-base64-encoded-route", camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
+			Eventually(IntegrationLogs(ns, "resources-base64-encoded-route"), TestTimeoutShort).Should(ContainSubstring(string(expectedBytes)))
+			Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
+		})
+	})
+}
diff --git a/e2e/resources/resources_test.go b/e2e/resources/resources_test.go
deleted file mode 100644
index c7b08ba..0000000
--- a/e2e/resources/resources_test.go
+++ /dev/null
@@ -1,56 +0,0 @@
-// +build integration
-
-// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "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 resources
-
-import (
-	"testing"
-
-	. "github.com/onsi/gomega"
-
-	v1 "k8s.io/api/core/v1"
-
-	. "github.com/apache/camel-k/e2e/support"
-	camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
-)
-
-func TestRunResourceExamples(t *testing.T) {
-	WithNewTestNamespace(t, func(ns string) {
-		Expect(Kamel("install", "-n", ns).Execute()).To(Succeed())
-
-		t.Run("run java", func(t *testing.T) {
-			Expect(Kamel("run", "-n", ns, "./files/ResourcesText.java", "--resource", "./files/resources-data.txt").Execute()).To(Succeed())
-			Eventually(IntegrationPodPhase(ns, "resources-text"), TestTimeoutMedium).Should(Equal(v1.PodRunning))
-			Eventually(IntegrationCondition(ns, "resources-text", camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
-			Eventually(IntegrationLogs(ns, "resources-text"), TestTimeoutShort).Should(ContainSubstring("the file body"))
-			Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
-		})
-
-		t.Run("run java", func(t *testing.T) {
-			Expect(Kamel("run", "-n", ns, "./files/ResourcesBinary.java",
-				"--resource", "./files/resources-data.zip", "-d", "camel-zipfile").Execute()).To(Succeed())
-			Eventually(IntegrationPodPhase(ns, "resources-binary"), TestTimeoutMedium).Should(Equal(v1.PodRunning))
-			Eventually(IntegrationCondition(ns, "resources-binary", camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
-			Eventually(IntegrationLogs(ns, "resources-binary"), TestTimeoutShort).Should(ContainSubstring("the file body"))
-			Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
-		})
-	})
-}
diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go
index 3e364a6..b287529 100644
--- a/pkg/cmd/util.go
+++ b/pkg/cmd/util.go
@@ -18,9 +18,7 @@ limitations under the License.
 package cmd
 
 import (
-	"bytes"
 	"context"
-	"encoding/base64"
 	"encoding/csv"
 	"encoding/json"
 	"fmt"
@@ -243,11 +241,10 @@ func fieldByMapstructureTagName(target reflect.Value, tagName string) (reflect.S
 }
 
 func compressToString(content []byte) (string, error) {
-	var b bytes.Buffer
-
-	if err := gzip.Compress(&b, content); err != nil {
+	bytes, err := gzip.CompressBase64(content)
+	if err != nil {
 		return "", err
 	}
 
-	return base64.StdEncoding.EncodeToString(b.Bytes()), nil
+	return string(bytes), nil
 }
diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go
index 1005db3..93f34e0 100644
--- a/pkg/resources/resources.go
+++ b/pkg/resources/resources.go
@@ -467,9 +467,9 @@ var assets = func() http.FileSystem {
 		"/traits.yaml": &vfsgen۰CompressedFileInfo{
 			name:             "traits.yaml",
 			modTime:          time.Time{},
-			uncompressedSize: 38387,
+			uncompressedSize: 38635,
 
-			compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x6d\x6f\x1c\x37\xd2\xe0\x77\xff\x0a\x42\xcf\x01\x7a\xc1\x4c\x4b\xce\x22\xbb\x39\xdd\xf9\x16\x8a\xed\xec\x2a\x89\x6d\x9d\xe5\xcd\xe2\xe0\x0b\x76\x38\xdd\x35\x33\xb4\xd8\x64\x2f\xc9\x96\x3c\x39\xdc\x7f\x3f\xb0\x8a\x6f\x3d\xd3\x92\x46\x4e\x14\x44\x87\x67\xf7\x43\x2c\xa9\xbb\x58\x2c\x16\xeb\xbd\xaa\x9d\xe1\xc2\xd9\xd3\x67\x53\xa6\x78\x0b\xa7\x8c\x2f\x16\x42\x09\xb7\x7e\xc6\x58\x27\xb9\x5b\x68\xd3\x9e\xb2\x05\x97\x [...]
+			compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x6d\x6f\x1c\x37\xd2\xe0\x77\xff\x0a\x42\xcf\x01\x7a\xc1\x4c\x4b\xce\x22\xbb\x39\xdd\xf9\x16\x8a\xed\xec\x2a\x89\x6d\x9d\xe5\xcd\xe2\xe0\x0b\x76\x38\xdd\x35\x33\xb4\xd8\x64\x2f\xc9\x96\x3c\x39\xdc\x7f\x3f\xb0\x8a\x6f\x3d\xd3\x92\x46\x4e\x14\x44\x87\x67\xf7\x43\x2c\xa9\xbb\x58\x2c\x16\xeb\xbd\xaa\x9d\xe1\xc2\xd9\xd3\x67\x53\xa6\x78\x0b\xa7\x8c\x2f\x16\x42\x09\xb7\x7e\xc6\x58\x27\xb9\x5b\x68\xd3\x9e\xb2\x05\x97\x [...]
 		},
 	}
 	fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
diff --git a/script/Makefile b/script/Makefile
index b19990f..fcfc9b5 100644
--- a/script/Makefile
+++ b/script/Makefile
@@ -168,8 +168,8 @@ test-local: build
 	#go test -timeout 60m -v ./e2e/local -tags=integration
 
 test-resources: build
-	STAGING_RUNTIME_REPO="$(STAGING_RUNTIME_REPO)" go test -timeout 60m -v ./e2e/resources -tags=integration
-	#go test -timeout 60m -v ./e2e/resources -tags=integration
+	STAGING_RUNTIME_REPO="$(STAGING_RUNTIME_REPO)" go test -timeout 60m -v ./e2e/config -tags=integration
+	#go test -timeout 60m -v ./e2e/config -tags=integration
 
 test-kamel-cli: build
 	STAGING_RUNTIME_REPO="$(STAGING_RUNTIME_REPO)" go test -timeout 60m -v ./e2e/common/cli -tags=integration