github.com/rohankumardubey/nomad@v0.11.8/e2e/framework/case.go (about)

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