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