-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add --dry-run flag to prepare and restore commands #147
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
tonyandrewmeyer
merged 49 commits into
canonical:main
from
tonyandrewmeyer:feat-dry-run
Feb 17, 2026
Merged
Changes from 36 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
00495f8
feat: add --dry-run flag to prepare and restore commands
tonyandrewmeyer c503c9f
Address copilot review comments.
tonyandrewmeyer 517e628
Initial plan
Copilot 342f1a2
Address remaining review feedback on dry-run feature
Copilot e4732c9
Merge pull request #3 from tonyandrewmeyer/copilot/sub-pr-2
tonyandrewmeyer 3cf2557
Simplify README addition.
tonyandrewmeyer 66fdfc3
More minimal diff.
tonyandrewmeyer 8a78a81
Tweak punctuation.
tonyandrewmeyer f0189dd
Avoid having to define past and present tense.
tonyandrewmeyer 286e243
Update README.md
tonyandrewmeyer 6de765e
Update README.md
tonyandrewmeyer b329c5d
Fix name clash.
tonyandrewmeyer 3a9b8b6
Sacrifice more human readable descriptions in favour of fewer custom …
tonyandrewmeyer 2e5e6c7
fix: Update spread tests to match auto-print output format
tonyandrewmeyer 5d72d39
Update internal/packages/snap_handler.go
tonyandrewmeyer 9ce55e3
Update README.md
tonyandrewmeyer fbf667a
fix: Address review comments for dry-run feature
tonyandrewmeyer 13aae96
Update internal/packages/dryrun_test.go
tonyandrewmeyer a9bff3c
Update cmd/main.go
tonyandrewmeyer b5c3edd
Update tests/dry-run-prepare/task.yaml
tonyandrewmeyer f73da87
Update tests/dry-run-restore/task.yaml
tonyandrewmeyer 8c62bd2
Update internal/system/dryrun.go
tonyandrewmeyer c15a7d2
fix: Improve TestDryRunWorkerDelegatesReadOperations test
tonyandrewmeyer bedc395
Merge branch 'main' into feat-dry-run
tonyandrewmeyer 33993f8
Merge branch 'main' into feat-dry-run
tonyandrewmeyer c83b485
Make the dry run output copy and pastable into a shell.
tonyandrewmeyer 62cead2
Remove the 'heading' dry run statements.
tonyandrewmeyer 712439a
stdout doesn't need to be surrounded by locks, so we can just do dire…
tonyandrewmeyer 026aab7
Rather than suppressing log messages entirely in dry-run mode, just b…
tonyandrewmeyer 99a12f6
Remove pointless test.
tonyandrewmeyer 753475f
Remove outdated comment.
tonyandrewmeyer e299673
Update cmd/main.go
tonyandrewmeyer 1a4a857
Remove test that isn't really checking anything, particularly now tha…
tonyandrewmeyer b5d99b2
Adjust the non-command dry-run steps to also be shell-like.
tonyandrewmeyer a3a9760
Remove duplicate skipping of config recording in dry-run mode.
tonyandrewmeyer d5c69c1
Merge branch 'main' into feat-dry-run
tonyandrewmeyer 61b01e0
Do exact changes, per review suggestion.
tonyandrewmeyer fe4888d
Simplify dry-run explanation.
tonyandrewmeyer 4721a66
Dry run is a CLI arg, but isn't part of config.
tonyandrewmeyer 1832787
Keep checking if a command exists in the path, but run and log just t…
tonyandrewmeyer bec5014
restore should behave the same way with and without --dry-run
tonyandrewmeyer 3a0f694
Fix the way that dry-run is handled with restore.
tonyandrewmeyer 1e79d0f
Add integration tests that verify the load-saved-config behaviour.
tonyandrewmeyer 15af2b8
This test is really just testing the packages, and we have tests for …
tonyandrewmeyer 1752da3
Adjust the dry run behaviour for bootstrap.
tonyandrewmeyer 81e1c80
Update internal/concierge/manager.go
tonyandrewmeyer cc887bf
Change approach to run commands with an optional read-only flag.
tonyandrewmeyer e39051d
Use the readonly flag for which iptables as well.
tonyandrewmeyer 6b11a49
Merge branch 'main' into feat-dry-run
tonyandrewmeyer 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
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
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
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,201 @@ | ||
| package packages | ||
tonyandrewmeyer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/user" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/canonical/concierge/internal/system" | ||
| ) | ||
|
|
||
| // testDryRunWorker is a test implementation that tracks commands | ||
| // for verification without actually executing them | ||
| type testDryRunWorker struct { | ||
| mu sync.Mutex | ||
| executedCmds []string | ||
| mockSnapInfo map[string]*system.SnapInfo | ||
| } | ||
|
|
||
| func newTestDryRunWorker() *testDryRunWorker { | ||
| return &testDryRunWorker{ | ||
| executedCmds: []string{}, | ||
| mockSnapInfo: map[string]*system.SnapInfo{}, | ||
| } | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) User() *user.User { | ||
| return &user.User{Username: "test", Uid: "1000", Gid: "1000", HomeDir: "/tmp"} | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) Run(c *system.Command) ([]byte, error) { | ||
| // This mock returns success without actually executing the command. | ||
| // We track the command strings for potential test assertions. | ||
| t.mu.Lock() | ||
| t.executedCmds = append(t.executedCmds, c.CommandString()) | ||
| t.mu.Unlock() | ||
| return []byte{}, nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) RunMany(commands ...*system.Command) error { | ||
| for _, c := range commands { | ||
| t.Run(c) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) RunExclusive(c *system.Command) ([]byte, error) { | ||
| return t.Run(c) | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) RunWithRetries(c *system.Command, maxDuration time.Duration) ([]byte, error) { | ||
| return t.Run(c) | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) WriteHomeDirFile(filepath string, contents []byte) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) ReadHomeDirFile(filepath string) ([]byte, error) { | ||
| return nil, fmt.Errorf("file not found") | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) ReadFile(filePath string) ([]byte, error) { | ||
| return nil, fmt.Errorf("file not found") | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) SnapInfo(snap string, channel string) (*system.SnapInfo, error) { | ||
| if info, ok := t.mockSnapInfo[snap]; ok { | ||
| return info, nil | ||
| } | ||
| return &system.SnapInfo{Installed: false, Classic: false}, nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) SnapChannels(snap string) ([]string, error) { | ||
| return []string{"stable", "edge"}, nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) RemovePath(path string) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) MkdirAll(path string, perm os.FileMode) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) ChownAll(path string, user *user.User) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testDryRunWorker) MockSnapInfo(name string, installed, classic bool) { | ||
| t.mockSnapInfo[name] = &system.SnapInfo{ | ||
| Installed: installed, | ||
| Classic: classic, | ||
| } | ||
| } | ||
|
|
||
| // Verify testDryRunWorker implements system.Worker | ||
| var _ system.Worker = (*testDryRunWorker)(nil) | ||
|
|
||
| func TestSnapHandlerExecutesCorrectCommands(t *testing.T) { | ||
| drw := newTestDryRunWorker() | ||
| drw.MockSnapInfo("existing-snap", true, false) | ||
|
|
||
| snaps := []*system.Snap{ | ||
| system.NewSnap("new-snap", "stable", []string{}), | ||
| system.NewSnap("existing-snap", "stable", []string{}), | ||
| } | ||
|
|
||
| handler := NewSnapHandler(drw, snaps) | ||
| err := handler.Prepare() | ||
| if err != nil { | ||
| t.Fatalf("Prepare should not fail: %v", err) | ||
| } | ||
|
|
||
| // Verify the correct commands were issued | ||
| foundInstall := false | ||
| foundRefresh := false | ||
| for _, cmd := range drw.executedCmds { | ||
| if strings.Contains(cmd, "snap install new-snap") { | ||
| foundInstall = true | ||
| } | ||
| if strings.Contains(cmd, "snap refresh existing-snap") { | ||
| foundRefresh = true | ||
| } | ||
| } | ||
|
|
||
| if !foundInstall { | ||
| t.Errorf("expected 'snap install new-snap' command, got: %v", drw.executedCmds) | ||
| } | ||
| if !foundRefresh { | ||
| t.Errorf("expected 'snap refresh existing-snap' command, got: %v", drw.executedCmds) | ||
| } | ||
| } | ||
|
|
||
| func TestSnapHandlerRestoreExecutesCorrectCommands(t *testing.T) { | ||
| drw := newTestDryRunWorker() | ||
|
|
||
| snaps := []*system.Snap{ | ||
| system.NewSnap("snap-to-remove", "stable", []string{}), | ||
| } | ||
|
|
||
| handler := NewSnapHandler(drw, snaps) | ||
| err := handler.Restore() | ||
| if err != nil { | ||
| t.Fatalf("Restore should not fail: %v", err) | ||
| } | ||
|
|
||
| foundRemove := false | ||
| for _, cmd := range drw.executedCmds { | ||
| if strings.Contains(cmd, "snap remove snap-to-remove") { | ||
| foundRemove = true | ||
| } | ||
| } | ||
|
|
||
| if !foundRemove { | ||
| t.Errorf("expected 'snap remove snap-to-remove' command, got: %v", drw.executedCmds) | ||
| } | ||
| } | ||
|
|
||
| func TestDebHandlerExecutesCorrectCommands(t *testing.T) { | ||
| drw := newTestDryRunWorker() | ||
|
|
||
| debs := []*Deb{ | ||
| {Name: "make"}, | ||
| {Name: "python3"}, | ||
| } | ||
|
|
||
| handler := NewDebHandler(drw, debs) | ||
| err := handler.Prepare() | ||
| if err != nil { | ||
| t.Fatalf("Prepare should not fail: %v", err) | ||
| } | ||
|
|
||
| foundUpdate := false | ||
| foundMake := false | ||
| foundPython := false | ||
| for _, cmd := range drw.executedCmds { | ||
| if strings.Contains(cmd, "apt-get update") { | ||
| foundUpdate = true | ||
| } | ||
| if strings.Contains(cmd, "apt-get install") && strings.Contains(cmd, "make") { | ||
| foundMake = true | ||
| } | ||
| if strings.Contains(cmd, "apt-get install") && strings.Contains(cmd, "python3") { | ||
| foundPython = true | ||
| } | ||
| } | ||
|
|
||
| if !foundUpdate { | ||
| t.Errorf("expected 'apt-get update' command, got: %v", drw.executedCmds) | ||
| } | ||
| if !foundMake { | ||
| t.Errorf("expected 'apt-get install make' command, got: %v", drw.executedCmds) | ||
| } | ||
| if !foundPython { | ||
| t.Errorf("expected 'apt-get install python3' command, got: %v", drw.executedCmds) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.