github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/e2e/framework/case.go (about)

     1  package framework
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/nomad/api"
     7  )
     8  
     9  // TestSuite defines a set of test cases and under what conditions to run them
    10  type TestSuite struct {
    11  	Component string // Name of the component/system/feature tested
    12  
    13  	CanRunLocal bool        // Flags if the cases are safe to run on a local nomad cluster
    14  	Cases       []TestCase  // Cases to run
    15  	Constraints Constraints // Environment constraints to follow
    16  	Parallel    bool        // If true, will run test cases in parallel
    17  	Slow        bool        // Slow test suites don't run by default
    18  
    19  	// API Clients
    20  	Consul bool
    21  	Vault  bool
    22  }
    23  
    24  // Constraints that must be satisfied for a TestSuite to run
    25  type Constraints struct {
    26  	Provider    string   // Cloud provider ex. 'aws', 'azure', 'gcp'
    27  	OS          string   // Operating system ex. 'windows', 'linux'
    28  	Arch        string   // CPU architecture ex. 'amd64', 'arm64'
    29  	Environment string   // Environment name ex. 'simple'
    30  	Tags        []string // Generic tags that must all exist in the environment
    31  }
    32  
    33  func (c Constraints) matches(env Environment) error {
    34  	if len(c.Provider) != 0 && c.Provider != env.Provider {
    35  		return fmt.Errorf("provider constraint does not match environment")
    36  	}
    37  
    38  	if len(c.OS) != 0 && c.OS != env.OS {
    39  		return fmt.Errorf("os constraint does not match environment")
    40  	}
    41  
    42  	if len(c.Arch) != 0 && c.Arch != env.Arch {
    43  		return fmt.Errorf("arch constraint does not match environment")
    44  	}
    45  
    46  	if len(c.Environment) != 0 && c.Environment != env.Name {
    47  		return fmt.Errorf("environment constraint does not match environment name")
    48  	}
    49  
    50  	for _, t := range c.Tags {
    51  		if _, ok := env.Tags[t]; !ok {
    52  			return fmt.Errorf("tags constraint failed, tag '%s' is not included in environment", t)
    53  		}
    54  	}
    55  	return nil
    56  }
    57  
    58  // TC is the base test case which should be embedded in TestCase implementations.
    59  type TC struct {
    60  	cluster *ClusterInfo
    61  }
    62  
    63  // Nomad returns a configured nomad api client
    64  func (tc *TC) Nomad() *api.Client {
    65  	return tc.cluster.NomadClient
    66  }
    67  
    68  // Name returns the name of the test case which is set to the name of the
    69  // implementing type.
    70  func (tc *TC) Name() string {
    71  	return tc.cluster.Name
    72  }
    73  
    74  func (tc *TC) setClusterInfo(info *ClusterInfo) {
    75  	tc.cluster = info
    76  }