1 // Copyright (C) 2021-2024 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",
130 "--no-install-recommends",
131 "-o", "Dpkg::Options::=--force-confdef",
132 "-o", "Dpkg::Options::=--force-confold",
136 Env: append(os.Environ(),
137 "DEBIAN_FRONTEND=noninteractive",
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
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",
156 PackageChanges: []safcm.PackageChange{
161 Name: "package-three",
169 "packages not yet installed (error)",
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
183 []byte("fake stdout/stderr"),
188 fmt.Errorf("fake error"),
191 Path: "/usr/bin/dpkg-query",
193 "/usr/bin/dpkg-query",
195 `--showformat=${Status}\t${Package}\n`,
197 Stdout: &bytes.Buffer{},
198 Stderr: &bytes.Buffer{},
200 Path: "/usr/bin/apt-get",
207 "--no-install-recommends",
208 "-o", "Dpkg::Options::=--force-confdef",
209 "-o", "Dpkg::Options::=--force-confold",
213 Env: append(os.Environ(),
214 "DEBIAN_FRONTEND=noninteractive",
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
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",
232 PackageChanges: []safcm.PackageChange{
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"`),
245 "packages not yet installed (dry-run)",
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
264 Path: "/usr/bin/dpkg-query",
266 "/usr/bin/dpkg-query",
268 `--showformat=${Status}\t${Package}\n`,
270 Stdout: &bytes.Buffer{},
271 Stderr: &bytes.Buffer{},
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
285 PackageChanges: []safcm.PackageChange{
298 for _, tc := range tests {
299 t.Run(tc.name, func(t *testing.T) {
300 s, res := prepareSync(tc.req, &testRunner{
303 resStdout: tc.stdout,
304 resStderr: tc.stderr,
308 err := s.syncPackagesDebian()
309 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
312 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
313 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)