Skip to content

Commit

Permalink
fix: improve message
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-codefresh committed Oct 24, 2024
1 parent 0a39544 commit 9a5ecd5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
14 changes: 8 additions & 6 deletions acr_controller/service/acr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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()),
Expand Down
48 changes: 18 additions & 30 deletions acr_controller/service/acr_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
}

0 comments on commit 9a5ecd5

Please sign in to comment.