This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
forked from mackerelio/mackerel-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_init_test.go
69 lines (62 loc) · 1.69 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
package main
import (
"flag"
"io/ioutil"
"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, err := ioutil.TempDir("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary dir for test")
}
defer os.RemoveAll(root)
{
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, _ := ioutil.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)
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)
}
}
}