]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm/sync_sync_test.go
Move synchronization loop into new package frontend
[safcm/safcm.git] / cmd / safcm / sync_sync_test.go
index 889aa2f21ad0098e2c14e29f659ee23f5c05a6f6..40c5c556eccd5961513abe52c4671ed467a6b7db 100644 (file)
@@ -40,7 +40,6 @@ func TestHostSyncReq(t *testing.T) {
                project   string
                host      string
                detected  []string
-               level     safcm.LogLevel
                exp       safcm.MsgSyncReq
                expEvents []string
                expErr    error
@@ -54,7 +53,6 @@ func TestHostSyncReq(t *testing.T) {
                        "project",
                        "host1.example.org",
                        nil,
-                       safcm.LogDebug3,
                        safcm.MsgSyncReq{
                                Groups: []string{
                                        "all",
@@ -148,124 +146,21 @@ func TestHostSyncReq(t *testing.T) {
                                },
                        },
                        []string{
-                               "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",
+                               "3 false host groups: all group group3 host1.example.org remove",
+                               "3 false host group priorities (descending): host1.example.org",
                        },
                        nil,
                },
 
-               {
-                       "project: host1 (log level info)",
-                       "project",
-                       "host1.example.org",
-                       nil,
-                       safcm.LogInfo,
-                       safcm.MsgSyncReq{
-                               Groups: []string{
-                                       "all",
-                                       "group",
-                                       "group3",
-                                       "remove",
-                                       "host1.example.org",
-                               },
-                               Files: map[string]*safcm.File{
-                                       "/": &safcm.File{Path: "/",
-                                               OrigGroup: "group",
-                                               Mode:      fs.ModeDir | 0755 | fs.ModeSetgid,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                               TriggerCommands: []string{
-                                                       "touch /.update",
-                                               },
-                                       },
-                                       "/etc": &safcm.File{
-                                               OrigGroup: "group",
-                                               Path:      "/etc",
-                                               Mode:      fs.ModeDir | 0755,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                       },
-                                       "/etc/.hidden": &safcm.File{
-                                               OrigGroup: "group",
-                                               Path:      "/etc/.hidden",
-                                               Mode:      0100 | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                               Data:      []byte("..."),
-                                       },
-                                       "/etc/motd": &safcm.File{
-                                               OrigGroup: "group",
-                                               Path:      "/etc/motd",
-                                               Mode:      0644,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                               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",
-                                               Path:      "/etc/rc.local",
-                                               Mode:      0700,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                               Data:      []byte("#!/bin/sh\n"),
-                                               TriggerCommands: []string{
-                                                       "/etc/rc.local",
-                                               },
-                                       },
-                                       "/etc/resolv.conf": &safcm.File{
-                                               OrigGroup: "group",
-                                               Path:      "/etc/resolv.conf",
-                                               Mode:      0641,
-                                               User:      "user",
-                                               Uid:       -1,
-                                               Group:     "group",
-                                               Gid:       -1,
-                                               Data:      []byte("nameserver ::1\n"),
-                                               TriggerCommands: []string{
-                                                       "echo resolv.conf updated",
-                                               },
-                                       },
-                                       "/etc/test": &safcm.File{
-                                               OrigGroup: "group",
-                                               Path:      "/etc/test",
-                                               Mode:      os.ModeSymlink | 0777,
-                                               Uid:       -1,
-                                               Gid:       -1,
-                                               Data:      []byte("doesnt-exist"),
-                                       },
-                               },
-                               Packages: []string{
-                                       "unbound",
-                                       "unbound-anchor",
-                               },
-                               Services: []string{
-                                       "unbound",
-                               },
-                               Commands: []*safcm.Command{
-                                       {
-                                               OrigGroup: "group",
-                                               Cmd:       "echo command one",
-                                       },
-                                       {
-                                               OrigGroup: "group",
-                                               Cmd:       "echo -n command two",
-                                       },
-                               },
-                       },
-                       nil,
-                       nil,
-               },
-
                {
                        "conflict: file",
                        "project-conflict-file",
                        "host1.example.org",
                        nil,
-                       safcm.LogDebug3,
                        safcm.MsgSyncReq{},
                        []string{
-                               "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
-                               "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org",
+                               "3 false host groups: all dns host1.example.org",
+                               "3 false host group priorities (descending): host1.example.org",
                        },
                        fmt.Errorf("groups dns and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
                },
@@ -276,11 +171,10 @@ func TestHostSyncReq(t *testing.T) {
                        []string{
                                "detected_other",
                        },
-                       safcm.LogDebug3,
                        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 (descending): host2.example.org",
+                               "3 false host groups: all detected_other host2.example.org other",
+                               "3 false host group priorities (descending): host2.example.org",
                        },
                        fmt.Errorf("groups other and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
                },
@@ -290,11 +184,10 @@ func TestHostSyncReq(t *testing.T) {
                        "project-conflict-dir",
                        "host1.example.org",
                        nil,
-                       safcm.LogDebug3,
                        safcm.MsgSyncReq{},
                        []string{
-                               "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
-                               "host1.example.org: <nil> 3 host group priorities (descending): host1.example.org",
+                               "3 false host groups: all dns host1.example.org",
+                               "3 false host group priorities (descending): host1.example.org",
                        },
                        fmt.Errorf("groups dns and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
                },
@@ -305,11 +198,10 @@ func TestHostSyncReq(t *testing.T) {
                        []string{
                                "detected_other",
                        },
-                       safcm.LogDebug3,
                        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 (descending): host2.example.org",
+                               "3 false host groups: all detected_other host2.example.org other",
+                               "3 false host group priorities (descending): host2.example.org",
                        },
                        fmt.Errorf("groups other and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
                },
@@ -319,7 +211,6 @@ func TestHostSyncReq(t *testing.T) {
                        "project-group-cycle",
                        "host1.example.org",
                        nil,
-                       safcm.LogDebug3,
                        safcm.MsgSyncReq{},
                        nil,
                        fmt.Errorf("groups.yaml: cycle while expanding group \"group-b\""),
@@ -330,7 +221,6 @@ func TestHostSyncReq(t *testing.T) {
                        "project-group_priority",
                        "host1.example.org",
                        nil,
-                       safcm.LogDebug3,
                        safcm.MsgSyncReq{
                                Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
                                Files: map[string]*safcm.File{
@@ -421,10 +311,10 @@ func TestHostSyncReq(t *testing.T) {
                                },
                        },
                        []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 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`,
+                               "3 false host groups: all group-a group-b host1.example.org",
+                               "3 false host group priorities (descending): host1.example.org group-a group-b all",
+                               `4 false files: "/etc": group group-a overwrites triggers from group group-b`,
+                               `4 false files: "/etc": group host1.example.org overwrites triggers from group group-a`,
                        },
                        nil,
                },
@@ -434,7 +324,6 @@ func TestHostSyncReq(t *testing.T) {
                        "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{
@@ -456,8 +345,8 @@ func TestHostSyncReq(t *testing.T) {
                                },
                        },
                        []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",
+                               "3 false host groups: all group-a group-b host1.example.org",
+                               "3 false host group priorities (descending): host1.example.org group-a",
                        },
                        nil,
                },
@@ -483,43 +372,22 @@ func TestHostSyncReq(t *testing.T) {
                        if err != nil {
                                t.Fatal(err)
                        }
-                       cfg.LogLevel = tc.level
 
                        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{}{}
-                       }()
-
                        s := &Sync{
                                host:      allHosts.Map[tc.host],
                                config:    cfg,
                                allHosts:  allHosts,
                                allGroups: allGroups,
-                               events:    ch,
+                               logFunc: func(level safcm.LogLevel, escaped bool, msg string) {
+                                       events = append(events,
+                                               fmt.Sprintf("%d %v %s", level, escaped, msg))
+                               },
                        }
 
                        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)
                })