github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/flags/flags_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package flags 16 17 import ( 18 "net/url" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestUriString(t *testing.T) { 25 tests := [...]struct { 26 in Uri 27 want string 28 }{ 29 { 30 Uri{ 31 Key: "gcm", 32 }, 33 "gcm", 34 }, 35 { 36 Uri{ 37 Key: "influxdb", 38 Val: url.URL{ 39 Scheme: "http", 40 Host: "monitoring-influxdb:8086", 41 RawQuery: "key=val&key2=val2", 42 }, 43 }, 44 "influxdb:http://monitoring-influxdb:8086?key=val&key2=val2", 45 }, 46 } 47 for _, c := range tests { 48 assert.Equal(t, c.want, c.in.String()) 49 } 50 } 51 52 func TestUriSet(t *testing.T) { 53 tests := [...]struct { 54 in string 55 want Uri 56 wantErr bool 57 }{ 58 {"", Uri{}, true}, 59 {":", Uri{}, true}, 60 {":foo", Uri{}, true}, 61 {"key:incorrecturl/%gh&%ij", Uri{}, true}, 62 { 63 "gcm", 64 Uri{Key: "gcm"}, 65 false, 66 }, 67 { 68 "gcm:", 69 Uri{Key: "gcm"}, 70 false, 71 }, 72 { 73 "influxdb:http://monitoring-influxdb:8086?key=val&key2=val2", 74 Uri{ 75 Key: "influxdb", 76 Val: url.URL{ 77 Scheme: "http", 78 Host: "monitoring-influxdb:8086", 79 RawQuery: "key=val&key2=val2", 80 }, 81 }, 82 false, 83 }, 84 { 85 "gcm:?metrics=all", 86 Uri{ 87 Key: "gcm", 88 Val: url.URL{ 89 RawQuery: "metrics=all", 90 }, 91 }, 92 false, 93 }, 94 } 95 for _, c := range tests { 96 var uri Uri 97 err := uri.Set(c.in) 98 if c.wantErr { 99 assert.NotNil(t, err) 100 } else { 101 assert.Nil(t, err) 102 assert.Equal(t, c.want, uri) 103 } 104 } 105 } 106 107 func TestUrisString(t *testing.T) { 108 tests := [...]struct { 109 in Uris 110 want string 111 }{ 112 { 113 Uris{ 114 Uri{Key: "gcm"}, 115 }, 116 "[gcm]", 117 }, 118 { 119 Uris{ 120 Uri{Key: "gcm"}, 121 Uri{ 122 Key: "influxdb", 123 Val: url.URL{Path: "foo"}, 124 }, 125 }, 126 "[gcm influxdb:foo]", 127 }, 128 } 129 for _, c := range tests { 130 assert.Equal(t, c.want, c.in.String()) 131 } 132 } 133 134 func TestUrisSet(t *testing.T) { 135 tests := [...]struct { 136 in []string 137 want Uris 138 wantErr bool 139 }{ 140 {[]string{""}, Uris{}, true}, 141 {[]string{":foo"}, Uris{}, true}, 142 { 143 []string{"gcm"}, 144 Uris{ 145 Uri{Key: "gcm"}, 146 }, 147 false, 148 }, 149 { 150 []string{"gcm", "influxdb:foo"}, 151 Uris{ 152 Uri{Key: "gcm"}, 153 Uri{ 154 Key: "influxdb", 155 Val: url.URL{Path: "foo"}, 156 }, 157 }, 158 false, 159 }, 160 } 161 for _, c := range tests { 162 var uris Uris 163 var err error 164 for _, s := range c.in { 165 if err = uris.Set(s); err != nil { 166 break 167 } 168 } 169 if c.wantErr { 170 assert.NotNil(t, err) 171 } else { 172 assert.Nil(t, err) 173 assert.Equal(t, c.want, uris) 174 } 175 } 176 }