Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrappers, tests/main/snap-service-timer: restore missing commit, add spread test for timer services #4758

Merged
10 changes: 10 additions & 0 deletions tests/lib/snaps/test-snapd-timer-service/bin/loop
@@ -0,0 +1,10 @@
#!/bin/sh

echo "called on $(date) as $1"

for _ in $(seq 10); do
echo "looping"
sleep 0.5
done

echo "finishing on $(date)"
13 changes: 13 additions & 0 deletions tests/lib/snaps/test-snapd-timer-service/meta/snap.yaml
@@ -0,0 +1,13 @@
name: test-snapd-timer-service
version: 1.0
apps:
regular-timer:
command: bin/loop regular-timer
daemon: simple
# Run every 15 minutes
timer: 0:00-24:00/96
random-timer:
command: bin/loop random-timer
daemon: simple
# Run roughly every 15 minutes, with (some) random distribution
timer: 0:00~24:00/96
51 changes: 51 additions & 0 deletions tests/main/snap-service-timer/task.yaml
@@ -0,0 +1,51 @@
summary: Check that snap timer services work

execute: |
echo "When the service snap is installed"
. $TESTSLIB/snaps.sh
install_local test-snapd-timer-service

echo "We can see the timers being active"
for service in regular-timer random-timer; do
systemctl show -p ActiveState snap.test-snapd-timer-service.$service.timer | MATCH "ActiveState=active"
systemctl show -p SubState snap.test-snapd-timer-service.$service.timer | MATCH "SubState=waiting"
systemctl show -p Triggers snap.test-snapd-timer-service.$service.timer | \
MATCH "snap.test-snapd-timer-service.$service.service"
systemctl show -p UnitFileState snap.test-snapd-timer-service.$service.timer | MATCH "UnitFileState=enabled"
done
# systemctl list-timers output:
# NEXT LEFT LAST PASSED UNIT ACTIVATES
# Fri 2018-02-23 11:00:00 CET 3min 25s left Fri 2018-02-23 10:45:36 CET 10min ago snap.timer-service-snap.regular-timer.timer snap.timer-service-snap.regular-timer.service
# Fri 2018-02-23 11:01:00 CET 4min 25s left Fri 2018-02-23 10:51:36 CET 4min 58s ago snap.timer-service-snap.random-timer.timer snap.timer-service-snap.random-timer.service
echo "When disabled, times are not listed by systemd"
snap disable test-snapd-timer-service
if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
for service in regular-timer random-timer; do
systemctl show -p UnitFileState snap.test-snapd-timer-service.$service.timer | MATCH "UnitFileState="
done
else
! systemctl list-timers | MATCH "test-snapd-timer-service"
fi

echo "When reenabled, the timers are present again"
snap enable test-snapd-timer-service
if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
for service in regular-timer random-timer; do
systemctl show -p UnitFileState snap.test-snapd-timer-service.$service.timer | MATCH "UnitFileState=enabled"
done
else
systemctl list-timers | MATCH "test-snapd-timer-service"
fi

echo "When removed, times are not listed by systemd"
snap remove test-snapd-timer-service
if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
for service in regular-timer random-timer; do
systemctl show -p UnitFileState snap.test-snapd-timer-service.$service.timer | MATCH "UnitFileState="
done
else
! systemctl list-timers | MATCH "test-snapd-timer-service"
fi

echo "No timer files are left behind"
test $(find /etc/systemd/system -name 'snap.test-snapd-timer-service.*.timer' | wc -l) -eq "0"
5 changes: 2 additions & 3 deletions wrappers/services.go
Expand Up @@ -247,7 +247,7 @@ func AddSnapServices(s *snap.Info, inter interacter) (err error) {
enabled = append(enabled, svcName)
}

if len(enabled) > 0 {
if len(written) > 0 {
if err := sysd.DaemonReload(); err != nil {
return err
}
Expand Down Expand Up @@ -560,8 +560,7 @@ X-Snappy=yes
[Timer]
Unit={{.ServiceFileName}}
{{ range .Schedules }}OnCalendar={{ . }}
{{- end }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we used to generate a broken service file that needs to be updated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The piece of code that did this got merged just today. On top of this, the snap won't install as systemd will reject the timer file we produced.


{{ end }}
[Install]
WantedBy={{.TimersTarget}}
`
Expand Down
3 changes: 2 additions & 1 deletion wrappers/services_gen_test.go
Expand Up @@ -335,6 +335,7 @@ X-Snappy=yes
[Timer]
Unit=snap.snap.app.service
OnCalendar=*-*-* 10:00
OnCalendar=*-*-* 11:00

[Install]
WantedBy=timers.target
Expand All @@ -352,7 +353,7 @@ WantedBy=timers.target
Daemon: "simple",
StopTimeout: timeout.DefaultTimeout,
Timer: &snap.TimerInfo{
Timer: "10:00-12:00",
Timer: "10:00-12:00/2",
},
}
service.Timer.App = service
Expand Down
54 changes: 54 additions & 0 deletions wrappers/services_test.go
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/systemd"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/wrappers"
Expand Down Expand Up @@ -835,3 +836,56 @@ func (s *servicesTestSuite) TestFailedAddSnapCleansUp(c *C) {
c.Assert(err, IsNil)
c.Assert(matches, HasLen, 0, Commentf("the following autogenerated files were left behind: %v", matches))
}

func (s *servicesTestSuite) TestAddServicesDidReload(c *C) {
const base = `name: hello-snap
version: 1.10
summary: hello
description: Hello...
apps:
`
onlyServices := snaptest.MockSnap(c, base+`
svc1:
command: bin/hello
daemon: simple
`, &snap.SideInfo{Revision: snap.R(12)})

onlySockets := snaptest.MockSnap(c, base+`
svc1:
command: bin/hello
daemon: simple
plugs: [network-bind]
sockets:
sock1:
listen-stream: $SNAP_COMMON/sock1.socket
socket-mode: 0666
`, &snap.SideInfo{Revision: snap.R(12)})

onlyTimers := snaptest.MockSnap(c, base+`
svc1:
command: bin/hello
daemon: oneshot
timer: 10:00-12:00
`, &snap.SideInfo{Revision: snap.R(12)})

var sysdLog [][]string
r := systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
sysdLog = append(sysdLog, cmd)
return []byte("ActiveState=inactive\n"), nil
})
defer r()

for i, info := range []*snap.Info{onlyServices, onlySockets, onlyTimers} {
sysdLog = [][]string{}
err := wrappers.AddSnapServices(info, &progress.Null)
c.Assert(err, IsNil)
reloads := 0
c.Logf("calls: %v", sysdLog)
for _, call := range sysdLog {
if strutil.ListContains(call, "daemon-reload") {
reloads += 1
}
}
c.Check(reloads >= 1, Equals, true, Commentf("test-case %v did not reload services as expected", i))
}
}