Debian Codegraph @

env/env_test.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
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 package env
18
19 import (
20 "os"
21 "strconv"
22 "testing"
23 )
24
25 func TestGetString(t *testing.T) {
26 const expected = "foo"
27
28 key := "STRING_SET_VAR"
29 os.Setenv(key, expected)
30 if e, a := expected, GetString(key, "~"+expected); e != a {
31 t.Fatalf("expected %#v==%#v", e, a)
32 }
33
34 key = "STRING_UNSET_VAR"
35 if e, a := expected, GetString(key, expected); e != a {
36 t.Fatalf("expected %#v==%#v", e, a)
37 }
38 }
39
40 func TestGetInt(t *testing.T) {
41 const expected = 1
42 const defaultValue = 2
43
44 key := "INT_SET_VAR"
45 os.Setenv(key, strconv.Itoa(expected))
46 returnVal, _ := GetInt(key, defaultValue)
47 if e, a := expected, returnVal; e != a {
48 t.Fatalf("expected %#v==%#v", e, a)
49 }
50
51 key = "INT_UNSET_VAR"
52 returnVal, _ = GetInt(key, defaultValue)
53 if e, a := defaultValue, returnVal; e != a {
54 t.Fatalf("expected %#v==%#v", e, a)
55 }
56
57 key = "INT_SET_VAR"
58 os.Setenv(key, "not-an-int")
59 returnVal, err := GetInt(key, defaultValue)
60 if e, a := defaultValue, returnVal; e != a {
61 t.Fatalf("expected %#v==%#v", e, a)
62 }
63 if err == nil {
64 t.Error("expected error")
65 }
66 }
67
68 func TestGetFloat64(t *testing.T) {
69 const expected = 1.0
70 const defaultValue = 2.0
71
72 key := "FLOAT_SET_VAR"
73 os.Setenv(key, "1.0")
74 returnVal, _ := GetFloat64(key, defaultValue)
75 if e, a := expected, returnVal; e != a {
76 t.Fatalf("expected %#v==%#v", e, a)
77 }
78
79 key = "FLOAT_UNSET_VAR"
80 returnVal, _ = GetFloat64(key, defaultValue)
81 if e, a := defaultValue, returnVal; e != a {
82 t.Fatalf("expected %#v==%#v", e, a)
83 }
84
85 key = "FLOAT_SET_VAR"
86 os.Setenv(key, "not-a-float")
87 returnVal, err := GetFloat64(key, defaultValue)
88 if e, a := defaultValue, returnVal; e != a {
89 t.Fatalf("expected %#v==%#v", e, a)
90 }
91 if err == nil || err.Error() != "strconv.ParseFloat: parsing \"not-a-float\": invalid syntax" {
92 t.Fatalf("unexpected error: %#v", err)
93 }
94 }
95
96 func TestGetBool(t *testing.T) {
97 const expected = true
98 const defaultValue = false
99
100 key := "BOOL_SET_VAR"
101 os.Setenv(key, "true")
102 returnVal, _ := GetBool(key, defaultValue)
103 if e, a := expected, returnVal; e != a {
104 t.Fatalf("expected %#v==%#v", e, a)
105 }
106
107 key = "BOOL_UNSET_VAR"
108 returnVal, _ = GetBool(key, defaultValue)
109 if e, a := defaultValue, returnVal; e != a {
110 t.Fatalf("expected %#v==%#v", e, a)
111 }
112
113 key = "BOOL_SET_VAR"
114 os.Setenv(key, "not-a-bool")
115 returnVal, err := GetBool(key, defaultValue)
116 if e, a := defaultValue, returnVal; e != a {
117 t.Fatalf("expected %#v==%#v", e, a)
118 }
119 if err == nil || err.Error() != "strconv.ParseBool: parsing \"not-a-bool\": invalid syntax" {
120 t.Fatalf("unexpected error: %#v", err)
121 }
122 }