Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | // This file implements an exit handler that tries to shut down all the |
| 18 | // running servers before finally exiting. There are 2 triggers to this |
| 19 | // clean exit thread: signals and an exit channel. |
| 20 | |
| 21 | package afrouterd |
| 22 | |
| 23 | import ( |
| 24 | "github.com/stretchr/testify/assert" |
| 25 | "os" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func TestGetIntEnv(t *testing.T) { |
| 30 | |
| 31 | err := os.Setenv("testkey", "123") |
| 32 | assert.Nil(t, err) |
| 33 | |
| 34 | defer func() { os.Unsetenv("testkey") }() |
| 35 | |
| 36 | v := GetIntEnv("testkey", 0, 1000, 456) |
| 37 | assert.Equal(t, 123, v) |
| 38 | |
| 39 | v = GetIntEnv("doesnotexist", 0, 1000, 456) |
| 40 | assert.Equal(t, 456, v) |
| 41 | } |
| 42 | |
| 43 | func TestGetIntEnvTooLow(t *testing.T) { |
| 44 | |
| 45 | err := os.Setenv("testkey", "-1") |
| 46 | assert.Nil(t, err) |
| 47 | |
| 48 | defer func() { os.Unsetenv("testkey") }() |
| 49 | |
| 50 | defer func() { |
| 51 | if r := recover(); r == nil { |
| 52 | t.Errorf("The code did not panic") |
| 53 | } |
| 54 | }() |
| 55 | _ = GetIntEnv("testkey", 0, 1000, 456) |
| 56 | } |
| 57 | |
| 58 | func TestGetIntEnvTooHigh(t *testing.T) { |
| 59 | |
| 60 | err := os.Setenv("testkey", "1001") |
| 61 | assert.Nil(t, err) |
| 62 | |
| 63 | defer func() { os.Unsetenv("testkey") }() |
| 64 | |
| 65 | defer func() { |
| 66 | if r := recover(); r == nil { |
| 67 | t.Errorf("The code did not panic") |
| 68 | } |
| 69 | }() |
| 70 | _ = GetIntEnv("testkey", 0, 1000, 456) |
| 71 | } |
| 72 | |
| 73 | func TestGetIntEnvNotInteger(t *testing.T) { |
| 74 | |
| 75 | err := os.Setenv("testkey", "stuff") |
| 76 | assert.Nil(t, err) |
| 77 | |
| 78 | defer func() { os.Unsetenv("testkey") }() |
| 79 | |
| 80 | defer func() { |
| 81 | if r := recover(); r == nil { |
| 82 | t.Errorf("The code did not panic") |
| 83 | } |
| 84 | }() |
| 85 | _ = GetIntEnv("testkey", 0, 1000, 456) |
| 86 | } |
| 87 | |
| 88 | func TestGetStrEnv(t *testing.T) { |
| 89 | |
| 90 | err := os.Setenv("testkey", "abc") |
| 91 | assert.Nil(t, err) |
| 92 | |
| 93 | defer func() { os.Unsetenv("testkey") }() |
| 94 | |
| 95 | v := GetStrEnv("testkey", "def") |
| 96 | assert.Equal(t, "abc", v) |
| 97 | |
| 98 | v = GetStrEnv("doesnotexist", "def") |
| 99 | assert.Equal(t, "def", v) |
| 100 | } |