-
Notifications
You must be signed in to change notification settings - Fork 90
/
do_init_test.go
78 lines (70 loc) · 1.92 KB
/
do_init_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
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDoInitialize(t *testing.T) {
{
err := doInitialize(&flag.FlagSet{}, []string{})
if err == nil || err.Error() != "-apikey option is required" {
t.Errorf("-apikey option is required")
}
}
root := t.TempDir()
{
// not exist config file directory
confdir := filepath.Join(root, "confdir-test")
conffile := filepath.Join(confdir, "mackerel-agent.conf")
argv := []string{"-conf", conffile, "-apikey", "hoge"}
err := doInit(&flag.FlagSet{}, argv)
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
}
{
conffile := filepath.Join(root, "mackerel-agent.conf")
argv := []string{"-conf", conffile, "-apikey", "hoge"}
err := doInit(&flag.FlagSet{}, argv)
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
content, _ := os.ReadFile(conffile)
if string(content) != `apikey = "hoge"`+"\n" {
t.Errorf("somthing went wrong: %s", string(content))
}
err = doInitialize(&flag.FlagSet{}, argv)
if _, ok := err.(apikeyAlreadySetError); !ok {
t.Errorf("err should be `apikeyAlreadySetError` but :%s", err)
}
}
{
f := filepath.Join(root, "mackerel-agent2.conf")
confFile, err := os.Create(f)
if err != nil {
t.Fatalf("Could not create temporary file for test")
}
confFile.Close()
argv := []string{"-conf", f, "-apikey", "hoge"}
err = doInit(&flag.FlagSet{}, argv)
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
}
{
f := filepath.Join(root, "mackerel-agent-invalid.conf")
conffile, err := os.Create(f)
if err != nil {
t.Errorf("err should be nil but: %s", err)
}
conffile.WriteString(`dummy = "`)
conffile.Sync()
conffile.Close()
err = doInit(&flag.FlagSet{}, []string{"-conf", f, "-apikey=hoge"})
if err == nil || !strings.HasPrefix(err.Error(), "failed to load the config") {
t.Errorf("should return load config error but: %s", err)
}
}
}