Skip to content

Commit b0f2d52

Browse files
committed
minor test case polishing
1 parent d3d5eb8 commit b0f2d52

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

daemon/httpd/handler/file_upload.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ func (upload *HandleFileUpload) Handle(w http.ResponseWriter, r *http.Request) {
125125
// Generate a temporary file that preserves extension name of the original
126126
tmpFileName := hex.EncodeToString(randName) + filepath.Ext(fileHeader.Filename)
127127
if err := os.MkdirAll(fileUploadStorage, 0700); err != nil {
128-
http.Error(w, `failed to store file`, http.StatusInternalServerError)
128+
http.Error(w, fmt.Sprintf("failed to store file: %v", err), http.StatusInternalServerError)
129129
return
130130
}
131131
tmpFile, err := os.OpenFile(filepath.Join(fileUploadStorage, tmpFileName), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
132132
if err != nil {
133-
http.Error(w, `failed to store file`, http.StatusInternalServerError)
133+
http.Error(w, fmt.Sprintf("failed to store file: %v", err), http.StatusInternalServerError)
134134
return
135135
}
136136
defer tmpFile.Close()

daemon/httpd/httpd.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/HouzuoGuo/laitos/testingstub"
2929
"github.com/HouzuoGuo/laitos/toolbox"
3030
"github.com/prometheus/client_golang/prometheus"
31+
"github.com/stretchr/testify/assert"
3132
)
3233

3334
const (
@@ -401,9 +402,8 @@ func TestAPIHandlers(httpd *Daemon, t testingstub.T) {
401402
ContentType: fileUploadRequestWriter.FormDataContentType(),
402403
Body: fileUploadRequestBody,
403404
}, addr+"/upload")
404-
if err != nil || resp.StatusCode != http.StatusOK {
405-
t.Fatal(err, resp, string(resp.Body))
406-
}
405+
assert.NoError(t, err)
406+
assert.Equal(t, http.StatusOK, resp.StatusCode)
407407
// File upload - download
408408
var downloadFileName string
409409
for _, line := range strings.Split(string(resp.Body), "\n") {

daemon/maintenance/maintenance.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ type Daemon struct {
9191
IntervalSec determines the rate of execution of maintenance routine. This is not a sleep duration. The constant
9292
rate of execution is maintained by taking away routine's elapsed time from actual interval between runs.
9393
*/
94-
IntervalSec int `json:"IntervalSec"`
95-
MailClient inet.MailClient `json:"MailClient"` // Send notification mails via this mailer
96-
Recipients []string `json:"Recipients"` // Address of recipients of notification mails
97-
FeaturesToTest *toolbox.FeatureSet `json:"-"` // FeaturesToTest are toolbox features to be tested during health check.
98-
MailCmdRunnerToTest *mailcmd.CommandRunner `json:"-"` // MailCmdRunnerToTest is mail command runner to be tested during health check.
99-
HTTPHandlersToCheck httpd.HandlerCollection `json:"-"` // HTTPHandlersToCheck are the URL handlers of an HTTP daemon to be tested during health check.
94+
IntervalSec int `json:"IntervalSec"`
95+
MailClient inet.MailClient `json:"MailClient"` // Send notification mails via this mailer
96+
Recipients []string `json:"Recipients"` // Address of recipients of notification mails
97+
ToolboxSelfTest *toolbox.FeatureSet `json:"-"` // FeaturesToTest are toolbox features to be tested during health check.
98+
MailCommandRunnerSelfTest *mailcmd.CommandRunner `json:"-"` // MailCmdRunnerToTest is mail command runner to be tested during health check.
99+
HttpHandlersSelfTest httpd.HandlerCollection `json:"-"` // HTTPHandlersToCheck are the URL handlers of an HTTP daemon to be tested during health check.
100100

101101
// UploadReportToS3Bucket is the name of S3 bucket into which the maintenance daemon shall upload its summary reports.
102102
UploadReportToS3Bucket string `json:"UploadReportToS3Bucket"`
@@ -161,22 +161,22 @@ func (daemon *Daemon) Execute(ctx context.Context) (string, bool) {
161161
}()
162162
go func() {
163163
// Toolbox feature self test - the routine itself also uses concurrency internally
164-
if daemon.FeaturesToTest != nil {
165-
featureErr = daemon.FeaturesToTest.SelfTest()
164+
if daemon.ToolboxSelfTest != nil {
165+
featureErr = daemon.ToolboxSelfTest.SelfTest()
166166
}
167167
waitAllChecks.Done()
168168
}()
169169
go func() {
170170
// Mail command runner test - the routine itself also uses concurrency internally
171-
if daemon.MailCmdRunnerToTest != nil && daemon.MailCmdRunnerToTest.ReplyMailClient.IsConfigured() {
172-
mailCmdRunnerErr = daemon.MailCmdRunnerToTest.SelfTest()
171+
if daemon.MailCommandRunnerSelfTest != nil && daemon.MailCommandRunnerSelfTest.ReplyMailClient.IsConfigured() {
172+
mailCmdRunnerErr = daemon.MailCommandRunnerSelfTest.SelfTest()
173173
}
174174
waitAllChecks.Done()
175175
}()
176176
go func() {
177177
// HTTP special handler test - the routine itself also uses concurrency internally
178-
if daemon.HTTPHandlersToCheck != nil {
179-
httpHandlersErr = daemon.HTTPHandlersToCheck.SelfTest()
178+
if daemon.HttpHandlersSelfTest != nil {
179+
httpHandlersErr = daemon.HttpHandlersSelfTest.SelfTest()
180180
}
181181
waitAllChecks.Done()
182182
}()
@@ -440,7 +440,7 @@ func TestMaintenance(check *Daemon, t testingstub.T) {
440440
t.Fatal("did not run pre script")
441441
}
442442
// Break a feature
443-
check.FeaturesToTest.LookupByTrigger[".s"] = &toolbox.Shell{}
443+
check.ToolboxSelfTest.LookupByTrigger[".s"] = &toolbox.Shell{}
444444
if result, ok := check.Execute(context.Background()); ok || !strings.Contains(result, "Shell.SelfTest") { // broken shell configuration
445445
t.Fatal(result)
446446
}
@@ -450,7 +450,7 @@ func TestMaintenance(check *Daemon, t testingstub.T) {
450450
} else if !strings.Contains(string(content), "Shell.SelfTest") { // broken shell configuration
451451
t.Fatal(string(content))
452452
}
453-
check.FeaturesToTest.LookupByTrigger[".s"] = &toolbox.Shell{
453+
check.ToolboxSelfTest.LookupByTrigger[".s"] = &toolbox.Shell{
454454
Unrestricted: true,
455455
InterpreterPath: "/bin/bash",
456456
}

daemon/maintenance/maintenance_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010
func TestMaintenance_Execute(t *testing.T) {
1111
features := toolbox.GetTestCommandProcessor().Features
1212
maint := Daemon{
13-
BlockSystemLoginExcept: []string{"root", "howard"},
14-
EnableStartServices: []string{"does-not-exist"},
15-
DisableStopServices: []string{"does-not-exist"},
16-
InstallPackages: []string{"htop"},
17-
SetTimeZone: "UTC",
18-
TuneLinux: true,
19-
DoEnhanceFileSecurity: true,
20-
SwapFileSizeMB: 100,
21-
FeaturesToTest: features,
22-
ScriptForUnix: "touch /tmp/laitos-maintenance-pre-script-test",
23-
MailCmdRunnerToTest: nil, // deliberately nil because it is not involved in this test
24-
HTTPHandlersToCheck: nil, // deliberately nil because it is not involved in this test
13+
BlockSystemLoginExcept: []string{"root", "howard"},
14+
EnableStartServices: []string{"does-not-exist"},
15+
DisableStopServices: []string{"does-not-exist"},
16+
InstallPackages: []string{"htop"},
17+
SetTimeZone: "UTC",
18+
TuneLinux: true,
19+
DoEnhanceFileSecurity: true,
20+
SwapFileSizeMB: 100,
21+
ToolboxSelfTest: features,
22+
ScriptForUnix: "touch /tmp/laitos-maintenance-pre-script-test",
23+
MailCommandRunnerSelfTest: nil, // deliberately nil because it is not involved in this test
24+
HttpHandlersSelfTest: nil, // deliberately nil because it is not involved in this test
2525
}
2626

2727
// Test default settings

launcher/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ func (config *Config) GetSimpleIPSvcD() *simpleipsvcd.Daemon {
389389
// GetMaintenance constructs a system maintenance / health check daemon from configuration and return.
390390
func (config *Config) GetMaintenance() *maintenance.Daemon {
391391
config.maintenanceInit.Do(func() {
392-
config.Maintenance.FeaturesToTest = config.Features
392+
config.Maintenance.ToolboxSelfTest = config.Features
393393
config.Maintenance.MailClient = config.MailClient
394-
config.Maintenance.MailCmdRunnerToTest = config.GetMailCommandRunner()
395-
config.Maintenance.HTTPHandlersToCheck = config.GetHTTPD().HandlerCollection
394+
config.Maintenance.MailCommandRunnerSelfTest = config.GetMailCommandRunner()
395+
config.Maintenance.HttpHandlersSelfTest = config.GetHTTPD().HandlerCollection
396396
if err := config.Maintenance.Initialise(); err != nil {
397397
config.logger.Abort("", err, "the daemon failed to initialise")
398398
return

0 commit comments

Comments
 (0)