storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/main_test.go (about) 1 //go:build testrunmain 2 // +build testrunmain 3 4 /* 5 * MinIO Cloud Storage, (C) 2021 MinIO, Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package main 21 22 import ( 23 "log" 24 "os" 25 "os/signal" 26 "strings" 27 "syscall" 28 "testing" 29 30 minio "storj.io/minio/cmd" 31 _ "storj.io/minio/cmd/gateway" 32 ) 33 34 // TestRunMain takes arguments from APP_ARGS env variable and calls minio.Main(args) 35 // 1. Build and RUN test executable: 36 // $ go test -tags testrunmain -covermode count -coverpkg="./..." -c -tags testrunmain 37 // $ APP_ARGS="server /tmp/test" ./minio.test -test.run "^TestRunMain$" -test.coverprofile coverage.cov 38 // 39 // 1. As an alternative you can also run the system under test by just by calling "go test" 40 // $ APP_ARGS="server /tmp/test" go test -cover -tags testrunmain -covermode count -coverpkg="./..." -coverprofile=coverage.cov 41 // 42 // 2. Run System-Tests (when using GitBash prefix this line with MSYS_NO_PATHCONV=1) 43 // Note the the SERVER_ENDPOINT must be reachable from inside the docker container (so don't use localhost!) 44 // $ 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 45 // 46 // 3. Stop system under test by sending SIGTERM 47 // $ ctrl+c 48 // 49 // 4. Optionally transform coverage file to HTML 50 // $ go tool cover -html=./coverage.cov -o coverage.html 51 // 52 // 5. Optionally transform the coverage file to .csv 53 // $ cat coverage.cov | sed -E 's/mode: .*/source;from;to;stmnts;count/g' | sed -E 's/:| |,/;/g' > coverage.csv 54 func TestRunMain(t *testing.T) { 55 cancelChan := make(chan os.Signal, 1) 56 // catch SIGETRM or SIGINTERRUPT. The test must gracefully end to complete the test coverage. 57 signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT) 58 go func() { 59 // start minio server with params from env variable APP_ARGS 60 args := os.Getenv("APP_ARGS") 61 if args == "" { 62 log.Printf("No environment variable APP_ARGS found. Starting minio without parameters ...") 63 } else { 64 log.Printf("Starting \"minio %v\" ...", args) 65 } 66 minio.Main(strings.Split("minio.test "+args, " ")) 67 }() 68 sig := <-cancelChan 69 log.Printf("Caught SIGTERM %v", sig) 70 log.Print("You might want to transform the coverage.cov file to .html by calling:") 71 log.Print("$ go tool cover -html=./coverage.cov -o coverage.html") 72 // shutdown other goroutines gracefully 73 // close other resources 74 }