This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
config_test.go
79 lines (65 loc) · 2.06 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nbiot
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestFirstMatchingPath(t *testing.T) {
getFirstMatchingPath(".telenor-nbiot")
}
func TestFileDefaultConfig(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
contents := "address=http://example.com\ntoken=sometoken\n"
tempFile := "telenor-nbiot.testconfig"
tempPath := filepath.Join(cwd, "telenor-nbiot.testconfig")
ioutil.WriteFile(tempPath, []byte(contents), 0666)
defer os.Remove(tempPath)
// unset the environment first to make sure it won't interfere with the
// file. It might contain settings that is in use so set it back to the
// original value afterwards.
oldAddr := os.Getenv(AddressEnvironmentVariable)
oldToken := os.Getenv(TokenEnvironmentVariable)
defer func() {
os.Setenv(AddressEnvironmentVariable, oldAddr)
os.Setenv(TokenEnvironmentVariable, oldToken)
}()
os.Setenv(AddressEnvironmentVariable, "")
os.Setenv(TokenEnvironmentVariable, "")
address, token, err := addressTokenFromConfig(tempFile)
if err != nil {
t.Fatal(err)
}
if address != "http://example.com" || token != "sometoken" {
t.Fatalf("Configuration isn't the expected values: %s / %s", address, token)
}
contents = "token=foobar\nsome=thing\nother=thing\n\nsome=thing=other\n\n"
ioutil.WriteFile(getFirstMatchingPath(tempFile), []byte(contents), 0666)
_, _, err = addressTokenFromConfig(tempFile)
if err == nil {
t.Fatal("expected error")
}
}
func TestEnvironmentConfig(t *testing.T) {
oldAddr := os.Getenv(AddressEnvironmentVariable)
oldToken := os.Getenv(TokenEnvironmentVariable)
defer func() {
os.Setenv(AddressEnvironmentVariable, oldAddr)
os.Setenv(TokenEnvironmentVariable, oldToken)
}()
os.Setenv(AddressEnvironmentVariable, "something")
os.Setenv(TokenEnvironmentVariable, "other")
address, token, err := addressTokenFromConfig(ConfigFile)
if err != nil {
t.Fatal(err)
}
if address != "something" {
t.Fatal("Expected environment variable to override config")
}
if token != "other" {
t.Fatal("Expected environment variable to override config")
}
}