github.phpd.cn/cilium/cilium@v1.6.12/test/helpers/scope.go (about)

     1  package helpers
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	ginkgoconfig "github.com/onsi/ginkgo/config"
     9  )
    10  
    11  // UserDefinedScope in case that the test scope is defined by the user instead
    12  // of the focus string.
    13  var UserDefinedScope string
    14  
    15  // GetScope returns the scope for the currently running test.
    16  func GetScope() (string, error) {
    17  	if UserDefinedScope != "" {
    18  		return UserDefinedScope, nil
    19  	}
    20  
    21  	focusString := strings.TrimSpace(strings.ToLower(ginkgoconfig.GinkgoConfig.FocusString))
    22  	switch {
    23  	case strings.HasPrefix(focusString, "run"):
    24  		return Runtime, nil
    25  	case strings.HasPrefix(focusString, K8s):
    26  		return K8s, nil
    27  	case strings.Contains(focusString, "nightly"):
    28  		// Nightly tests run in a Kubernetes environment.
    29  		return K8s, nil
    30  	default:
    31  		return "", errors.New("Scope cannot be set")
    32  	}
    33  }
    34  
    35  // GetScopeWithVersion returns the scope of the running test. If the scope is
    36  // k8s, then it returns k8s scope along with the version of k8s that is running.
    37  func GetScopeWithVersion() string {
    38  	// No errors check here, test never reach here because test should fail
    39  	// before.
    40  	result, _ := GetScope()
    41  	if result != K8s {
    42  		return result
    43  	}
    44  	return fmt.Sprintf("%s-%s", result, GetCurrentK8SEnv())
    45  }