Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Oct 31, 2024
1 parent cfda309 commit abfa863
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sonic_service_client/dbus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *DbusClient) ConfigReloadForce(config string, caller string) error {
modName := "config"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".reload"
intName := c.intNamePrefix + modName + ".reload_force"
_, err := DbusApi(busName, busPath, intName, 240, config, caller)
return err
}
Expand Down
67 changes: 22 additions & 45 deletions sonic_service_client/dbus_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,6 @@ func TestConfigReloadNegative(t *testing.T) {
}
}

func TestConfigReloadTimeout(t *testing.T) {
mock1 := gomonkey.ApplyFunc(dbus.SystemBus, func() (conn *dbus.Conn, err error) {
return &dbus.Conn{}, nil
})
defer mock1.Reset()
mock2 := gomonkey.ApplyMethod(reflect.TypeOf(&dbus.Object{}), "Go", func(obj *dbus.Object, method string, flags dbus.Flags, ch chan *dbus.Call, args ...interface{}) *dbus.Call {
if method != "org.SONiC.HostService.config.reload" {
t.Errorf("Wrong method: %v", method)
}
return &dbus.Call{}
})
defer mock2.Reset()

client, err := NewDbusClient()
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
err = client.ConfigReload("abc", "")
if err == nil {
t.Errorf("ConfigReload should timeout: %v", err)
}
}

func TestConfigReloadForce(t *testing.T) {
mock1 := gomonkey.ApplyFunc(dbus.SystemBus, func() (conn *dbus.Conn, err error) {
return &dbus.Conn{}, nil
Expand Down Expand Up @@ -244,15 +221,20 @@ func TestConfigReloadForceNegative(t *testing.T) {
}
}

func TestConfigReloadForceTimeout(t *testing.T) {
func TestConfigSave(t *testing.T) {
mock1 := gomonkey.ApplyFunc(dbus.SystemBus, func() (conn *dbus.Conn, err error) {
return &dbus.Conn{}, nil
})
defer mock1.Reset()
mock2 := gomonkey.ApplyMethod(reflect.TypeOf(&dbus.Object{}), "Go", func(obj *dbus.Object, method string, flags dbus.Flags, ch chan *dbus.Call, args ...interface{}) *dbus.Call {
if method != "org.SONiC.HostService.config.reload_force" {
if method != "org.SONiC.HostService.config.save" {
t.Errorf("Wrong method: %v", method)
}
ret := &dbus.Call{}
ret.Err = nil
ret.Body = make([]interface{}, 2)
ret.Body[0] = int32(0)
ch <- ret
return &dbus.Call{}
})
defer mock2.Reset()
Expand All @@ -261,13 +243,14 @@ func TestConfigReloadForceTimeout(t *testing.T) {
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
err = client.ConfigReloadForce("abc", "")
if err == nil {
t.Errorf("ConfigReload should timeout: %v", err)
err = client.ConfigSave("abc")
if err != nil {
t.Errorf("ConfigSave should pass: %v", err)
}
}

func TestConfigSave(t *testing.T) {
func TestConfigSaveNegative(t *testing.T) {
err_msg := "This is the mock error message"
mock1 := gomonkey.ApplyFunc(dbus.SystemBus, func() (conn *dbus.Conn, err error) {
return &dbus.Conn{}, nil
})
Expand All @@ -279,7 +262,8 @@ func TestConfigSave(t *testing.T) {
ret := &dbus.Call{}
ret.Err = nil
ret.Body = make([]interface{}, 2)
ret.Body[0] = int32(0)
ret.Body[0] = int32(1)
ret.Body[1] = err_msg
ch <- ret
return &dbus.Call{}
})
Expand All @@ -290,13 +274,15 @@ func TestConfigSave(t *testing.T) {
t.Errorf("NewDbusClient failed: %v", err)
}
err = client.ConfigSave("abc")
if err != nil {
t.Errorf("ConfigSave should pass: %v", err)
if err == nil {
t.Errorf("ConfigSave should fail")
}
if err.Error() != err_msg {
t.Errorf("Wrong error: %v", err)
}
}

func TestConfigSaveNegative(t *testing.T) {
err_msg := "This is the mock error message"
func TestConfigSaveTimeout(t *testing.T) {
mock1 := gomonkey.ApplyFunc(dbus.SystemBus, func() (conn *dbus.Conn, err error) {
return &dbus.Conn{}, nil
})
Expand All @@ -305,12 +291,6 @@ func TestConfigSaveNegative(t *testing.T) {
if method != "org.SONiC.HostService.config.save" {
t.Errorf("Wrong method: %v", method)
}
ret := &dbus.Call{}
ret.Err = nil
ret.Body = make([]interface{}, 2)
ret.Body[0] = int32(1)
ret.Body[1] = err_msg
ch <- ret
return &dbus.Call{}
})
defer mock2.Reset()
Expand All @@ -319,12 +299,9 @@ func TestConfigSaveNegative(t *testing.T) {
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
err = client.ConfigSave("abc")
err = client.ConfigSave("")
if err == nil {
t.Errorf("ConfigSave should fail")
}
if err.Error() != err_msg {
t.Errorf("Wrong error: %v", err)
t.Errorf("ConfigSave should timeout: %v", err)
}
}

Expand Down

0 comments on commit abfa863

Please sign in to comment.