2021-12-13 21:23:31 -05: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-19 14:05:16 -04:00
|
|
|
"os"
|
2021-12-13 21:23:31 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-12-26 01:02:54 -05: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 {
|
|
|
|
testCase := testCase
|
|
|
|
t.Run("", func(t *testing.T) {
|
2022-09-19 14:05:16 -04:00
|
|
|
tmpfile, err := os.CreateTemp("", "testfile")
|
2021-12-26 01:02:54 -05: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-13 21:23:31 -05:00
|
|
|
func Test_minioEnvironFromFile(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
content string
|
|
|
|
expectedErr bool
|
|
|
|
expectedEkvs []envKV
|
|
|
|
}{
|
2022-01-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
`
|
2021-12-13 21:23:31 -05: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-20 16:13:06 -05:00
|
|
|
// Value with double quotes
|
2022-01-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
`export MINIO_ROOT_USER="minio"`,
|
2021-12-20 16:13:06 -05:00
|
|
|
false,
|
|
|
|
[]envKV{
|
|
|
|
{
|
|
|
|
Key: "MINIO_ROOT_USER",
|
|
|
|
Value: "minio",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Value with single quotes
|
2022-01-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
`export MINIO_ROOT_USER='minio'`,
|
2021-12-20 16:13:06 -05:00
|
|
|
false,
|
|
|
|
[]envKV{
|
|
|
|
{
|
|
|
|
Key: "MINIO_ROOT_USER",
|
|
|
|
Value: "minio",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
`
|
2021-12-13 21:23:31 -05: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-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
`
|
2021-12-13 21:23:31 -05:00
|
|
|
export MINIO_ROOT_USERminio
|
|
|
|
export MINIO_ROOT_PASSWORD=minio123`,
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
},
|
2022-04-07 19:02:51 -04:00
|
|
|
{
|
|
|
|
`
|
2022-06-21 13:37:15 -04:00
|
|
|
# simple comment
|
2022-04-07 19:02:51 -04: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-13 21:23:31 -05:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
testCase := testCase
|
|
|
|
t.Run("", func(t *testing.T) {
|
2022-09-19 14:05:16 -04:00
|
|
|
tmpfile, err := os.CreateTemp("", "testfile")
|
2021-12-13 21:23:31 -05: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-07 19:02:51 -04:00
|
|
|
|
|
|
|
if len(ekvs) != len(testCase.expectedEkvs) {
|
|
|
|
t.Errorf("expected %v keys, got %v keys", len(testCase.expectedEkvs), len(ekvs))
|
|
|
|
}
|
|
|
|
|
2021-12-13 21:23:31 -05:00
|
|
|
if !reflect.DeepEqual(ekvs, testCase.expectedEkvs) {
|
|
|
|
t.Errorf("expected %v, got %v", testCase.expectedEkvs, ekvs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|