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