github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/update-notifier_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/minio/minio/internal/color"
    27  )
    28  
    29  // Tests update notifier string builder.
    30  func TestPrepareUpdateMessage(t *testing.T) {
    31  	testCases := []struct {
    32  		older time.Duration
    33  		dlURL string
    34  
    35  		expectedSubStr string
    36  	}{
    37  		// Testcase index 0
    38  		{72 * time.Hour, "my_download_url", "3 days before the latest release"},
    39  		{3 * time.Hour, "https://my_download_url_is_huge/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "3 hours before the latest release"},
    40  		{-72 * time.Hour, "another_update_url", ""},
    41  		{0, "another_update_url", ""},
    42  		{time.Hour, "", ""},
    43  		{0 * time.Second, "my_download_url", "now"},
    44  		{1 * time.Second, "my_download_url", "1 second before the latest release"},
    45  		{37 * time.Second, "my_download_url", "37 seconds before the latest release"},
    46  		{60 * time.Second, "my_download_url", "1 minute before the latest release"},
    47  		{61 * time.Second, "my_download_url", "1 minute before the latest release"},
    48  
    49  		// Testcase index 10
    50  		{37 * time.Minute, "my_download_url", "37 minutes before the latest release"},
    51  		{1 * time.Hour, "my_download_url", "1 hour before the latest release"},
    52  		{61 * time.Minute, "my_download_url", "1 hour before the latest release"},
    53  		{122 * time.Minute, "my_download_url", "2 hours before the latest release"},
    54  		{24 * time.Hour, "my_download_url", "1 day before the latest release"},
    55  		{25 * time.Hour, "my_download_url", "1 day before the latest release"},
    56  		{49 * time.Hour, "my_download_url", "2 days before the latest release"},
    57  		{7 * 24 * time.Hour, "my_download_url", "1 week before the latest release"},
    58  		{8 * 24 * time.Hour, "my_download_url", "1 week before the latest release"},
    59  		{15 * 24 * time.Hour, "my_download_url", "2 weeks before the latest release"},
    60  
    61  		// Testcase index 20
    62  		{30 * 24 * time.Hour, "my_download_url", "1 month before the latest release"},
    63  		{31 * 24 * time.Hour, "my_download_url", "1 month before the latest release"},
    64  		{61 * 24 * time.Hour, "my_download_url", "2 months before the latest release"},
    65  		{360 * 24 * time.Hour, "my_download_url", "1 year before the latest release"},
    66  		{361 * 24 * time.Hour, "my_download_url", "1 year before the latest release"},
    67  		{2 * 365 * 24 * time.Hour, "my_download_url", "2 years before the latest release"},
    68  	}
    69  
    70  	plainMsg := "You are running an older version of MinIO released"
    71  
    72  	for i, testCase := range testCases {
    73  		output := prepareUpdateMessage(testCase.dlURL, testCase.older)
    74  		line1 := fmt.Sprintf("%s %s", plainMsg, color.YellowBold(testCase.expectedSubStr))
    75  		line2 := fmt.Sprintf("Update: %s", color.CyanBold(testCase.dlURL))
    76  		// Uncomment below to see message appearance:
    77  		// fmt.Println(output)
    78  		switch {
    79  		case testCase.dlURL == "" && output != "":
    80  			t.Errorf("Testcase %d: no newer release available but got an update message: %s", i+1, output)
    81  		case output == "" && testCase.dlURL != "" && testCase.older > 0:
    82  			t.Errorf("Testcase %d: newer release is available but got empty update message!", i+1)
    83  		case output == "" && (testCase.dlURL == "" || testCase.older <= 0):
    84  			// Valid no update message case. No further
    85  			// validation needed.
    86  			continue
    87  		case !strings.Contains(output, line1):
    88  			t.Errorf("Testcase %d: output '%s' did not contain line 1: '%s'", i+1, output, line1)
    89  		case !strings.Contains(output, line2):
    90  			t.Errorf("Testcase %d: output '%s' did not contain line 2: '%s'", i+1, output, line2)
    91  		}
    92  	}
    93  }