Skip to content

Commit

Permalink
fixes for backup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allen-munsch committed Oct 15, 2018
1 parent d0b14d2 commit 0fe782d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
17 changes: 11 additions & 6 deletions cmd/commands_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func serversBackupGetSchedule(cmd *cli.Cmd) {
cmd.Spec = "SUBID"
id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")

server, err := GetClient().BackupSetSchedule(*id)
server, err := GetClient().BackupGetSchedule(*id)
if err != nil {
log.Fatal(err)
}
Expand All @@ -92,11 +92,16 @@ func serversBackupSetSchedule(cmd *cli.Cmd) {

id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
cronType := cmd.StringArg("C cronType", "", "Backup cron type. Can be one of (daily, weekly, monthly, daily_alt_even, daily_alt_odd)")
hour := cmd.StringArg("H hour", "", "(optional) Hour value (0-23). Applicable to crons: daily, weekly, monthly, daily_alt_even, daily_alt_odd")
dayOfWeek := cmd.StringArg("w dow", "", "(optional) Day-of-week value (0-6). Applicable to crons: weekly")
dayOfMonth := cmd.StringArg("m dom", "", "(optional) Day-of-month value (1-28). Applicable to crons: monthly")

server, err := GetClient().BackupSetSchedule(*id, *cronType, *hour, *dayOfWeek, *dayOfMonth)
hour := cmd.IntOpt("H hour", 0, "(optional) Hour value (0-23). Applicable to crons: daily, weekly, monthly, daily_alt_even, daily_alt_odd")
dayOfWeek := cmd.IntOpt("w dow", 0, "(optional) Day-of-week value (0-6). Applicable to crons: weekly")
dayOfMonth := cmd.IntOpt("m dom", 0, "(optional) Day-of-month value (1-28). Applicable to crons: monthly")
bs := vultr.BackupSchedule{
CronType: *cronType,
Hour: *hour,
Dow: *dayOfWeek,
Dom: *dayOfMonth,
}
err := GetClient().BackupSetSchedule(*id, bs)
if err != nil {
log.Fatal(err)
}
Expand Down
28 changes: 18 additions & 10 deletions lib/backup.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package lib

import (
"fmt"
"net/url"
"sort"
"strings"
"time"
)

// Backup of a virtual machine
Expand All @@ -17,14 +18,19 @@ type Backup struct {

type backups []Backup

func (s backups) Len() int { return len(s) }
func (s backups) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s backups) Less(i, j int) bool {
return strings.ToLower(s[i].Name) < strings.ToLower(s[j].Name)
func (bs backups) Len() int { return len(bs) }
func (bs backups) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] }

// sort by most recent
func (bs backups) Less(i, j int) bool {
timeLayout := "2006-01-02 15:04:05" // oh my : https://golang.org/src/time/format.go
t1, _ := time.Parse(timeLayout, bs[i].Created)
t2, _ := time.Parse(timeLayout, bs[j].Created)
return t1.After(t2)
}

// GetBackups retrieves a list of all backups on Vultr account
func (c *Client) GetBackups(id string, backupid string) (backups []Backup, err error) {
func (c *Client) GetBackups(id string, backupid string) ([]Backup, error) {
var backupMap map[string]Backup
values := url.Values{
"SUBID": {id},
Expand All @@ -35,9 +41,11 @@ func (c *Client) GetBackups(id string, backupid string) (backups []Backup, err e
return nil, err
}

for _, backup := range backupMap {
backups = append(backups, backup)
var backup []Backup
for _, b := range backupMap {
fmt.Println(b)
backup = append(backup, b)
}
sort.Sort(backups(backups))
return backups, nil
sort.Sort(backups(backup))
return backup, nil
}
14 changes: 7 additions & 7 deletions lib/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ func Test_Backups_GetBackups_OK(t *testing.T) {
}`)
defer server.Close()

snapshots, err := client.GetBackups()
snapshots, err := client.GetBackups("123456789", "1")
if err != nil {
t.Error(err)
}
if assert.NotNil(t, snapshots) {
assert.Equal(t, 2, len(snapshots))

assert.Equal(t, "543d340f6dbce", snapshots[0].ID)
assert.Equal(t, "2014-10-13 16:11:46", snapshots[0].Created)
assert.Equal(t, "a", snapshots[0].Description)
assert.Equal(t, "10000000", snapshots[0].Size)
assert.Equal(t, "complete", snapshots[0].Status)

assert.Equal(t, "543d34149403a", snapshots[0].ID)
assert.Equal(t, "2014-10-14 12:40:40", snapshots[0].Created)
assert.Equal(t, "Automatic server backup", snapshots[0].Description)
assert.Equal(t, "42949672960", snapshots[0].Size)
assert.Equal(t, "complete", snapshots[0].Status)

assert.Equal(t, "543d340f6dbce", snapshots[1].ID)
assert.Equal(t, "2014-10-13 16:11:46", snapshots[1].Created)
assert.Equal(t, "a", snapshots[1].Description)
assert.Equal(t, "10000000", snapshots[1].Size)
assert.Equal(t, "complete", snapshots[1].Status)
}
}
24 changes: 16 additions & 8 deletions lib/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,30 +649,38 @@ func (c *Client) EnablePrivateNetworkForServer(id, networkID string) error {
// BackupSchedule represents a scheduled backup on a server
// see: server/backup_set_schedule, server/backup_get_schedule
type BackupSchedule struct {
Enabled bool `json:"enabled"`
CronType string `json:"cron_type"`
NextScheduledTimeUtc string `json:"next_scheduled_time_utc"`
Hour int `json:"hour"`
Dow int `json:"dow"`
Dom int `json:"dom"`
}

type BackupScheduleResponse struct {
Enabled bool `json:"enabled"`
BackupSchedule
}

// BackupGetSchedule
func (c *Client) BackupGetSchedule(id string) (BackupSchedule, error) {
func (c *Client) BackupGetSchedule(id string) (BackupScheduleResponse, error) {
values := url.Values{
"SUBID": {id},
}
return c.post(`server/backup_get_schedule`, values, nil)
var bsr BackupScheduleResponse
if err := c.post(`server/backup_get_schedule`, values, &bsr); err != nil {
return bsr, err
}
return bsr, nil
}

// BackupSetSchedule
func (c *Client) BackupSetSchedule(id string, cronType string, hour int, dayOfWeek int, dayOfMonth int) error {
func (c *Client) BackupSetSchedule(id string, bs BackupSchedule) error {
values := url.Values{
"SUBID": {id},
"cron_type": {cronType},
"hour": {hour},
"dow": {dayOfWeek},
"dom": {dayOfMonth},
"cron_type": {bs.CronType},
"hour": {string(bs.Hour)},
"dow": {string(bs.Dow)},
"dom": {string(bs.Dom)},
}
return c.post(`server/backup_set_schedule`, values, nil)
}
17 changes: 8 additions & 9 deletions lib/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,17 +712,16 @@ func Test_Servers_BackupGetSchedule_OK(t *testing.T) {
defer server.Close()

backupSchedule, err := client.BackupGetSchedule("123456789")
if err {
if err != nil {
t.Error(err)
}
if assert.NotNil(t, backupSchedule) {
assert.Equal(t, 1, len(backupSchedule))
assert.Equal(t, true, backupSchedule[0].Enabled)
assert.Equal(t, "weekly", backupSchedule[0].CronType)
assert.Equal(t, "2016-05-07 08:00:00", backupSchedule[0].NextScheduledTimeUtc)
assert.Equal(t, "8", backupSchedule[0].Hour)
assert.Equal(t, "6", backupSchedule[0].Dow)
assert.Equal(t, "0", backupSchedule[0].Dom)
assert.Equal(t, true, backupSchedule.Enabled)
assert.Equal(t, "weekly", backupSchedule.CronType)
assert.Equal(t, "2016-05-07 08:00:00", backupSchedule.NextScheduledTimeUtc)
assert.Equal(t, 8, backupSchedule.Hour)
assert.Equal(t, 6, backupSchedule.Dow)
assert.Equal(t, 0, backupSchedule.Dom)

}

Expand All @@ -732,5 +731,5 @@ func Test_Servers_BackupSetSchedule_OK(t *testing.T) {
server, client := getTestServerAndClient(http.StatusOK, `{no-response?!}`)
defer server.Close()

assert.Nil(t, client.BackupSetSchedule("123456789"))
assert.Nil(t, client.BackupSetSchedule("123456789", BackupSchedule{}))
}

0 comments on commit 0fe782d

Please sign in to comment.