diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f11bd69c5..7ba9533f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ master ] + branches: [master] pull_request: - branches: [ master ] + branches: [master] concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - flags: [ '', '-race' ] + flags: ["", "-race"] steps: - name: Checkout code uses: actions/checkout@v4 @@ -24,7 +24,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.24' + go-version: "1.24" cache: true - name: Build @@ -35,7 +35,21 @@ jobs: GOFLAGS: ${{ matrix.flags }} LOG_LEVEL: error run: | - go test -coverprofile=profile${{ matrix.flags }}.out -covermode=atomic -v -coverpkg=./... ./... -json | tee test-report${{ matrix.flags }}.json + go test -coverprofile=profile${{ matrix.flags }}.out -covermode=atomic -coverpkg=./... ./... -json 2>&1 | tee test-report${{ matrix.flags }}.json + TEST_EXIT_CODE=${PIPESTATUS[0]} + exit $TEST_EXIT_CODE + + - name: Fix Test Report + if: always() + run: | + if [ -f test-report${{ matrix.flags }}.json ]; then + # Add final event if missing + if ! tail -1 test-report${{ matrix.flags }}.json | grep -q '"Action":"end"'; then + echo '{"Time":"'$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")'","Action":"end","Package":".","Elapsed":0}' >> test-report${{ matrix.flags }}.json + fi + else + echo '{"Time":"'$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")'","Action":"end","Package":".","Elapsed":0}' > test-report${{ matrix.flags }}.json + fi - name: Coverage report run: | @@ -82,7 +96,7 @@ jobs: strategy: fail-fast: false matrix: - flags: [ '', '-race' ] + flags: ["", "-race"] steps: - name: Checkout code uses: actions/checkout@v4 @@ -90,7 +104,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.24' + go-version: "1.24" cache: true - name: Run docker containers @@ -101,7 +115,9 @@ jobs: GOFLAGS: ${{ matrix.flags }} LOG_LEVEL: error run: | - go test ./e2e -coverprofile=profile-e2e${{ matrix.flags }}.out -covermode=atomic -tags=e2e_new -timeout=3m -coverpkg=./... -json | tee test-report-e2e${{ matrix.flags }}.json + go test ./e2e -coverprofile=profile-e2e${{ matrix.flags }}.out -covermode=atomic -tags=e2e_new -timeout=3m -coverpkg=./... -json 2>&1 | tee test-report-e2e${{ matrix.flags }}.json + TEST_EXIT_CODE=${PIPESTATUS[0]} + exit $TEST_EXIT_CODE - name: Coverage report run: | @@ -110,7 +126,7 @@ jobs: else echo "No coverage data generated for ${{ matrix.flags }} tests" > coverage-e2e${{ matrix.flags }}.html fi - + - name: Upload test artifacts uses: actions/upload-artifact@v4 with: @@ -161,7 +177,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.24' + go-version: "1.24" cache: true - name: Lint @@ -186,7 +202,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.24' + go-version: "1.24" cache: true - name: Generate doc @@ -210,7 +226,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.24' + go-version: "1.24" cache: true - name: Download dependencies diff --git a/plugin/input/file/watcher.go b/plugin/input/file/watcher.go index cd3afaec3..08c148345 100644 --- a/plugin/input/file/watcher.go +++ b/plugin/input/file/watcher.go @@ -150,7 +150,7 @@ func (w *watcher) notify(e notify.Event, path string) { return } - w.logger.Infof("notify %s %s", e, path) + w.logger.Debugf("notify %s %s", e, path) for _, pattern := range w.paths.Exclude { match, err := doublestar.PathMatch(pattern, path) diff --git a/plugin/input/file/watcher_test.go b/plugin/input/file/watcher_test.go index c4f8e160e..f82e02d99 100644 --- a/plugin/input/file/watcher_test.go +++ b/plugin/input/file/watcher_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" "time" @@ -32,7 +33,9 @@ func TestWatcher(t *testing.T) { t.Run(tt.name, func(t *testing.T) { dir := t.TempDir() shouldCreate := atomic.Int64{} + var wg sync.WaitGroup notifyFn := func(_ notify.Event, _ string, _ os.FileInfo) { + defer wg.Done() shouldCreate.Inc() } ctl := metric.NewCtl("test", prometheus.NewRegistry()) @@ -67,8 +70,7 @@ func TestWatcher(t *testing.T) { require.NoError(t, err) err = f2.Close() require.NoError(t, err) - - time.Sleep(10 * time.Millisecond) + wg.Add(1) f1, err = os.OpenFile(f1Name, os.O_WRONLY, 0o600) require.NoError(t, err) @@ -77,13 +79,11 @@ func TestWatcher(t *testing.T) { err = f1.Close() require.NoError(t, err) - time.Sleep(10 * time.Millisecond) - err = os.Remove(f1Name) require.NoError(t, err) + wg.Add(1) - time.Sleep(10 * time.Millisecond) - + wg.Wait() require.Equal(t, int64(2), shouldCreate.Load()) }) } @@ -193,7 +193,10 @@ func TestWatcherPaths(t *testing.T) { time.Sleep(10 * time.Millisecond) before := shouldCreate.Load() - w.notify(notify.Create, filename) + + resolvedFilename, err := resolvePathLinks(filename) + require.NoError(t, err) + w.notify(notify.Create, resolvedFilename) after := shouldCreate.Load() isNotified := after-before != 0 @@ -216,9 +219,9 @@ func TestCommonPathPrefix(t *testing.T) { func TestResolvePathLinks(t *testing.T) { a := assert.New(t) - originalDir := t.TempDir() + originalDir, _ := resolvePathLinks(t.TempDir()) - dirWithLink := t.TempDir() + dirWithLink, _ := resolvePathLinks(t.TempDir()) dirLink := filepath.Join(dirWithLink, "symlink") err := os.Symlink(originalDir, dirLink) if err != nil {