]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/main_sync_test.go
sync: remove duplicate "priority" from group priority log message
[safcm/safcm.git] / cmd / safcm / main_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 main_test
17
18 import (
19         "fmt"
20         "net"
21         "os"
22         "os/exec"
23         "regexp"
24         "runtime"
25         "strings"
26         "testing"
27         "time"
28
29         "ruderich.org/simon/safcm/testutil"
30 )
31
32 func TestSyncSshEndToEnd(t *testing.T) {
33         cwd, err := os.Getwd()
34         if err != nil {
35                 t.Fatal(err)
36         }
37         defer os.Chdir(cwd)
38
39         var suffix string
40         // Needs different options in sshd_config
41         if runtime.GOOS == "openbsd" {
42                 suffix = ".openbsd"
43         }
44
45         sshDir := cwd + "/testdata/ssh"
46         sshCmd := exec.Command("/usr/sbin/sshd",
47                 "-D", // stay in foreground
48                 "-e", // write messages to stderr instead of syslog
49                 "-f", sshDir+"/sshd/sshd_config"+suffix,
50                 "-h", sshDir+"/sshd/ssh_host_key",
51                 "-o", "AuthorizedKeysFile="+sshDir+"/ssh/authorized_keys",
52         )
53         sshCmd.Stderr = os.Stderr
54         err = sshCmd.Start()
55         if err != nil {
56                 t.Fatal(err)
57         }
58         defer sshCmd.Process.Kill()
59
60         // Wait until SSH server is ready (up to 30 seconds)
61         for i := 0; i < 30; i++ {
62                 conn, err := net.Dial("tcp", "127.0.0.1:29327")
63                 if err == nil {
64                         conn.Close()
65                         break
66                 }
67                 time.Sleep(time.Second)
68         }
69
70         err = os.Chdir(sshDir + "/project")
71         if err != nil {
72                 t.Fatal(err)
73         }
74
75         tests := []struct {
76                 name   string
77                 remove bool
78                 args   []string
79                 exp    string
80                 expErr error
81         }{
82
83                 {
84                         "no settings",
85                         true,
86                         []string{"no-settings.example.org"},
87                         `<LOG>[info]    [no-settings.example.org] remote helper upload in progress
88 <LOG>[info]    [no-settings.example.org] no changes
89 `,
90                         nil,
91                 },
92                 {
93                         "no settings (no helper upload)",
94                         false,
95                         []string{"no-settings.example.org"},
96                         `<LOG>[info]    [no-settings.example.org] no changes
97 `,
98                         nil,
99                 },
100                 {
101                         "no settings (error)",
102                         true,
103                         []string{"-log", "error", "no-settings.example.org"},
104                         ``,
105                         nil,
106                 },
107                 {
108                         "no settings (verbose)",
109                         true,
110                         []string{"-log", "verbose", "no-settings.example.org"},
111                         `<LOG>[info]    [no-settings.example.org] remote helper upload in progress
112 <LOG>[verbose] [no-settings.example.org] host groups: all <DET> <DET> no-settings.example.org
113 <LOG>[verbose] [no-settings.example.org] host group priorities (descending): no-settings.example.org
114 <LOG>[info]    [no-settings.example.org] no changes
115 `,
116                         nil,
117                 },
118                 {
119                         "no settings (debug2)",
120                         true,
121                         []string{"-log", "debug2", "no-settings.example.org"},
122                         `<LOG>[info]    [no-settings.example.org] remote helper upload in progress
123 <LOG>[verbose] [no-settings.example.org] host groups: all <DET> <DET> no-settings.example.org
124 <LOG>[verbose] [no-settings.example.org] host group priorities (descending): no-settings.example.org
125 <LOG>[info]    [no-settings.example.org] no changes
126 `,
127                         nil,
128                 },
129         }
130
131         remotePath := fmt.Sprintf("/tmp/safcm-remote-%d", os.Getuid())
132
133         logRegexp := regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `)
134         detectedRegexp := regexp.MustCompile(`detected_\S+`)
135
136         for _, tc := range tests {
137                 t.Run(tc.name, func(t *testing.T) {
138                         if tc.remove {
139                                 os.Remove(remotePath)
140                         }
141
142                         args := append([]string{"sync",
143                                 "-sshconfig", sshDir + "/ssh/ssh_config",
144                         }, tc.args...)
145                         cmd := exec.Command("../../../../../safcm", args...)
146                         out, err := cmd.CombinedOutput()
147
148                         var tmp []string
149                         for _, x := range strings.Split(string(out), "\n") {
150                                 // Strip parts which change on each run (LOG)
151                                 // or depending on the system (DET)
152                                 x = logRegexp.ReplaceAllString(x, "<LOG>")
153                                 x = detectedRegexp.ReplaceAllString(x, "<DET>")
154                                 tmp = append(tmp, x)
155                         }
156                         res := strings.Join(tmp, "\n")
157
158                         testutil.AssertEqual(t, "res", res, tc.exp)
159                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
160                 })
161         }
162
163         os.Remove(remotePath)
164 }