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