-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out duplicate code in the backup-providers (#90)
- Loading branch information
Showing
19 changed files
with
189 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,34 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers" | ||
) | ||
|
||
// Sort the given list of backup versions | ||
func Sort(versions []*providers.BackupVersion) { | ||
sort.Slice(versions, func(i, j int) bool { | ||
return versions[i].Date.After(versions[j].Date) | ||
}) | ||
} | ||
|
||
// Latest returns latest backup version | ||
func Latest(versions []*providers.BackupVersion) *providers.BackupVersion { | ||
Sort(versions) | ||
if len(versions) == 0 { | ||
return nil | ||
} | ||
return versions[0] | ||
} | ||
|
||
// Latest returns the backup version at given version | ||
func Get(versions []*providers.BackupVersion, version string) (*providers.BackupVersion, error) { | ||
for _, backup := range versions { | ||
if version == backup.Version { | ||
return backup, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("version %q not found", version) | ||
} |
This file contains 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,71 @@ | ||
package common | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSort(t *testing.T) { | ||
now := time.Now() | ||
tests := []struct { | ||
name string | ||
versions []*providers.BackupVersion | ||
wantedVersions []*providers.BackupVersion | ||
}{ | ||
{ | ||
name: "mixed", | ||
versions: []*providers.BackupVersion{ | ||
{Name: "2.tgz", Date: now.Add(2 * time.Hour)}, | ||
{Name: "1.tgz", Date: now.Add(1 * time.Hour)}, | ||
{Name: "5.tgz", Date: now.Add(5 * time.Hour)}, | ||
{Name: "0.tgz", Date: now}, | ||
{Name: "3.tgz", Date: now.Add(3 * time.Hour)}, | ||
}, | ||
wantedVersions: []*providers.BackupVersion{ | ||
{Name: "0.tgz", Date: now}, | ||
{Name: "1.tgz", Date: now.Add(1 * time.Hour)}, | ||
{Name: "2.tgz", Date: now.Add(2 * time.Hour)}, | ||
{Name: "3.tgz", Date: now.Add(3 * time.Hour)}, | ||
{Name: "5.tgz", Date: now.Add(5 * time.Hour)}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
Sort(tt.versions) | ||
require.ElementsMatch(t, tt.versions, tt.wantedVersions) | ||
}) | ||
} | ||
} | ||
|
||
func TestLatest(t *testing.T) { | ||
now := time.Now() | ||
newestBackup := &providers.BackupVersion{Name: "5.tgz", Date: now.Add(5 * time.Hour)} | ||
tests := []struct { | ||
name string | ||
versions []*providers.BackupVersion | ||
want *providers.BackupVersion | ||
}{ | ||
{ | ||
versions: []*providers.BackupVersion{ | ||
{Name: "2.tgz", Date: now.Add(2 * time.Hour)}, | ||
{Name: "0.tgz", Date: now}, | ||
{Name: "1.tgz", Date: now.Add(1 * time.Hour)}, | ||
newestBackup, | ||
{Name: "3.tgz", Date: now.Add(3 * time.Hour)}, | ||
}, | ||
want: newestBackup, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Latest(tt.versions); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Latest() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.