github.com/timstclair/heapster@v0.20.0-alpha1/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 for _, c := range tests { 86 var uri Uri 87 err := uri.Set(c.in) 88 if c.wantErr { 89 assert.NotNil(t, err) 90 } else { 91 assert.Nil(t, err) 92 assert.Equal(t, c.want, uri) 93 } 94 } 95 } 96 97 func TestUrisString(t *testing.T) { 98 tests := [...]struct { 99 in Uris 100 want string 101 }{ 102 { 103 Uris{ 104 Uri{Key: "gcm"}, 105 }, 106 "[gcm]", 107 }, 108 { 109 Uris{ 110 Uri{Key: "gcm"}, 111 Uri{ 112 Key: "influxdb", 113 Val: url.URL{Path: "foo"}, 114 }, 115 }, 116 "[gcm influxdb:foo]", 117 }, 118 } 119 for _, c := range tests { 120 assert.Equal(t, c.want, c.in.String()) 121 } 122 } 123 124 func TestUrisSet(t *testing.T) { 125 tests := [...]struct { 126 in []string 127 want Uris 128 wantErr bool 129 }{ 130 {[]string{""}, Uris{}, true}, 131 {[]string{":foo"}, Uris{}, true}, 132 { 133 []string{"gcm"}, 134 Uris{ 135 Uri{Key: "gcm"}, 136 }, 137 false, 138 }, 139 { 140 []string{"gcm", "influxdb:foo"}, 141 Uris{ 142 Uri{Key: "gcm"}, 143 Uri{ 144 Key: "influxdb", 145 Val: url.URL{Path: "foo"}, 146 }, 147 }, 148 false, 149 }, 150 } 151 for _, c := range tests { 152 var uris Uris 153 var err error 154 for _, s := range c.in { 155 if err = uris.Set(s); err != nil { 156 break 157 } 158 } 159 if c.wantErr { 160 assert.NotNil(t, err) 161 } else { 162 assert.Nil(t, err) 163 assert.Equal(t, c.want, uris) 164 } 165 } 166 }