...

Source file src/edge-infra.dev/test/framework/context.go

Documentation: edge-infra.dev/test/framework

     1  package framework
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // testContext is a private struct used to create a global test context leveraged
    12  // by all
    13  type testContext struct {
    14  	Labels     map[string]string
    15  	SkipLabels map[string]string
    16  	RepoRoot   string
    17  }
    18  
    19  // Context is the public instance of our private context struct.  All framework
    20  // instances and specific suite logic should interact with this object when
    21  // reading or updating test context
    22  var Context testContext
    23  
    24  // RegisterCommonFlags registers flags for the framework that are common to
    25  // all suites (e.g., parameters for Ginkgo or other config that effects
    26  // how the tests are ran) and sets some defaults for Ginkgo
    27  func RegisterCommonFlags(flags *flag.FlagSet) {
    28  	flags.Func("labels", "only run tests with the provided comma separated labels", func(s string) error {
    29  		Context.Labels = commaSepValues(s)
    30  		return nil
    31  	})
    32  
    33  	flags.Func("skip-labels", "run all tests except those with the provided comma separated labels", func(s string) error {
    34  		Context.SkipLabels = commaSepValues(s)
    35  		return nil
    36  	})
    37  
    38  	flags.StringVar(&Context.RepoRoot, "repo-root", "", "absolute path to repository root")
    39  }
    40  
    41  // parses comma separated map values
    42  func commaSepValues(s string) map[string]string {
    43  	a := strings.Split(s, ",")
    44  	r := map[string]string{}
    45  	for _, v := range a {
    46  		if v != "" {
    47  			kv := strings.Split(v, "=")
    48  			// drop malformed input
    49  			if len(kv) > 1 {
    50  				r[kv[0]] = kv[1]
    51  			}
    52  		}
    53  	}
    54  	return r
    55  }
    56  
    57  // Validate checks that the base test context was provided valid values via
    58  // config
    59  func (c *testContext) Validate() {
    60  	if c.Labels != nil && c.SkipLabels != nil {
    61  		panic(errors.New("-labels and -skip-labels are mutually exclusive"))
    62  	}
    63  }
    64  
    65  // ResolvePath resolves the provided path relative to the repository root.
    66  // If a repository root isnt provided via flag, it panics.
    67  func ResolvePath(p ...string) string {
    68  	if Context.RepoRoot == "" {
    69  		panic(fmt.Sprintf("cant resolve path to %s, -repo-root not provided", p))
    70  	}
    71  
    72  	elements := append([]string{Context.RepoRoot}, p...)
    73  
    74  	return filepath.Join(elements...)
    75  }
    76  

View as plain text