storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/update-notifier_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 "time" 24 25 "storj.io/minio/pkg/color" 26 ) 27 28 // Tests update notifier string builder. 29 func TestPrepareUpdateMessage(t *testing.T) { 30 testCases := []struct { 31 older time.Duration 32 dlURL string 33 34 expectedSubStr string 35 }{ 36 // Testcase index 0 37 {72 * time.Hour, "my_download_url", "3 days ago"}, 38 {3 * time.Hour, "https://my_download_url_is_huge/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "3 hours ago"}, 39 {-72 * time.Hour, "another_update_url", ""}, 40 {0, "another_update_url", ""}, 41 {time.Hour, "", ""}, 42 {0 * time.Second, "my_download_url", "now"}, 43 {1 * time.Second, "my_download_url", "1 second ago"}, 44 {37 * time.Second, "my_download_url", "37 seconds ago"}, 45 {60 * time.Second, "my_download_url", "1 minute ago"}, 46 {61 * time.Second, "my_download_url", "1 minute ago"}, 47 48 // Testcase index 10 49 {37 * time.Minute, "my_download_url", "37 minutes ago"}, 50 {1 * time.Hour, "my_download_url", "1 hour ago"}, 51 {61 * time.Minute, "my_download_url", "1 hour ago"}, 52 {122 * time.Minute, "my_download_url", "2 hours ago"}, 53 {24 * time.Hour, "my_download_url", "1 day ago"}, 54 {25 * time.Hour, "my_download_url", "1 day ago"}, 55 {49 * time.Hour, "my_download_url", "2 days ago"}, 56 {7 * 24 * time.Hour, "my_download_url", "1 week ago"}, 57 {8 * 24 * time.Hour, "my_download_url", "1 week ago"}, 58 {15 * 24 * time.Hour, "my_download_url", "2 weeks ago"}, 59 60 // Testcase index 20 61 {30 * 24 * time.Hour, "my_download_url", "1 month ago"}, 62 {31 * 24 * time.Hour, "my_download_url", "1 month ago"}, 63 {61 * 24 * time.Hour, "my_download_url", "2 months ago"}, 64 {360 * 24 * time.Hour, "my_download_url", "1 year ago"}, 65 {361 * 24 * time.Hour, "my_download_url", "1 year ago"}, 66 {2 * 365 * 24 * time.Hour, "my_download_url", "2 years ago"}, 67 } 68 69 plainMsg := "You are running an older version of MinIO released" 70 71 for i, testCase := range testCases { 72 output := prepareUpdateMessage(testCase.dlURL, testCase.older) 73 line1 := fmt.Sprintf("%s %s", plainMsg, color.YellowBold(testCase.expectedSubStr)) 74 line2 := fmt.Sprintf("Update: %s", color.CyanBold(testCase.dlURL)) 75 // Uncomment below to see message appearance: 76 // fmt.Println(output) 77 switch { 78 case testCase.dlURL == "" && output != "": 79 t.Errorf("Testcase %d: no newer release available but got an update message: %s", i+1, output) 80 case output == "" && testCase.dlURL != "" && testCase.older > 0: 81 t.Errorf("Testcase %d: newer release is available but got empty update message!", i+1) 82 case output == "" && (testCase.dlURL == "" || testCase.older <= 0): 83 // Valid no update message case. No further 84 // validation needed. 85 continue 86 case !strings.Contains(output, line1): 87 t.Errorf("Testcase %d: output '%s' did not contain line 1: '%s'", i+1, output, line1) 88 case !strings.Contains(output, line2): 89 t.Errorf("Testcase %d: output '%s' did not contain line 2: '%s'", i+1, output, line2) 90 } 91 } 92 }