|
| 1 | +package persist |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/newrelic/infra-integrations-sdk/log" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +var tmpDir string |
| 13 | + |
| 14 | +func setupTestCase(t *testing.T) func(t *testing.T) { |
| 15 | + t.Log("setup test case") |
| 16 | + |
| 17 | + assert.NoError(t, os.RemoveAll(filepath.Join(os.TempDir(), integrationsDir))) |
| 18 | + tmpDir = tmpIntegrationDir() |
| 19 | + |
| 20 | + files := []struct { |
| 21 | + name string |
| 22 | + lastMod time.Duration |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "com.newrelic.fake-a.json", |
| 26 | + lastMod: 1 * time.Second, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "com.newrelic.fake-b.json", |
| 30 | + lastMod: 80 * time.Second, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "com.newrelic.fake-c.json", |
| 34 | + lastMod: 80 * time.Second, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "com.newrelic.flex-b.json", |
| 38 | + lastMod: 80 * time.Second, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + for _, file := range files { |
| 43 | + f, err := os.Create(filepath.Join(tmpDir, file.name)) |
| 44 | + assert.NoError(t, err) |
| 45 | + |
| 46 | + lastChanged := time.Now().Local().Add(-file.lastMod) |
| 47 | + err = os.Chtimes(f.Name(), lastChanged, lastChanged) |
| 48 | + assert.NoError(t, err) |
| 49 | + } |
| 50 | + |
| 51 | + return func(t *testing.T) { |
| 52 | + t.Log("teardown test case") |
| 53 | + assert.NoError(t, os.RemoveAll(tmpDir)) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestStorePath_CleanOldFiles(t *testing.T) { |
| 58 | + |
| 59 | + // GIVEN a tmp directory with multiple files |
| 60 | + tearDownFn := setupTestCase(t) |
| 61 | + defer tearDownFn(t) |
| 62 | + |
| 63 | + // WHEN new store file is generated |
| 64 | + newPath, err := NewStorePath("com.newrelic.fake", "c", log.Discard, 1*time.Minute) |
| 65 | + assert.NoError(t, err) |
| 66 | + |
| 67 | + // THEN only old files with different integration ID are removed |
| 68 | + newPath.CleanOldFiles() |
| 69 | + |
| 70 | + files, err := filepath.Glob(filepath.Join(tmpDir, "*")) |
| 71 | + assert.NoError(t, err) |
| 72 | + |
| 73 | + expected := []string{ |
| 74 | + filepath.Join(tmpDir, "com.newrelic.fake-a.json"), |
| 75 | + filepath.Join(tmpDir, "com.newrelic.fake-c.json"), |
| 76 | + filepath.Join(tmpDir, "com.newrelic.flex-b.json"), |
| 77 | + } |
| 78 | + assert.Equal(t, expected, files) |
| 79 | +} |
| 80 | + |
| 81 | +func TestStorePath_GetFilePath(t *testing.T) { |
| 82 | + storeFile, err := NewStorePath("com.newrelic.fake", "c", log.Discard, 1*time.Minute) |
| 83 | + assert.NoError(t, err) |
| 84 | + |
| 85 | + expected := filepath.Join(tmpIntegrationDir(), "com.newrelic.fake-c.json") |
| 86 | + assert.Equal(t, expected, storeFile.GetFilePath()) |
| 87 | +} |
| 88 | + |
| 89 | +func TestStorePath_glob(t *testing.T) { |
| 90 | + storeFile, err := NewStorePath("com.newrelic.fake", "c", log.Discard, 1*time.Minute) |
| 91 | + assert.NoError(t, err) |
| 92 | + |
| 93 | + tmp, ok := storeFile.(*storePath) |
| 94 | + assert.True(t, ok) |
| 95 | + |
| 96 | + expected := filepath.Join(tmpIntegrationDir(), "com.newrelic.fake-*.json") |
| 97 | + assert.Equal(t, expected, tmp.glob()) |
| 98 | +} |
0 commit comments