...

Source file src/go.etcd.io/etcd/server/v3/main_test.go

Documentation: go.etcd.io/etcd/server/v3

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  	"os/signal"
    21  	"strings"
    22  	"syscall"
    23  	"testing"
    24  
    25  	"go.etcd.io/etcd/server/v3/etcdmain"
    26  )
    27  
    28  func SplitTestArgs(args []string) (testArgs, appArgs []string) {
    29  	for i, arg := range os.Args {
    30  		switch {
    31  		case strings.HasPrefix(arg, "-test."):
    32  			testArgs = append(testArgs, arg)
    33  		case i == 0:
    34  			appArgs = append(appArgs, arg)
    35  			testArgs = append(testArgs, arg)
    36  		default:
    37  			appArgs = append(appArgs, arg)
    38  		}
    39  	}
    40  	return
    41  }
    42  
    43  func TestEmpty(t *testing.T) {}
    44  
    45  /**
    46   * The purpose of this "test" is to run etcd server with code-coverage
    47   * collection turned on. The technique is documented here:
    48   *
    49   * https://www.cyphar.com/blog/post/20170412-golang-integration-coverage
    50   */
    51  func TestMain(m *testing.M) {
    52  	// don't launch etcd server when invoked via go test
    53  	if strings.HasSuffix(os.Args[0], ".test") {
    54  		log.Printf("skip launching etcd server when invoked via go test")
    55  		return
    56  	}
    57  
    58  	testArgs, appArgs := SplitTestArgs(os.Args)
    59  
    60  	notifier := make(chan os.Signal, 1)
    61  	signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM)
    62  	go etcdmain.Main(appArgs)
    63  	<-notifier
    64  
    65  	// This will generate coverage files:
    66  	os.Args = testArgs
    67  	m.Run()
    68  }
    69  

View as plain text