github.com/hernad/nomad@v1.6.112/command/namespace_status_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "encoding/json" 8 "strings" 9 "testing" 10 11 "github.com/hernad/nomad/api" 12 "github.com/hernad/nomad/ci" 13 "github.com/mitchellh/cli" 14 "github.com/posener/complete" 15 "github.com/shoenig/test/must" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestNamespaceStatusCommand_Implements(t *testing.T) { 20 ci.Parallel(t) 21 var _ cli.Command = &NamespaceStatusCommand{} 22 } 23 24 func TestNamespaceStatusCommand_Fails(t *testing.T) { 25 ci.Parallel(t) 26 ui := cli.NewMockUi() 27 cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}} 28 29 // Fails on misuse 30 code := cmd.Run([]string{"some", "bad", "args"}) 31 must.One(t, code) 32 33 must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd)) 34 35 ui.ErrorWriter.Reset() 36 37 code = cmd.Run([]string{"-address=nope", "foo"}) 38 must.One(t, code) 39 40 must.StrContains(t, ui.ErrorWriter.String(), "retrieving namespace") 41 ui.ErrorWriter.Reset() 42 } 43 44 func TestNamespaceStatusCommand_Run(t *testing.T) { 45 ci.Parallel(t) 46 47 // Create a server 48 srv, client, url := testServer(t, true, nil) 49 defer srv.Shutdown() 50 51 ui := cli.NewMockUi() 52 cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}} 53 54 // Create a namespace 55 ns := &api.Namespace{ 56 Name: "foo", 57 } 58 _, err := client.Namespaces().Register(ns, nil) 59 must.NoError(t, err) 60 61 // Check status on namespace 62 code := cmd.Run([]string{"-address=" + url, ns.Name}) 63 must.Zero(t, code) 64 65 // Check for basic spec 66 out := ui.OutputWriter.String() 67 if !strings.Contains(out, "= foo") { 68 t.Fatalf("expected quota, got: %s", out) 69 } 70 71 ui.OutputWriter.Reset() 72 73 // List json 74 code = cmd.Run([]string{"-address=" + url, "-json", ns.Name}) 75 must.Zero(t, code) 76 77 outJson := api.Namespace{} 78 err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson) 79 must.NoError(t, err) 80 81 ui.OutputWriter.Reset() 82 83 // Go template to format the output 84 code = cmd.Run([]string{"-address=" + url, "-t", "{{.Name}}", ns.Name}) 85 must.Zero(t, code) 86 87 out = ui.OutputWriter.String() 88 must.StrContains(t, out, "foo") 89 90 ui.OutputWriter.Reset() 91 ui.ErrorWriter.Reset() 92 } 93 94 func TestNamespaceStatusCommand_Run_Quota(t *testing.T) { 95 ci.Parallel(t) 96 97 // Create a server 98 srv, client, url := testServer(t, true, nil) 99 defer srv.Shutdown() 100 101 if !srv.Enterprise { 102 t.Skip("Skipping enterprise-only quota test") 103 } 104 105 ui := cli.NewMockUi() 106 cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}} 107 108 // Create a quota to delete 109 qs := testQuotaSpec() 110 _, err := client.Quotas().Register(qs, nil) 111 assert.Nil(t, err) 112 113 // Create a namespace 114 ns := &api.Namespace{ 115 Name: "foo", 116 Quota: qs.Name, 117 } 118 _, err = client.Namespaces().Register(ns, nil) 119 assert.Nil(t, err) 120 121 // Check status on namespace 122 code := cmd.Run([]string{"-address=" + url, ns.Name}) 123 must.Zero(t, code) 124 125 out := ui.OutputWriter.String() 126 127 // Check for basic spec 128 must.StrContains(t, out, "= foo") 129 130 // Check for usage 131 must.StrContains(t, out, "0 / 100") 132 133 } 134 135 func TestNamespaceStatusCommand_AutocompleteArgs(t *testing.T) { 136 ci.Parallel(t) 137 138 srv, client, url := testServer(t, true, nil) 139 defer srv.Shutdown() 140 141 ui := cli.NewMockUi() 142 cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}} 143 144 // Create a namespace 145 ns := &api.Namespace{ 146 Name: "foo", 147 } 148 _, err := client.Namespaces().Register(ns, nil) 149 must.NoError(t, err) 150 151 args := complete.Args{Last: "f"} 152 predictor := cmd.AutocompleteArgs() 153 154 res := predictor.Predict(args) 155 must.One(t, len(res)) 156 must.StrContains(t, ns.Name, res[0]) 157 } 158 159 // This test should demonstrate the behavior of a namespace 160 // and prefix collision. In that case, the Namespace status 161 // command should pull the matching namespace rather than 162 // displaying the multiple match error 163 func TestNamespaceStatusCommand_NamespaceMatchesPrefix(t *testing.T) { 164 ci.Parallel(t) 165 166 // Create a server 167 srv, client, url := testServer(t, true, nil) 168 defer srv.Shutdown() 169 170 ui := cli.NewMockUi() 171 cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}} 172 173 // Create a namespace that uses foo as a prefix 174 ns := &api.Namespace{Name: "fooBar"} 175 _, err := client.Namespaces().Register(ns, nil) 176 must.NoError(t, err) 177 178 // Create a foo namespace 179 ns2 := &api.Namespace{Name: "foo"} 180 _, err = client.Namespaces().Register(ns2, nil) 181 must.NoError(t, err) 182 183 // Adding a NS after to prevent sort from creating 184 // false successes 185 ns = &api.Namespace{Name: "fooBaz"} 186 _, err = client.Namespaces().Register(ns, nil) 187 must.NoError(t, err) 188 189 // Check status on namespace 190 code := cmd.Run([]string{"-address=" + url, ns2.Name}) 191 must.Zero(t, code) 192 193 // Check to ensure we got the proper foo 194 must.StrContains(t, ui.OutputWriter.String(), "= foo\n") 195 }