]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/sync_test.go
4c0f08d5abdf6515f4da8df503b2088fd71d355d
[safcm/safcm.git] / cmd / safcm-remote / sync / sync_test.go
1 // Copyright (C) 2021  Simon Ruderich
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package sync
17
18 import (
19         "bytes"
20         "fmt"
21         "os/exec"
22         "reflect"
23         "sync"
24         "testing"
25
26         "github.com/google/go-cmp/cmp"
27         "github.com/google/go-cmp/cmp/cmpopts"
28
29         "ruderich.org/simon/safcm"
30         "ruderich.org/simon/safcm/cmd/safcm-remote/log"
31         "ruderich.org/simon/safcm/cmd/safcm-remote/run"
32 )
33
34 type testRunner struct {
35         t         *testing.T
36         expCmds   []*exec.Cmd
37         resStdout [][]byte
38         resStderr [][]byte
39         resError  []error
40 }
41
42 func (r *testRunner) Run(cmd *exec.Cmd) error {
43         stdout, stderr, resErr := r.check("run", cmd)
44         _, err := cmd.Stdout.Write(stdout)
45         if err != nil {
46                 panic(err)
47         }
48         _, err = cmd.Stderr.Write(stderr)
49         if err != nil {
50                 panic(err)
51         }
52         return resErr
53 }
54 func (r *testRunner) CombinedOutput(cmd *exec.Cmd) ([]byte, error) {
55         stdout, stderr, err := r.check("combinedOutput", cmd)
56         if stderr != nil {
57                 // stdout also contains stderr
58                 r.t.Fatalf("CombinedOutput: stderr != nil, but %v", stderr)
59         }
60         return stdout, err
61 }
62 func (r *testRunner) check(method string, cmd *exec.Cmd) (
63         []byte, []byte, error) {
64
65         if len(r.expCmds) == 0 {
66                 r.t.Fatalf("%s: empty expCmds", method)
67         }
68         if len(r.resStdout) == 0 {
69                 r.t.Fatalf("%s: empty resStdout", method)
70         }
71         if len(r.resStderr) == 0 {
72                 r.t.Fatalf("%s: empty resStderr", method)
73         }
74         if len(r.resError) == 0 {
75                 r.t.Fatalf("%s: empty resError", method)
76         }
77
78         exp := r.expCmds[0]
79         r.expCmds = r.expCmds[1:]
80         if !reflect.DeepEqual(exp, cmd) {
81                 r.t.Errorf("%s: %s", method,
82                         cmp.Diff(exp, cmd, cmpopts.IgnoreUnexported(
83                                 exec.Cmd{},
84                                 bytes.Buffer{})))
85         }
86
87         var stdout, stderr []byte
88         var err error
89
90         stdout, r.resStdout = r.resStdout[0], r.resStdout[1:]
91         stderr, r.resStderr = r.resStderr[0], r.resStderr[1:]
92         err, r.resError = r.resError[0], r.resError[1:]
93
94         return stdout, stderr, err
95 }
96
97 type syncTestResult struct {
98         ch     chan string
99         wg     sync.WaitGroup
100         dbg    []string
101         runner *testRunner
102 }
103
104 func prepareSync(req safcm.MsgSyncReq, runner *testRunner) (
105         *Sync, *syncTestResult) {
106
107         res := &syncTestResult{
108                 ch:     make(chan string),
109                 runner: runner,
110         }
111         res.wg.Add(1)
112         go func() {
113                 for {
114                         x, ok := <-res.ch
115                         if !ok {
116                                 break
117                         }
118                         res.dbg = append(res.dbg, x)
119                 }
120                 res.wg.Done()
121         }()
122
123         logger := log.NewLogger(logPrefix,
124                 func(level safcm.LogLevel, format string, a ...interface{}) {
125                         res.ch <- fmt.Sprintf("%d: %s", level,
126                                 fmt.Sprintf(format, a...))
127                 })
128         return &Sync{
129                 req: req,
130                 cmd: run.NewCmd(runner, logger),
131                 log: logger,
132         }, res
133 }
134
135 func (s *syncTestResult) Wait() []string {
136         close(s.ch)
137         s.wg.Wait()
138
139         // All expected commands must have been executed
140         if len(s.runner.expCmds) != 0 {
141                 s.runner.t.Errorf("expCmds left: %v", s.runner.expCmds)
142         }
143         if len(s.runner.resStdout) != 0 {
144                 s.runner.t.Errorf("resStdout left: %v", s.runner.resStdout)
145         }
146         if len(s.runner.resStderr) != 0 {
147                 s.runner.t.Errorf("resStderr left: %v", s.runner.resStderr)
148         }
149         if len(s.runner.resError) != 0 {
150                 s.runner.t.Errorf("resError left: %v", s.runner.resError)
151         }
152
153         return s.dbg
154 }