-
Notifications
You must be signed in to change notification settings - Fork 0
fix: agent reconciles pipeline directory on every poll #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+154
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/TerrifiedBug/vectorflow/agent/internal/client" | ||
| "github.com/TerrifiedBug/vectorflow/agent/internal/config" | ||
| ) | ||
|
|
||
| type mockConfigFetcher struct { | ||
| resp *client.ConfigResponse | ||
| } | ||
|
|
||
| func (m *mockConfigFetcher) GetConfig() (*client.ConfigResponse, error) { | ||
| return m.resp, nil | ||
| } | ||
|
|
||
| func TestPoll_RemovesOrphanedPipelineFiles(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pipelinesDir := filepath.Join(tmpDir, "pipelines") | ||
| os.MkdirAll(pipelinesDir, 0700) | ||
|
|
||
| // Pre-existing files: one valid pipeline, one orphan, one orphan with metrics sidecar | ||
| os.WriteFile(filepath.Join(pipelinesDir, "pipeline-a.yaml"), []byte("valid"), 0600) | ||
| os.WriteFile(filepath.Join(pipelinesDir, "orphan-b.yaml"), []byte("stale"), 0600) | ||
| os.WriteFile(filepath.Join(pipelinesDir, "orphan-b.yaml.vf-metrics.yaml"), []byte("stale-metrics"), 0600) | ||
| os.WriteFile(filepath.Join(pipelinesDir, "orphan-c.yaml"), []byte("stale2"), 0600) | ||
| // Non-yaml file should be left alone | ||
| os.WriteFile(filepath.Join(pipelinesDir, "notes.txt"), []byte("keep"), 0600) | ||
|
|
||
| mc := &mockConfigFetcher{ | ||
| resp: &client.ConfigResponse{ | ||
| Pipelines: []client.PipelineConfig{ | ||
| { | ||
| PipelineID: "pipeline-a", | ||
| PipelineName: "Pipeline A", | ||
| ConfigYaml: "valid", | ||
| Checksum: "abc123", | ||
| Version: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| p := &poller{ | ||
| cfg: &config.Config{DataDir: tmpDir}, | ||
| client: mc, | ||
| known: make(map[string]pipelineState), | ||
| } | ||
|
|
||
| actions, err := p.Poll() | ||
| if err != nil { | ||
| t.Fatalf("Poll() error: %v", err) | ||
| } | ||
|
|
||
| // pipeline-a should be started (new to known map) | ||
| if len(actions) != 1 || actions[0].PipelineID != "pipeline-a" { | ||
| t.Errorf("expected 1 start action for pipeline-a, got %d actions", len(actions)) | ||
| } | ||
|
|
||
| // pipeline-a.yaml should still exist | ||
| if _, err := os.Stat(filepath.Join(pipelinesDir, "pipeline-a.yaml")); err != nil { | ||
| t.Error("pipeline-a.yaml should exist") | ||
| } | ||
|
|
||
| // orphan-b.yaml and its metrics sidecar should be deleted | ||
| if _, err := os.Stat(filepath.Join(pipelinesDir, "orphan-b.yaml")); !os.IsNotExist(err) { | ||
| t.Error("orphan-b.yaml should be deleted") | ||
| } | ||
| if _, err := os.Stat(filepath.Join(pipelinesDir, "orphan-b.yaml.vf-metrics.yaml")); !os.IsNotExist(err) { | ||
| t.Error("orphan-b.yaml.vf-metrics.yaml should be deleted") | ||
| } | ||
|
|
||
| // orphan-c.yaml should be deleted | ||
| if _, err := os.Stat(filepath.Join(pipelinesDir, "orphan-c.yaml")); !os.IsNotExist(err) { | ||
| t.Error("orphan-c.yaml should be deleted") | ||
| } | ||
|
|
||
| // notes.txt (non-yaml) should be left alone | ||
| if _, err := os.Stat(filepath.Join(pipelinesDir, "notes.txt")); err != nil { | ||
| t.Error("notes.txt should still exist") | ||
| } | ||
| } | ||
|
|
||
| func TestPoll_EmptyResponseCleansAllFiles(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pipelinesDir := filepath.Join(tmpDir, "pipelines") | ||
| os.MkdirAll(pipelinesDir, 0700) | ||
|
|
||
| os.WriteFile(filepath.Join(pipelinesDir, "old-a.yaml"), []byte("stale"), 0600) | ||
| os.WriteFile(filepath.Join(pipelinesDir, "old-a.yaml.vf-metrics.yaml"), []byte("stale-metrics"), 0600) | ||
| os.WriteFile(filepath.Join(pipelinesDir, "old-b.yaml"), []byte("stale"), 0600) | ||
|
|
||
| mc := &mockConfigFetcher{ | ||
| resp: &client.ConfigResponse{ | ||
| Pipelines: []client.PipelineConfig{}, | ||
| }, | ||
| } | ||
|
|
||
| p := &poller{ | ||
| cfg: &config.Config{DataDir: tmpDir}, | ||
| client: mc, | ||
| known: make(map[string]pipelineState), | ||
| } | ||
|
|
||
| _, err := p.Poll() | ||
| if err != nil { | ||
| t.Fatalf("Poll() error: %v", err) | ||
| } | ||
|
|
||
| entries, _ := os.ReadDir(pipelinesDir) | ||
| for _, e := range entries { | ||
| if strings.HasSuffix(e.Name(), ".yaml") { | ||
| t.Errorf("expected all yaml files deleted, found: %s", e.Name()) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlogged
os.Removeerrors in reconciliation blockBoth
os.Removecalls discard their return values silently, even though aslog.Warnis already emitted a line above. Compare this to the supervisor's equivalent cleanup atsupervisor.go:205–206, which logs a warning when the remove fails. If an orphaned file can't be deleted (e.g., locked by another process or a permission issue), there's no indication of the failure.