mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-10-31 09:26:52 +00:00 
			
		
		
		
	runtime: delete useless src/runtime/cli/exit.go
simply use os.Exit() replace exit() delete useless ci/go-no-os-exit.sh; Fixes: #2295 Signed-off-by: Binbin Zhang <binbin36520@gmail.com>
This commit is contained in:
		| @@ -1,30 +0,0 @@ | ||||
| #!/bin/bash | ||||
| # Copyright (c) 2018 Intel Corporation | ||||
| # | ||||
| # SPDX-License-Identifier: Apache-2.0 | ||||
| # | ||||
| # Check there are no os.Exit() calls creeping into the code | ||||
| # We don't use that exit path in the Kata codebase. | ||||
|  | ||||
| # Allow the path to check to be over-ridden. | ||||
| # Default to the current directory. | ||||
| go_packages=${1:-.} | ||||
|  | ||||
| echo "Checking for no os.Exit() calls for package [${go_packages}]" | ||||
|  | ||||
| candidates=`go list -f '{{.Dir}}/*.go' $go_packages` | ||||
| for f in $candidates; do | ||||
| 	filename=`basename $f` | ||||
| 	# skip all go test files | ||||
| 	[[ $filename == *_test.go ]] && continue | ||||
| 	# skip exit.go where, the only file we should call os.Exit() from. | ||||
| 	[[ $filename == "exit.go" ]] && continue | ||||
| 	files="$f $files" | ||||
| done | ||||
|  | ||||
| [ -z "$files" ] && echo "No files to check, skipping" && exit 0 | ||||
|  | ||||
| if egrep -n '\<os\.Exit\>' $files; then | ||||
| 	echo "Direct calls to os.Exit() are forbidden, please use exit() so atexit() works" | ||||
| 	exit 1 | ||||
| fi | ||||
| @@ -573,7 +573,6 @@ $(MONITOR_OUTPUT): $(SOURCES) $(GENERATED_FILES) $(MAKEFILE_LIST) .git-commit | ||||
|  | ||||
| .PHONY: \ | ||||
| 	check \ | ||||
| 	check-go-static \ | ||||
| 	coverage \ | ||||
| 	default \ | ||||
| 	install \ | ||||
| @@ -595,7 +594,7 @@ $(GENERATED_FILES): %: %.in $(MAKEFILE_LIST) VERSION .git-commit | ||||
|  | ||||
| generate-config: $(CONFIGS) | ||||
|  | ||||
| check: check-go-static | ||||
| check: | ||||
|  | ||||
| test: install-hook go-test | ||||
|  | ||||
| @@ -610,10 +609,6 @@ go-test: $(GENERATED_FILES) | ||||
| 	go clean -testcache | ||||
| 	go test -v -mod=vendor ./... | ||||
|  | ||||
| check-go-static: | ||||
| 	$(QUIET_CHECK)../../ci/go-no-os-exit.sh ./cmd/kata-runtime | ||||
| 	$(QUIET_CHECK)../../ci/go-no-os-exit.sh ./virtcontainers | ||||
|  | ||||
| coverage: | ||||
| 	go test -v -mod=vendor -covermode=atomic -coverprofile=coverage.txt ./... | ||||
| 	go tool cover -html=coverage.txt -o coverage.html | ||||
|   | ||||
| @@ -1,28 +0,0 @@ | ||||
| // Copyright (c) 2017 Intel Corporation | ||||
| // | ||||
| // SPDX-License-Identifier: Apache-2.0 | ||||
| // | ||||
|  | ||||
| package main | ||||
|  | ||||
| import "os" | ||||
|  | ||||
| var atexitFuncs []func() | ||||
|  | ||||
| var exitFunc = os.Exit | ||||
|  | ||||
| // atexit registers a function f that will be run when exit is called. The | ||||
| // handlers so registered will be called the in reverse order of their | ||||
| // registration. | ||||
| func atexit(f func()) { | ||||
| 	atexitFuncs = append(atexitFuncs, f) | ||||
| } | ||||
|  | ||||
| // exit calls all atexit handlers before exiting the process with status. | ||||
| func exit(status int) { | ||||
| 	for i := len(atexitFuncs) - 1; i >= 0; i-- { | ||||
| 		f := atexitFuncs[i] | ||||
| 		f() | ||||
| 	} | ||||
| 	exitFunc(status) | ||||
| } | ||||
| @@ -1,42 +0,0 @@ | ||||
| // Copyright (c) 2017 Intel Corporation | ||||
| // | ||||
| // SPDX-License-Identifier: Apache-2.0 | ||||
| // | ||||
|  | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| var testFoo string | ||||
|  | ||||
| func testFunc() { | ||||
| 	testFoo = "bar" | ||||
| } | ||||
|  | ||||
| func TestExit(t *testing.T) { | ||||
| 	assert := assert.New(t) | ||||
|  | ||||
| 	var testExitStatus int | ||||
| 	exitFunc = func(status int) { | ||||
| 		testExitStatus = status | ||||
| 	} | ||||
|  | ||||
| 	defer func() { | ||||
| 		exitFunc = os.Exit | ||||
| 	}() | ||||
|  | ||||
| 	// test with no atexit functions added. | ||||
| 	exit(1) | ||||
| 	assert.Equal(testExitStatus, 1) | ||||
|  | ||||
| 	// test with a function added to the atexit list. | ||||
| 	atexit(testFunc) | ||||
| 	exit(0) | ||||
| 	assert.Equal(testFoo, "bar") | ||||
| 	assert.Equal(testExitStatus, 0) | ||||
| } | ||||
| @@ -33,6 +33,8 @@ import ( | ||||
| // arch is the architecture for the running program | ||||
| const arch = goruntime.GOARCH | ||||
|  | ||||
| var exitFunc = os.Exit | ||||
|  | ||||
| var usage = fmt.Sprintf(`%s runtime | ||||
|  | ||||
| %s is a command line program for running applications packaged | ||||
| @@ -328,7 +330,7 @@ func handleShowConfig(context *cli.Context) { | ||||
| 			fmt.Fprintf(defaultOutputFile, "%s\n", file) | ||||
| 		} | ||||
|  | ||||
| 		exit(0) | ||||
| 		exitFunc(0) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -448,7 +450,7 @@ func userWantsUsage(context *cli.Context) bool { | ||||
| func fatal(err error) { | ||||
| 	kataLog.Error(err) | ||||
| 	fmt.Fprintln(defaultErrorFile, err) | ||||
| 	exit(1) | ||||
| 	exitFunc(1) | ||||
| } | ||||
|  | ||||
| type fatalWriter struct { | ||||
|   | ||||
| @@ -93,7 +93,7 @@ func TestMain(m *testing.M) { | ||||
| 	if path.Base(os.Args[0]) == katautils.NAME+".coverage" || | ||||
| 		path.Base(os.Args[0]) == katautils.NAME { | ||||
| 		main() | ||||
| 		exit(0) | ||||
| 		exitFunc(0) | ||||
| 	} | ||||
|  | ||||
| 	runUnitTests(m) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user