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
65 []*exec.Cmd{&exec.Cmd{
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: sync remote: packages: detected debian",
77 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
78 `5: sync remote: 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
87 "4: sync remote: packages: checking package-one package-two",
94 "packages not yet installed",
103 []byte(`install ok installed golang
104 install ok installed golang-1.16
105 install ok installed golang-1.16-doc
106 install ok installed golang-1.16-go
107 install ok installed golang-1.16-src
108 install ok installed package-two
110 []byte("fake stdout/stderr"),
114 []*exec.Cmd{&exec.Cmd{
115 Path: "/usr/bin/dpkg-query",
117 "/usr/bin/dpkg-query",
119 `--showformat=${Status}\t${Package}\n`,
121 Stdout: &bytes.Buffer{},
122 Stderr: &bytes.Buffer{},
124 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: sync remote: packages: detected debian",
142 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
143 `5: sync remote: 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 "4: sync remote: packages: checking package-one package-two package-three",
152 "3: sync remote: packages: installing package-one package-three",
153 `4: sync remote: 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"`,
154 "5: sync remote: packages: command output:\nfake stdout/stderr",
157 PackageChanges: []safcm.PackageChange{
162 Name: "package-three",
170 "packages not yet installed (error)",
178 []byte(`install ok installed golang
179 install ok installed golang-1.16
180 install ok installed golang-1.16-doc
181 install ok installed golang-1.16-go
182 install ok installed golang-1.16-src
184 []byte("fake stdout/stderr"),
189 fmt.Errorf("fake error"),
191 []*exec.Cmd{&exec.Cmd{
192 Path: "/usr/bin/dpkg-query",
194 "/usr/bin/dpkg-query",
196 `--showformat=${Status}\t${Package}\n`,
198 Stdout: &bytes.Buffer{},
199 Stderr: &bytes.Buffer{},
201 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: sync remote: packages: detected debian",
219 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
220 `5: sync remote: 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 "4: sync remote: packages: checking package-one package-two",
228 "3: sync remote: packages: installing package-one package-two",
229 `4: sync remote: 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"`,
230 "5: sync remote: packages: command output:\nfake stdout/stderr",
233 PackageChanges: []safcm.PackageChange{
242 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"`),
246 "packages not yet installed (dry-run)",
255 []byte(`install ok installed golang
256 install ok installed golang-1.16
257 install ok installed golang-1.16-doc
258 install ok installed golang-1.16-go
259 install ok installed golang-1.16-src
264 []*exec.Cmd{&exec.Cmd{
265 Path: "/usr/bin/dpkg-query",
267 "/usr/bin/dpkg-query",
269 `--showformat=${Status}\t${Package}\n`,
271 Stdout: &bytes.Buffer{},
272 Stderr: &bytes.Buffer{},
275 "4: sync remote: packages: detected debian",
276 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
277 `5: sync remote: packages: command stdout:
278 install ok installed golang
279 install ok installed golang-1.16
280 install ok installed golang-1.16-doc
281 install ok installed golang-1.16-go
282 install ok installed golang-1.16-src
284 "4: sync remote: packages: checking package-one package-two",
287 PackageChanges: []safcm.PackageChange{
300 for _, tc := range tests {
301 t.Run(tc.name, func(t *testing.T) {
302 s, res := prepareSync(tc.req, &testRunner{
305 resStdout: tc.stdout,
306 resStderr: tc.stderr,
310 err := s.syncPackagesDebian()
311 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
314 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
315 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)