Skip to content

Commit

Permalink
Skip saving metadata if using newer agent with older helm chart versi…
Browse files Browse the repository at this point in the history
…on (#120)
  • Loading branch information
viktorasm authored Jun 13, 2023
1 parent 5c245af commit f484d45
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/services/monitor/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type Metadata struct {
}

func (m *Metadata) Save(file string) error {
if file == "" {
// if monitor is running standalone or with an old chart version, and saving of
// metadata is not configured, we don't need to do anything here
return nil
}
contents, err := json.Marshal(m)
if err != nil {
return fmt.Errorf("marshaling: %w", err)
Expand Down
50 changes: 50 additions & 0 deletions internal/services/monitor/metatada_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,65 @@ package monitor

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/google/uuid"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestSaveMetadata(t *testing.T) {

tests := map[string]struct {
createDir string
file string
expectedError *string
}{
"not configured": {
file: "",
expectedError: nil,
},
"invalid file dir": {
file: "no_such_dir/abc",
expectedError: lo.ToPtr("open.*no such file or directory"),
},
"valid dir": {
createDir: "metadata",
file: "metadata/info",
},
}

for testName, tt := range tests {
tt := tt
t.Run(testName, func(t *testing.T) {
r := require.New(t)
baseDir := t.TempDir()
if tt.createDir != "" {
r.NoError(os.MkdirAll(filepath.Join(baseDir, tt.createDir), 0700))
}
m := Metadata{
ClusterID: uuid.New().String(),
ProcessID: 123,
}
saveTo := tt.file
if tt.file != "" {
saveTo = filepath.Join(baseDir, tt.file)
}

err := m.Save(saveTo)
if tt.expectedError == nil {
r.NoError(err)
} else {
r.Regexp(*tt.expectedError, err.Error())
}
})
}
}

func Test_monitor_waitForAgentMetadata(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
Expand Down

0 comments on commit f484d45

Please sign in to comment.