github.com/hernad/nomad@v1.6.112/e2e/v3/namespaces3/namespaces3.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package namespaces3 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/hashicorp/go-set" 11 nomadapi "github.com/hernad/nomad/api" 12 "github.com/hernad/nomad/e2e/v3/util3" 13 "github.com/hernad/nomad/helper" 14 "github.com/shoenig/test" 15 "github.com/shoenig/test/must" 16 ) 17 18 type Names struct { 19 t *testing.T 20 21 nomadClient *nomadapi.Client 22 23 noCleanup bool 24 timeout time.Duration 25 verbose bool 26 27 apply *set.HashSet[*Namespace, string] 28 remove *set.Set[string] 29 } 30 31 func (g *Names) logf(msg string, args ...any) { 32 util3.Log3(g.t, g.verbose, msg, args...) 33 } 34 35 func (g *Names) cleanup() { 36 if g.noCleanup { 37 return 38 } 39 40 namespaceAPI := g.nomadClient.Namespaces() 41 42 // remove any namespaces we created (or updated) 43 for _, namespace := range g.apply.Slice() { 44 name := namespace.Name 45 g.logf("cleanup namespace %q", name) 46 _, err := namespaceAPI.Delete(name, nil) 47 test.NoError(g.t, err, test.Sprintf("unable to delete namespace %q", name)) 48 } 49 } 50 51 type Option func(*Names) 52 53 type Cleanup func() 54 55 type Namespace struct { 56 Name string 57 Description string 58 } 59 60 func (ns *Namespace) Hash() string { 61 return ns.Name 62 } 63 64 func (ns *Namespace) String() string { 65 return ns.Name 66 } 67 68 func (n *Names) setClient() { 69 nomadClient, nomadErr := nomadapi.NewClient(nomadapi.DefaultConfig()) 70 must.NoError(n.t, nomadErr, must.Sprint("failed to create nomad api client")) 71 n.nomadClient = nomadClient 72 } 73 74 func configure(t *testing.T, opts ...Option) Cleanup { 75 g := &Names{ 76 t: t, 77 timeout: 10 * time.Second, 78 apply: set.NewHashSet[*Namespace, string](3), 79 remove: set.New[string](3), 80 } 81 82 for _, opt := range opts { 83 opt(g) 84 } 85 86 g.setClient() 87 g.run() 88 89 return g.cleanup 90 } 91 92 func (g *Names) run() { 93 namespacesAPI := g.nomadClient.Namespaces() 94 95 // do deletions 96 for _, namespace := range g.remove.Slice() { 97 g.logf("delete namespace %q", namespace) 98 _, err := namespacesAPI.Delete(namespace, nil) 99 must.NoError(g.t, err) 100 } 101 102 // do applies 103 for _, namespace := range g.apply.Slice() { 104 g.logf("apply namespace %q", namespace) 105 _, err := namespacesAPI.Register(&nomadapi.Namespace{ 106 Name: namespace.Name, 107 Description: namespace.Description, 108 }, nil) 109 must.NoError(g.t, err) 110 } 111 } 112 113 // Create a namespace of the given name. 114 func Create(t *testing.T, name string, opts ...Option) Cleanup { 115 namespace := &Namespace{Name: name} 116 opt := apply(namespace) 117 return configure(t, append(opts, opt)...) 118 } 119 120 // Create namespaces of the given names. 121 func CreateN(t *testing.T, names []string, opts ...Option) Cleanup { 122 creations := helper.ConvertSlice(names, func(name string) Option { 123 namespace := &Namespace{Name: name} 124 return apply(namespace) 125 }) 126 return configure(t, append(opts, creations...)...) 127 } 128 129 // Delete the namespace of the given name. 130 func Delete(t *testing.T, name string, opts ...Option) Cleanup { 131 opt := remove(name) 132 return configure(t, append(opts, opt)...) 133 } 134 135 func apply(namespace *Namespace) Option { 136 return func(g *Names) { 137 g.apply.Insert(namespace) 138 } 139 } 140 141 func remove(name string) Option { 142 return func(g *Names) { 143 g.remove.Insert(name) 144 } 145 } 146 147 // DisableCleanup will disable the automatic removal of any namespaces 148 // created using the Create() Option. 149 func DisableCleanup() Option { 150 return func(n *Names) { 151 n.noCleanup = true 152 } 153 } 154 155 func Timeout(timeout time.Duration) Option { 156 return func(n *Names) { 157 n.timeout = timeout 158 } 159 } 160 161 // Verbose will enable verbose logging. 162 func Verbose(on bool) Option { 163 return func(n *Names) { 164 n.verbose = on 165 } 166 }