summaryrefslogtreecommitdiff
path: root/lib/config_test.go
blob: 41c62764e7f45c997b1b42447dabed3df29182c2 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package lib

import (
	"encoding/json"
	"testing"
)

func TestRegisterInputConfigCreator(t *testing.T) {
	resetConfigCreators()

	if err := RegisterInputConfigCreator("sample", func(a Action, data json.RawMessage) (InputConverter, error) {
		return mockInputConverter{typ: "sample", action: a}, nil
	}); err != nil {
		t.Fatalf("RegisterInputConfigCreator() error = %v", err)
	}

	if err := RegisterInputConfigCreator("sample", nil); err == nil {
		t.Fatalf("expected error for duplicated creator")
	}
}

func TestCreateInputConfig(t *testing.T) {
	resetConfigCreators()

	if _, err := createInputConfig("unknown", ActionAdd, nil); err == nil {
		t.Fatalf("expected error for unknown config type")
	}

	_ = RegisterInputConfigCreator("known", func(a Action, data json.RawMessage) (InputConverter, error) {
		return mockInputConverter{typ: "known", action: a}, nil
	})

	cfg, err := createInputConfig("known", ActionRemove, nil)
	if err != nil {
		t.Fatalf("createInputConfig() error = %v", err)
	}
	if cfg.GetAction() != ActionRemove || cfg.GetType() != "known" {
		t.Fatalf("unexpected converter: %v %v", cfg.GetType(), cfg.GetAction())
	}
}

func TestRegisterOutputConfigCreator(t *testing.T) {
	resetConfigCreators()

	if err := RegisterOutputConfigCreator("sample", func(a Action, data json.RawMessage) (OutputConverter, error) {
		return mockOutputConverter{typ: "sample", action: a}, nil
	}); err != nil {
		t.Fatalf("RegisterOutputConfigCreator() error = %v", err)
	}

	if err := RegisterOutputConfigCreator("sample", nil); err == nil {
		t.Fatalf("expected error for duplicated creator")
	}
}

func TestCreateOutputConfig(t *testing.T) {
	resetConfigCreators()

	if _, err := createOutputConfig("unknown", ActionAdd, nil); err == nil {
		t.Fatalf("expected error for unknown config type")
	}

	_ = RegisterOutputConfigCreator("known", func(a Action, data json.RawMessage) (OutputConverter, error) {
		return mockOutputConverter{typ: "known", action: a}, nil
	})

	cfg, err := createOutputConfig("known", ActionOutput, nil)
	if err != nil {
		t.Fatalf("createOutputConfig() error = %v", err)
	}
	if cfg.GetAction() != ActionOutput || cfg.GetType() != "known" {
		t.Fatalf("unexpected converter: %v %v", cfg.GetType(), cfg.GetAction())
	}
}

func TestInputConvConfigUnmarshalJSON(t *testing.T) {
	resetConfigCreators()
	_ = RegisterInputConfigCreator("stub", func(a Action, data json.RawMessage) (InputConverter, error) {
		return mockInputConverter{typ: "stub", action: a}, nil
	})

	jsonData := []byte(`{"type":"stub","action":"add","args":{}}`)
	var cfg inputConvConfig
	if err := cfg.UnmarshalJSON(jsonData); err != nil {
		t.Fatalf("UnmarshalJSON() error = %v", err)
	}
	if cfg.iType != "stub" || cfg.action != ActionAdd {
		t.Fatalf("unexpected values: %v %v", cfg.iType, cfg.action)
	}

	if err := cfg.UnmarshalJSON([]byte(`{"type":"stub","action":"invalid"}`)); err == nil {
		t.Fatalf("expected error for invalid action")
	}

	if err := cfg.UnmarshalJSON([]byte(`{"type":"unknown","action":"add"}`)); err == nil {
		t.Fatalf("expected error for unknown type")
	}

	if err := cfg.UnmarshalJSON([]byte(`{`)); err == nil {
		t.Fatalf("expected json error")
	}
}

func TestOutputConvConfigUnmarshalJSON(t *testing.T) {
	resetConfigCreators()
	_ = RegisterOutputConfigCreator("stub", func(a Action, data json.RawMessage) (OutputConverter, error) {
		return mockOutputConverter{typ: "stub", action: a}, nil
	})

	jsonData := []byte(`{"type":"stub","args":{}}`)
	var cfg outputConvConfig
	if err := cfg.UnmarshalJSON(jsonData); err != nil {
		t.Fatalf("UnmarshalJSON() error = %v", err)
	}
	if cfg.iType != "stub" || cfg.action != ActionOutput {
		t.Fatalf("unexpected values: %v %v", cfg.iType, cfg.action)
	}

	if err := cfg.UnmarshalJSON([]byte(`{"type":"stub","action":"invalid"}`)); err == nil {
		t.Fatalf("expected error for invalid action")
	}

	if err := cfg.UnmarshalJSON([]byte(`{"type":"unknown","action":"add"}`)); err == nil {
		t.Fatalf("expected error for unknown type")
	}

	if err := cfg.UnmarshalJSON([]byte(`{`)); err == nil {
		t.Fatalf("expected json error")
	}
}