Add main_test.go to run system tests with coverage (#11783)

-  Build and RUN test executable: 
```
$ go test -tags testrunmain -covermode count
	-coverpkg="./..." -c -tags testrunmain
$ APP_ARGS="server /tmp/test" ./minio.test
	-test.run "^TestRunMain$"
	-test.coverprofile coverage.cov
```

- Or run the system under test just by calling go test
```
$ APP_ARGS="server /tmp/test" go test
	-cover
	-tags testrunmain
	-coverpkg="./..."
	-covermode count
	-coverprofile=coverage.cov
```

- Run System-Tests (when using GitBash prefix this 
  line with MSYS_NO_PATHCONV=1) Note the 
  SERVER_ENDPOINT must be reachable from 
  inside the docker container (so don't use localhost!)
```
$ docker run
	-e MINT_MODE=full
	-e SERVER_ENDPOINT=192.168.47.11:9000
	-e ACCESS_KEY=minioadmin
	-e SECRET_KEY=minioadmin
	-v /tmp/mint/log:/mint/log
	minio/mint
``

- Stop system under test  by sending SIGTERM
```
$ ctrl+c
```

- Transform coverage file to HTML
```
$ go tool cover -html=./coverage.cov -o coverage.html
```
This commit is contained in:
iternity-dotcom 2021-03-18 21:14:44 +01:00 committed by GitHub
parent 14d89eaae4
commit d46c3c07a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

73
main_test.go Normal file
View File

@ -0,0 +1,73 @@
// +build testrunmain
/*
* MinIO Cloud Storage, (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"testing"
minio "github.com/minio/minio/cmd"
_ "github.com/minio/minio/cmd/gateway"
)
// TestRunMain takes arguments from APP_ARGS env variable and calls minio.Main(args)
// 1. Build and RUN test executable:
// $ go test -tags testrunmain -covermode count -coverpkg="./..." -c -tags testrunmain
// $ APP_ARGS="server /tmp/test" ./minio.test -test.run "^TestRunMain$" -test.coverprofile coverage.cov
//
// 1. As an alternative you can also run the system under test by just by calling "go test"
// $ APP_ARGS="server /tmp/test" go test -cover -tags testrunmain -covermode count -coverpkg="./..." -coverprofile=coverage.cov
//
// 2. Run System-Tests (when using GitBash prefix this line with MSYS_NO_PATHCONV=1)
// Note the the SERVER_ENDPOINT must be reachable from inside the docker container (so don't use localhost!)
// $ docker run -e MINT_MODE=full -e SERVER_ENDPOINT=192.168.47.11:9000 -e ACCESS_KEY=minioadmin -e SECRET_KEY=minioadmin -v /tmp/mint/log:/mint/log minio/mint
//
// 3. Stop system under test by sending SIGTERM
// $ ctrl+c
//
// 4. Optionally transform coverage file to HTML
// $ go tool cover -html=./coverage.cov -o coverage.html
//
// 5. Optionally transform the coverage file to .csv
// $ cat coverage.cov | sed -E 's/mode: .*/source;from;to;stmnts;count/g' | sed -E 's/:| |,/;/g' > coverage.csv
func TestRunMain(t *testing.T) {
cancelChan := make(chan os.Signal, 1)
// catch SIGETRM or SIGINTERRUPT. The test must gracefully end to complete the test coverage.
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
// start minio server with params from env variable APP_ARGS
args := os.Getenv("APP_ARGS")
if args == "" {
log.Printf("No environment variable APP_ARGS found. Starting minio without parameters ...")
} else {
log.Printf("Starting \"minio %v\" ...", args)
}
minio.Main(strings.Split("minio.test "+args, " "))
}()
sig := <-cancelChan
log.Printf("Caught SIGTERM %v", sig)
log.Print("You might want to transform the coverage.cov file to .html by calling:")
log.Print("$ go tool cover -html=./coverage.cov -o coverage.html")
// shutdown other goroutines gracefully
// close other resources
}