github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/config_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 config 19 20 import ( 21 "testing" 22 ) 23 24 func TestKVFields(t *testing.T) { 25 tests := []struct { 26 input string 27 keys []string 28 expectedFields map[string]struct{} 29 }{ 30 // No keys present 31 { 32 input: "", 33 keys: []string{"comment"}, 34 expectedFields: map[string]struct{}{}, 35 }, 36 // No keys requested for tokenizing 37 { 38 input: `comment="Hi this is my comment ="`, 39 keys: []string{}, 40 expectedFields: map[string]struct{}{}, 41 }, 42 // Single key requested and present 43 { 44 input: `comment="Hi this is my comment ="`, 45 keys: []string{"comment"}, 46 expectedFields: map[string]struct{}{`comment="Hi this is my comment ="`: {}}, 47 }, 48 // Keys and input order of k=v is same. 49 { 50 input: `connection_string="host=localhost port=2832" comment="really long comment"`, 51 keys: []string{"connection_string", "comment"}, 52 expectedFields: map[string]struct{}{ 53 `connection_string="host=localhost port=2832"`: {}, 54 `comment="really long comment"`: {}, 55 }, 56 }, 57 // Keys with spaces in between 58 { 59 input: `enable=on format=namespace connection_string=" host=localhost port=5432 dbname = cesnietor sslmode=disable" table=holicrayoli`, 60 keys: []string{"enable", "connection_string", "comment", "format", "table"}, 61 expectedFields: map[string]struct{}{ 62 `enable=on`: {}, 63 `format=namespace`: {}, 64 `connection_string=" host=localhost port=5432 dbname = cesnietor sslmode=disable"`: {}, 65 `table=holicrayoli`: {}, 66 }, 67 }, 68 // One of the keys is not present and order of input has changed. 69 { 70 input: `comment="really long comment" connection_string="host=localhost port=2832"`, 71 keys: []string{"connection_string", "comment", "format"}, 72 expectedFields: map[string]struct{}{ 73 `connection_string="host=localhost port=2832"`: {}, 74 `comment="really long comment"`: {}, 75 }, 76 }, 77 // Incorrect delimiter, expected fields should be empty. 78 { 79 input: `comment:"really long comment" connection_string:"host=localhost port=2832"`, 80 keys: []string{"connection_string", "comment"}, 81 expectedFields: map[string]struct{}{}, 82 }, 83 // Incorrect type of input v/s required keys. 84 { 85 input: `comme="really long comment" connection_str="host=localhost port=2832"`, 86 keys: []string{"connection_string", "comment"}, 87 expectedFields: map[string]struct{}{}, 88 }, 89 } 90 for _, test := range tests { 91 test := test 92 t.Run("", func(t *testing.T) { 93 gotFields := kvFields(test.input, test.keys) 94 if len(gotFields) != len(test.expectedFields) { 95 t.Errorf("Expected keys %d, found %d", len(test.expectedFields), len(gotFields)) 96 } 97 found := true 98 for _, field := range gotFields { 99 _, ok := test.expectedFields[field] 100 found = found && ok 101 } 102 if !found { 103 t.Errorf("Expected %s, got %s", test.expectedFields, gotFields) 104 } 105 }) 106 } 107 } 108 109 func TestValidRegion(t *testing.T) { 110 tests := []struct { 111 name string 112 success bool 113 }{ 114 {name: "us-east-1", success: true}, 115 {name: "us_east", success: true}, 116 {name: "helloWorld", success: true}, 117 {name: "-fdslka", success: false}, 118 {name: "^00[", success: false}, 119 {name: "my region", success: false}, 120 {name: "%%$#!", success: false}, 121 } 122 123 for _, test := range tests { 124 t.Run(test.name, func(t *testing.T) { 125 ok := validRegionRegex.MatchString(test.name) 126 if test.success != ok { 127 t.Errorf("Expected %t, got %t", test.success, ok) 128 } 129 }) 130 } 131 }