| 
									
										
										
										
											2016-11-20 15:20:13 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-11-20 15:20:13 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-11-20 15:20:13 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/color" | 
					
						
							| 
									
										
										
										
											2016-11-20 15:20:13 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests update notifier string builder.
 | 
					
						
							| 
									
										
										
										
											2017-12-16 04:33:42 +08:00
										 |  |  | func TestPrepareUpdateMessage(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 	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, "", ""}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{0 * time.Second, "my_download_url", "now"}, | 
					
						
							|  |  |  | 		{1 * time.Second, "my_download_url", "1 second ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{37 * time.Second, "my_download_url", "37 seconds ago"}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{60 * time.Second, "my_download_url", "1 minute ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{61 * time.Second, "my_download_url", "1 minute ago"}, | 
					
						
							| 
									
										
										
										
											2017-04-26 18:38:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		// Testcase index 10
 | 
					
						
							|  |  |  | 		{37 * time.Minute, "my_download_url", "37 minutes ago"}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{1 * time.Hour, "my_download_url", "1 hour ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{61 * time.Minute, "my_download_url", "1 hour ago"}, | 
					
						
							|  |  |  | 		{122 * time.Minute, "my_download_url", "2 hours ago"}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{24 * time.Hour, "my_download_url", "1 day ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{25 * time.Hour, "my_download_url", "1 day ago"}, | 
					
						
							|  |  |  | 		{49 * time.Hour, "my_download_url", "2 days ago"}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{7 * 24 * time.Hour, "my_download_url", "1 week ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{8 * 24 * time.Hour, "my_download_url", "1 week ago"}, | 
					
						
							|  |  |  | 		{15 * 24 * time.Hour, "my_download_url", "2 weeks ago"}, | 
					
						
							| 
									
										
										
										
											2017-04-26 18:38:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		// Testcase index 20
 | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{30 * 24 * time.Hour, "my_download_url", "1 month ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{31 * 24 * time.Hour, "my_download_url", "1 month ago"}, | 
					
						
							|  |  |  | 		{61 * 24 * time.Hour, "my_download_url", "2 months ago"}, | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 		{360 * 24 * time.Hour, "my_download_url", "1 year ago"}, | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		{361 * 24 * time.Hour, "my_download_url", "1 year ago"}, | 
					
						
							|  |  |  | 		{2 * 365 * 24 * time.Hour, "my_download_url", "2 years ago"}, | 
					
						
							| 
									
										
										
										
											2017-04-26 18:38:35 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 	plainMsg := "You are running an older version of MinIO released" | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2017-12-16 04:33:42 +08:00
										 |  |  | 		output := prepareUpdateMessage(testCase.dlURL, testCase.older) | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		line1 := fmt.Sprintf("%s %s", plainMsg, color.YellowBold(testCase.expectedSubStr)) | 
					
						
							|  |  |  | 		line2 := fmt.Sprintf("Update: %s", color.CyanBold(testCase.dlURL)) | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		// Uncomment below to see message appearance:
 | 
					
						
							|  |  |  | 		// fmt.Println(output)
 | 
					
						
							|  |  |  | 		switch { | 
					
						
							|  |  |  | 		case testCase.dlURL == "" && output != "": | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 			t.Errorf("Testcase %d: no newer release available but got an update message: %s", i+1, output) | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		case output == "" && testCase.dlURL != "" && testCase.older > 0: | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 			t.Errorf("Testcase %d: newer release is available but got empty update message!", i+1) | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		case output == "" && (testCase.dlURL == "" || testCase.older <= 0): | 
					
						
							|  |  |  | 			// Valid no update message case. No further
 | 
					
						
							|  |  |  | 			// validation needed.
 | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		case !strings.Contains(output, line1): | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 			t.Errorf("Testcase %d: output '%s' did not contain line 1: '%s'", i+1, output, line1) | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		case !strings.Contains(output, line2): | 
					
						
							| 
									
										
										
										
											2019-04-03 09:28:39 +08:00
										 |  |  | 			t.Errorf("Testcase %d: output '%s' did not contain line 2: '%s'", i+1, output, line2) | 
					
						
							| 
									
										
										
										
											2017-06-01 00:22:00 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-20 15:20:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |