| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 	"encoding/hex" | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2019-03-06 09:28:14 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 07:12:13 +08:00
										 |  |  | func TestMinioVersionToReleaseTime(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		version    string | 
					
						
							|  |  |  | 		isOfficial bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"2017-09-29T19:16:56Z", true}, | 
					
						
							|  |  |  | 		{"RELEASE.2017-09-29T19-16-56Z", false}, | 
					
						
							|  |  |  | 		{"DEVELOPMENT.GOGET", false}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		_, err := minioVersionToReleaseTime(testCase.version) | 
					
						
							|  |  |  | 		if (err == nil) != testCase.isOfficial { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", | 
					
						
							|  |  |  | 				i+1, testCase.isOfficial, err == nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestReleaseTagToNFromTimeConversion(t *testing.T) { | 
					
						
							|  |  |  | 	utcLoc, _ := time.LoadLocation("") | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		t      time.Time | 
					
						
							|  |  |  | 		tag    string | 
					
						
							|  |  |  | 		errStr string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{time.Date(2017, time.September, 29, 19, 16, 56, 0, utcLoc), | 
					
						
							|  |  |  | 			"RELEASE.2017-09-29T19-16-56Z", ""}, | 
					
						
							|  |  |  | 		{time.Date(2017, time.August, 5, 0, 0, 53, 0, utcLoc), | 
					
						
							|  |  |  | 			"RELEASE.2017-08-05T00-00-53Z", ""}, | 
					
						
							|  |  |  | 		{time.Now().UTC(), "2017-09-29T19:16:56Z", | 
					
						
							|  |  |  | 			"2017-09-29T19:16:56Z is not a valid release tag"}, | 
					
						
							|  |  |  | 		{time.Now().UTC(), "DEVELOPMENT.GOGET", | 
					
						
							|  |  |  | 			"DEVELOPMENT.GOGET is not a valid release tag"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		if testCase.errStr != "" { | 
					
						
							|  |  |  | 			got := releaseTimeToReleaseTag(testCase.t) | 
					
						
							|  |  |  | 			if got != testCase.tag && testCase.errStr == "" { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.tag, got) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		tagTime, err := releaseTagToReleaseTime(testCase.tag) | 
					
						
							|  |  |  | 		if err != nil && err.Error() != testCase.errStr { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.errStr, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-25 03:11:20 +08:00
										 |  |  | 		if err == nil && !tagTime.Equal(testCase.t) { | 
					
						
							| 
									
										
										
										
											2017-10-10 07:12:13 +08:00
										 |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.t, tagTime) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | func TestDownloadURL(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 	sci := os.Getenv("MINIO_CI_CD") | 
					
						
							| 
									
										
										
										
											2019-12-18 09:34:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 	os.Setenv("MINIO_CI_CD", "") | 
					
						
							|  |  |  | 	defer os.Setenv("MINIO_CI_CD", sci) | 
					
						
							| 
									
										
										
										
											2019-12-18 09:34:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 07:12:13 +08:00
										 |  |  | 	minioVersion1 := releaseTimeToReleaseTag(UTCNow()) | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | 	durl := getDownloadURL(minioVersion1) | 
					
						
							| 
									
										
										
										
											2019-03-06 09:28:14 +08:00
										 |  |  | 	if IsDocker() { | 
					
						
							| 
									
										
										
										
											2021-07-08 09:07:29 +08:00
										 |  |  | 		if durl != "podman pull minio/minio:"+minioVersion1 { | 
					
						
							|  |  |  | 			t.Errorf("Expected %s, got %s", "podman pull minio/minio:"+minioVersion1, durl) | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-03-06 09:28:14 +08:00
										 |  |  | 		if runtime.GOOS == "windows" { | 
					
						
							|  |  |  | 			if durl != minioReleaseURL+"minio.exe" { | 
					
						
							|  |  |  | 				t.Errorf("Expected %s, got %s", minioReleaseURL+"minio.exe", durl) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if durl != minioReleaseURL+"minio" { | 
					
						
							|  |  |  | 				t.Errorf("Expected %s, got %s", minioReleaseURL+"minio", durl) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5") | 
					
						
							|  |  |  | 	durl = getDownloadURL(minioVersion1) | 
					
						
							|  |  |  | 	if durl != kubernetesDeploymentDoc { | 
					
						
							|  |  |  | 		t.Errorf("Expected %s, got %s", kubernetesDeploymentDoc, durl) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 	os.Unsetenv("KUBERNETES_SERVICE_HOST") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111") | 
					
						
							|  |  |  | 	durl = getDownloadURL(minioVersion1) | 
					
						
							|  |  |  | 	if durl != mesosDeploymentDoc { | 
					
						
							|  |  |  | 		t.Errorf("Expected %s, got %s", mesosDeploymentDoc, durl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	os.Unsetenv("MESOS_CONTAINER_NAME") | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | // Tests user agent string.
 | 
					
						
							|  |  |  | func TestUserAgent(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		envName     string | 
					
						
							|  |  |  | 		envValue    string | 
					
						
							|  |  |  | 		mode        string | 
					
						
							|  |  |  | 		expectedStr string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			envName:     "", | 
					
						
							|  |  |  | 			envValue:    "", | 
					
						
							|  |  |  | 			mode:        globalMinioModeFS, | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 			expectedStr: fmt.Sprintf("MinIO (%s; %s; %s; source) MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET", runtime.GOOS, runtime.GOARCH, globalMinioModeFS), | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			envName:     "MESOS_CONTAINER_NAME", | 
					
						
							|  |  |  | 			envValue:    "mesos-11111", | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 			mode:        globalMinioModeErasure, | 
					
						
							|  |  |  | 			expectedStr: fmt.Sprintf("MinIO (%s; %s; %s; %s; source) MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/universe-%s", runtime.GOOS, runtime.GOARCH, globalMinioModeErasure, "dcos", "mesos-1111"), | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			envName:     "KUBERNETES_SERVICE_HOST", | 
					
						
							|  |  |  | 			envValue:    "10.11.148.5", | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 			mode:        globalMinioModeErasure, | 
					
						
							|  |  |  | 			expectedStr: fmt.Sprintf("MinIO (%s; %s; %s; %s; source) MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET", runtime.GOOS, runtime.GOARCH, globalMinioModeErasure, "kubernetes"), | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 		sci := os.Getenv("MINIO_CI_CD") | 
					
						
							|  |  |  | 		os.Setenv("MINIO_CI_CD", "") | 
					
						
							| 
									
										
										
										
											2019-12-18 09:34:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		os.Setenv(testCase.envName, testCase.envValue) | 
					
						
							|  |  |  | 		if testCase.envName == "MESOS_CONTAINER_NAME" { | 
					
						
							|  |  |  | 			os.Setenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION", "mesos-1111") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		str := getUserAgent(testCase.mode) | 
					
						
							| 
									
										
										
										
											2019-03-06 09:28:14 +08:00
										 |  |  | 		expectedStr := testCase.expectedStr | 
					
						
							|  |  |  | 		if IsDocker() { | 
					
						
							|  |  |  | 			expectedStr = strings.Replace(expectedStr, "; source", "; docker; source", -1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if str != expectedStr { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected: %s, got: %s", i+1, expectedStr, str) | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 		os.Setenv("MINIO_CI_CD", sci) | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 		os.Unsetenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION") | 
					
						
							|  |  |  | 		os.Unsetenv(testCase.envName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests if the environment we are running is in DCOS.
 | 
					
						
							|  |  |  | func TestIsDCOS(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 	sci := os.Getenv("MINIO_CI_CD") | 
					
						
							|  |  |  | 	os.Setenv("MINIO_CI_CD", "") | 
					
						
							|  |  |  | 	defer os.Setenv("MINIO_CI_CD", sci) | 
					
						
							| 
									
										
										
										
											2019-12-18 09:34:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 08:33:21 +08:00
										 |  |  | 	os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111") | 
					
						
							|  |  |  | 	dcos := IsDCOS() | 
					
						
							|  |  |  | 	if !dcos { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %t, got %t", true, dcos) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	os.Unsetenv("MESOS_CONTAINER_NAME") | 
					
						
							|  |  |  | 	dcos = IsDCOS() | 
					
						
							|  |  |  | 	if dcos { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %t, got %t", false, dcos) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | // Tests if the environment we are running is in kubernetes.
 | 
					
						
							|  |  |  | func TestIsKubernetes(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-05-22 23:40:59 +08:00
										 |  |  | 	sci := os.Getenv("MINIO_CI_CD") | 
					
						
							|  |  |  | 	os.Setenv("MINIO_CI_CD", "") | 
					
						
							|  |  |  | 	defer os.Setenv("MINIO_CI_CD", sci) | 
					
						
							| 
									
										
										
										
											2019-12-18 09:34:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 17:42:12 +08:00
										 |  |  | 	os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5") | 
					
						
							|  |  |  | 	kubernetes := IsKubernetes() | 
					
						
							|  |  |  | 	if !kubernetes { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %t, got %t", true, kubernetes) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	os.Unsetenv("KUBERNETES_SERVICE_HOST") | 
					
						
							|  |  |  | 	kubernetes = IsKubernetes() | 
					
						
							|  |  |  | 	if kubernetes { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %t, got %t", false, kubernetes) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-25 04:17:28 +08:00
										 |  |  | // Tests if the environment we are running is Helm chart.
 | 
					
						
							|  |  |  | func TestGetHelmVersion(t *testing.T) { | 
					
						
							|  |  |  | 	createTempFile := func(content string) string { | 
					
						
							|  |  |  | 		tmpfile, err := ioutil.TempFile("", "helm-testfile-") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Unable to create temporary file. %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if _, err = tmpfile.Write([]byte(content)); err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Unable to create temporary file. %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err = tmpfile.Close(); err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Unable to create temporary file. %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return tmpfile.Name() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	filename := createTempFile( | 
					
						
							|  |  |  | 		`app="virtuous-rat-minio" | 
					
						
							|  |  |  | chart="minio-0.1.3" | 
					
						
							|  |  |  | heritage="Tiller" | 
					
						
							|  |  |  | pod-template-hash="818089471"`) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	defer os.Remove(filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		filename       string | 
					
						
							|  |  |  | 		expectedResult string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"", ""}, | 
					
						
							|  |  |  | 		{"/tmp/non-existing-file", ""}, | 
					
						
							|  |  |  | 		{filename, "minio-0.1.3"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  | 		result := getHelmVersion(testCase.filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if testCase.expectedResult != result { | 
					
						
							|  |  |  | 			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, result) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | func TestDownloadReleaseData(t *testing.T) { | 
					
						
							|  |  |  | 	httpServer1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | 
					
						
							|  |  |  | 	defer httpServer1.Close() | 
					
						
							|  |  |  | 	httpServer2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 		fmt.Fprintln(w, "fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z") | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 	defer httpServer2.Close() | 
					
						
							|  |  |  | 	httpServer3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 		http.Error(w, "", http.StatusNotFound) | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 	defer httpServer3.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		releaseChecksumURL string | 
					
						
							|  |  |  | 		expectedResult     string | 
					
						
							|  |  |  | 		expectedErr        error | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{httpServer1.URL, "", nil}, | 
					
						
							|  |  |  | 		{httpServer2.URL, "fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z\n", nil}, | 
					
						
							|  |  |  | 		{httpServer3.URL, "", fmt.Errorf("Error downloading URL " + httpServer3.URL + ". Response: 404 Not Found")}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 		u, err := url.Parse(testCase.releaseChecksumURL) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		result, err := downloadReleaseURL(u, 1*time.Second, "") | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 		if testCase.expectedErr == nil { | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if err == nil { | 
					
						
							|  |  |  | 			t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err) | 
					
						
							|  |  |  | 		} else if testCase.expectedErr.Error() != err.Error() { | 
					
						
							|  |  |  | 			t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if testCase.expectedResult != result { | 
					
						
							|  |  |  | 			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, result) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParseReleaseData(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-10-10 07:12:13 +08:00
										 |  |  | 	releaseTime, _ := releaseTagToReleaseTime("RELEASE.2016-10-07T01-16-39Z") | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2021-04-09 00:51:11 +08:00
										 |  |  | 		data                string | 
					
						
							|  |  |  | 		expectedResult      time.Time | 
					
						
							|  |  |  | 		expectedSha256hex   string | 
					
						
							|  |  |  | 		expectedReleaseInfo string | 
					
						
							|  |  |  | 		expectedErr         bool | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2021-04-09 00:51:11 +08:00
										 |  |  | 		{"more than two fields", time.Time{}, "", "", true}, | 
					
						
							|  |  |  | 		{"more than", time.Time{}, "", "", true}, | 
					
						
							|  |  |  | 		{"more than.two.fields", time.Time{}, "", "", true}, | 
					
						
							|  |  |  | 		{"more minio.RELEASE.fields", time.Time{}, "", "", true}, | 
					
						
							|  |  |  | 		{"more minio.RELEASE.2016-10-07T01-16-39Z", time.Time{}, "", "", true}, | 
					
						
							|  |  |  | 		{"fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z\n", releaseTime, "fbe246edbd382902db9a4035df7dce8cb441357d", | 
					
						
							|  |  |  | 			"minio.RELEASE.2016-10-07T01-16-39Z", false}, | 
					
						
							|  |  |  | 		{"fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z.customer-hotfix\n", releaseTime, "fbe246edbd382902db9a4035df7dce8cb441357d", | 
					
						
							|  |  |  | 			"minio.RELEASE.2016-10-07T01-16-39Z.customer-hotfix", false}, | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 07:12:13 +08:00
										 |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2021-04-09 00:51:11 +08:00
										 |  |  | 		sha256Sum, result, releaseInfo, err := parseReleaseData(testCase.data) | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 		if !testCase.expectedErr { | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 				t.Errorf("error case %d: expected no error, got: %v", i+1, err) | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else if err == nil { | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 			t.Errorf("error case %d: expected error got: %v", i+1, err) | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-16 04:33:42 +08:00
										 |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2020-07-23 23:03:31 +08:00
										 |  |  | 			if hex.EncodeToString(sha256Sum) != testCase.expectedSha256hex { | 
					
						
							|  |  |  | 				t.Errorf("case %d: result: expected: %v, got: %x", i+1, testCase.expectedSha256hex, sha256Sum) | 
					
						
							| 
									
										
										
										
											2017-12-16 04:33:42 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if !testCase.expectedResult.Equal(result) { | 
					
						
							|  |  |  | 				t.Errorf("case %d: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-04-09 00:51:11 +08:00
										 |  |  | 			if testCase.expectedReleaseInfo != releaseInfo { | 
					
						
							|  |  |  | 				t.Errorf("case %d: result: expected: %v, got: %v", i+1, testCase.expectedReleaseInfo, releaseInfo) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-15 16:31:00 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |