"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) {
}{
// NOTE: Also update MsgSyncReq in safcm-remote test cases
- // when changing anything here!
+ // changing the MsgSyncReq struct!
{
"project: host1",
Groups: []string{
"all",
"group",
+ "group3",
"remove",
"host1.example.org",
},
Mode: 0644,
Uid: -1,
Gid: -1,
- Data: []byte("Welcome to Host ONE\n\n\n\n"),
+ Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
},
"/etc/rc.local": &safcm.File{
OrigGroup: "group",
Services: []string{
"unbound",
},
- Commands: []string{
- "echo command one",
- "echo -n command two",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo command one",
+ },
+ {
+ OrigGroup: "group",
+ Cmd: "echo -n command two",
+ },
},
},
[]string{
- "host1.example.org: <nil> 3 host groups: all group host1.example.org remove",
- "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
+ "host1.example.org: <nil> 3 host groups: all group group3 host1.example.org remove",
+ "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org",
},
nil,
},
Groups: []string{
"all",
"group",
+ "group3",
"remove",
"host1.example.org",
},
Mode: 0644,
Uid: -1,
Gid: -1,
- Data: []byte("Welcome to Host ONE\n\n\n\n"),
+ Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
},
"/etc/rc.local": &safcm.File{
OrigGroup: "group",
Services: []string{
"unbound",
},
- Commands: []string{
- "echo command one",
- "echo -n command two",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo command one",
+ },
+ {
+ OrigGroup: "group",
+ Cmd: "echo -n command two",
+ },
},
},
nil,
safcm.MsgSyncReq{},
[]string{
"host1.example.org: <nil> 3 host groups: all dns host1.example.org",
- "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
+ "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org",
},
- fmt.Errorf("groups dns and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
+ fmt.Errorf("groups dns and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
},
{
"conflict: file from detected group",
safcm.MsgSyncReq{},
[]string{
"host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
- "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
+ "host2.example.org: <nil> 3 host group priorities (descending): host2.example.org",
},
- fmt.Errorf("groups other and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
+ fmt.Errorf("groups other and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
},
{
safcm.MsgSyncReq{},
[]string{
"host1.example.org: <nil> 3 host groups: all dns host1.example.org",
- "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
+ "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org",
},
- fmt.Errorf("groups dns and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
+ fmt.Errorf("groups dns and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
},
{
"conflict: dir from detected group",
safcm.MsgSyncReq{},
[]string{
"host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
- "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
+ "host2.example.org: <nil> 3 host group priorities (descending): host2.example.org",
},
- fmt.Errorf("groups other and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
+ fmt.Errorf("groups other and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
},
{
},
{
- "group_order",
- "project-group_order",
+ "group_priority",
+ "project-group_priority",
"host1.example.org",
nil,
safcm.LogDebug3,
},
[]string{
"host1.example.org: <nil> 3 host groups: all group-a group-b host1.example.org",
- "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org group-a group-b all",
+ "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org group-a group-b all",
`host1.example.org: <nil> 4 files: "/etc": group group-a overwrites triggers from group group-b`,
`host1.example.org: <nil> 4 files: "/etc": group host1.example.org overwrites triggers from group group-a`,
},
nil,
},
+
+ {
+ "group_priority (single group)",
+ "project-group_priority-single",
+ "host1.example.org",
+ nil,
+ safcm.LogDebug3,
+ safcm.MsgSyncReq{
+ Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
+ Files: map[string]*safcm.File{
+ "/": {
+ Path: "/",
+ Mode: fs.ModeDir | 0755,
+ Uid: -1,
+ Gid: -1,
+ OrigGroup: "group-a",
+ },
+ "/file.txt": {
+ Path: "/file.txt",
+ Mode: 0644,
+ Uid: -1,
+ Gid: -1,
+ Data: []byte("file.txt: from group-a\n"),
+ OrigGroup: "group-a",
+ },
+ },
+ },
+ []string{
+ "host1.example.org: <nil> 3 host groups: all group-a group-b host1.example.org",
+ "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org group-a",
+ },
+ nil,
+ },
}
for _, tc := range tests {
- err = os.Chdir(filepath.Join(cwd, "testdata", tc.project))
- if err != nil {
- t.Fatal(err)
- }
-
- // `safcm fixperms` in case user has strict umask
- log.SetOutput(io.Discard)
- err := MainFixperms()
- if err != nil {
- t.Fatal(err)
- }
- log.SetOutput(os.Stderr)
+ t.Run(tc.name, func(t *testing.T) {
+ err = os.Chdir(filepath.Join(cwd,
+ "testdata", tc.project))
+ if err != nil {
+ t.Fatal(err)
+ }
- cfg, allHosts, allGroups, err := LoadBaseFiles()
- if err != nil {
- t.Fatal(err)
- }
- cfg.LogLevel = tc.level
+ // `safcm fixperms` in case user has strict umask
+ log.SetOutput(io.Discard)
+ err := MainFixperms()
+ if err != nil {
+ t.Fatal(err)
+ }
+ log.SetOutput(os.Stderr)
- var events []string
- ch := make(chan Event)
- done := make(chan struct{})
- go func() {
- for {
- x, ok := <-ch
- if !ok {
- break
- }
- if x.ConnEvent.Type != 0 {
- panic("unexpected ConnEvent")
- }
- events = append(events,
- fmt.Sprintf("%s: %v %d %s",
- x.Host.Name,
- x.Error, x.Log.Level,
- x.Log.Text))
+ cfg, allHosts, allGroups, err := LoadBaseFiles()
+ if err != nil {
+ t.Fatal(err)
}
- done <- struct{}{}
- }()
+ cfg.LogLevel = tc.level
- s := &Sync{
- host: allHosts.Map[tc.host],
- config: cfg,
- allHosts: allHosts,
- allGroups: allGroups,
- events: ch,
- }
+ var events []string
+ ch := make(chan Event)
+ done := make(chan struct{})
+ go func() {
+ for {
+ x, ok := <-ch
+ if !ok {
+ break
+ }
+ if x.ConnEvent.Type != 0 {
+ panic("unexpected ConnEvent")
+ }
+ events = append(events,
+ fmt.Sprintf("%s: %v %d %s",
+ x.Host.Name,
+ x.Error, x.Log.Level,
+ x.Log.Text))
+ }
+ done <- struct{}{}
+ }()
- res, err := s.hostSyncReq(tc.detected)
- if !reflect.DeepEqual(tc.exp, res) {
- t.Errorf("%s: res: %s", tc.name,
- 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("%s: err = %#v, want %#v",
- tc.name, err, tc.expErr)
- }
+ s := &Sync{
+ host: allHosts.Map[tc.host],
+ config: cfg,
+ allHosts: allHosts,
+ allGroups: allGroups,
+ events: ch,
+ }
- close(ch)
- <-done
- if !reflect.DeepEqual(tc.expEvents, events) {
- t.Errorf("%s: events: %s", tc.name,
- cmp.Diff(tc.expEvents, events))
- }
+ res, err := s.hostSyncReq(tc.detected)
+ testutil.AssertEqual(t, "res", res, tc.exp)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
+ close(ch)
+ <-done
+ testutil.AssertEqual(t, "events",
+ events, tc.expEvents)
+ })
}
}