| 
									
										
										
										
											2021-12-14 10:23:31 +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/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2022-09-20 02:05:16 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 14:02:54 +08:00
										 |  |  | func Test_readFromSecret(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		content       string | 
					
						
							|  |  |  | 		expectedErr   bool | 
					
						
							|  |  |  | 		expectedValue string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"value\n", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			"value", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			" \t\n Hello, Gophers \n\t\r\n", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			"Hello, Gophers", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  | 		t.Run("", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-19 00:25:55 +08:00
										 |  |  | 			tmpfile, err := os.CreateTemp(t.TempDir(), "testfile") | 
					
						
							| 
									
										
										
										
											2021-12-26 14:02:54 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Error(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			tmpfile.WriteString(testCase.content) | 
					
						
							|  |  |  | 			tmpfile.Sync() | 
					
						
							|  |  |  | 			tmpfile.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			value, err := readFromSecret(tmpfile.Name()) | 
					
						
							|  |  |  | 			if err != nil && !testCase.expectedErr { | 
					
						
							|  |  |  | 				t.Error(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if err == nil && testCase.expectedErr { | 
					
						
							|  |  |  | 				t.Error(errors.New("expected error, found success")) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if value != testCase.expectedValue { | 
					
						
							|  |  |  | 				t.Errorf("Expected %s, got %s", testCase.expectedValue, value) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | func Test_minioEnvironFromFile(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		content      string | 
					
						
							|  |  |  | 		expectedErr  bool | 
					
						
							|  |  |  | 		expectedEkvs []envKV | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | export MINIO_ROOT_USER=minio | 
					
						
							|  |  |  | export MINIO_ROOT_PASSWORD=minio123`, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			[]envKV{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_USER", | 
					
						
							|  |  |  | 					Value: "minio", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_PASSWORD", | 
					
						
							|  |  |  | 					Value: "minio123", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2021-12-21 05:13:06 +08:00
										 |  |  | 		// Value with double quotes
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`export MINIO_ROOT_USER="minio"`, | 
					
						
							| 
									
										
										
										
											2021-12-21 05:13:06 +08:00
										 |  |  | 			false, | 
					
						
							|  |  |  | 			[]envKV{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_USER", | 
					
						
							|  |  |  | 					Value: "minio", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Value with single quotes
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`export MINIO_ROOT_USER='minio'`, | 
					
						
							| 
									
										
										
										
											2021-12-21 05:13:06 +08:00
										 |  |  | 			false, | 
					
						
							|  |  |  | 			[]envKV{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_USER", | 
					
						
							|  |  |  | 					Value: "minio", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | MINIO_ROOT_USER=minio | 
					
						
							|  |  |  | MINIO_ROOT_PASSWORD=minio123`, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			[]envKV{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_USER", | 
					
						
							|  |  |  | 					Value: "minio", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_PASSWORD", | 
					
						
							|  |  |  | 					Value: "minio123", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | export MINIO_ROOT_USERminio | 
					
						
							|  |  |  | export MINIO_ROOT_PASSWORD=minio123`, | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-04-08 07:02:51 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2022-06-22 01:37:15 +08:00
										 |  |  | # simple comment | 
					
						
							| 
									
										
										
										
											2022-04-08 07:02:51 +08:00
										 |  |  | # MINIO_ROOT_USER=minioadmin | 
					
						
							|  |  |  | # MINIO_ROOT_PASSWORD=minioadmin | 
					
						
							|  |  |  | MINIO_ROOT_USER=minio | 
					
						
							|  |  |  | MINIO_ROOT_PASSWORD=minio123`, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			[]envKV{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_USER", | 
					
						
							|  |  |  | 					Value: "minio", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Key:   "MINIO_ROOT_PASSWORD", | 
					
						
							|  |  |  | 					Value: "minio123", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  | 		t.Run("", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-19 00:25:55 +08:00
										 |  |  | 			tmpfile, err := os.CreateTemp(t.TempDir(), "testfile") | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Error(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			tmpfile.WriteString(testCase.content) | 
					
						
							|  |  |  | 			tmpfile.Sync() | 
					
						
							|  |  |  | 			tmpfile.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ekvs, err := minioEnvironFromFile(tmpfile.Name()) | 
					
						
							|  |  |  | 			if err != nil && !testCase.expectedErr { | 
					
						
							|  |  |  | 				t.Error(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if err == nil && testCase.expectedErr { | 
					
						
							|  |  |  | 				t.Error(errors.New("expected error, found success")) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-04-08 07:02:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if len(ekvs) != len(testCase.expectedEkvs) { | 
					
						
							|  |  |  | 				t.Errorf("expected %v keys, got %v keys", len(testCase.expectedEkvs), len(ekvs)) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 10:23:31 +08:00
										 |  |  | 			if !reflect.DeepEqual(ekvs, testCase.expectedEkvs) { | 
					
						
							|  |  |  | 				t.Errorf("expected %v, got %v", testCase.expectedEkvs, ekvs) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |