-
Notifications
You must be signed in to change notification settings - Fork 52
/
main_test.go
46 lines (41 loc) · 1012 Bytes
/
main_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
package main
import (
"bytes"
"errors"
"os"
"testing"
)
func TestWriteToDisk(t *testing.T) {
tests := map[string]struct {
cfg dockerConfig
want []byte
wantErr error
}{
"success": {cfg: dockerConfig{Debug: false, LogDriver: "json-file"}, want: []byte(`{"debug":false,"log-driver":"json-file"}`)},
"success - empty struct": {cfg: dockerConfig{}, want: []byte(`{"debug":false}`)},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a temporary directory
dir, err := os.MkdirTemp("", "hook-docker")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
loc := dir + "daemon.json"
err = tt.cfg.writeToDisk(loc)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("got err %v, want %v", err, tt.wantErr)
}
if tt.wantErr == nil {
got, err := os.ReadFile(loc)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, tt.want) {
t.Fatalf("\ngot:\n %s\nwant:\n %s", got, tt.want)
}
}
})
}
}