1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
26 "github.com/google/go-cmp/cmp"
27 "github.com/google/go-cmp/cmp/cmpopts"
29 "ruderich.org/simon/safcm"
30 "ruderich.org/simon/safcm/cmd/safcm-remote/log"
31 "ruderich.org/simon/safcm/cmd/safcm-remote/run"
34 // testRunner implements run.Runner to test commands without actually running
36 type testRunner struct {
44 func (r *testRunner) Run(cmd *exec.Cmd) error {
45 stdout, stderr, resErr := r.check("run", cmd)
46 _, err := cmd.Stdout.Write(stdout)
50 _, err = cmd.Stderr.Write(stderr)
56 func (r *testRunner) CombinedOutput(cmd *exec.Cmd) ([]byte, error) {
59 stdout, stderr, err := r.check("combinedOutput", cmd)
61 // stdout also contains stderr
62 r.t.Fatalf("CombinedOutput: stderr != nil, but %v", stderr)
66 func (r *testRunner) check(method string, cmd *exec.Cmd) (
67 []byte, []byte, error) {
70 if len(r.expCmds) == 0 {
71 r.t.Fatalf("%s: empty expCmds", method)
73 if len(r.resStdout) == 0 {
74 r.t.Fatalf("%s: empty resStdout", method)
76 if len(r.resStderr) == 0 {
77 r.t.Fatalf("%s: empty resStderr", method)
79 if len(r.resError) == 0 {
80 r.t.Fatalf("%s: empty resError", method)
84 r.expCmds = r.expCmds[1:]
85 if !reflect.DeepEqual(exp, cmd) {
86 opts := cmpopts.IgnoreUnexported(exec.Cmd{}, bytes.Buffer{})
87 r.t.Errorf("%s: %s", method, cmp.Diff(exp, cmd, opts))
90 var stdout, stderr []byte
93 stdout, r.resStdout = r.resStdout[0], r.resStdout[1:]
94 stderr, r.resStderr = r.resStderr[0], r.resStderr[1:]
95 err, r.resError = r.resError[0], r.resError[1:]
97 return stdout, stderr, err
100 type syncTestResult struct {
107 func prepareSync(req safcm.MsgSyncReq, runner *testRunner) (
108 *Sync, *syncTestResult) {
110 res := &syncTestResult{
111 ch: make(chan string),
121 res.dbg = append(res.dbg, x)
126 logger := log.NewLogger(
127 func(level safcm.LogLevel, msg string) {
128 res.ch <- fmt.Sprintf("%d: %s", level, msg)
132 cmd: run.NewCmd(runner, logger),
137 func (s *syncTestResult) Wait() []string {
143 // All expected commands must have been executed
144 if len(s.runner.expCmds) != 0 {
145 s.runner.t.Errorf("expCmds left: %v", s.runner.expCmds)
147 if len(s.runner.resStdout) != 0 {
148 s.runner.t.Errorf("resStdout left: %v", s.runner.resStdout)
150 if len(s.runner.resStderr) != 0 {
151 s.runner.t.Errorf("resStderr left: %v", s.runner.resStderr)
153 if len(s.runner.resError) != 0 {
154 s.runner.t.Errorf("resError left: %v", s.runner.resError)