github.com/hernad/nomad@v1.6.112/command/acl_auth_method_info_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hernad/nomad/ci"
    10  	"github.com/hernad/nomad/command/agent"
    11  	"github.com/hernad/nomad/nomad/mock"
    12  	"github.com/hernad/nomad/nomad/structs"
    13  	"github.com/mitchellh/cli"
    14  	"github.com/shoenig/test/must"
    15  )
    16  
    17  func TestACLAuthMethodInfoCommand(t *testing.T) {
    18  	ci.Parallel(t)
    19  
    20  	config := func(c *agent.Config) {
    21  		c.ACL.Enabled = true
    22  	}
    23  
    24  	srv, _, url := testServer(t, true, config)
    25  	state := srv.Agent.Server().State()
    26  	defer srv.Shutdown()
    27  
    28  	// Bootstrap an initial ACL token
    29  	token := srv.RootToken
    30  	must.NotNil(t, token)
    31  
    32  	// Create a test auth method
    33  	method := &structs.ACLAuthMethod{
    34  		Name: "test-auth-method",
    35  		Config: &structs.ACLAuthMethodConfig{
    36  			OIDCDiscoveryURL: "http://example.com",
    37  		},
    38  	}
    39  	method.SetHash()
    40  	must.NoError(t, state.UpsertACLAuthMethods(1000, []*structs.ACLAuthMethod{method}))
    41  
    42  	ui := cli.NewMockUi()
    43  	cmd := &ACLAuthMethodInfoCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    44  
    45  	// Attempt to get info without a valid management token
    46  	invalidToken := mock.ACLToken()
    47  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID, method.Name})
    48  	must.One(t, code)
    49  
    50  	// Get info with a valid management token
    51  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, method.Name})
    52  	must.Zero(t, code)
    53  
    54  	// Check the output
    55  	out := ui.OutputWriter.String()
    56  	must.StrContains(t, out, method.Name)
    57  }