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/>.
26 "github.com/google/go-cmp/cmp"
28 "ruderich.org/simon/safcm"
31 func TestSyncPackagesDebian(t *testing.T) {
40 expResp safcm.MsgSyncResp
44 // NOTE: Also update MsgSyncResp in safcm test cases when
45 // changing anything here!
48 "packages already installed",
56 []byte(`install ok installed golang
57 install ok installed golang-1.16
58 install ok installed golang-1.16-doc
59 install ok installed golang-1.16-go
60 install ok installed golang-1.16-src
61 hold ok installed package-one
62 install ok installed package-two
67 []*exec.Cmd{&exec.Cmd{
68 Path: "/usr/bin/dpkg-query",
70 "/usr/bin/dpkg-query",
72 `--showformat=${Status}\t${Package}\n`,
74 Stdout: &bytes.Buffer{},
75 Stderr: &bytes.Buffer{},
78 "4: sync remote: packages: detected debian",
79 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
80 `5: sync remote: packages: command stdout:
81 install ok installed golang
82 install ok installed golang-1.16
83 install ok installed golang-1.16-doc
84 install ok installed golang-1.16-go
85 install ok installed golang-1.16-src
86 hold ok installed package-one
87 install ok installed package-two
89 "4: sync remote: packages: checking package-one package-two",
96 "packages not yet installed",
105 []byte(`install ok installed golang
106 install ok installed golang-1.16
107 install ok installed golang-1.16-doc
108 install ok installed golang-1.16-go
109 install ok installed golang-1.16-src
110 install ok installed package-two
112 []byte("fake stdout/stderr"),
116 []*exec.Cmd{&exec.Cmd{
117 Path: "/usr/bin/dpkg-query",
119 "/usr/bin/dpkg-query",
121 `--showformat=${Status}\t${Package}\n`,
123 Stdout: &bytes.Buffer{},
124 Stderr: &bytes.Buffer{},
126 Path: "/usr/bin/apt-get",
132 "--no-install-recommends",
133 "-o", "Dpkg::Options::=--force-confdef",
134 "-o", "Dpkg::Options::=--force-confold",
138 Env: append(os.Environ(),
139 "DEBIAN_FRONTEND=noninteractive",
143 "4: sync remote: packages: detected debian",
144 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
145 `5: sync remote: packages: command stdout:
146 install ok installed golang
147 install ok installed golang-1.16
148 install ok installed golang-1.16-doc
149 install ok installed golang-1.16-go
150 install ok installed golang-1.16-src
151 install ok installed package-two
153 "4: sync remote: packages: checking package-one package-two package-three",
154 "3: sync remote: packages: installing package-one package-three",
155 `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"`,
156 "5: sync remote: packages: command output:\nfake stdout/stderr",
159 PackageChanges: []safcm.PackageChange{
164 Name: "package-three",
172 "packages not yet installed (error)",
180 []byte(`install ok installed golang
181 install ok installed golang-1.16
182 install ok installed golang-1.16-doc
183 install ok installed golang-1.16-go
184 install ok installed golang-1.16-src
186 []byte("fake stdout/stderr"),
191 fmt.Errorf("fake error"),
193 []*exec.Cmd{&exec.Cmd{
194 Path: "/usr/bin/dpkg-query",
196 "/usr/bin/dpkg-query",
198 `--showformat=${Status}\t${Package}\n`,
200 Stdout: &bytes.Buffer{},
201 Stderr: &bytes.Buffer{},
203 Path: "/usr/bin/apt-get",
209 "--no-install-recommends",
210 "-o", "Dpkg::Options::=--force-confdef",
211 "-o", "Dpkg::Options::=--force-confold",
215 Env: append(os.Environ(),
216 "DEBIAN_FRONTEND=noninteractive",
220 "4: sync remote: packages: detected debian",
221 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
222 `5: sync remote: packages: command stdout:
223 install ok installed golang
224 install ok installed golang-1.16
225 install ok installed golang-1.16-doc
226 install ok installed golang-1.16-go
227 install ok installed golang-1.16-src
229 "4: sync remote: packages: checking package-one package-two",
230 "3: sync remote: packages: installing package-one package-two",
231 `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"`,
232 "5: sync remote: packages: command output:\nfake stdout/stderr",
235 PackageChanges: []safcm.PackageChange{
244 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"`),
248 "packages not yet installed (dry-run)",
257 []byte(`install ok installed golang
258 install ok installed golang-1.16
259 install ok installed golang-1.16-doc
260 install ok installed golang-1.16-go
261 install ok installed golang-1.16-src
266 []*exec.Cmd{&exec.Cmd{
267 Path: "/usr/bin/dpkg-query",
269 "/usr/bin/dpkg-query",
271 `--showformat=${Status}\t${Package}\n`,
273 Stdout: &bytes.Buffer{},
274 Stderr: &bytes.Buffer{},
277 "4: sync remote: packages: detected debian",
278 `4: sync remote: packages: running "/usr/bin/dpkg-query" "--show" "--showformat=${Status}\\t${Package}\\n"`,
279 `5: sync remote: packages: command stdout:
280 install ok installed golang
281 install ok installed golang-1.16
282 install ok installed golang-1.16-doc
283 install ok installed golang-1.16-go
284 install ok installed golang-1.16-src
286 "4: sync remote: packages: checking package-one package-two",
289 PackageChanges: []safcm.PackageChange{
302 for _, tc := range tests {
303 t.Run(tc.name, func(t *testing.T) {
304 s, res := prepareSync(tc.req, &testRunner{
307 resStdout: tc.stdout,
308 resStderr: tc.stderr,
312 err := s.syncPackagesDebian()
313 // Ugly but the simplest way to compare errors (including nil)
314 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
315 t.Errorf("err = %#v, want %#v",
320 if !reflect.DeepEqual(tc.expResp, s.resp) {
322 cmp.Diff(tc.expResp, s.resp))
324 if !reflect.DeepEqual(tc.expDbg, dbg) {
326 cmp.Diff(tc.expDbg, dbg))