-
Notifications
You must be signed in to change notification settings - Fork 440
WRKLDS-1191: oc rsync: Add --last flag #2078
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
Open
tchap
wants to merge
1
commit into
openshift:main
Choose a base branch
from
tchap:rsync-last-n-include
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package rsync | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sort" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| // TestNewTarStrategy_FileDiscovery tests the specific file discovery logic in NewTarStrategy. | ||
| func TestNewTarStrategy_FileDiscovery(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| originalIncludes []string | ||
| discoveredFiles []string | ||
| discoveryError error | ||
| expectedIncludes []string | ||
| }{ | ||
| { | ||
| name: "discovery finds files - replaces original includes", | ||
| originalIncludes: []string{"*.log", "*.txt"}, | ||
| discoveredFiles: []string{"newest.log", "middle.log", "oldest.log"}, | ||
| expectedIncludes: []string{"newest.log", "middle.log", "oldest.log"}, | ||
| }, | ||
| { | ||
| name: "discovery finds no files - keeps original includes", | ||
| originalIncludes: []string{"*.log", "*.txt"}, | ||
| discoveredFiles: []string{}, | ||
| expectedIncludes: []string{"*.log", "*.txt"}, | ||
| }, | ||
| { | ||
| name: "discovery fails - keeps original includes", | ||
| originalIncludes: []string{"*.log", "*.txt"}, | ||
| discoveryError: errors.New("command failed"), | ||
| expectedIncludes: []string{"*.log", "*.txt"}, | ||
| }, | ||
| { | ||
| name: "no original includes but discovery finds files", | ||
| originalIncludes: []string{}, | ||
| discoveredFiles: []string{"file1.txt", "file2.txt"}, | ||
| expectedIncludes: []string{"file1.txt", "file2.txt"}, | ||
| }, | ||
| { | ||
| name: "no original includes and no discovery", | ||
| originalIncludes: []string{}, | ||
| discoveredFiles: []string{}, | ||
| expectedIncludes: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Init the strategy. | ||
| options := &RsyncOptions{ | ||
| RsyncInclude: tc.originalIncludes, | ||
| Last: 3, // Enable file discovery | ||
| Source: &PathSpec{PodName: "test-pod", Path: "/test/path"}, | ||
| Destination: &PathSpec{Path: "/local/path"}, | ||
| fileDiscovery: &mockFileDiscoverer{ | ||
| files: tc.discoveredFiles, | ||
| err: tc.discoveryError, | ||
| }, | ||
| } | ||
|
|
||
| strategy := NewTarStrategy(options).(*tarStrategy) | ||
|
|
||
| // Verify the result matches expectations. | ||
| sort.Strings(strategy.Includes) | ||
| sort.Strings(tc.expectedIncludes) | ||
| if !cmp.Equal(strategy.Includes, tc.expectedIncludes) { | ||
| t.Errorf("expected includes mismatch: \n%s\n", | ||
| cmp.Diff(tc.expectedIncludes, strategy.Includes)) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,7 @@ | ||
| package rsync | ||
|
|
||
| // fileDiscoverer discovers files at the given path, | ||
| // limiting the list to lastN most recently modified files. | ||
| type fileDiscoverer interface { | ||
| DiscoverFiles(basePath string, lastN uint) ([]string, error) | ||
| } |
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,65 @@ | ||
| package rsync | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // localFileDiscoverer implements fileDiscoverer interface for local directories. | ||
| type localFileDiscoverer struct{} | ||
|
|
||
| func newLocalFileDiscoverer() localFileDiscoverer { | ||
| return localFileDiscoverer{} | ||
| } | ||
|
|
||
| func (discoverer localFileDiscoverer) DiscoverFiles(basePath string, last uint) ([]string, error) { | ||
| klog.V(4).Infof("Discovering files in local directory %s (last = %d)", basePath, last) | ||
|
|
||
| entries, err := os.ReadDir(basePath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read directory %s: %w", basePath, err) | ||
| } | ||
|
|
||
| type fileInfo struct { | ||
| name string | ||
| modTime time.Time | ||
| } | ||
|
|
||
| files := make([]fileInfo, 0, len(entries)) | ||
| for _, entry := range entries { | ||
| if entry.IsDir() { | ||
| continue // Skip directories, only process regular files. | ||
| } | ||
|
|
||
| info, err := entry.Info() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get file info for %s: %w", entry.Name(), err) | ||
| } | ||
|
|
||
| files = append(files, fileInfo{ | ||
| name: entry.Name(), | ||
| modTime: info.ModTime(), | ||
| }) | ||
| } | ||
|
|
||
| // Sort by modification time (newest first). | ||
| sort.Slice(files, func(i, j int) bool { | ||
| return files[i].modTime.After(files[j].modTime) | ||
| }) | ||
|
|
||
| // Limit to the latest N files. | ||
| if len(files) > int(last) { | ||
| files = files[:last] | ||
| } | ||
|
|
||
| // Extract just the file names (relative paths). | ||
| result := make([]string, len(files)) | ||
| for i, file := range files { | ||
| result[i] = file.name | ||
| } | ||
| return result, nil | ||
| } |
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.