129 Commits

Author SHA1 Message Date
Maciej Borzecki
0434dc4575 timeutil: fix data race in unit tests
Exported object is running in a separate goroutine, accesses to fields need to
by synchronized.

```
==================
WARNING: DATA RACE
Read at 0x00c000536430 by goroutine 36:
  github.com/snapcore/snapd/timeutil_test.timedate1Api.Get()
      /home/maciek/work/canonical/snapd/timeutil/synchronized_test.go:87 +0x126
  runtime.call64()
      /usr/lib/go/src/runtime/asm_amd64.s:772 +0x42
  reflect.Value.Call()
      /usr/lib/go/src/reflect/value.go:380 +0xb5
  github.com/godbus/dbus.exportedMethod.Call()
      /home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/default_handler.go:128 +0x219
  github.com/godbus/dbus.(*exportedMethod).Call()
      <autogenerated>:1 +0x84
  github.com/godbus/dbus.(*Conn).handleCall()
      /home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/export.go:153 +0x591
  github.com/godbus/dbus.(*Conn).inWorker.gowrap1()
      /home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/conn.go:334 +0x44

Previous write at 0x00c000536430 by goroutine 30:
  github.com/snapcore/snapd/timeutil_test.(*syncedSuite).TestIsNTPSynchronized()
      /home/maciek/work/canonical/snapd/timeutil/synchronized_test.go:118 +0x298
  runtime.call16()
      /usr/lib/go/src/runtime/asm_amd64.s:770 +0x42
  reflect.Value.Call()
      /usr/lib/go/src/reflect/value.go:380 +0xb5
  gopkg.in/check%2ev1.(*suiteRunner).forkTest.func1()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:775 +0x9c5
  gopkg.in/check%2ev1.(*suiteRunner).forkCall.func1()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:669 +0xe9

Goroutine 36 (running) created at:
  github.com/godbus/dbus.(*Conn).inWorker()
      /home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/conn.go:334 +0x346
  github.com/godbus/dbus.(*Conn).Auth.gowrap1()
      /home/maciek/work/canonical/snapd/vendor/github.com/godbus/dbus/auth.go:118 +0x33

Goroutine 30 (running) created at:
  gopkg.in/check%2ev1.(*suiteRunner).forkCall()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:666 +0x5ba
  gopkg.in/check%2ev1.(*suiteRunner).forkTest()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:757 +0x155
  gopkg.in/check%2ev1.(*suiteRunner).runTest()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:812 +0x419
  gopkg.in/check%2ev1.(*suiteRunner).run()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/check.go:618 +0x3c6
  gopkg.in/check%2ev1.Run()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:92 +0x44
  gopkg.in/check%2ev1.RunAll()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:84 +0x124
  gopkg.in/check%2ev1.TestingT()
      /home/maciek/work/canonical/snapd/vendor/gopkg.in/check.v1/run.go:72 +0x5d3
  github.com/snapcore/snapd/timeutil_test.Test()
      /home/maciek/work/canonical/snapd/timeutil/schedule_test.go:33 +0x26
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1742 +0x44
==================
```

Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
2024-04-08 09:46:27 +02:00
Miguel Pires
ce612b151b timeutil: check error result in test
Adds an error assertion and replaces a deferred restore with
testutil.Backup (follow-up of #12859).

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-06-12 14:37:13 +01:00
Alex Murray
e32b0fe388 timeutil: fix tests to run in non-UTC timezones (#12859)
* timeutil: fix tests to run in non-UTC timezones

TestParseSchedule() has always failed for me - unless I set TZ=UTC before
running go test, since one of the tested time intervals encompasses a daylight
savings time transition here in my local timezone and so the result is different
in this case. Consequently, since this test always fails, building the snapd deb
or snap also fails for me locally. Fix this by forcing golang to use the UTC
timezone when running these tests, regardless of how they were invoked.

Signed-off-by: Alex Murray <alex.murray@canonical.com>
Co-authored-by: Miguel Pires <miguel.pires@canonical.com>
2023-06-07 14:38:43 +01:00
Miguel Pires
d097436c1c many: fix formatting w/ gofmt 1.19
Go 1.19 includes some changes to gofmt which intend to make lists and
heading clearer when rendered (https://go.dev/doc/go1.19). This commit
is the result of running the new gofmt and manually fixing some of it.
This was necessary because the new gofmt assumed lines beginning w/ tabs
to start lists or examples. While this is often true in our codebase,
we occasionally also use tabs to indent the lines after a TODO or FIXME
prefix or in yaml (e.g., excerpts of a snap.yaml). This meant that a lot of the
reformatted comments were broken and had to be fixed manually.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-16 14:23:11 +01:00
Miguel Pires
742f1fe219 many: correct struct fields and output key
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2022-12-12 12:37:20 +01:00
Miguel Pires
859f6ca2a0 cmd/snap: show user+gating hold info in 'snap info'
Display information the hold placed by the user and the longest hold
placed by a gating snap, in the output of 'snap info'. The times are
displayed in an imprecise but user-friendly way.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2022-12-12 12:37:20 +01:00
Michael Vogt
277eb06412 many: replace use of "sanity" with more inclusive naming
This commit replaces the use of "sanity" with more inclusive
naming.

When `sanity` is used in a more general sense either `validity`
or `quick` is used.
2022-03-14 18:22:34 +01:00
Michael Vogt
ec78fe20c8 timeutil: remove (debug) logging from IsNTPSynchronized
The logging in this generic helper is unneeded and excessive. It
can also be called a lot which spams the logger.
2021-12-09 19:32:44 +01:00
Michael Vogt
f2ccd2bd90 timeutil: return NoTimedate1Error if it can't connect to the system bus
When no system bus connection is possible, also return NoTimedate1Error.
This will fix a FTBFS inside the sbuild environment.
2021-11-05 16:52:47 +01:00
Michael Vogt
d550f860a2 many: wait for up to 10min for NTP syncronization before autorefresh
* many: wait for up to 10min for NTP syncronization before autorefresh

We got bugreports that on systems without a proper clock the
auto-refresh code gets confused and reports strange refresh times.

It looks like this is triggered by snapd starting up and while in
the middle of the startup the NTP sync happens and that confuses
the autorefresh code that tries to calculate the next refresh window
because the clock is jumping.

To avoid this happening this commit adds code that waits for up
to 10min for the NTP syncronization to happen. This does not
fully eliminate the bug (that we still don't fully understand)
but it should fix it for most systems that have some sort of
network connection.

* {device,snap}state: rework ntp sync detection

* devicestate: rename/move ntpSyncedOrWaitedLongerThan

* timeutil: add missing dbus based tests

* devicestate: add tests around CanAutoRefresh and ntpSyncedOrWaitedLongerThan

* devicestate: clarify comments around TestNTPSyncedOrWaitedLongerThan

* devicestate: use time.Minute (thanks to Alberto)

* devicestate: use time.Nanosecond explicitely (thanks to Alberto)

* devicestate,timeutil: add/use timeutil.NoTimedate1Error

* timeutil: adjust test for new behavior

* devicestate: use errors.As and make test more realistic by using NoTimedate1Error.Err

* timeutil: remove unused code (thanks to Maciej)

* devicestate: ensure m.ntpSynchronized is cached and this is tested

* timeutil: fix misleading comment

* timeutil: fix typo (thank to Ian)

* devicestate: rename ntpSynchronized->ntpSyncedOrTimedOut to better reflect what it's about
2021-11-04 12:53:36 +01:00
Miguel Pires
db9c63b734 many: remove unused code 2021-08-26 18:24:02 +01:00
Maciej Borzecki
73599ae861 timeutil: add a unit test case for trivial schedule
Add a trivial test case for completeness.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-02-05 13:35:35 +01:00
Samuele Pedroni
fbf2dfde24 many: start using randutil instead of directly math/rand
have a static check that math/rand is not used directly

drop some easter eggs that were math/rand based
2020-02-03 14:43:35 +01:00
Maciej Borzecki
dc473ef825 timeutil: fix schedule for same weekday spans, eg. mon1-mon
Earlier refactors allow for the actual date range check to be simplified.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-10-17 11:47:06 +02:00
Maciej Borzecki
42244332f6 timeutil: tweaks and comments
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-10-17 11:46:54 +02:00
Maciej Borzecki
f100430a7c timeutil: add and update comments on schedule helpers and internal structures
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-10-08 12:09:43 +02:00
Maciej Borzecki
b6e90c8bad timeutil: tweak calculation of the 1st day of the next month
Optimize the code to find out the 1st day of the next month.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-10-04 12:19:17 +02:00
Maciej Borzecki
36d0178cc5 timeutil: naming tweaks, typos, add test with zero time
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-10-04 12:19:07 +02:00
Maciej Borzecki
16bfe67605 timeutil: fix schedule ambiguity, add anchor points, make schedule a contigous 1 week span
As agreed, in order to resolve the ambiguity, each schedule is anchored at
either start (eg. mon1-fri), or at the end (mon-fri1). The anchor defines the
fixed time-point of the schedule, where the other end defines the span around
that point. Previously accepted schedules are not silently degraded to be
anchored at the starting point.

Having resolved the ambiguity, a week span including the last day of the month
can now be accepted and handled correctly, thus mon5-fri or mon-fri5 are valid schedules.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-09-30 13:16:54 +02:00
Maciej Borzecki
c82a8bbf5f timeutil: fix schedules with ambiguous nth weekday spans
The nth weekday is confusing on it's own. Adding week spans, the end result is
fuzzy at best.

Consider the following calendar:
        August 2018
   Su Mo Tu We Th Fr Sa
             1  2  3  4
    5  6  7  8  9 10 11
   12 13 14 15 16 17 18
   19 20 21 22 23 24 25
   26 27 28 29 30 31

The schedule `mon1-tue1` matches 06.08 and 07.08, both days being he first
occurrence within the month.

However, extending the schedule to `mon1-fri1`, this should match: 01.08-03.08
and 06.08-07.08 (5 days total), so that the weekdays are inside the week span
defined in the schedule. If we let go of this property, and just match anything
between 03.08 and 06.08, the update could happen over the weekend, which is
clearly not intended.

Similarly, a schedule `mon2-fri2`, matches dates: 13.08-14.08 and 08.08-10.08 (5
days total).

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-09-11 08:10:36 +02:00
Maciej Borzecki
85661c386d timeutil: fix first weekday of the month schedule
Given a schedule with first weekday of the month (eg. wed1 - first Wednesday),
if a matching weekday happens on the first day of the month, it will be skipped
and instead the schedule will fall on the next same weekday of the month.

Consider the calendar:
    August 2018
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

In case the schedule is on wed1, we are expecting the next window on 2018.08.01,
but instead the next window is scheduled for 2018.08.08.

The patch fixes buggy nth-weekday-of-the-month calculation for this edge case.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2018-07-30 10:51:24 +02:00
John R. Lenton
d1afe66a8b timeutil: in Human, count days with fingers
don't try to be smart.
2018-03-23 17:52:44 +00:00
Samuele Pedroni
3d7f2b5647 many: support holding refreshes by setting refresh.hold
This also introduces setting seed-time, to have an anchor point if using refresh.hold before the first refresh.
2018-03-12 18:00:06 +01:00
John R. Lenton
d67cc12794 cmd/snap, timeutil: fix issues found in review.
Update --abs-time description (30->60 days), rename 'cutoff' to 'cutoffDays'.
2018-03-09 00:23:27 +01:00
John R. Lenton
3db515cd43 cmd/snap, timeutil: cutoff is 60 days, arg is --abs-time
After feedback from @niemeyer (thanks!).
2018-03-08 11:04:43 +01:00