"io/fs"
"os"
"path/filepath"
- "reflect"
"syscall"
"testing"
- "github.com/google/go-cmp/cmp"
-
ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+ "ruderich.org/simon/safcm/testutil"
)
func TestHandle(t *testing.T) {
}
changes, err := handle(tc.path, tc.line, tc.create)
- if !reflect.DeepEqual(tc.expChanges, changes) {
- t.Errorf("changes: %s",
- cmp.Diff(tc.expChanges, changes))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "changes",
+ changes, tc.expChanges)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
files, err := ft.WalkDir(path)
if err != nil {
t.Fatal(err)
}
- if !reflect.DeepEqual(tc.expFiles, files) {
- t.Errorf("files: %s",
- cmp.Diff(tc.expFiles, files))
- }
+ testutil.AssertEqual(t, "files", files, tc.expFiles)
})
}
"io/fs"
"os"
"os/exec"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestSyncCommands(t *testing.T) {
s.triggers = tc.triggers
err := s.syncCommands()
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
dbg := res.Wait()
- if !reflect.DeepEqual(tc.expResp, s.resp) {
- t.Errorf("resp: %s",
- cmp.Diff(tc.expResp, s.resp))
- }
- if !reflect.DeepEqual(tc.expDbg, dbg) {
- t.Errorf("dbg: %s",
- cmp.Diff(tc.expDbg, dbg))
- }
+ testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+ testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
})
}
}
"math/rand"
"os"
"path/filepath"
- "reflect"
"regexp"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+ "ruderich.org/simon/safcm/testutil"
)
var randFilesRegexp = regexp.MustCompile(`\d+"$`)
s.setDefaults()
err := s.syncFiles()
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
dbg := res.Wait()
// Remove random file names from result
for i, x := range dbg {
dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`)
}
- if !reflect.DeepEqual(tc.expDbg, dbg) {
- t.Errorf("dbg: %s",
- cmp.Diff(tc.expDbg, dbg))
- }
+ testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
files, err := ft.WalkDir(path)
if err != nil {
t.Fatal(err)
}
- if !reflect.DeepEqual(tc.expFiles, files) {
- t.Errorf("files: %s",
- cmp.Diff(tc.expFiles, files))
- }
+ testutil.AssertEqual(t, "files", files, tc.expFiles)
- if !reflect.DeepEqual(tc.expResp, s.resp) {
- t.Errorf("resp: %s",
- cmp.Diff(tc.expResp, s.resp))
- }
- if !reflect.DeepEqual(tc.triggers, s.triggers) {
- t.Errorf("triggers: %s",
- cmp.Diff(tc.triggers, s.triggers))
- }
+ testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+ testutil.AssertEqual(t, "triggers",
+ s.triggers, tc.triggers)
})
}
var changed bool
err := s.syncFile(tc.file, &changed)
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
dbg := res.Wait()
// Remove random file names from result
for i, x := range dbg {
dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`)
}
- if !reflect.DeepEqual(tc.expDbg, dbg) {
- t.Errorf("dbg: %s",
- cmp.Diff(tc.expDbg, dbg))
- }
+ testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
files, err := ft.WalkDir(path)
if err != nil {
t.Fatal(err)
}
- if !reflect.DeepEqual(tc.expFiles, files) {
- t.Errorf("files: %s",
- cmp.Diff(tc.expFiles, files))
- }
+ testutil.AssertEqual(t, "files", files, tc.expFiles)
- if tc.expChanged != changed {
- t.Errorf("changed = %#v, want %#v",
- changed, tc.expChanged)
- }
- if !reflect.DeepEqual(tc.expResp, s.resp) {
- t.Errorf("resp: %s",
- cmp.Diff(tc.expResp, s.resp))
- }
+ testutil.AssertEqual(t, "changed", changed, tc.expChanged)
+ testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
})
}
"fmt"
"os"
"os/exec"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestSyncPackagesDebian(t *testing.T) {
})
err := s.syncPackagesDebian()
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
dbg := res.Wait()
- if !reflect.DeepEqual(tc.expResp, s.resp) {
- t.Errorf("resp: %s",
- cmp.Diff(tc.expResp, s.resp))
- }
- if !reflect.DeepEqual(tc.expDbg, dbg) {
- t.Errorf("dbg: %s",
- cmp.Diff(tc.expDbg, dbg))
- }
+ testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+ testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
})
}
}
"bytes"
"fmt"
"os/exec"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestSyncServicesSystemd(t *testing.T) {
})
err := s.syncServicesSystemd()
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
dbg := res.Wait()
- if !reflect.DeepEqual(tc.expResp, s.resp) {
- t.Errorf("resp: %s",
- cmp.Diff(tc.expResp, s.resp))
- }
- if !reflect.DeepEqual(tc.expDbg, dbg) {
- t.Errorf("dbg: %s",
- cmp.Diff(tc.expDbg, dbg))
- }
+ testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+ testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
})
}
}
package sync
import (
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
+ "ruderich.org/simon/safcm/testutil"
)
func TestTriggerPaths(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := triggerPaths(tc.path)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
"fmt"
"io/fs"
"os"
- "reflect"
"syscall"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func chmod(name string, perm int) {
for _, tc := range tests {
t.Run(tc.group, func(t *testing.T) {
res, err := LoadFiles(tc.group)
-
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"fmt"
"os"
"path/filepath"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
+ "ruderich.org/simon/safcm/testutil"
)
func TestLoadGroups(t *testing.T) {
}
res, err := LoadGroups(tc.cfg, tc.hosts)
-
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
t.Run(tc.name, func(t *testing.T) {
res, err := ResolveHostGroups(tc.host, allGroups,
tc.detected)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"fmt"
"os"
"path/filepath"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
+ "ruderich.org/simon/safcm/testutil"
)
func TestLoadHosts(t *testing.T) {
}
res, err := LoadHosts()
-
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"fmt"
"io/fs"
"os"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestLoadPermissions(t *testing.T) {
}
err = LoadPermissions(tc.group, files)
- if !reflect.DeepEqual(tc.exp, files) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, files))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", files, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"fmt"
"io/fs"
"os"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestLoadTemplates(t *testing.T) {
err = LoadTemplates(tc.group, files,
host, groups, allHosts, allGroups)
- if !reflect.DeepEqual(tc.exp, files) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, files))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", files, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"fmt"
"io/fs"
"os"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestLoadTriggers(t *testing.T) {
}
err = LoadTriggers(tc.group, files)
- if !reflect.DeepEqual(tc.exp, files) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, files))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", files, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
"io/fs"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
"ruderich.org/simon/safcm/cmd/safcm/config"
+ "ruderich.org/simon/safcm/testutil"
)
func TestFormatFileChanges(t *testing.T) {
}
res := s.formatFileChanges(tc.changes)
- if tc.exp != res {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
}
res := s.formatPackageChanges(tc.changes)
- if tc.exp != res {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
}
res := s.formatServiceChanges(tc.changes)
- if tc.exp != res {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
}
res := s.formatCommandChanges(tc.changes)
- if tc.exp != res {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
package main
import (
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestHostInfoRespToGroups(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := hostInfoRespToGroups(tc.resp)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
"log"
"os"
"path/filepath"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm"
+ "ruderich.org/simon/safcm/testutil"
)
func TestHostSyncReq(t *testing.T) {
}
res, err := s.hostSyncReq(tc.detected)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
close(ch)
<-done
- if !reflect.DeepEqual(tc.expEvents, events) {
- t.Errorf("events: %s",
- cmp.Diff(tc.expEvents, events))
- }
+ testutil.AssertEqual(t, "events",
+ events, tc.expEvents)
})
}
}
import (
"fmt"
"os"
- "reflect"
"testing"
- "github.com/google/go-cmp/cmp"
-
"ruderich.org/simon/safcm/cmd/safcm/config"
+ "ruderich.org/simon/safcm/testutil"
)
func TestHostsToSync(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res, err := hostsToSync(tc.names, allHosts, allGroups)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("err = %#v, want %#v",
- err, tc.expErr)
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
})
}
}
import (
"testing"
- "github.com/google/go-cmp/cmp"
+ "ruderich.org/simon/safcm/testutil"
)
func TestEscapeControlCharacters(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := EscapeControlCharacters(tc.isTTY, tc.x)
- if tc.exp != res {
- t.Errorf("res: %s", cmp.Diff(tc.exp, res))
- }
+ testutil.AssertEqual(t, "res", res, tc.exp)
})
}
}
--- /dev/null
+// Utility functions useful for tests
+
+// Copyright (C) 2021 Simon Ruderich
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package testutil
+
+import (
+ "fmt"
+ "reflect"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func AssertEqual(t *testing.T, name string, act, exp interface{}) {
+ if !reflect.DeepEqual(act, exp) {
+ t.Errorf("%s: %s", name, cmp.Diff(exp, act))
+ }
+}
+
+func AssertErrorEqual(t *testing.T, name string, act, exp error) {
+ // Ugly but the simplest way to compare errors (including nil)
+ actStr := fmt.Sprintf("%s", act)
+ expStr := fmt.Sprintf("%s", exp)
+ if actStr != expStr {
+ t.Errorf("err = %s (%#v), want %s (%#v)",
+ actStr, act, expStr, exp)
+ }
+}