]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/packages_debian_test.go
ci/run: run golangci-lint
[safcm/safcm.git] / 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{{
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-upgrade",
129                                         "--no-install-recommends",
130                                         "-o", "Dpkg::Options::=--force-confdef",
131                                         "-o", "Dpkg::Options::=--force-confold",
132                                         "package-one",
133                                         "package-three",
134                                 },
135                                 Env: append(os.Environ(),
136                                         "DEBIAN_FRONTEND=noninteractive",
137                                 ),
138                         }},
139                         []string{
140                                 "4: packages: checking package-one package-two package-three (debian detected)",
141                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
142                                 `5: packages: command stdout:
143 install ok installed    golang
144 install ok installed    golang-1.16
145 install ok installed    golang-1.16-doc
146 install ok installed    golang-1.16-go
147 install ok installed    golang-1.16-src
148 install ok installed    package-two
149 `,
150                                 "3: packages: installing package-one package-three",
151                                 `4: 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"`,
152                                 "5: packages: command output:\nfake stdout/stderr",
153                         },
154                         safcm.MsgSyncResp{
155                                 PackageChanges: []safcm.PackageChange{
156                                         {
157                                                 Name: "package-one",
158                                         },
159                                         {
160                                                 Name: "package-three",
161                                         },
162                                 },
163                         },
164                         nil,
165                 },
166
167                 {
168                         "packages not yet installed (error)",
169                         safcm.MsgSyncReq{
170                                 Packages: []string{
171                                         "package-one",
172                                         "package-two",
173                                 },
174                         },
175                         [][]byte{
176                                 []byte(`install ok installed    golang
177 install ok installed    golang-1.16
178 install ok installed    golang-1.16-doc
179 install ok installed    golang-1.16-go
180 install ok installed    golang-1.16-src
181 `),
182                                 []byte("fake stdout/stderr"),
183                         },
184                         [][]byte{nil, nil},
185                         []error{
186                                 nil,
187                                 fmt.Errorf("fake error"),
188                         },
189                         []*exec.Cmd{{
190                                 Path: "/usr/bin/dpkg-query",
191                                 Args: []string{
192                                         "/usr/bin/dpkg-query",
193                                         "--show",
194                                         `--showformat=${Status}\t${Package}\n`,
195                                 },
196                                 Stdout: &bytes.Buffer{},
197                                 Stderr: &bytes.Buffer{},
198                         }, {
199                                 Path: "/usr/bin/apt-get",
200                                 Args: []string{
201                                         "/usr/bin/apt-get",
202                                         "install",
203                                         "--assume-yes",
204                                         "--no-upgrade",
205                                         "--no-install-recommends",
206                                         "-o", "Dpkg::Options::=--force-confdef",
207                                         "-o", "Dpkg::Options::=--force-confold",
208                                         "package-one",
209                                         "package-two",
210                                 },
211                                 Env: append(os.Environ(),
212                                         "DEBIAN_FRONTEND=noninteractive",
213                                 ),
214                         }},
215                         []string{
216                                 "4: packages: checking package-one package-two (debian detected)",
217                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
218                                 `5: packages: command stdout:
219 install ok installed    golang
220 install ok installed    golang-1.16
221 install ok installed    golang-1.16-doc
222 install ok installed    golang-1.16-go
223 install ok installed    golang-1.16-src
224 `,
225                                 "3: packages: installing package-one package-two",
226                                 `4: 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"`,
227                                 "5: packages: command output:\nfake stdout/stderr",
228                         },
229                         safcm.MsgSyncResp{
230                                 PackageChanges: []safcm.PackageChange{
231                                         {
232                                                 Name: "package-one",
233                                         },
234                                         {
235                                                 Name: "package-two",
236                                         },
237                                 },
238                         },
239                         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"`),
240                 },
241
242                 {
243                         "packages not yet installed (dry-run)",
244                         safcm.MsgSyncReq{
245                                 DryRun: true,
246                                 Packages: []string{
247                                         "package-one",
248                                         "package-two",
249                                 },
250                         },
251                         [][]byte{
252                                 []byte(`install ok installed    golang
253 install ok installed    golang-1.16
254 install ok installed    golang-1.16-doc
255 install ok installed    golang-1.16-go
256 install ok installed    golang-1.16-src
257 `),
258                         },
259                         [][]byte{nil},
260                         []error{nil},
261                         []*exec.Cmd{{
262                                 Path: "/usr/bin/dpkg-query",
263                                 Args: []string{
264                                         "/usr/bin/dpkg-query",
265                                         "--show",
266                                         `--showformat=${Status}\t${Package}\n`,
267                                 },
268                                 Stdout: &bytes.Buffer{},
269                                 Stderr: &bytes.Buffer{},
270                         }},
271                         []string{
272                                 "4: packages: checking package-one package-two (debian detected)",
273                                 `4: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
274                                 `5: packages: command stdout:
275 install ok installed    golang
276 install ok installed    golang-1.16
277 install ok installed    golang-1.16-doc
278 install ok installed    golang-1.16-go
279 install ok installed    golang-1.16-src
280 `,
281                         },
282                         safcm.MsgSyncResp{
283                                 PackageChanges: []safcm.PackageChange{
284                                         {
285                                                 Name: "package-one",
286                                         },
287                                         {
288                                                 Name: "package-two",
289                                         },
290                                 },
291                         },
292                         nil,
293                 },
294         }
295
296         for _, tc := range tests {
297                 t.Run(tc.name, func(t *testing.T) {
298                         s, res := prepareSync(tc.req, &testRunner{
299                                 t:         t,
300                                 expCmds:   tc.expCmds,
301                                 resStdout: tc.stdout,
302                                 resStderr: tc.stderr,
303                                 resError:  tc.errors,
304                         })
305
306                         err := s.syncPackagesDebian()
307                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
308                         dbg := res.Wait()
309
310                         testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
311                         testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
312                 })
313         }
314 }