]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/packages_debian_test.go
bd54a2fdd5d927ae2372aef4f86161813ccf936f
[safcm/safcm.git] / cmd / safcm-remote / sync / packages_debian_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 sync
17
18 import (
19         "bytes"
20         "fmt"
21         "os"
22         "os/exec"
23         "testing"
24
25         "ruderich.org/simon/safcm"
26         "ruderich.org/simon/safcm/testutil"
27 )
28
29 func TestSyncPackagesDebian(t *testing.T) {
30         tests := []struct {
31                 name    string
32                 req     safcm.MsgSyncReq
33                 stdout  [][]byte
34                 stderr  [][]byte
35                 errors  []error
36                 expCmds []*exec.Cmd
37                 expDbg  []string
38                 expResp safcm.MsgSyncResp
39                 expErr  error
40         }{
41
42                 // NOTE: Also update MsgSyncResp in safcm test cases when
43                 // changing the MsgSyncResp struct!
44
45                 {
46                         "packages already installed",
47                         safcm.MsgSyncReq{
48                                 Packages: []string{
49                                         "package-one",
50                                         "package-two",
51                                 },
52                         },
53                         [][]byte{
54                                 []byte(`install ok installed    golang
55 install ok installed    golang-1.16
56 install ok installed    golang-1.16-doc
57 install ok installed    golang-1.16-go
58 install ok installed    golang-1.16-src
59 hold ok installed       package-one
60 install ok installed    package-two
61 `),
62                         },
63                         [][]byte{nil},
64                         []error{nil},
65                         []*exec.Cmd{&exec.Cmd{
66                                 Path: "/usr/bin/dpkg-query",
67                                 Args: []string{
68                                         "/usr/bin/dpkg-query",
69                                         "--show",
70                                         `--showformat=${Status}\t${Package}\n`,
71                                 },
72                                 Stdout: &bytes.Buffer{},
73                                 Stderr: &bytes.Buffer{},
74                         }},
75                         []string{
76                                 "4: sync remote: packages: detected debian",
77                                 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
78                                 `5: sync remote: packages: command stdout:
79 install ok installed    golang
80 install ok installed    golang-1.16
81 install ok installed    golang-1.16-doc
82 install ok installed    golang-1.16-go
83 install ok installed    golang-1.16-src
84 hold ok installed       package-one
85 install ok installed    package-two
86 `,
87                                 "4: sync remote: packages: checking package-one package-two",
88                         },
89                         safcm.MsgSyncResp{},
90                         nil,
91                 },
92
93                 {
94                         "packages not yet installed",
95                         safcm.MsgSyncReq{
96                                 Packages: []string{
97                                         "package-one",
98                                         "package-two",
99                                         "package-three",
100                                 },
101                         },
102                         [][]byte{
103                                 []byte(`install ok installed    golang
104 install ok installed    golang-1.16
105 install ok installed    golang-1.16-doc
106 install ok installed    golang-1.16-go
107 install ok installed    golang-1.16-src
108 install ok installed    package-two
109 `),
110                                 []byte("fake stdout/stderr"),
111                         },
112                         [][]byte{nil, nil},
113                         []error{nil, nil},
114                         []*exec.Cmd{&exec.Cmd{
115                                 Path: "/usr/bin/dpkg-query",
116                                 Args: []string{
117                                         "/usr/bin/dpkg-query",
118                                         "--show",
119                                         `--showformat=${Status}\t${Package}\n`,
120                                 },
121                                 Stdout: &bytes.Buffer{},
122                                 Stderr: &bytes.Buffer{},
123                         }, &exec.Cmd{
124                                 Path: "/usr/bin/apt-get",
125                                 Args: []string{
126                                         "/usr/bin/apt-get",
127                                         "install",
128                                         "--assume-yes",
129                                         "--no-upgrade",
130                                         "--no-install-recommends",
131                                         "-o", "Dpkg::Options::=--force-confdef",
132                                         "-o", "Dpkg::Options::=--force-confold",
133                                         "package-one",
134                                         "package-three",
135                                 },
136                                 Env: append(os.Environ(),
137                                         "DEBIAN_FRONTEND=noninteractive",
138                                 ),
139                         }},
140                         []string{
141                                 "4: sync remote: packages: detected debian",
142                                 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
143                                 `5: sync remote: packages: command stdout:
144 install ok installed    golang
145 install ok installed    golang-1.16
146 install ok installed    golang-1.16-doc
147 install ok installed    golang-1.16-go
148 install ok installed    golang-1.16-src
149 install ok installed    package-two
150 `,
151                                 "4: sync remote: packages: checking package-one package-two package-three",
152                                 "3: sync remote: packages: installing package-one package-three",
153                                 `4: sync remote: packages: running "/usr/bin/apt-get" "install" "--assume-yes" "--no-upgrade" "--no-install-recommends" "-o" "Dpkg::Options::=--force-confdef" "-o" "Dpkg::Options::=--force-confold" "package-one" "package-three"`,
154                                 "5: sync remote: packages: command output:\nfake stdout/stderr",
155                         },
156                         safcm.MsgSyncResp{
157                                 PackageChanges: []safcm.PackageChange{
158                                         {
159                                                 Name: "package-one",
160                                         },
161                                         {
162                                                 Name: "package-three",
163                                         },
164                                 },
165                         },
166                         nil,
167                 },
168
169                 {
170                         "packages not yet installed (error)",
171                         safcm.MsgSyncReq{
172                                 Packages: []string{
173                                         "package-one",
174                                         "package-two",
175                                 },
176                         },
177                         [][]byte{
178                                 []byte(`install ok installed    golang
179 install ok installed    golang-1.16
180 install ok installed    golang-1.16-doc
181 install ok installed    golang-1.16-go
182 install ok installed    golang-1.16-src
183 `),
184                                 []byte("fake stdout/stderr"),
185                         },
186                         [][]byte{nil, nil},
187                         []error{
188                                 nil,
189                                 fmt.Errorf("fake error"),
190                         },
191                         []*exec.Cmd{&exec.Cmd{
192                                 Path: "/usr/bin/dpkg-query",
193                                 Args: []string{
194                                         "/usr/bin/dpkg-query",
195                                         "--show",
196                                         `--showformat=${Status}\t${Package}\n`,
197                                 },
198                                 Stdout: &bytes.Buffer{},
199                                 Stderr: &bytes.Buffer{},
200                         }, &exec.Cmd{
201                                 Path: "/usr/bin/apt-get",
202                                 Args: []string{
203                                         "/usr/bin/apt-get",
204                                         "install",
205                                         "--assume-yes",
206                                         "--no-upgrade",
207                                         "--no-install-recommends",
208                                         "-o", "Dpkg::Options::=--force-confdef",
209                                         "-o", "Dpkg::Options::=--force-confold",
210                                         "package-one",
211                                         "package-two",
212                                 },
213                                 Env: append(os.Environ(),
214                                         "DEBIAN_FRONTEND=noninteractive",
215                                 ),
216                         }},
217                         []string{
218                                 "4: sync remote: packages: detected debian",
219                                 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
220                                 `5: sync remote: packages: command stdout:
221 install ok installed    golang
222 install ok installed    golang-1.16
223 install ok installed    golang-1.16-doc
224 install ok installed    golang-1.16-go
225 install ok installed    golang-1.16-src
226 `,
227                                 "4: sync remote: packages: checking package-one package-two",
228                                 "3: sync remote: packages: installing package-one package-two",
229                                 `4: sync remote: packages: running "/usr/bin/apt-get" "install" "--assume-yes" "--no-upgrade" "--no-install-recommends" "-o" "Dpkg::Options::=--force-confdef" "-o" "Dpkg::Options::=--force-confold" "package-one" "package-two"`,
230                                 "5: sync remote: packages: command output:\nfake stdout/stderr",
231                         },
232                         safcm.MsgSyncResp{
233                                 PackageChanges: []safcm.PackageChange{
234                                         {
235                                                 Name: "package-one",
236                                         },
237                                         {
238                                                 Name: "package-two",
239                                         },
240                                 },
241                         },
242                         fmt.Errorf(`"/usr/bin/apt-get" "install" "--assume-yes" "--no-upgrade" "--no-install-recommends" "-o" "Dpkg::Options::=--force-confdef" "-o" "Dpkg::Options::=--force-confold" "package-one" "package-two" failed: fake error; output: "fake stdout/stderr"`),
243                 },
244
245                 {
246                         "packages not yet installed (dry-run)",
247                         safcm.MsgSyncReq{
248                                 DryRun: true,
249                                 Packages: []string{
250                                         "package-one",
251                                         "package-two",
252                                 },
253                         },
254                         [][]byte{
255                                 []byte(`install ok installed    golang
256 install ok installed    golang-1.16
257 install ok installed    golang-1.16-doc
258 install ok installed    golang-1.16-go
259 install ok installed    golang-1.16-src
260 `),
261                         },
262                         [][]byte{nil},
263                         []error{nil},
264                         []*exec.Cmd{&exec.Cmd{
265                                 Path: "/usr/bin/dpkg-query",
266                                 Args: []string{
267                                         "/usr/bin/dpkg-query",
268                                         "--show",
269                                         `--showformat=${Status}\t${Package}\n`,
270                                 },
271                                 Stdout: &bytes.Buffer{},
272                                 Stderr: &bytes.Buffer{},
273                         }},
274                         []string{
275                                 "4: sync remote: packages: detected debian",
276                                 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
277                                 `5: sync remote: packages: command stdout:
278 install ok installed    golang
279 install ok installed    golang-1.16
280 install ok installed    golang-1.16-doc
281 install ok installed    golang-1.16-go
282 install ok installed    golang-1.16-src
283 `,
284                                 "4: sync remote: packages: checking package-one package-two",
285                         },
286                         safcm.MsgSyncResp{
287                                 PackageChanges: []safcm.PackageChange{
288                                         {
289                                                 Name: "package-one",
290                                         },
291                                         {
292                                                 Name: "package-two",
293                                         },
294                                 },
295                         },
296                         nil,
297                 },
298         }
299
300         for _, tc := range tests {
301                 t.Run(tc.name, func(t *testing.T) {
302                         s, res := prepareSync(tc.req, &testRunner{
303                                 t:         t,
304                                 expCmds:   tc.expCmds,
305                                 resStdout: tc.stdout,
306                                 resStderr: tc.stderr,
307                                 resError:  tc.errors,
308                         })
309
310                         err := s.syncPackagesDebian()
311                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
312                         dbg := res.Wait()
313
314                         testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
315                         testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
316                 })
317         }
318 }