Ai
1 Star 0 Fork 1

mysnapcore/mysnapd

forked from tupelo-shen/mysnapd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
synchronized_test.go 3.69 KB
一键复制 编辑 原始数据 按行查看 历史
tupelo-shen 提交于 2022-11-07 22:32 +08:00 . fix: timeutil commit
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 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 timeutil_test
import (
"errors"
"fmt"
"github.com/godbus/dbus"
. "gopkg.in/check.v1"
"gitee.com/mysnapcore/mysnapd/dbusutil"
"gitee.com/mysnapcore/mysnapd/testutil"
"gitee.com/mysnapcore/mysnapd/timeutil"
)
const (
timedate1BusName = "org.freedesktop.timedate1"
timedate1ObjectPath = "/org/freedesktop/timedate1"
)
type mockTimedate1 struct {
conn *dbus.Conn
NTPSynchronized bool
getPropertyCalled []string
}
func newMockTimedate1() (*mockTimedate1, error) {
conn, err := dbusutil.SessionBusPrivate()
if err != nil {
return nil, err
}
server := &mockTimedate1{
conn: conn,
}
reply, err := conn.RequestName(timedate1BusName, dbus.NameFlagDoNotQueue)
if err != nil {
conn.Close()
return nil, err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
conn.Close()
return nil, fmt.Errorf("cannot obtain bus name %q", timedate1BusName)
}
return server, nil
}
func (server *mockTimedate1) Export() {
server.conn.Export(timedate1Api{server}, timedate1ObjectPath, "org.freedesktop.DBus.Properties")
}
func (server *mockTimedate1) Stop() error {
if _, err := server.conn.ReleaseName(timedate1BusName); err != nil {
return err
}
return server.conn.Close()
}
type timedate1Api struct {
server *mockTimedate1
}
func (a timedate1Api) Get(iff, prop string) (dbus.Variant, *dbus.Error) {
a.server.getPropertyCalled = append(a.server.getPropertyCalled, fmt.Sprintf("if=%s;prop=%s", iff, prop))
return dbus.MakeVariant(a.server.NTPSynchronized), nil
}
type syncedSuite struct {
testutil.BaseTest
testutil.DBusTest
}
var _ = Suite(&syncedSuite{})
func (s *syncedSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.DBusTest.SetUpTest(c)
restore := dbusutil.MockOnlySystemBusAvailable(s.SessionBus)
s.AddCleanup(restore)
}
func (s *syncedSuite) TearDownTest(c *C) {
s.DBusTest.TearDownTest(c)
s.BaseTest.TearDownTest(c)
}
func (s *syncedSuite) TestIsNTPSynchronized(c *C) {
backend, err := newMockTimedate1()
c.Assert(err, IsNil)
defer backend.Stop()
backend.Export()
for _, v := range []bool{true, false} {
backend.getPropertyCalled = nil
backend.NTPSynchronized = v
synced, err := timeutil.IsNTPSynchronized()
c.Assert(err, IsNil)
c.Check(synced, Equals, v)
c.Check(backend.getPropertyCalled, DeepEquals, []string{
"if=org.freedesktop.timedate1;prop=NTPSynchronized",
})
}
}
func (s *syncedSuite) TestIsNTPSynchronizedStrangeEr(c *C) {
backend, err := newMockTimedate1()
c.Assert(err, IsNil)
defer backend.Stop()
// Note that we did not export anything here - this error is a bit
// artificial
_, err = timeutil.IsNTPSynchronized()
c.Check(err, ErrorMatches, `cannot check for ntp sync: Object does not implement the interface`)
}
func (s *syncedSuite) TestIsNTPSynchronizedNoTimedatectlNoErr(c *C) {
// note that there is no mock timedate1 created so we are on an empty bus
synced, err := timeutil.IsNTPSynchronized()
c.Check(err, ErrorMatches, `cannot find org.freedesktop.timedate1 dbus service: .*`)
c.Check(errors.As(err, &timeutil.NoTimedate1Error{}), Equals, true)
c.Check(synced, Equals, false)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.1.0

搜索帮助