initialise
[debian/configureit.git] / configureit_test.go
1
2 package configureit
3
4 import (
5         "testing"
6         "os"
7 )
8
9 func makeSimpleConfig() *Config {
10         testConfig := New()
11         testConfig.Add("key_a", NewStringOption("default 1"))
12         testConfig.Add("key_b", NewIntOption(2))
13         testConfig.Add("user_test", NewUserOption(""))
14         testConfig.Add("user test 2", NewUserOption(""))
15         return testConfig
16 }
17
18 func TestConfig(t *testing.T) {
19         testConfig := makeSimpleConfig()
20
21         tv := testConfig.Get("key_a")
22         if nil == tv {
23                 t.Errorf("Couldn't find key_a in testConfig")
24         } else {
25                 if !tv.IsDefault() {
26                         t.Errorf("key_a reported non-default without changes")
27                 }
28                 sopt, ok := tv.(*StringOption)
29                 if !ok {
30                         t.Errorf("Failed return assertion for key_a back to StringOption")
31                 }
32                 if sopt.Value != "default 1" {
33                         t.Errorf("key_a Value doesn't match initial configured value.")
34                 }
35         }
36
37         tv = testConfig.Get("key_b")
38         if nil == tv {
39                 t.Errorf("Couldn't find key_b in testConfig")
40         } else {
41                 if !tv.IsDefault() {
42                         t.Errorf("key_b reported non-default without changes")
43                 }
44                 iopt, ok := tv.(*IntOption)
45                 if !ok {
46                         t.Errorf("Failed return assertion for key_b back to IntOption")
47                 }
48                 if iopt.Value != 2 {
49                         t.Errorf("key_b Value doesn't match initial configured value.")
50                 }
51         }
52
53         tv = testConfig.Get("user_test")
54         if nil == tv {
55                 t.Errorf("Couldn't find user_test in testConfig")
56         } else {
57                 if !tv.IsDefault() {
58                         t.Errorf("user_test reported non-default without changes")
59                 }
60                 uopt, ok := tv.(*UserOption)
61                 if !ok {
62                         t.Errorf("Failed return assertion for user_test back to UserOption")
63                 }
64                 _, err := uopt.User()
65                 if err != EmptyUserSet {
66                         t.Errorf("user_test didn't claim it set empty.")
67                 }
68         }
69
70         tv = testConfig.Get("user test 2")
71         if nil == tv {
72                 t.Errorf("Couldn't find \"user test 2\" in testConfig")
73         } else {
74                 if !tv.IsDefault() {
75                         t.Errorf("user test 2 reported non-default without changes")
76                 }
77                 uopt, ok := tv.(*UserOption)
78                 if !ok {
79                         t.Errorf("Failed return assertion for user test 2 back to UserOption")
80                 }
81                 _, err := uopt.User()
82                 if err != EmptyUserSet {
83                         t.Errorf("user test 2 didn't claim it set empty.")
84                 }
85         }
86
87         tv = testConfig.Get("key_c")
88         if nil != tv {
89                 t.Errorf("Found non-existant key_c in testConfig")
90         }
91 }
92
93 func TestFileRead(t *testing.T) {
94         testConfig := makeSimpleConfig()
95         fh, err := os.Open("sample.conf")
96         if err != nil {
97                 t.Fatalf("Failed to open sample.conf: %s", err)
98         }
99         err = testConfig.Read(fh, 1)
100         if err != nil {
101                 t.Fatalf("Got error reading config: %s", err)
102         }
103         fh.Close()
104
105         tv := testConfig.Get("key_a")
106         if nil == tv {
107                 t.Errorf("Couldn't find key_a in testConfig")
108         } else {
109                 if tv.IsDefault() {
110                         t.Errorf("key_a reported default despite config file")
111                 }
112                 sopt, ok := tv.(*StringOption)
113                 if !ok {
114                         t.Errorf("Failed return assertion for key_a back to StringOption")
115                 }
116                 if sopt.Value != "Alternate Value" {
117                         t.Errorf("key_a Value doesn't match expected value.")
118                 }
119         }
120
121         tv = testConfig.Get("key_b")
122         if nil == tv {
123                 t.Errorf("Couldn't find key_b in testConfig")
124         } else {
125                 if tv.IsDefault() {
126                         t.Errorf("key_b reported default despite config file")
127                 }
128                 iopt, ok := tv.(*IntOption)
129                 if !ok {
130                         t.Errorf("Failed return assertion for key_b back to IntOption")
131                 }
132                 if iopt.Value != 27 {
133                         t.Errorf("key_b Value doesn't match expected value.")
134                 }
135         }
136
137         tv = testConfig.Get("user_test")
138         if nil == tv {
139                 t.Errorf("Couldn't find user_test in testConfig")
140         } else {
141                 if tv.IsDefault() {
142                         t.Errorf("user_test reported default despite changes")
143                 }
144                 uopt, ok := tv.(*UserOption)
145                 if !ok {
146                         t.Errorf("Failed return assertion for user_test back to UserOption")
147                 }
148                 uinfo, err := uopt.User()
149                 if err != nil {
150                         t.Errorf("Error whilst looking up UID: %s", err)
151                 }
152                 if uinfo.Uid != 0 {
153                         t.Errorf("user_test Value doesn't match expected value.")
154                 }
155         }
156
157         tv = testConfig.Get("user test 2")
158         if nil == tv {
159                 t.Errorf("Couldn't find \"user test 2\" in testConfig")
160         } else {
161                 if tv.IsDefault() {
162                         t.Errorf("user test 2 reported default despite changes")
163                 }
164                 uopt, ok := tv.(*UserOption)
165                 if !ok {
166                         t.Errorf("Failed return assertion for user test 2 back to UserOption")
167                 }
168                 uinfo, err := uopt.User()
169                 if err != nil {
170                         t.Errorf("Error whilst looking up UID: %s", err)
171                 }
172                 if uinfo.Uid != 1 {
173                         t.Errorf("user test 2 Value doesn't match expected value.")
174                 }
175                 if uinfo.Username != "daemon" {
176                         t.Errorf("user test 2 name lookup didn't match expected value (ignore if not debian/ubuntu/linux?).")
177                 }
178         }
179 }
180