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

[camel-k] branch main updated: fix(#3705) Fix misleading message when updating unchanged integration

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

pcongiusti 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 7d8c0baa6 fix(#3705) Fix misleading message when updating unchanged integration
7d8c0baa6 is described below

commit 7d8c0baa631b045e6f09d096c303beb291852d2e
Author: Jan Bouska <jb...@redhat.com>
AuthorDate: Mon Apr 24 14:04:36 2023 +0200

    fix(#3705) Fix misleading message when updating unchanged integration
---
 pkg/cmd/run.go          | 22 +++++++++++++++++-----
 pkg/cmd/run_test.go     | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
 pkg/util/test/client.go |  6 +++++-
 3 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 73708f6ec..2911e87c2 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -562,14 +562,26 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
 
 	if existing == nil {
 		err = c.Create(o.Context, integration)
+		if err != nil {
+			return nil, err
+		}
 		fmt.Fprintln(cmd.OutOrStdout(), `Integration "`+name+`" created`)
 	} else {
-		err = c.Patch(o.Context, integration, ctrl.MergeFromWithOptions(existing, ctrl.MergeFromWithOptimisticLock{}))
-		fmt.Fprintln(cmd.OutOrStdout(), `Integration "`+name+`" updated`)
-	}
+		patch := ctrl.MergeFrom(existing)
+		d, err := patch.Data(integration)
+		if err != nil {
+			return nil, err
+		}
 
-	if err != nil {
-		return nil, err
+		if string(d) == "{}" {
+			fmt.Fprintln(cmd.OutOrStdout(), `Integration "`+name+`" unchanged`)
+			return integration, nil
+		}
+		err = c.Patch(o.Context, integration, patch)
+		if err != nil {
+			return nil, err
+		}
+		fmt.Fprintln(cmd.OutOrStdout(), `Integration "`+name+`" updated`)
 	}
 
 	return integration, nil
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index 5322319c8..f31330f7f 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -22,12 +22,14 @@ import (
 	"fmt"
 	"os"
 	"path/filepath"
+	"strings"
 	"testing"
 
 	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
 	traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
 	"github.com/apache/camel-k/v2/pkg/platform"
 	"github.com/apache/camel-k/v2/pkg/trait"
+	"github.com/apache/camel-k/v2/pkg/util/defaults"
 	"github.com/apache/camel-k/v2/pkg/util/test"
 
 	"github.com/spf13/cobra"
@@ -38,6 +40,17 @@ import (
 const (
 	cmdRun            = "run"
 	integrationSource = "example.js"
+	yamlIntegration   = `# camel-k: language=yaml
+
+- from:
+    uri: "timer:yaml"
+    parameters:
+      period: "1000"
+    steps:
+      - setBody:
+          constant: "Hello Camel K from yaml"
+      - to: "log:info"
+`
 )
 
 // nolint: unparam
@@ -54,9 +67,10 @@ func initializeRunCmdOptions(t *testing.T) (*runCmdOptions, *cobra.Command, Root
 // nolint: unparam
 func initializeRunCmdOptionsWithOutput(t *testing.T) (*runCmdOptions, *cobra.Command, RootCmdOptions) {
 	t.Helper()
-
 	defaultIntegrationPlatform := v1.NewIntegrationPlatform("default", platform.DefaultPlatformName)
-	fakeClient, _ := test.NewFakeClient(&defaultIntegrationPlatform)
+	c := v1.NewCamelCatalog(defaultIntegrationPlatform.Namespace, defaults.DefaultRuntimeVersion)
+	c.Spec = v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Provider: defaultIntegrationPlatform.Status.Build.RuntimeProvider, Version: defaultIntegrationPlatform.Status.Build.RuntimeVersion}}
+	fakeClient, _ := test.NewFakeClient(&defaultIntegrationPlatform, &c)
 
 	options, rootCmd := kamelTestPreAddCommandInitWithClient(fakeClient)
 	runCmdOptions := addTestRunCmdWithOutput(*options, rootCmd)
@@ -773,3 +787,33 @@ func TestPropertyShouldNotExpand(t *testing.T) {
 	assert.Contains(t, output, "buildProp = ${value}")
 	assert.Contains(t, output, "key = ${value}")
 }
+
+func TestRunOutput(t *testing.T) {
+	var tmpFile1 *os.File
+	var err error
+	if tmpFile1, err = os.CreateTemp("", "camel-k-*.yaml"); err != nil {
+		t.Error(err)
+	}
+	defer tmpFile1.Close()
+
+	assert.Nil(t, tmpFile1.Sync())
+	assert.Nil(t, os.WriteFile(tmpFile1.Name(), []byte(yamlIntegration), 0o400))
+
+	_, rootCmd, _ := initializeRunCmdOptionsWithOutput(t)
+	output, err := test.ExecuteCommand(rootCmd, cmdRun, tmpFile1.Name())
+	_, fileName := filepath.Split(tmpFile1.Name())
+	integrationName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
+	assert.Nil(t, err)
+	assert.Equal(t, fmt.Sprintf("Integration \"%s\" created\n", integrationName), output)
+
+	output, err = test.ExecuteCommand(rootCmd, cmdRun, tmpFile1.Name())
+	assert.Nil(t, err)
+	assert.Equal(t, fmt.Sprintf("Integration \"%s\" unchanged\n", integrationName), output)
+
+	assert.Nil(t, os.WriteFile(tmpFile1.Name(), []byte(strings.Replace(yamlIntegration, "Hello", "Hi", 1)), 0o400))
+	assert.Nil(t, tmpFile1.Sync())
+	output, err = test.ExecuteCommand(rootCmd, cmdRun, tmpFile1.Name())
+	assert.Nil(t, err)
+	assert.Equal(t, fmt.Sprintf("Integration \"%s\" updated\n", integrationName), output)
+
+}
diff --git a/pkg/util/test/client.go b/pkg/util/test/client.go
index adbb8eaa2..7fc121389 100644
--- a/pkg/util/test/client.go
+++ b/pkg/util/test/client.go
@@ -150,7 +150,11 @@ func (c *FakeClient) GetCurrentNamespace(kubeConfig string) (string, error) {
 
 // Patch mimicks patch for server-side apply and simply creates the obj.
 func (c *FakeClient) Patch(ctx context.Context, obj controller.Object, patch controller.Patch, opts ...controller.PatchOption) error {
-	return c.Create(ctx, obj)
+	if err := c.Create(ctx, obj); err != nil {
+		// Create fails if object already exists. Try to update it.
+		return c.Update(ctx, obj)
+	}
+	return nil
 }
 
 func (c *FakeClient) DisableAPIGroupDiscovery(group string) {