Skip to content

Commit 471c4d6

Browse files
refactor: Print error messages to console for cacct
* Add more e2e tests for cacct to test if error messages are properly shown
1 parent 7b447b7 commit 471c4d6

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ test-e2e: $(PROMTOOL) build pkg/collector/testdata/sys/.unpacked pkg/collector/t
192192
./scripts/e2e-test.sh -s cacct-custom-format
193193
./scripts/e2e-test.sh -s cacct-admin-user
194194
./scripts/e2e-test.sh -s cacct-forbid-query
195+
./scripts/e2e-test.sh -s cacct-invalid-config
195196
./scripts/e2e-test.sh -s cacct-tsdata
196197
./scripts/e2e-test.sh -s cacct-tsdata-fail
197198
else
@@ -278,6 +279,7 @@ test-e2e-update: build pkg/collector/testdata/sys/.unpacked pkg/collector/testda
278279
./scripts/e2e-test.sh -s cacct-custom-format -u || true
279280
./scripts/e2e-test.sh -s cacct-admin-user -u || true
280281
./scripts/e2e-test.sh -s cacct-forbid-query -u || true
282+
./scripts/e2e-test.sh -s cacct-invalid-config -u || true
281283
./scripts/e2e-test.sh -s cacct-tsdata -u || true
282284
./scripts/e2e-test.sh -s cacct-tsdata-fail -u || true
283285
else

cmd/cacct/main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ var (
223223
// Custom errors.
224224
var (
225225
errNoPerm = errors.New("forbidden response from API server")
226+
errConfig = errors.New("unable to get cacct config")
227+
errUser = errors.New("unable to change user context")
226228
errInternal = errors.New("internal server error")
227229
)
228230

@@ -491,7 +493,7 @@ func main() {
491493
// the config file can be read as the owner of this app
492494
config, err := readConfig()
493495
if err != nil {
494-
os.Exit(checkErr(fmt.Errorf("failed to read config file: %w", err)))
496+
os.Exit(checkErr(fmt.Errorf("%w: failed to read config file: %w", errConfig, err)))
495497
}
496498

497499
// Now time to drop privileges so that rest of app will be run as regular user
@@ -503,23 +505,23 @@ func main() {
503505
// Convert UID anf GID to int
504506
uid, err := strconv.Atoi(currentUser.Uid)
505507
if err != nil {
506-
os.Exit(checkErr(fmt.Errorf("failed to get current user uid: %w", err)))
508+
os.Exit(checkErr(fmt.Errorf("%w: failed to get current user uid: %w", errUser, err)))
507509
}
508510

509511
gid, err := strconv.Atoi(currentUser.Gid)
510512
if err != nil {
511-
os.Exit(checkErr(fmt.Errorf("failed to get current user gid: %w", err)))
513+
os.Exit(checkErr(fmt.Errorf("%w: failed to get current user gid: %w", errUser, err)))
512514
}
513515

514516
// Set UID and GID to current user
515517
err = syscall.Setuid(uid)
516518
if err != nil {
517-
os.Exit(checkErr(fmt.Errorf("failed to set current user uid: %w", err)))
519+
os.Exit(checkErr(fmt.Errorf("%w: failed to set current user uid: %w", errUser, err)))
518520
}
519521

520522
err = syscall.Setgid(gid)
521523
if err != nil {
522-
os.Exit(checkErr(fmt.Errorf("failed to set current user gid: %w", err)))
524+
os.Exit(checkErr(fmt.Errorf("%w: failed to set current user gid: %w", errUser, err)))
523525
}
524526
}
525527

@@ -799,9 +801,15 @@ func checkErr(err error) int {
799801
if err != nil {
800802
switch {
801803
case errors.Is(err, errNoPerm):
802-
fmt.Fprintln(os.Stderr, "forbidden. It is likely that the user is attempting to view statistics of others")
804+
fmt.Fprintln(os.Stderr, "error: forbidden. It is likely that the user is attempting to view statistics of others")
803805
case errors.Is(err, errInternal):
804-
fmt.Fprintln(os.Stderr, "server did not return any data due to unknown error")
806+
fmt.Fprintln(os.Stderr, "error: server did not return any data due to unknown error")
807+
case errors.Is(err, errConfig):
808+
fmt.Fprintln(os.Stderr, "error: "+errConfig.Error())
809+
case errors.Is(err, errUser):
810+
fmt.Fprintln(os.Stderr, "error: "+errUser.Error())
811+
default:
812+
fmt.Fprintln(os.Stderr, err.Error())
805813
}
806814

807815
return 1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
forbidden. It is likely that the user is attempting to view statistics of others
1+
error: forbidden. It is likely that the user is attempting to view statistics of others
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: unable to get cacct config

scripts/e2e-test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ then
397397
then
398398
desc="cacct by normal user attempt to admin query"
399399
fixture='cmd/cacct/testdata/output/e2e-test-cacct-forbid-query.txt'
400+
elif [ "${scenario}" = "cacct-invalid-config" ]
401+
then
402+
desc="cacct using invalid config"
403+
fixture='cmd/cacct/testdata/output/e2e-test-cacct-invalid-config.txt'
400404
elif [ "${scenario}" = "cacct-tsdata" ]
401405
then
402406
desc="cacct to dump time series data"
@@ -1505,6 +1509,9 @@ then
15051509
elif [ "${scenario}" = "cacct-forbid-query" ]
15061510
then
15071511
./bin/cacct --current-user=usr3 --config-path="cmd/cacct/testdata" --starttime="2022-02-20" --endtime="2022-03-20" --user=usr1,usr2 > "${fixture_output}" 2>&1 || true
1512+
elif [ "${scenario}" = "cacct-invalid-config" ]
1513+
then
1514+
./bin/cacct --current-user=usr1 --config-path="nonexistant/testdata" --starttime="2022-02-20" --endtime="2022-03-20" > "${fixture_output}" 2>&1 || true
15081515
elif [ "${scenario}" = "cacct-tsdata" ]
15091516
then
15101517
./bin/cacct --current-user=usr1 --config-path="cmd/cacct/testdata" --job="147973" --ts --ts.out-dir="${tmpdir}/ts" > "${fixture_output}" 2>&1

0 commit comments

Comments
 (0)