|
|
|
@ -17,7 +17,7 @@ |
|
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"runtime" |
|
|
|
|
"fmt" |
|
|
|
|
"strings" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
@ -26,25 +26,69 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Tests update notifier string builder.
|
|
|
|
|
func TestUpdateNotifier(t *testing.T) { |
|
|
|
|
plainMsg := "You are running an older version of Minio released " |
|
|
|
|
colorMsg := plainMsg |
|
|
|
|
yellow := color.New(color.FgYellow, color.Bold).SprintfFunc() |
|
|
|
|
if runtime.GOOS == "windows" { |
|
|
|
|
plainMsg += "3 days from now" |
|
|
|
|
colorMsg += yellow("3 days from now") |
|
|
|
|
} else { |
|
|
|
|
plainMsg += "2 days from now" |
|
|
|
|
colorMsg += yellow("2 days from now") |
|
|
|
|
} |
|
|
|
|
func TestComputeUpdateMessage(t *testing.T) { |
|
|
|
|
testCases := []struct { |
|
|
|
|
older time.Duration |
|
|
|
|
dlURL string |
|
|
|
|
|
|
|
|
|
expectedSubStr string |
|
|
|
|
}{ |
|
|
|
|
// Testcase index 0
|
|
|
|
|
{72 * time.Hour, "my_download_url", "3 days ago"}, |
|
|
|
|
{3 * time.Hour, "https://my_download_url_is_huge/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "3 hours ago"}, |
|
|
|
|
{-72 * time.Hour, "another_update_url", ""}, |
|
|
|
|
{0, "another_update_url", ""}, |
|
|
|
|
{time.Hour, "", ""}, |
|
|
|
|
{1 * time.Second, "my_download_url", "now"}, |
|
|
|
|
{2 * time.Second, "my_download_url", "1 second ago"}, |
|
|
|
|
{37 * time.Second, "my_download_url", "37 seconds ago"}, |
|
|
|
|
{60 * time.Second, "my_download_url", "60 seconds ago"}, |
|
|
|
|
{61 * time.Second, "my_download_url", "1 minute ago"}, |
|
|
|
|
|
|
|
|
|
updateMsg := colorizeUpdateMessage(minioReleaseURL, time.Duration(72*time.Hour)) |
|
|
|
|
// Testcase index 10
|
|
|
|
|
{37 * time.Minute, "my_download_url", "37 minutes ago"}, |
|
|
|
|
{1 * time.Hour, "my_download_url", "60 minutes ago"}, |
|
|
|
|
{61 * time.Minute, "my_download_url", "1 hour ago"}, |
|
|
|
|
{122 * time.Minute, "my_download_url", "2 hours ago"}, |
|
|
|
|
{24 * time.Hour, "my_download_url", "24 hours ago"}, |
|
|
|
|
{25 * time.Hour, "my_download_url", "1 day ago"}, |
|
|
|
|
{49 * time.Hour, "my_download_url", "2 days ago"}, |
|
|
|
|
{7 * 24 * time.Hour, "my_download_url", "7 days ago"}, |
|
|
|
|
{8 * 24 * time.Hour, "my_download_url", "1 week ago"}, |
|
|
|
|
{15 * 24 * time.Hour, "my_download_url", "2 weeks ago"}, |
|
|
|
|
|
|
|
|
|
if !(strings.Contains(updateMsg, plainMsg) || strings.Contains(updateMsg, colorMsg)) { |
|
|
|
|
t.Fatal("Duration string not found in colorized update message", updateMsg) |
|
|
|
|
// Testcase index 20
|
|
|
|
|
{30 * 24 * time.Hour, "my_download_url", "4 weeks ago"}, |
|
|
|
|
{31 * 24 * time.Hour, "my_download_url", "1 month ago"}, |
|
|
|
|
{61 * 24 * time.Hour, "my_download_url", "2 months ago"}, |
|
|
|
|
{360 * 24 * time.Hour, "my_download_url", "12 months ago"}, |
|
|
|
|
{361 * 24 * time.Hour, "my_download_url", "1 year ago"}, |
|
|
|
|
{2 * 365 * 24 * time.Hour, "my_download_url", "2 years ago"}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !strings.Contains(updateMsg, minioReleaseURL) { |
|
|
|
|
t.Fatal("Update message not found in colorized update message", minioReleaseURL) |
|
|
|
|
plainMsg := "You are running an older version of Minio released" |
|
|
|
|
yellow := color.New(color.FgYellow, color.Bold).SprintfFunc() |
|
|
|
|
cyan := color.New(color.FgCyan, color.Bold).SprintFunc() |
|
|
|
|
|
|
|
|
|
for i, testCase := range testCases { |
|
|
|
|
output := computeUpdateMessage(testCase.dlURL, testCase.older) |
|
|
|
|
line1 := fmt.Sprintf("%s %s", plainMsg, yellow(testCase.expectedSubStr)) |
|
|
|
|
line2 := fmt.Sprintf("Update: %s", cyan(testCase.dlURL)) |
|
|
|
|
// Uncomment below to see message appearance:
|
|
|
|
|
// fmt.Println(output)
|
|
|
|
|
switch { |
|
|
|
|
case testCase.dlURL == "" && output != "": |
|
|
|
|
t.Errorf("Testcase %d: no newer release available but got an update message: %s", i, output) |
|
|
|
|
case output == "" && testCase.dlURL != "" && testCase.older > 0: |
|
|
|
|
t.Errorf("Testcase %d: newer release is available but got empty update message!", i) |
|
|
|
|
case output == "" && (testCase.dlURL == "" || testCase.older <= 0): |
|
|
|
|
// Valid no update message case. No further
|
|
|
|
|
// validation needed.
|
|
|
|
|
continue |
|
|
|
|
case !strings.Contains(output, line1): |
|
|
|
|
t.Errorf("Testcase %d: output '%s' did not contain line 1: '%s'", i, output, line1) |
|
|
|
|
case !strings.Contains(output, line2): |
|
|
|
|
t.Errorf("Testcase %d: output '%s' did not contain line 2: '%s'", i, output, line2) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|