github.com/hernad/nomad@v1.6.112/e2e/acl/helpers.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package acl
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hashicorp/go-set"
    10  	"github.com/hernad/nomad/api"
    11  	"github.com/shoenig/test"
    12  	"github.com/shoenig/test/must"
    13  )
    14  
    15  // TestResourceType indicates what the resource is so the Cleanup process can
    16  // use the correct API.
    17  type TestResourceType int
    18  
    19  const (
    20  	NamespaceTestResourceType TestResourceType = iota
    21  	ACLPolicyTestResourceType
    22  	ACLRoleTestResourceType
    23  	ACLTokenTestResourceType
    24  )
    25  
    26  // Cleanup stores Nomad resources that have been created by a test which will
    27  // need to be deleted once the test exits. This ensures other tests can run in
    28  // a clean environment and reduces the potential for conflicts.
    29  type Cleanup struct {
    30  	namespaces  *set.Set[string]
    31  	aclPolicies *set.Set[string]
    32  	aclRoles    *set.Set[string]
    33  	aclTokens   *set.Set[string]
    34  }
    35  
    36  // NewCleanup generates an initialized Cleanup object for immediate use.
    37  func NewCleanup() *Cleanup {
    38  	return &Cleanup{
    39  		namespaces:  set.New[string](0),
    40  		aclPolicies: set.New[string](0),
    41  		aclRoles:    set.New[string](0),
    42  		aclTokens:   set.New[string](0),
    43  	}
    44  }
    45  
    46  // Run triggers a Cleanup of all the stored resources. This should typically be
    47  // called via defer, so it will always Run no matter if the test fails or not.
    48  // Any failure will ultimately fail the test, but will not stop the attempts to
    49  // delete all the resources.
    50  func (c *Cleanup) Run(t *testing.T, nomadClient *api.Client) {
    51  	for _, namespace := range c.namespaces.Slice() {
    52  		_, err := nomadClient.Namespaces().Delete(namespace, nil)
    53  		test.NoError(t, err)
    54  	}
    55  
    56  	for _, policy := range c.aclPolicies.Slice() {
    57  		_, err := nomadClient.ACLPolicies().Delete(policy, nil)
    58  		test.NoError(t, err)
    59  	}
    60  
    61  	for _, role := range c.aclRoles.Slice() {
    62  		_, err := nomadClient.ACLRoles().Delete(role, nil)
    63  		test.NoError(t, err)
    64  	}
    65  
    66  	for _, token := range c.aclTokens.Slice() {
    67  		_, err := nomadClient.ACLTokens().Delete(token, nil)
    68  		test.NoError(t, err)
    69  	}
    70  
    71  	must.NoError(t, nomadClient.System().GarbageCollect())
    72  }
    73  
    74  // Add the resource identifier to the resource tracker. It will be removed by
    75  // the Cleanup function once it is triggered.
    76  func (c *Cleanup) Add(id string, resourceType TestResourceType) {
    77  	switch resourceType {
    78  	case NamespaceTestResourceType:
    79  		c.namespaces.Insert(id)
    80  	case ACLPolicyTestResourceType:
    81  		c.aclPolicies.Insert(id)
    82  	case ACLRoleTestResourceType:
    83  		c.aclRoles.Insert(id)
    84  	case ACLTokenTestResourceType:
    85  		c.aclTokens.Insert(id)
    86  	}
    87  }
    88  
    89  // Remove the resource identifier from the resource tracker, indicating it is
    90  // no longer existing on the cluster and does not need to be cleaned.
    91  func (c *Cleanup) Remove(id string, resourceType TestResourceType) {
    92  	switch resourceType {
    93  	case NamespaceTestResourceType:
    94  		c.namespaces.Remove(id)
    95  	case ACLPolicyTestResourceType:
    96  		c.aclPolicies.Remove(id)
    97  	case ACLRoleTestResourceType:
    98  		c.aclRoles.Remove(id)
    99  	case ACLTokenTestResourceType:
   100  		c.aclTokens.Remove(id)
   101  	}
   102  }