github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/common-main_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  	"errors"
    22  	"os"
    23  	"reflect"
    24  	"testing"
    25  )
    26  
    27  func Test_readFromSecret(t *testing.T) {
    28  	testCases := []struct {
    29  		content       string
    30  		expectedErr   bool
    31  		expectedValue string
    32  	}{
    33  		{
    34  			"value\n",
    35  			false,
    36  			"value",
    37  		},
    38  		{
    39  			" \t\n Hello, Gophers \n\t\r\n",
    40  			false,
    41  			"Hello, Gophers",
    42  		},
    43  	}
    44  
    45  	for _, testCase := range testCases {
    46  		testCase := testCase
    47  		t.Run("", func(t *testing.T) {
    48  			tmpfile, err := os.CreateTemp("", "testfile")
    49  			if err != nil {
    50  				t.Error(err)
    51  			}
    52  			tmpfile.WriteString(testCase.content)
    53  			tmpfile.Sync()
    54  			tmpfile.Close()
    55  
    56  			value, err := readFromSecret(tmpfile.Name())
    57  			if err != nil && !testCase.expectedErr {
    58  				t.Error(err)
    59  			}
    60  			if err == nil && testCase.expectedErr {
    61  				t.Error(errors.New("expected error, found success"))
    62  			}
    63  			if value != testCase.expectedValue {
    64  				t.Errorf("Expected %s, got %s", testCase.expectedValue, value)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func Test_minioEnvironFromFile(t *testing.T) {
    71  	testCases := []struct {
    72  		content      string
    73  		expectedErr  bool
    74  		expectedEkvs []envKV
    75  	}{
    76  		{
    77  			`
    78  export MINIO_ROOT_USER=minio
    79  export MINIO_ROOT_PASSWORD=minio123`,
    80  			false,
    81  			[]envKV{
    82  				{
    83  					Key:   "MINIO_ROOT_USER",
    84  					Value: "minio",
    85  				},
    86  				{
    87  					Key:   "MINIO_ROOT_PASSWORD",
    88  					Value: "minio123",
    89  				},
    90  			},
    91  		},
    92  		// Value with double quotes
    93  		{
    94  			`export MINIO_ROOT_USER="minio"`,
    95  			false,
    96  			[]envKV{
    97  				{
    98  					Key:   "MINIO_ROOT_USER",
    99  					Value: "minio",
   100  				},
   101  			},
   102  		},
   103  		// Value with single quotes
   104  		{
   105  			`export MINIO_ROOT_USER='minio'`,
   106  			false,
   107  			[]envKV{
   108  				{
   109  					Key:   "MINIO_ROOT_USER",
   110  					Value: "minio",
   111  				},
   112  			},
   113  		},
   114  		{
   115  			`
   116  MINIO_ROOT_USER=minio
   117  MINIO_ROOT_PASSWORD=minio123`,
   118  			false,
   119  			[]envKV{
   120  				{
   121  					Key:   "MINIO_ROOT_USER",
   122  					Value: "minio",
   123  				},
   124  				{
   125  					Key:   "MINIO_ROOT_PASSWORD",
   126  					Value: "minio123",
   127  				},
   128  			},
   129  		},
   130  		{
   131  			`
   132  export MINIO_ROOT_USERminio
   133  export MINIO_ROOT_PASSWORD=minio123`,
   134  			true,
   135  			nil,
   136  		},
   137  		{
   138  			`
   139  # simple comment
   140  # MINIO_ROOT_USER=minioadmin
   141  # MINIO_ROOT_PASSWORD=minioadmin
   142  MINIO_ROOT_USER=minio
   143  MINIO_ROOT_PASSWORD=minio123`,
   144  			false,
   145  			[]envKV{
   146  				{
   147  					Key:   "MINIO_ROOT_USER",
   148  					Value: "minio",
   149  				},
   150  				{
   151  					Key:   "MINIO_ROOT_PASSWORD",
   152  					Value: "minio123",
   153  				},
   154  			},
   155  		},
   156  	}
   157  	for _, testCase := range testCases {
   158  		testCase := testCase
   159  		t.Run("", func(t *testing.T) {
   160  			tmpfile, err := os.CreateTemp("", "testfile")
   161  			if err != nil {
   162  				t.Error(err)
   163  			}
   164  			tmpfile.WriteString(testCase.content)
   165  			tmpfile.Sync()
   166  			tmpfile.Close()
   167  
   168  			ekvs, err := minioEnvironFromFile(tmpfile.Name())
   169  			if err != nil && !testCase.expectedErr {
   170  				t.Error(err)
   171  			}
   172  			if err == nil && testCase.expectedErr {
   173  				t.Error(errors.New("expected error, found success"))
   174  			}
   175  
   176  			if len(ekvs) != len(testCase.expectedEkvs) {
   177  				t.Errorf("expected %v keys, got %v keys", len(testCase.expectedEkvs), len(ekvs))
   178  			}
   179  
   180  			if !reflect.DeepEqual(ekvs, testCase.expectedEkvs) {
   181  				t.Errorf("expected %v, got %v", testCase.expectedEkvs, ekvs)
   182  			}
   183  		})
   184  	}
   185  }