]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/packages_debian_test.go
8f38548eb7284ac1afc2d68c67eeba3ed974fccc
[safcm/safcm.git] / remote / sync / packages_debian_test.go
1 // Copyright (C) 2021-2024  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{{
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: packages: checking package-one package-two (debian detected)",
77                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
78                                 `5: 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                         },
88                         safcm.MsgSyncResp{},
89                         nil,
90                 },
91
92                 {
93                         "packages not yet installed",
94                         safcm.MsgSyncReq{
95                                 Packages: []string{
96                                         "package-one",
97                                         "package-two",
98                                         "package-three",
99                                 },
100                         },
101                         [][]byte{
102                                 []byte(`install ok installed    golang
103 install ok installed    golang-1.16
104 install ok installed    golang-1.16-doc
105 install ok installed    golang-1.16-go
106 install ok installed    golang-1.16-src
107 install ok installed    package-two
108 `),
109                                 []byte("fake stdout/stderr"),
110                         },
111                         [][]byte{nil, nil},
112                         []error{nil, nil},
113                         []*exec.Cmd{{
114                                 Path: "/usr/bin/dpkg-query",
115                                 Args: []string{
116                                         "/usr/bin/dpkg-query",
117                                         "--show",
118                                         `--showformat=${Status}\t${Package}\n`,
119                                 },
120                                 Stdout: &bytes.Buffer{},
121                                 Stderr: &bytes.Buffer{},
122                         }, {
123                                 Path: "/usr/bin/apt-get",
124                                 Args: []string{
125                                         "/usr/bin/apt-get",
126                                         "install",
127                                         "--assume-yes",
128                                         "--no-remove",
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: packages: checking package-one package-two package-three (debian detected)",
142                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
143                                 `5: 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                                 "3: packages: installing package-one package-three",
152                                 `4: packages: running "/usr/bin/apt-get" "install" "--assume-yes" "--no-remove" "--no-upgrade" "--no-install-recommends" "-o" "Dpkg::Options::=--force-confdef" "-o" "Dpkg::Options::=--force-confold" "package-one" "package-three"`,
153                                 "5: packages: command output:\nfake stdout/stderr",
154                         },
155                         safcm.MsgSyncResp{
156                                 PackageChanges: []safcm.PackageChange{
157                                         {
158                                                 Name: "package-one",
159                                         },
160                                         {
161                                                 Name: "package-three",
162                                         },
163                                 },
164                         },
165                         nil,
166                 },
167
168                 {
169                         "packages not yet installed (error)",
170                         safcm.MsgSyncReq{
171                                 Packages: []string{
172                                         "package-one",
173                                         "package-two",
174                                 },
175                         },
176                         [][]byte{
177                                 []byte(`install ok installed    golang
178 install ok installed    golang-1.16
179 install ok installed    golang-1.16-doc
180 install ok installed    golang-1.16-go
181 install ok installed    golang-1.16-src
182 `),
183                                 []byte("fake stdout/stderr"),
184                         },
185                         [][]byte{nil, nil},
186                         []error{
187                                 nil,
188                                 fmt.Errorf("fake error"),
189                         },
190                         []*exec.Cmd{{
191                                 Path: "/usr/bin/dpkg-query",
192                                 Args: []string{
193                                         "/usr/bin/dpkg-query",
194                                         "--show",
195                                         `--showformat=${Status}\t${Package}\n`,
196                                 },
197                                 Stdout: &bytes.Buffer{},
198                                 Stderr: &bytes.Buffer{},
199                         }, {
200                                 Path: "/usr/bin/apt-get",
201                                 Args: []string{
202                                         "/usr/bin/apt-get",
203                                         "install",
204                                         "--assume-yes",
205                                         "--no-remove",
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: packages: checking package-one package-two (debian detected)",
219                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
220                                 `5: 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                                 "3: packages: installing package-one package-two",
228                                 `4: packages: running "/usr/bin/apt-get" "install" "--assume-yes" "--no-remove" "--no-upgrade" "--no-install-recommends" "-o" "Dpkg::Options::=--force-confdef" "-o" "Dpkg::Options::=--force-confold" "package-one" "package-two"`,
229                                 "5: packages: command output:\nfake stdout/stderr",
230                         },
231                         safcm.MsgSyncResp{
232                                 PackageChanges: []safcm.PackageChange{
233                                         {
234                                                 Name: "package-one",
235                                         },
236                                         {
237                                                 Name: "package-two",
238                                         },
239                                 },
240                         },
241                         fmt.Errorf(`"/usr/bin/apt-get" "install" "--assume-yes" "--no-remove" "--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"`),
242                 },
243
244                 {
245                         "packages not yet installed (dry-run)",
246                         safcm.MsgSyncReq{
247                                 DryRun: true,
248                                 Packages: []string{
249                                         "package-one",
250                                         "package-two",
251                                 },
252                         },
253                         [][]byte{
254                                 []byte(`install ok installed    golang
255 install ok installed    golang-1.16
256 install ok installed    golang-1.16-doc
257 install ok installed    golang-1.16-go
258 install ok installed    golang-1.16-src
259 `),
260                         },
261                         [][]byte{nil},
262                         []error{nil},
263                         []*exec.Cmd{{
264                                 Path: "/usr/bin/dpkg-query",
265                                 Args: []string{
266                                         "/usr/bin/dpkg-query",
267                                         "--show",
268                                         `--showformat=${Status}\t${Package}\n`,
269                                 },
270                                 Stdout: &bytes.Buffer{},
271                                 Stderr: &bytes.Buffer{},
272                         }},
273                         []string{
274                                 "4: packages: checking package-one package-two (debian detected)",
275                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
276                                 `5: packages: command stdout:
277 install ok installed    golang
278 install ok installed    golang-1.16
279 install ok installed    golang-1.16-doc
280 install ok installed    golang-1.16-go
281 install ok installed    golang-1.16-src
282 `,
283                         },
284                         safcm.MsgSyncResp{
285                                 PackageChanges: []safcm.PackageChange{
286                                         {
287                                                 Name: "package-one",
288                                         },
289                                         {
290                                                 Name: "package-two",
291                                         },
292                                 },
293                         },
294                         nil,
295                 },
296         }
297
298         for _, tc := range tests {
299                 t.Run(tc.name, func(t *testing.T) {
300                         s, res := prepareSync(tc.req, &testRunner{
301                                 t:         t,
302                                 expCmds:   tc.expCmds,
303                                 resStdout: tc.stdout,
304                                 resStderr: tc.stderr,
305                                 resError:  tc.errors,
306                         })
307
308                         err := s.syncPackagesDebian()
309                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
310                         dbg := res.Wait()
311
312                         testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
313                         testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
314                 })
315         }
316 }