Files
snapd/cmd/snap-exec/export_test.go
Michael Vogt 0275484761 snap-exec: fix detection if cups interface is connected
* snap-exec: fix detection if `cups` interface is connected

The cups interface needs a way to set the environment variable
`CUPS_SERVER` if the `cups` interface is connected. However we
have no mechanism for setting environment vars based on interface
connections currently. To workaround this, the cups work added
a workaround in `snap-exec` that tried to detect if `cups` is
connected and when it is set the environment. Unfortunately the
code in there was too simplistic because it just checked if
the directory `/var/cups` exists. However ths dir now always
exists because it's needed as the mount point.

This commit fixes the detection by checking if `/var/cups` is
a bind mount. This is checked by looking at the `stat()` data
and the `dev_t` field in there. If they differ it means the
bind mount exists and the only thing that creates this bind
mount is the `cups` `MountConnectedPlug()` code.

This is not great but it fixes the spread failure we see
in the `cups-control` test (which is a real bug) and should
be good enough until we have a proper interface backend that
can set environment variables.

* tests: re-enable interfaces-cups-control test

For unclear reasons the error message on unplug changes when I
run this on my 20.04 system so I updated the tests - it most likely
because the test-snapd-cups-control-consumer snap moved from
core16 to core20 a week ago but that is not 100% confirmed.

* snap-exec: fix unit test for CUPS_SERVER workaround
2022-04-04 13:17:50 +02:00

73 lines
1.6 KiB
Go

// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"syscall"
"github.com/snapcore/snapd/testutil"
)
var (
ExpandEnvCmdArgs = expandEnvCmdArgs
FindCommand = findCommand
ParseArgs = parseArgs
Run = run
ExecApp = execApp
ExecHook = execHook
)
func MockSyscallExec(f func(argv0 string, argv []string, envv []string) (err error)) func() {
origSyscallExec := syscallExec
syscallExec = f
return func() {
syscallExec = origSyscallExec
}
}
func SetOptsCommand(s string) {
opts.Command = s
}
func GetOptsCommand() string {
return opts.Command
}
func SetOptsHook(s string) {
opts.Hook = s
}
func GetOptsHook() string {
return opts.Hook
}
// MockOsReadlink is for use in tests
func MockOsReadlink(f func(string) (string, error)) func() {
realOsReadlink := osReadlink
osReadlink = f
return func() {
osReadlink = realOsReadlink
}
}
func MockSyscallStat(f func(string, *syscall.Stat_t) (err error)) func() {
r := testutil.Backup(&syscallStat)
syscallStat = f
return r
}