github.com/hernad/nomad@v1.6.112/e2e/framework/case.go (about)

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