|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/mongodb/mongodb-kubernetes/diffwatch/pkg/diffwatch" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// ignored project-root/tmp dir where test outputs will be stored |
| 18 | +const tmpDir = "../../../../tmp/diffwatch" |
| 19 | +const cleanupAfterTest = false |
| 20 | + |
| 21 | +// TestDiffWatcherFromFile is a manual test that triggers simulated sequence of changes. |
| 22 | +// Intended for manual inspection of files. |
| 23 | +// |
| 24 | +// How to run: |
| 25 | +// 1. Create ops-manager-kubernetes/tmp directory |
| 26 | +// 2. Comment t.Skip and run the test |
| 27 | +// 3. View latest files: |
| 28 | +// find $(find tmp/diffwatch -d 1 -type d | sort -n | tail -n 1) -type f | sort -rV | fzf --preview 'cat {}' |
| 29 | +func TestDiffWatcherFromFile(t *testing.T) { |
| 30 | + t.Skip("Test intended to manual run, comment skip to run") |
| 31 | + ctx, cancel := context.WithCancel(context.Background()) |
| 32 | + defer cancel() |
| 33 | + |
| 34 | + require.NoError(t, os.MkdirAll(tmpDir, 0770)) |
| 35 | + tempDir, err := os.MkdirTemp(tmpDir, time.Now().Format("20060102_150405")) |
| 36 | + require.NoError(t, err) |
| 37 | + defer func() { |
| 38 | + if cleanupAfterTest { |
| 39 | + _ = os.RemoveAll(tempDir) |
| 40 | + } |
| 41 | + }() |
| 42 | + |
| 43 | + watchedFile := fmt.Sprintf("%s/watched.json", tempDir) |
| 44 | + go watchChanges(ctx, watchedFile, tempDir, nil, 4, 4, []string{"ignoredField", `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}`}) |
| 45 | + |
| 46 | + diffwatch.DefaultWatchInterval = time.Millisecond * 100 |
| 47 | + diffwatch.DefaultProgressInterval = time.Millisecond * 200 |
| 48 | + applyChange(t, "resources/base.json", watchedFile) |
| 49 | + applyChange(t, "resources/changed_1.json", watchedFile) |
| 50 | + applyChange(t, "resources/changed_2.json", watchedFile) |
| 51 | + time.Sleep(diffwatch.DefaultProgressInterval * 2) |
| 52 | + applyChange(t, "resources/changed_3.json", watchedFile) |
| 53 | + applyChange(t, "resources/changed_3_ignored_only.json", watchedFile) |
| 54 | + applyChange(t, "resources/changed_3_ignored_only_2.json", watchedFile) |
| 55 | + applyChange(t, "resources/changed_4_ignored_only_ts.json", watchedFile) |
| 56 | + applyChange(t, "resources/changed_4_ignored_only_ts_and_other.json", watchedFile) |
| 57 | + applyChange(t, "resources/changed_5.json", watchedFile) |
| 58 | +} |
| 59 | + |
| 60 | +func TestDiffWatcherFromStream(t *testing.T) { |
| 61 | + t.Skip("Test intended to manual run, comment skip to run") |
| 62 | + ctx, cancel := context.WithCancel(context.Background()) |
| 63 | + defer cancel() |
| 64 | + |
| 65 | + buf := bytes.Buffer{} |
| 66 | + files := []string{ |
| 67 | + "resources/base.json", |
| 68 | + "resources/changed_1.json", |
| 69 | + "resources/changed_2.json", |
| 70 | + "resources/changed_3.json", |
| 71 | + "resources/changed_4_ignored_only_ts.json", |
| 72 | + "resources/changed_4_ignored_only_ts_and_other.json", |
| 73 | + "resources/changed_5.json", |
| 74 | + } |
| 75 | + for _, file := range files { |
| 76 | + fileBytes, err := os.ReadFile(file) |
| 77 | + require.NoError(t, err) |
| 78 | + buf.Write(fileBytes) |
| 79 | + } |
| 80 | + |
| 81 | + require.NoError(t, os.MkdirAll(tmpDir, 0770)) |
| 82 | + tempDir, err := os.MkdirTemp(tmpDir, time.Now().Format("20060102_150405")) |
| 83 | + require.NoError(t, err) |
| 84 | + defer func() { |
| 85 | + if cleanupAfterTest { |
| 86 | + _ = os.RemoveAll(tempDir) |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + watchedFile := "watched.file" |
| 91 | + _ = watchChanges(ctx, watchedFile, tempDir, &buf, 2, 2, []string{"ignoredField", `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}`}) |
| 92 | + cancel() |
| 93 | + time.Sleep(time.Second * 1) |
| 94 | +} |
| 95 | + |
| 96 | +func applyChange(t *testing.T, srcFilePath string, dstFilePath string) { |
| 97 | + assert.NoError(t, copyFile(srcFilePath, dstFilePath, 0660)) |
| 98 | + time.Sleep(diffwatch.DefaultWatchInterval * 2) |
| 99 | +} |
| 100 | + |
| 101 | +func copyFile(srcFilePath string, dstFilePath string, mode os.FileMode) error { |
| 102 | + source, err := os.Open(srcFilePath) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + defer func() { |
| 107 | + _ = source.Close() |
| 108 | + }() |
| 109 | + |
| 110 | + destination, err := os.OpenFile(dstFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + defer func() { |
| 115 | + _ = destination.Close() |
| 116 | + }() |
| 117 | + |
| 118 | + _, err = io.Copy(destination, source) |
| 119 | + return err |
| 120 | +} |
0 commit comments