github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/configutil/env_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package configutil_test 19 20 import ( 21 "os" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/waldiirawan/apm-agent-go/v2/internal/configutil" 28 "github.com/waldiirawan/apm-agent-go/v2/internal/wildcard" 29 ) 30 31 func TestParseDurationEnv(t *testing.T) { 32 const envKey = "ELASTIC_APM_TEST_DURATION" 33 os.Unsetenv(envKey) 34 defer os.Unsetenv(envKey) 35 36 d, err := configutil.ParseDurationEnv(envKey, 42*time.Second) 37 assert.NoError(t, err) 38 assert.Equal(t, 42*time.Second, d) 39 40 os.Setenv(envKey, "1us") // us == microsecond. 41 _, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 42 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid unit in duration 1us (allowed units: ms, s, m)") 43 44 os.Setenv(envKey, "5s") 45 d, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 46 assert.NoError(t, err) 47 assert.Equal(t, 5*time.Second, d) 48 49 os.Setenv(envKey, "5ms") 50 d, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 51 assert.NoError(t, err) 52 assert.Equal(t, 5*time.Millisecond, d) 53 54 os.Setenv(envKey, "5m") 55 d, err = configutil.ParseDurationEnv(envKey, 42*time.Minute) 56 assert.NoError(t, err) 57 assert.Equal(t, 5*time.Minute, d) 58 59 os.Setenv(envKey, "5 h") 60 _, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 61 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid character ' ' in duration 5 h") 62 63 os.Setenv(envKey, "5h") 64 _, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 65 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid unit in duration 5h (allowed units: ms, s, m)") 66 67 os.Setenv(envKey, "5") 68 _, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 69 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: missing unit in duration 5 (allowed units: ms, s, m)") 70 71 os.Setenv(envKey, "blah") 72 _, err = configutil.ParseDurationEnv(envKey, 42*time.Second) 73 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid duration blah") 74 } 75 76 func TestParseDurationOptionsEnv(t *testing.T) { 77 const envKey = "ELASTIC_APM_TEST_DURATION" 78 os.Unsetenv(envKey) 79 defer os.Unsetenv(envKey) 80 81 os.Setenv(envKey, "5us") 82 d, err := configutil.ParseDurationEnvOptions(envKey, 10*time.Microsecond, configutil.DurationOptions{ 83 MinimumDurationUnit: time.Microsecond, 84 }) 85 assert.NoError(t, err) 86 assert.Equal(t, 5*time.Microsecond, d) 87 88 os.Setenv(envKey, "") 89 d, err = configutil.ParseDurationEnvOptions(envKey, 10*time.Microsecond, configutil.DurationOptions{ 90 MinimumDurationUnit: time.Microsecond, 91 }) 92 assert.NoError(t, err) 93 assert.Equal(t, 10*time.Microsecond, d) 94 95 os.Setenv(envKey, "1ns") 96 _, err = configutil.ParseDurationEnvOptions(envKey, 10*time.Microsecond, configutil.DurationOptions{ 97 MinimumDurationUnit: time.Microsecond, 98 }) 99 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid unit in duration 1ns (allowed units: us, ms, s, m)") 100 assert.Equal(t, 10*time.Microsecond, d) 101 102 os.Setenv(envKey, "1ns") 103 _, err = configutil.ParseDurationEnvOptions(envKey, 10*time.Microsecond, configutil.DurationOptions{ 104 MinimumDurationUnit: time.Nanosecond, 105 }) 106 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_DURATION: invalid unit in duration 1ns (allowed units: us, ms, s, m)") 107 assert.Equal(t, 10*time.Microsecond, d) 108 } 109 110 func TestParseSizeEnv(t *testing.T) { 111 const envKey = "ELASTIC_APM_TEST_SIZE" 112 os.Unsetenv(envKey) 113 defer os.Unsetenv(envKey) 114 115 d, err := configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 116 assert.NoError(t, err) 117 assert.Equal(t, 42*configutil.KByte, d) 118 119 os.Setenv(envKey, "5b") 120 d, err = configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 121 assert.NoError(t, err) 122 assert.Equal(t, 5*configutil.Byte, d) 123 124 os.Setenv(envKey, "5kb") 125 d, err = configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 126 assert.NoError(t, err) 127 assert.Equal(t, 5*configutil.KByte, d) 128 129 os.Setenv(envKey, "5mb") 130 d, err = configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 131 assert.NoError(t, err) 132 assert.Equal(t, 5*configutil.MByte, d) 133 134 os.Setenv(envKey, "5gb") 135 d, err = configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 136 assert.NoError(t, err) 137 assert.Equal(t, 5*configutil.GByte, d) 138 139 os.Setenv(envKey, "5GB") 140 d, err = configutil.ParseSizeEnv(envKey, 42*configutil.KByte) 141 assert.NoError(t, err) 142 assert.Equal(t, 5*configutil.GByte, d) 143 144 os.Setenv(envKey, "5 mb") 145 _, err = configutil.ParseSizeEnv(envKey, 0) 146 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_SIZE: invalid character ' ' in size 5 mb") 147 148 os.Setenv(envKey, "5tb") 149 _, err = configutil.ParseSizeEnv(envKey, 0) 150 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_SIZE: invalid unit in size 5tb (allowed units: B, KB, MB, GB)") 151 152 os.Setenv(envKey, "5") 153 _, err = configutil.ParseSizeEnv(envKey, 0) 154 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_SIZE: missing unit in size 5 (allowed units: B, KB, MB, GB)") 155 156 os.Setenv(envKey, "blah") 157 _, err = configutil.ParseSizeEnv(envKey, 0) 158 assert.EqualError(t, err, "failed to parse ELASTIC_APM_TEST_SIZE: invalid size blah") 159 } 160 161 func TestParseBoolEnv(t *testing.T) { 162 const envKey = "ELASTIC_APM_TEST_BOOL" 163 os.Unsetenv(envKey) 164 defer os.Unsetenv(envKey) 165 166 b, err := configutil.ParseBoolEnv(envKey, true) 167 assert.NoError(t, err) 168 assert.True(t, b) 169 170 os.Setenv(envKey, "true") 171 b, err = configutil.ParseBoolEnv(envKey, false) 172 assert.NoError(t, err) 173 assert.True(t, b) 174 175 os.Setenv(envKey, "false") 176 b, err = configutil.ParseBoolEnv(envKey, true) 177 assert.NoError(t, err) 178 assert.False(t, b) 179 180 os.Setenv(envKey, "falsk") 181 _, err = configutil.ParseBoolEnv(envKey, true) 182 assert.EqualError(t, err, `failed to parse ELASTIC_APM_TEST_BOOL: strconv.ParseBool: parsing "falsk": invalid syntax`) 183 } 184 185 func TestParseListEnv(t *testing.T) { 186 const envKey = "ELASTIC_APM_TEST_LIST" 187 os.Unsetenv(envKey) 188 defer os.Unsetenv(envKey) 189 190 defaultList := []string{"foo", "bar"} 191 192 list := configutil.ParseListEnv(envKey, ",", defaultList) 193 assert.Equal(t, defaultList, list) 194 195 os.Setenv(envKey, "a") 196 list = configutil.ParseListEnv(envKey, ",", defaultList) 197 assert.Equal(t, []string{"a"}, list) 198 199 os.Setenv(envKey, "a,b") 200 list = configutil.ParseListEnv(envKey, ",", defaultList) 201 assert.Equal(t, []string{"a", "b"}, list) 202 203 os.Setenv(envKey, ",a , b,") 204 list = configutil.ParseListEnv(envKey, ",", defaultList) 205 assert.Equal(t, []string{"a", "b"}, list) 206 207 os.Setenv(envKey, ",") 208 list = configutil.ParseListEnv(envKey, ",", defaultList) 209 assert.Len(t, list, 0) 210 211 os.Setenv(envKey, "a| b") 212 list = configutil.ParseListEnv(envKey, "|", defaultList) 213 assert.Equal(t, []string{"a", "b"}, list) 214 215 os.Setenv(envKey, "a b") 216 list = configutil.ParseListEnv(envKey, ",", defaultList) 217 assert.Equal(t, []string{"a b"}, list) 218 } 219 220 func TestParseWildcardPatternsEnv(t *testing.T) { 221 const envKey = "ELASTIC_APM_TEST_WILDCARDS" 222 os.Unsetenv(envKey) 223 defer os.Unsetenv(envKey) 224 225 newMatchers := func(p ...string) wildcard.Matchers { 226 matchers := make(wildcard.Matchers, len(p)) 227 for i, p := range p { 228 matchers[i] = wildcard.NewMatcher(p, wildcard.CaseInsensitive) 229 } 230 return matchers 231 } 232 defaultMatchers := newMatchers("default") 233 234 matchers := configutil.ParseWildcardPatternsEnv(envKey, defaultMatchers) 235 assert.Equal(t, defaultMatchers, matchers) 236 237 os.Setenv(envKey, "foo, bar") 238 expected := newMatchers("foo", "bar") 239 matchers = configutil.ParseWildcardPatternsEnv(envKey, defaultMatchers) 240 assert.Equal(t, expected, matchers) 241 242 os.Setenv(envKey, "foo, (?-i)bar") 243 expected[1] = wildcard.NewMatcher("bar", wildcard.CaseSensitive) 244 matchers = configutil.ParseWildcardPatternsEnv(envKey, defaultMatchers) 245 assert.Equal(t, expected, matchers) 246 247 os.Setenv(envKey, "(?i)foo, (?-i)bar") 248 matchers = configutil.ParseWildcardPatternsEnv(envKey, defaultMatchers) 249 assert.Equal(t, expected, matchers) 250 }