From 8e1f56fcdc1252391e0160776abfcd32552982e9 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 30 Aug 2023 13:32:35 -0700 Subject: [PATCH] Adding unit tests --- internal/pkg/agent/install/progress.go | 4 +- internal/pkg/agent/install/progress_test.go | 50 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 internal/pkg/agent/install/progress_test.go diff --git a/internal/pkg/agent/install/progress.go b/internal/pkg/agent/install/progress.go index 77333669bbc..264827e1fbc 100644 --- a/internal/pkg/agent/install/progress.go +++ b/internal/pkg/agent/install/progress.go @@ -25,9 +25,9 @@ func (pt *ProgressTracker) StepStart(msg string) { } func (pt *ProgressTracker) StepSucceeded() { - fmt.Fprintln(pt.writer, "DONE") + fmt.Fprintln(pt.writer, " DONE") } func (pt *ProgressTracker) StepFailed() { - fmt.Fprintln(pt.writer, "FAILED") + fmt.Fprintln(pt.writer, " FAILED") } diff --git a/internal/pkg/agent/install/progress_test.go b/internal/pkg/agent/install/progress_test.go new file mode 100644 index 00000000000..39dbe182f0d --- /dev/null +++ b/internal/pkg/agent/install/progress_test.go @@ -0,0 +1,50 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package install + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +type testWriter struct { + buf []byte +} + +func newTestWriter() *testWriter { + return &testWriter{ + buf: []byte{}, + } +} + +func (tw *testWriter) Write(p []byte) (int, error) { + tw.buf = append(tw.buf, p...) + return len(p), nil +} + +func TestProgress(t *testing.T) { + t.Run("single_step_immediate_failure", func(t *testing.T) { + w := newTestWriter() + pt := NewProgressTracker(w) + + pt.StepStart("step 1 starting") + pt.StepFailed() + + require.Equal(t, "step 1 starting... FAILED\n", string(w.buf)) + }) + + t.Run("multi_step_success", func(t *testing.T) { + w := newTestWriter() + pt := NewProgressTracker(w) + + pt.StepStart("step 1 starting") + pt.StepSucceeded() + pt.StepStart("step 2 starting") + pt.StepSucceeded() + + require.Equal(t, "step 1 starting... DONE\nstep 2 starting... DONE\n", string(w.buf)) + }) +}