1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
25 "ruderich.org/simon/safcm"
26 "ruderich.org/simon/safcm/testutil"
29 func TestSyncPackagesDebian(t *testing.T) {
38 expResp safcm.MsgSyncResp
42 // NOTE: Also update MsgSyncResp in safcm test cases when
43 // changing the MsgSyncResp struct!
46 "packages already installed",
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
66 Path: "/usr/bin/dpkg-query",
68 "/usr/bin/dpkg-query",
70 `--showformat=${Status}\t${Package}\n`,
72 Stdout: &bytes.Buffer{},
73 Stderr: &bytes.Buffer{},
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
93 "packages not yet installed",
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
109 []byte("fake stdout/stderr"),
114 Path: "/usr/bin/dpkg-query",
116 "/usr/bin/dpkg-query",
118 `--showformat=${Status}\t${Package}\n`,
120 Stdout: &bytes.Buffer{},
121 Stderr: &bytes.Buffer{},
123 Path: "/usr/bin/apt-get",
129 "--no-install-recommends",
130 "-o", "Dpkg::Options::=--force-confdef",
131 "-o", "Dpkg::Options::=--force-confold",
135 Env: append(os.Environ(),
136 "DEBIAN_FRONTEND=noninteractive",
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
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",
155 PackageChanges: []safcm.PackageChange{
160 Name: "package-three",
168 "packages not yet installed (error)",
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
182 []byte("fake stdout/stderr"),
187 fmt.Errorf("fake error"),
190 Path: "/usr/bin/dpkg-query",
192 "/usr/bin/dpkg-query",
194 `--showformat=${Status}\t${Package}\n`,
196 Stdout: &bytes.Buffer{},
197 Stderr: &bytes.Buffer{},
199 Path: "/usr/bin/apt-get",
205 "--no-install-recommends",
206 "-o", "Dpkg::Options::=--force-confdef",
207 "-o", "Dpkg::Options::=--force-confold",
211 Env: append(os.Environ(),
212 "DEBIAN_FRONTEND=noninteractive",
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
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",
230 PackageChanges: []safcm.PackageChange{
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"`),
243 "packages not yet installed (dry-run)",
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
262 Path: "/usr/bin/dpkg-query",
264 "/usr/bin/dpkg-query",
266 `--showformat=${Status}\t${Package}\n`,
268 Stdout: &bytes.Buffer{},
269 Stderr: &bytes.Buffer{},
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
283 PackageChanges: []safcm.PackageChange{
296 for _, tc := range tests {
297 t.Run(tc.name, func(t *testing.T) {
298 s, res := prepareSync(tc.req, &testRunner{
301 resStdout: tc.stdout,
302 resStderr: tc.stderr,
306 err := s.syncPackagesDebian()
307 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
310 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
311 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)