storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/update_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 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  	"encoding/hex"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"net/http/httptest"
    25  	"net/url"
    26  	"os"
    27  	"runtime"
    28  	"strings"
    29  	"testing"
    30  	"time"
    31  )
    32  
    33  func TestMinioVersionToReleaseTime(t *testing.T) {
    34  	testCases := []struct {
    35  		version    string
    36  		isOfficial bool
    37  	}{
    38  		{"2017-09-29T19:16:56Z", true},
    39  		{"RELEASE.2017-09-29T19-16-56Z", false},
    40  		{"DEVELOPMENT.GOGET", false},
    41  	}
    42  	for i, testCase := range testCases {
    43  		_, err := minioVersionToReleaseTime(testCase.version)
    44  		if (err == nil) != testCase.isOfficial {
    45  			t.Errorf("Test %d: Expected %v but got %v",
    46  				i+1, testCase.isOfficial, err == nil)
    47  		}
    48  	}
    49  }
    50  
    51  func TestReleaseTagToNFromTimeConversion(t *testing.T) {
    52  	utcLoc, _ := time.LoadLocation("")
    53  	testCases := []struct {
    54  		t      time.Time
    55  		tag    string
    56  		errStr string
    57  	}{
    58  		{time.Date(2017, time.September, 29, 19, 16, 56, 0, utcLoc),
    59  			"RELEASE.2017-09-29T19-16-56Z", ""},
    60  		{time.Date(2017, time.August, 5, 0, 0, 53, 0, utcLoc),
    61  			"RELEASE.2017-08-05T00-00-53Z", ""},
    62  		{time.Now().UTC(), "2017-09-29T19:16:56Z",
    63  			"2017-09-29T19:16:56Z is not a valid release tag"},
    64  		{time.Now().UTC(), "DEVELOPMENT.GOGET",
    65  			"DEVELOPMENT.GOGET is not a valid release tag"},
    66  	}
    67  	for i, testCase := range testCases {
    68  		if testCase.errStr != "" {
    69  			got := releaseTimeToReleaseTag(testCase.t)
    70  			if got != testCase.tag && testCase.errStr == "" {
    71  				t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.tag, got)
    72  			}
    73  		}
    74  		tagTime, err := releaseTagToReleaseTime(testCase.tag)
    75  		if err != nil && err.Error() != testCase.errStr {
    76  			t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.errStr, err.Error())
    77  		}
    78  		if err == nil && !tagTime.Equal(testCase.t) {
    79  			t.Errorf("Test %d: Expected %v but got %v", i+1, testCase.t, tagTime)
    80  		}
    81  	}
    82  
    83  }
    84  
    85  func TestDownloadURL(t *testing.T) {
    86  	sci := os.Getenv("MINIO_CI_CD")
    87  
    88  	os.Setenv("MINIO_CI_CD", "")
    89  	defer os.Setenv("MINIO_CI_CD", sci)
    90  
    91  	minioVersion1 := releaseTimeToReleaseTag(UTCNow())
    92  	durl := getDownloadURL(minioVersion1)
    93  	if IsDocker() {
    94  		if durl != "docker pull minio/minio:"+minioVersion1 {
    95  			t.Errorf("Expected %s, got %s", "docker pull minio/minio:"+minioVersion1, durl)
    96  		}
    97  	} else {
    98  		if runtime.GOOS == "windows" {
    99  			if durl != minioReleaseURL+"minio.exe" {
   100  				t.Errorf("Expected %s, got %s", minioReleaseURL+"minio.exe", durl)
   101  			}
   102  		} else {
   103  			if durl != minioReleaseURL+"minio" {
   104  				t.Errorf("Expected %s, got %s", minioReleaseURL+"minio", durl)
   105  			}
   106  		}
   107  	}
   108  
   109  	os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
   110  	durl = getDownloadURL(minioVersion1)
   111  	if durl != kubernetesDeploymentDoc {
   112  		t.Errorf("Expected %s, got %s", kubernetesDeploymentDoc, durl)
   113  	}
   114  	os.Unsetenv("KUBERNETES_SERVICE_HOST")
   115  
   116  	os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111")
   117  	durl = getDownloadURL(minioVersion1)
   118  	if durl != mesosDeploymentDoc {
   119  		t.Errorf("Expected %s, got %s", mesosDeploymentDoc, durl)
   120  	}
   121  	os.Unsetenv("MESOS_CONTAINER_NAME")
   122  }
   123  
   124  // Tests user agent string.
   125  func TestUserAgent(t *testing.T) {
   126  	testCases := []struct {
   127  		envName     string
   128  		envValue    string
   129  		mode        string
   130  		expectedStr string
   131  	}{
   132  		{
   133  			envName:     "",
   134  			envValue:    "",
   135  			mode:        globalMinioModeFS,
   136  			expectedStr: fmt.Sprintf("MinIO (%s; %s; %s; source) MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET", runtime.GOOS, runtime.GOARCH, globalMinioModeFS),
   137  		},
   138  		{
   139  			envName:     "MESOS_CONTAINER_NAME",
   140  			envValue:    "mesos-11111",
   141  			mode:        globalMinioModeErasure,
   142  			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"),
   143  		},
   144  		{
   145  			envName:     "KUBERNETES_SERVICE_HOST",
   146  			envValue:    "10.11.148.5",
   147  			mode:        globalMinioModeErasure,
   148  			expectedStr: fmt.Sprintf("MinIO (%s; %s; %s; %s; source) MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET MinIO/DEVELOPMENT.GOGET", runtime.GOOS, runtime.GOARCH, globalMinioModeErasure, "kubernetes"),
   149  		},
   150  	}
   151  
   152  	for i, testCase := range testCases {
   153  		sci := os.Getenv("MINIO_CI_CD")
   154  		os.Setenv("MINIO_CI_CD", "")
   155  
   156  		os.Setenv(testCase.envName, testCase.envValue)
   157  		if testCase.envName == "MESOS_CONTAINER_NAME" {
   158  			os.Setenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION", "mesos-1111")
   159  		}
   160  		str := getUserAgent(testCase.mode)
   161  		expectedStr := testCase.expectedStr
   162  		if IsDocker() {
   163  			expectedStr = strings.Replace(expectedStr, "; source", "; docker; source", -1)
   164  		}
   165  		if str != expectedStr {
   166  			t.Errorf("Test %d: expected: %s, got: %s", i+1, expectedStr, str)
   167  		}
   168  		os.Setenv("MINIO_CI_CD", sci)
   169  		os.Unsetenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION")
   170  		os.Unsetenv(testCase.envName)
   171  	}
   172  }
   173  
   174  // Tests if the environment we are running is in DCOS.
   175  func TestIsDCOS(t *testing.T) {
   176  	sci := os.Getenv("MINIO_CI_CD")
   177  	os.Setenv("MINIO_CI_CD", "")
   178  	defer os.Setenv("MINIO_CI_CD", sci)
   179  
   180  	os.Setenv("MESOS_CONTAINER_NAME", "mesos-1111")
   181  	dcos := IsDCOS()
   182  	if !dcos {
   183  		t.Fatalf("Expected %t, got %t", true, dcos)
   184  	}
   185  
   186  	os.Unsetenv("MESOS_CONTAINER_NAME")
   187  	dcos = IsDCOS()
   188  	if dcos {
   189  		t.Fatalf("Expected %t, got %t", false, dcos)
   190  	}
   191  }
   192  
   193  // Tests if the environment we are running is in kubernetes.
   194  func TestIsKubernetes(t *testing.T) {
   195  	sci := os.Getenv("MINIO_CI_CD")
   196  	os.Setenv("MINIO_CI_CD", "")
   197  	defer os.Setenv("MINIO_CI_CD", sci)
   198  
   199  	os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
   200  	kubernetes := IsKubernetes()
   201  	if !kubernetes {
   202  		t.Fatalf("Expected %t, got %t", true, kubernetes)
   203  	}
   204  	os.Unsetenv("KUBERNETES_SERVICE_HOST")
   205  	kubernetes = IsKubernetes()
   206  	if kubernetes {
   207  		t.Fatalf("Expected %t, got %t", false, kubernetes)
   208  	}
   209  }
   210  
   211  // Tests if the environment we are running is Helm chart.
   212  func TestGetHelmVersion(t *testing.T) {
   213  	createTempFile := func(content string) string {
   214  		tmpfile, err := ioutil.TempFile("", "helm-testfile-")
   215  		if err != nil {
   216  			t.Fatalf("Unable to create temporary file. %s", err)
   217  		}
   218  		if _, err = tmpfile.Write([]byte(content)); err != nil {
   219  			t.Fatalf("Unable to create temporary file. %s", err)
   220  		}
   221  		if err = tmpfile.Close(); err != nil {
   222  			t.Fatalf("Unable to create temporary file. %s", err)
   223  		}
   224  		return tmpfile.Name()
   225  	}
   226  
   227  	filename := createTempFile(
   228  		`app="virtuous-rat-minio"
   229  chart="minio-0.1.3"
   230  heritage="Tiller"
   231  pod-template-hash="818089471"`)
   232  
   233  	defer os.Remove(filename)
   234  
   235  	testCases := []struct {
   236  		filename       string
   237  		expectedResult string
   238  	}{
   239  		{"", ""},
   240  		{"/tmp/non-existing-file", ""},
   241  		{filename, "minio-0.1.3"},
   242  	}
   243  
   244  	for _, testCase := range testCases {
   245  		result := getHelmVersion(testCase.filename)
   246  
   247  		if testCase.expectedResult != result {
   248  			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, result)
   249  		}
   250  	}
   251  }
   252  
   253  func TestDownloadReleaseData(t *testing.T) {
   254  	httpServer1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
   255  	defer httpServer1.Close()
   256  	httpServer2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   257  		fmt.Fprintln(w, "fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z")
   258  	}))
   259  	defer httpServer2.Close()
   260  	httpServer3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   261  		http.Error(w, "", http.StatusNotFound)
   262  	}))
   263  	defer httpServer3.Close()
   264  
   265  	testCases := []struct {
   266  		releaseChecksumURL string
   267  		expectedResult     string
   268  		expectedErr        error
   269  	}{
   270  		{httpServer1.URL, "", nil},
   271  		{httpServer2.URL, "fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z\n", nil},
   272  		{httpServer3.URL, "", fmt.Errorf("Error downloading URL " + httpServer3.URL + ". Response: 404 Not Found")},
   273  	}
   274  
   275  	for _, testCase := range testCases {
   276  		u, err := url.Parse(testCase.releaseChecksumURL)
   277  		if err != nil {
   278  			t.Fatal(err)
   279  		}
   280  
   281  		result, err := downloadReleaseURL(u, 1*time.Second, "")
   282  		if testCase.expectedErr == nil {
   283  			if err != nil {
   284  				t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err)
   285  			}
   286  		} else if err == nil {
   287  			t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err)
   288  		} else if testCase.expectedErr.Error() != err.Error() {
   289  			t.Fatalf("error: expected: %v, got: %v", testCase.expectedErr, err)
   290  		}
   291  
   292  		if testCase.expectedResult != result {
   293  			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, result)
   294  		}
   295  	}
   296  }
   297  
   298  func TestParseReleaseData(t *testing.T) {
   299  	releaseTime, _ := releaseTagToReleaseTime("RELEASE.2016-10-07T01-16-39Z")
   300  	testCases := []struct {
   301  		data                string
   302  		expectedResult      time.Time
   303  		expectedSha256hex   string
   304  		expectedReleaseInfo string
   305  		expectedErr         bool
   306  	}{
   307  		{"more than two fields", time.Time{}, "", "", true},
   308  		{"more than", time.Time{}, "", "", true},
   309  		{"more than.two.fields", time.Time{}, "", "", true},
   310  		{"more minio.RELEASE.fields", time.Time{}, "", "", true},
   311  		{"more minio.RELEASE.2016-10-07T01-16-39Z", time.Time{}, "", "", true},
   312  		{"fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z\n", releaseTime, "fbe246edbd382902db9a4035df7dce8cb441357d",
   313  			"minio.RELEASE.2016-10-07T01-16-39Z", false},
   314  		{"fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z.customer-hotfix\n", releaseTime, "fbe246edbd382902db9a4035df7dce8cb441357d",
   315  			"minio.RELEASE.2016-10-07T01-16-39Z.customer-hotfix", false},
   316  	}
   317  
   318  	for i, testCase := range testCases {
   319  		sha256Sum, result, releaseInfo, err := parseReleaseData(testCase.data)
   320  		if !testCase.expectedErr {
   321  			if err != nil {
   322  				t.Errorf("error case %d: expected no error, got: %v", i+1, err)
   323  			}
   324  		} else if err == nil {
   325  			t.Errorf("error case %d: expected error got: %v", i+1, err)
   326  		}
   327  		if err == nil {
   328  			if hex.EncodeToString(sha256Sum) != testCase.expectedSha256hex {
   329  				t.Errorf("case %d: result: expected: %v, got: %x", i+1, testCase.expectedSha256hex, sha256Sum)
   330  			}
   331  			if !testCase.expectedResult.Equal(result) {
   332  				t.Errorf("case %d: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   333  			}
   334  			if testCase.expectedReleaseInfo != releaseInfo {
   335  				t.Errorf("case %d: result: expected: %v, got: %v", i+1, testCase.expectedReleaseInfo, releaseInfo)
   336  			}
   337  		}
   338  	}
   339  }