-
Notifications
You must be signed in to change notification settings - Fork 17
OADP-7895 Fix for the oc oadp nonadmin backup describe shows Pod Volu… #262
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
base: oadp-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| Copyright 2025 The OADP CLI Contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package backup | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestFormatPodVolumeBackupsCompact tests the formatPodVolumeBackupsCompact function | ||
| func TestFormatPodVolumeBackupsCompact(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please run |
||
| volumeInfo string | ||
| expectedLines []string | ||
| shouldContain []string | ||
| shouldNotContain []string | ||
| }{ | ||
| { | ||
| name: "empty volume info", | ||
| volumeInfo: "", | ||
| expectedLines: []string{}, | ||
| }, | ||
| { | ||
| name: "whitespace only volume info", | ||
| volumeInfo: " \n\t ", | ||
| expectedLines: []string{}, | ||
| }, | ||
| { | ||
| name: "malformed JSON", | ||
| volumeInfo: "not json at all", | ||
| expectedLines: []string{}, | ||
| }, | ||
| { | ||
| name: "single pod with single volume succeeded", | ||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"test-pod","podNamespace":"default","volumeName":"vol1","size":1024}}]`, | ||
| shouldContain: []string{ | ||
| "Completed:", | ||
| "default/test-pod:", | ||
| "vol1 (size: 1024)", | ||
| }, | ||
| }, | ||
| { | ||
| name: "single pod with multiple volumes", | ||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"test-pod","podNamespace":"default","volumeName":"vol1","size":1024}},{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"test-pod","podNamespace":"default","volumeName":"vol2","size":2048}}]`, | ||
| shouldContain: []string{ | ||
| "Completed:", | ||
| "default/test-pod:", | ||
| "vol1 (size: 1024)", | ||
| "vol2 (size: 2048)", | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple pods with volumes", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this test compare the complete expected output for the multi-pod case? The current substring checks do not verify indentation, line layout, or the sorted pod order that this formatter is meant to provide. |
||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"pod1","podNamespace":"ns1","volumeName":"vol1","size":1024}},{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"pod2","podNamespace":"ns2","volumeName":"vol2","size":2048}}]`, | ||
| shouldContain: []string{ | ||
| "Completed:", | ||
| "ns1/pod1:", | ||
| "ns2/pod2:", | ||
| "vol1 (size: 1024)", | ||
| "vol2 (size: 2048)", | ||
| }, | ||
| }, | ||
| { | ||
| name: "failed PVB should be excluded", | ||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"pod1","podNamespace":"ns1","volumeName":"vol1","size":1024}},{"backupMethod":"PodVolumeBackup","result":"Failed","pvbInfo":{"podName":"pod2","podNamespace":"ns2","volumeName":"vol2","size":2048}}]`, | ||
| shouldContain: []string{ | ||
| "Completed:", | ||
| "ns1/pod1:", | ||
| "vol1 (size: 1024)", | ||
| }, | ||
| shouldNotContain: []string{ | ||
| "ns2/pod2", | ||
| "vol2", | ||
| }, | ||
| }, | ||
| { | ||
| name: "non-PodVolumeBackup entries should be ignored", | ||
| volumeInfo: `[{"backupMethod":"VeleroSnapshot","result":"succeeded","pvbInfo":{"podName":"pod1","podNamespace":"ns1","volumeName":"vol1","size":1024}},{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"pod2","podNamespace":"ns2","volumeName":"vol2","size":2048}}]`, | ||
| shouldContain: []string{ | ||
| "Completed:", | ||
| "ns2/pod2:", | ||
| "vol2 (size: 2048)", | ||
| }, | ||
| shouldNotContain: []string{ | ||
| "ns1/pod1", | ||
| "vol1", | ||
| }, | ||
| }, | ||
| { | ||
| name: "empty array", | ||
| volumeInfo: `[]`, | ||
| expectedLines: []string{}, | ||
| }, | ||
| { | ||
| name: "missing pvbInfo should be skipped", | ||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"Completed"}]`, | ||
| expectedLines: []string{}, | ||
| }, | ||
| { | ||
| name: "missing size field defaults to 0", | ||
| volumeInfo: `[{"backupMethod":"PodVolumeBackup","result":"succeeded","pvbInfo":{"podName":"pod1","podNamespace":"ns1","volumeName":"vol1"}}]`, | ||
| shouldContain: []string{ | ||
| "ns1/pod1:", | ||
| "vol1 (size: 0)", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := formatPodVolumeBackupsCompact(tt.volumeInfo) | ||
|
|
||
| // Check empty results (when no expected lines and no "should contain") | ||
| if len(tt.expectedLines) == 0 && len(tt.shouldContain) == 0 && result != "" { | ||
| t.Errorf("expected empty output, got: %q", result) | ||
| } | ||
|
|
||
| // Check contains | ||
| for _, expected := range tt.shouldContain { | ||
| if !contains(result, expected) { | ||
| t.Errorf("expected output to contain %q, got:\n%s", expected, result) | ||
| } | ||
| } | ||
|
|
||
| // Check not contains | ||
| for _, notExpected := range tt.shouldNotContain { | ||
| if contains(result, notExpected) { | ||
| t.Errorf("expected output to NOT contain %q, got:\n%s", notExpected, result) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Helper function for testing | ||
| func contains(text string, substring string) bool { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace this helper with |
||
| return len(text) > 0 && (text == substring || len(substring) > 0 && (text[:len(substring)] == substring || len(text) > len(substring) && text[len(text)-len(substring):] == substring || findSubstring(text, substring))) | ||
| } | ||
|
|
||
| func findSubstring(text string, substring string) bool { | ||
| for i := 0; i <= len(text)-len(substring); i++ { | ||
| if text[i:i+len(substring)] == substring { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.