GSoC week 7-8 : Different dates issue during testing

The JavaScript’s Date() creates a date object based on the browser’s time. During testing in cockpit, this will be the date of the server machine that runs the PhantomJS even if we have set the test machine’s time inside it to be a future date by m.execute(“timedatectl set-time ‘2020–04–10 04:00:00’”).

What test does?

The test checks for the Next Run time shown in the timers page after creating a timer.

Expected behaviour: After setting up timers in a future date say 2020–04–10 04:00:00 and then changing the system clock to that future date 2020–04–10 03:00:00 , we should see “Today at 04:00 AM” as the Next Run.

This works correctly if we check this through our computer. But during tests, the time the browser has (machine running PhantomJS) and the time test machine has are different.

Possible approaches to fixing this:

  1. Using moment instead of Date
  2. Comparison of test results with systemctl list-timers

Using moment instead of Date

This approach uses moment at places where new Date() is called. Moment.js latest version has new feature that lets moment use alternate clock sources. So now we could change the current time to a future time within the js code and test it.

moment.now = function() { return Date.now() * 2; };
moment().toISOString() // "2061-10-13T22:06:22.934Z"

But this has a problem, the new feature sets up only time not time-zones. This means even after setting up the system in a future date by using b.eval_js() (to inject moment’s new time from tests), time differs due to different timezones. There is a way to set default timezone but that would require momentjs-timezone library as well. Having a new library just for tests is not idea.

Comparison of test results with systemctl list-timers

Timers created here are now compared with systemctl list-timers command. Since systemctl list-timers is run on the test machine, there won’t be any time difference (after setting up a fixed timezone inside test machine).

m.execute("LC_ALL=C timedatectl set-timezone UTC")
m.execute(“timedatectl set-time ‘2020–04–10 04:10:00’”)
# checks if timer runs on this hour at 26 min
self.assertIn(“Fri 2020–04–10 04:26”, m.execute(“LC_ALL=C systemctl list-timers”))

Next run time display in timers page

This approach doesn’t check the text shown in the Next Run time after creating timers instead it checks whether the timer was set up correctly through systemctl list-timers.

This approach also does not require momentjs update.