mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2026-12-05 06:34:30 +09:00
Pull request: AGDNS-3826-add-tests
Squashed commit of the following: commit 47c8508c711432d35e6abd38a67282bd4800973e Merge: e811803976344ecce8Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Apr 7 12:52:06 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-add-tests commit e81180397631de4f39005c76f8b962526c86bd86 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Apr 7 11:53:48 2026 +0700 ossvc: imp tests commit b146c6f382b136d5ba4c26472c637f3e2bd2a8fd Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Apr 6 10:33:49 2026 +0700 ossvc: imp tests commit d6489c228f8cf7b631298fc687b61e03cdd8f955 Merge: fc30010871d491ad0fAuthor: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Apr 6 09:19:04 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-add-tests # Conflicts: # internal/ossvc/defaultmanager_internal_test.go commit fc300108745f898fadf04494845ae53ca2840c09 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Apr 3 11:58:47 2026 +0700 ossvc: add tests uninstall commit c81bef3e0b40008352228d272004be04dce05916 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Apr 3 11:44:16 2026 +0700 ossvc: add tests stop commit 6618971bdcb544c10b09cc20949b9e93725f6426 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Apr 3 11:29:52 2026 +0700 ossvc: add tests restart commit 9777d78fd99233c35f504a45f59acd086bee7a92 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Apr 3 11:20:29 2026 +0700 ossvc: add tests install commit 59e53b9e5fe5f0ed342c4c4581f847613e3268d5 Merge: b965a7dcda3866c68eAuthor: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 10:11:29 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-use-executil-run commit b965a7dcdbbed2058ec0f05800a2c5925a8b2766 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 10:08:26 2026 +0700 scripts: imp code commit ea8efa085bd9fd5bacd6b899c052f0197f2ea9af Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 08:50:17 2026 +0700 ossvc: imp code commit 2ba0269f9cd13298dd1aaa0e6d9dd9e0c9879df2 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Apr 1 08:32:10 2026 +0700 ossvc: imp tests commit 5141a0a86ae17173a502d4a65315917bc36143b0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 12:33:45 2026 +0700 ossvc: imp tests commit 3ec53df0c200cb59ef0d7841b21aeab9f39bf8ad Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 10:53:23 2026 +0700 ossvc: imp tests commit 3030f92f7f8b745e387bb42e6d9af3f0fd8fbb6b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:19:16 2026 +0700 ossvc: imp code commit 4644f3fe20e0f711da91adf98faf4a88dad932a2 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:18:54 2026 +0700 all: imp code commit 282ee76141dabcaf37df555db41db2c2cc82472d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:08:54 2026 +0700 osvc: imp tests commit 8e4634570ac71ce4f16fe3796cc64d774c7dc257 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Mar 30 16:14:15 2026 +0700 osvc: imp tests commit 3abade03bed5d45e900c2e579c65eb5cc66990a7 Merge: 56ae2eb4f55d0d6ae5Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Mar 30 11:03:35 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-use-executil-run commit 56ae2eb4f8caea427afb87f46e90f21ae8d9fa39 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Mar 27 10:43:07 2026 +0700 aghnet: imp logs commit 7ead0907d39fab66602ab1d94ec17a477338b371 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:47:23 2026 +0700 all: imp code commit afa235d8acfeb8b4286a26b60dddd61545533761 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:38:30 2026 +0700 all: rm aghos.RunCommand commit bd36b286f26b068e145b5ffe2d2d23fb7c5525fa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:38:21 2026 +0700 ossvc: rm aghos.RunCommand commit bba1e37aa4a34f557cec9653abc2e90816e39054 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:13:05 2026 +0700 ossvc: pass cmd cons commit 6cc18f7801d669beb2188f79692a4a590fce2885 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 11:38:21 2026 +0700 scripts: rm aghos.RunCommand ... and 1 more commit
This commit is contained in:
1 parent
6344ecce8a
commit
9f7f8663ff
1 file changed
+296
-4
@@ -38,11 +38,11 @@ func TestManager_Status(t *testing.T) {
|
|||||||
wantErrMessage: "",
|
wantErrMessage: "",
|
||||||
}, {
|
}, {
|
||||||
onStatus: func() (s service.Status, err error) {
|
onStatus: func() (s service.Status, err error) {
|
||||||
return service.StatusRunning, errors.Error("test")
|
return service.StatusRunning, assert.AnError
|
||||||
},
|
},
|
||||||
name: "error",
|
name: "error",
|
||||||
wantStatus: "",
|
wantStatus: "",
|
||||||
wantErrMessage: "getting service status: test",
|
wantErrMessage: "getting service status: " + assert.AnError.Error(),
|
||||||
}, {
|
}, {
|
||||||
onStatus: func() (s service.Status, err error) {
|
onStatus: func() (s service.Status, err error) {
|
||||||
return service.StatusRunning, nil
|
return service.StatusRunning, nil
|
||||||
@@ -92,7 +92,7 @@ func TestManager_Status(t *testing.T) {
|
|||||||
func TestManager_Status_unixSystemV(t *testing.T) {
|
func TestManager_Status_unixSystemV(t *testing.T) {
|
||||||
svc := newTestSvc(t)
|
svc := newTestSvc(t)
|
||||||
svc.OnStatus = func() (s service.Status, err error) {
|
svc.OnStatus = func() (s service.Status, err error) {
|
||||||
return service.StatusUnknown, errors.Error("test")
|
return service.StatusUnknown, assert.AnError
|
||||||
}
|
}
|
||||||
|
|
||||||
m := &manager{
|
m := &manager{
|
||||||
@@ -111,7 +111,7 @@ func TestManager_Status_unixSystemV(t *testing.T) {
|
|||||||
name: "running",
|
name: "running",
|
||||||
wantStatus: StatusRunning,
|
wantStatus: StatusRunning,
|
||||||
}, {
|
}, {
|
||||||
cmdCons: newTestCmdConstructor(t, "", errors.Error("test")),
|
cmdCons: newTestCmdConstructor(t, "", assert.AnError),
|
||||||
name: "stopped",
|
name: "stopped",
|
||||||
wantStatus: StatusStopped,
|
wantStatus: StatusStopped,
|
||||||
}}
|
}}
|
||||||
@@ -129,6 +129,298 @@ func TestManager_Status_unixSystemV(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManager_Install(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: false,
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
installErr error
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
}{{
|
||||||
|
installErr: nil,
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
}, {
|
||||||
|
installErr: assert.AnError,
|
||||||
|
name: "error",
|
||||||
|
wantErrMsg: "installing service: " + assert.AnError.Error(),
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionInstall{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
svc.OnInstall = func() (err error) {
|
||||||
|
return tc.installErr
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_Install_unixSystemV(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
svc.OnInstall = func() (err error) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: aghos.IsOpenWrt(),
|
||||||
|
isUnixSystemV: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
cmdCons executil.CommandConstructor
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
}{{
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", nil),
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", assert.AnError),
|
||||||
|
name: "error",
|
||||||
|
wantErrMsg: "",
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionInstall{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
m.cmdCons = tc.cmdCons
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_Restart(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: false,
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
cmdCons executil.CommandConstructor
|
||||||
|
restartErr error
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
isUnixSystemV bool
|
||||||
|
}{{
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
restartErr: nil,
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}, {
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
restartErr: assert.AnError,
|
||||||
|
name: "error",
|
||||||
|
wantErrMsg: assert.AnError.Error(),
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", nil),
|
||||||
|
restartErr: assert.AnError,
|
||||||
|
name: "unix_systemv_restart",
|
||||||
|
wantErrMsg: assert.AnError.Error(),
|
||||||
|
isUnixSystemV: true,
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", errors.Error("initd_test")),
|
||||||
|
restartErr: assert.AnError,
|
||||||
|
name: "unix_systemv_restart_error",
|
||||||
|
wantErrMsg: assert.AnError.Error() + ` (restarting via init.d: starting: initd_test;` +
|
||||||
|
` stderr peek: ""; stdout peek: "")`,
|
||||||
|
isUnixSystemV: true,
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionRestart{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
m.cmdCons = tc.cmdCons
|
||||||
|
m.isUnixSystemV = tc.isUnixSystemV
|
||||||
|
|
||||||
|
svc.OnRestart = func() (err error) {
|
||||||
|
return tc.restartErr
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_Stop(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: false,
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
cmdCons executil.CommandConstructor
|
||||||
|
stopErr error
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
isUnixSystemV bool
|
||||||
|
}{{
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
stopErr: nil,
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}, {
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
stopErr: assert.AnError,
|
||||||
|
name: "error",
|
||||||
|
wantErrMsg: assert.AnError.Error(),
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", nil),
|
||||||
|
stopErr: assert.AnError,
|
||||||
|
name: "unix_systemv_stop",
|
||||||
|
wantErrMsg: assert.AnError.Error(),
|
||||||
|
isUnixSystemV: true,
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", errors.Error("initd_test")),
|
||||||
|
stopErr: assert.AnError,
|
||||||
|
name: "unix_systemv_stop_error",
|
||||||
|
wantErrMsg: assert.AnError.Error() + ` (stopping via init.d: starting: initd_test;` +
|
||||||
|
` stderr peek: ""; stdout peek: "")`,
|
||||||
|
isUnixSystemV: true,
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionStop{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
m.cmdCons = tc.cmdCons
|
||||||
|
m.isUnixSystemV = tc.isUnixSystemV
|
||||||
|
|
||||||
|
svc.OnStop = func() (err error) {
|
||||||
|
return tc.stopErr
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_Uninstall(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: false,
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
stopErr error
|
||||||
|
uninstallErr error
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
}{{
|
||||||
|
stopErr: nil,
|
||||||
|
uninstallErr: nil,
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
}, {
|
||||||
|
stopErr: errors.Error("stop_test"),
|
||||||
|
uninstallErr: assert.AnError,
|
||||||
|
name: "error_stop",
|
||||||
|
wantErrMsg: "uninstalling service: " + assert.AnError.Error(),
|
||||||
|
}, {
|
||||||
|
stopErr: nil,
|
||||||
|
uninstallErr: assert.AnError,
|
||||||
|
name: "error_uninstall",
|
||||||
|
wantErrMsg: "uninstalling service: " + assert.AnError.Error(),
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionUninstall{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
svc.OnStop = func() (err error) {
|
||||||
|
return tc.stopErr
|
||||||
|
}
|
||||||
|
svc.OnUninstall = func() (err error) {
|
||||||
|
return tc.uninstallErr
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_Uninstall_openWrt(t *testing.T) {
|
||||||
|
svc := newTestSvc(t)
|
||||||
|
svc.OnStop = func() (err error) { return nil }
|
||||||
|
svc.OnUninstall = func() (err error) { return nil }
|
||||||
|
|
||||||
|
m := &manager{
|
||||||
|
logger: testLogger,
|
||||||
|
cmdCons: executil.EmptyCommandConstructor{},
|
||||||
|
isOpenWrt: true,
|
||||||
|
isUnixSystemV: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
cmdCons executil.CommandConstructor
|
||||||
|
name string
|
||||||
|
wantErrMsg string
|
||||||
|
}{{
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", nil),
|
||||||
|
name: "success",
|
||||||
|
wantErrMsg: "",
|
||||||
|
}, {
|
||||||
|
cmdCons: newTestCmdConstructor(t, "", assert.AnError),
|
||||||
|
name: "error_disable",
|
||||||
|
wantErrMsg: "disabling service on openwrt: starting: " +
|
||||||
|
assert.AnError.Error() +
|
||||||
|
`; stderr peek: ""; stdout peek: ""`,
|
||||||
|
}}
|
||||||
|
|
||||||
|
action := &ActionUninstall{ServiceName: testServiceName}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
m.cmdCons = tc.cmdCons
|
||||||
|
|
||||||
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
||||||
|
err := m.Perform(ctx, action)
|
||||||
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// newTestSvc creates a new *testService with all methods set to panic, sets it
|
// newTestSvc creates a new *testService with all methods set to panic, sets it
|
||||||
// in the chosen system, and returns it. The caller can then set the desired
|
// in the chosen system, and returns it. The caller can then set the desired
|
||||||
// behavior of the service via overriding its methods.
|
// behavior of the service via overriding its methods.
|
||||||
|
|||||||
Reference in new issue
Block a user