File: config_test.go

package info (click to toggle)
golang-github-lorenzosaino-go-sysctl 0.3.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: makefile: 44
file content (113 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download
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
package sysctl

import (
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func Test_parseConfig(t *testing.T) {
	cases := []struct {
		name string
		path string
		ok   bool
		out  map[string]string
	}{
		{
			name: "ok",
			path: "testdata/config/sysctl-correct.conf",
			ok:   true,
			out: map[string]string{
				"kernel.domainname": "example.com",
				"kernel.modprobe":   "/sbin/mod probe",
				"kernel.hostname":   "example.com",
			},
		},
		{
			name: "empty",
			path: "testdata/config/sysctl-empty.conf",
			ok:   true,
			out:  map[string]string{},
		},
		{
			name: "only-comments",
			path: "testdata/config/sysctl-only-comments.conf",
			ok:   true,
			out:  map[string]string{},
		},
		{
			name: "malformatted",
			path: "testdata/config/sysctl-error.conf",
			ok:   false,
		},
		{
			name: "not-found",
			path: "testdata/config/not-found",
			ok:   false,
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			out := make(map[string]string)
			err := parseConfig(c.path, out)
			if c.ok && err != nil {
				t.Fatalf("error parsing: %v", err)
			}
			if !c.ok && err == nil {
				t.Fatalf("expected error when parsing %s but it succeeded", c.path)
			}
			if err != nil {
				t.Logf("err: %v", err)
				return
			}
			if diff := cmp.Diff(c.out, out); diff != "" {
				t.Fatalf("unexpected output from %s (-want +got):\n%s", c.path, diff)
			}
		})
	}
}

func TestLoadConfig(t *testing.T) {
	cases := []struct {
		name  string
		paths []string
		ok    bool
		out   map[string]string
	}{
		{
			name:  "not-found",
			paths: []string{"testdata/config/not-found"},
			ok:    false,
		},
		{
			name: "ok",
			paths: []string{
				"testdata/config/sysctl-a.conf",
				"testdata/config/sysctl-b.conf",
			},
			ok: true,
			out: map[string]string{
				"kernel.domainname": "b.com",
			},
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			out, err := LoadConfig(c.paths...)
			if c.ok && err != nil {
				t.Fatalf("error parsing: %v", err)
			}
			if !c.ok && err == nil {
				t.Fatalf("expected error when parsing [%s] but it succeeded", strings.Join(c.paths, ", "))
			}
			if err != nil {
				t.Logf("err: %v", err)
				return
			}
			if diff := cmp.Diff(c.out, out); diff != "" {
				t.Fatalf("unexpected output from %s (-want +got):\n%s", c.paths, diff)
			}
		})
	}
}