From 9a5ecd50aba6d0d4218bf6e8818af721427b5ddb Mon Sep 17 00:00:00 2001 From: pashakostohrys Date: Thu, 24 Oct 2024 16:11:24 +0300 Subject: [PATCH] fix: improve message --- acr_controller/service/acr_service.go | 14 ++++--- acr_controller/service/acr_service_test.go | 48 ++++++++-------------- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/acr_controller/service/acr_service.go b/acr_controller/service/acr_service.go index 12f73d77bd798..1cbcbe724e2ac 100644 --- a/acr_controller/service/acr_service.go +++ b/acr_controller/service/acr_service.go @@ -24,12 +24,14 @@ type acrService struct { applicationClientset appclientset.Interface applicationServiceClient argoclient.ApplicationClient lock sync.Mutex + logger *log.Logger } func NewACRService(applicationClientset appclientset.Interface, applicationServiceClient argoclient.ApplicationClient) ACRService { return &acrService{ applicationClientset: applicationClientset, applicationServiceClient: applicationServiceClient, + logger: log.New(), } } @@ -67,7 +69,7 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat } if getChangeRevision(app) != "" { - log.Infof("Change revision already calculated for application %s", app.Name) + c.logger.Infof("Change revision already calculated for application %s", app.Name) return nil } @@ -77,11 +79,11 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat } if revision == nil || *revision == "" { - log.Infof("Revision for application %s is empty", app.Name) + c.logger.Infof("Revision for application %s is empty", app.Name) return nil } - log.Infof("Change revision for application %s is %s", app.Name, *revision) + c.logger.Infof("Change revision for application %s is %s", app.Name, *revision) app, err = c.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(ctx, app.Name, metav1.GetOptions{}) if err != nil { @@ -91,17 +93,17 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat revisions := []string{*revision} if app.Status.OperationState != nil && app.Status.OperationState.Operation.Sync != nil { - log.Infof("Patch operation sync result for application %s", app.Name) + c.logger.Infof("Patch operation sync result for application %s", app.Name) return c.patchOperationSyncResultWithChangeRevision(ctx, app, revisions) } - log.Infof("Patch operation for application %s", app.Name) + c.logger.Infof("Patch operation for application %s", app.Name) return c.patchOperationWithChangeRevision(ctx, app, revisions) } func (c *acrService) calculateRevision(ctx context.Context, a *application.Application) (*string, error) { currentRevision, previousRevision := c.getRevisions(ctx, a) - log.Infof("Calculate revision for application '%s', current revision '%s', previous revision '%s'", a.Name, currentRevision, previousRevision) + c.logger.Infof("Calculate revision for application '%s', current revision '%s', previous revision '%s'", a.Name, currentRevision, previousRevision) changeRevisionResult, err := c.applicationServiceClient.GetChangeRevision(ctx, &appclient.ChangeRevisionRequest{ AppName: pointer.String(a.GetName()), Namespace: pointer.String(a.GetNamespace()), diff --git a/acr_controller/service/acr_service_test.go b/acr_controller/service/acr_service_test.go index 01ab9933543c0..6313481233072 100644 --- a/acr_controller/service/acr_service_test.go +++ b/acr_controller/service/acr_service_test.go @@ -2,10 +2,11 @@ package service import ( "context" - "io" - "os" "testing" + "github.com/sirupsen/logrus" + test2 "github.com/sirupsen/logrus/hooks/test" + "github.com/argoproj/argo-cd/v2/acr_controller/application/mocks" appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -204,35 +205,12 @@ status: status: Synced ` - -// Rather dirty hack to capture stdout from PrintResource() and PrintResourceList() -func captureOutput(f func() error) (string, error) { - stdout := os.Stdout - r, w, err := os.Pipe() - if err != nil { - return "", err - } - os.Stdout = w - err = f() - w.Close() - if err != nil { - os.Stdout = stdout - return "", err - } - str, err := io.ReadAll(r) - os.Stdout = stdout - if err != nil { - return "", err - } - return string(str), err -} - - func newTestACRService(client *mocks.ApplicationClient) *acrService { fakeAppsClientset := apps.NewSimpleClientset(createTestApp(syncedAppWithHistory)) return &acrService{ applicationClientset: fakeAppsClientset, applicationServiceClient: client, + logger: logrus.New(), } } @@ -311,7 +289,12 @@ func Test_ChangeRevision(r *testing.T) { client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{ Revision: pointer.String("new-revision"), }, nil) + + logger, logHook := test2.NewNullLogger() + acrService := newTestACRService(client) + acrService.logger = logger + app := createTestApp(syncedAppWithHistory) err := acrService.ChangeRevision(context.TODO(), app) @@ -322,10 +305,15 @@ func Test_ChangeRevision(r *testing.T) { assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision) - str, err := captureOutput(func() error { - return acrService.ChangeRevision(context.TODO(), app) - }) + err = acrService.ChangeRevision(context.TODO(), app) + require.NoError(t, err) - require.Equal(t, str, "Change revision already calculated for application guestbook") + + lastLogEntry := logHook.LastEntry() + if lastLogEntry == nil { + t.Fatal("No log entry") + } + + require.Equal(t, "Change revision already calculated for application guestbook", lastLogEntry.Message) }) }