Files
nomad/command/node_pool_info_test.go
James Rasell c80c60965f node pool: Allow specifying node identity ttl in HCL or JSON spec. (#26825)
The node identity TTL defaults to 24hr but can be altered by
setting the node identity TTL parameter. In order to allow setting
and viewing the value, the field is now plumbed through the CLI
and HTTP API.

In order to parse the HCL, a new helper package has been created
which contains generic parsing and decoding functionality for
dealing with HCL that contains time durations. hclsimple can be
used when this functionality is not needed. In order to parse the
JSON, custom marshal and unmarshal functions have been created as
used in many other places.

The node pool init command has been updated to include this new
parameter, although commented out, so reference. The info command
now includes the TTL in its output too.
2025-09-24 14:20:34 +01:00

165 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"strings"
"testing"
"time"
"github.com/hashicorp/cli"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/shoenig/test"
"github.com/shoenig/test/must"
)
func TestNodePoolInfoCommand_Implements(t *testing.T) {
ci.Parallel(t)
var _ cli.Command = &NodePoolInfoCommand{}
}
func TestNodePoolInfoCommand_Run(t *testing.T) {
ci.Parallel(t)
// Start test server.
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
waitForNodes(t, client)
// Register test node pools.
dev1 := &api.NodePool{
Name: "dev-1",
Description: "Test pool",
Meta: map[string]string{
"env": "test",
},
NodeIdentityTTL: 720 * time.Hour,
}
_, err := client.NodePools().Register(dev1, nil)
must.NoError(t, err)
dev1Output := `
Name = dev-1
Description = Test pool
Node Identity TTL = 720h0m0s
Metadata
env = test
Scheduler Configuration
No scheduler configuration`
dev1JsonOutput := `
"NodeIdentityTTL":"720h0m0s","Name":"dev-1","Description":"Test pool","Meta":{"env":"test"},"SchedulerConfiguration":null`
// These two node pools are used to test exact prefix match.
prod1 := &api.NodePool{Name: "prod-1"}
_, err = client.NodePools().Register(prod1, nil)
must.NoError(t, err)
prod12 := &api.NodePool{Name: "prod-12"}
_, err = client.NodePools().Register(prod12, nil)
must.NoError(t, err)
testCases := []struct {
name string
args []string
expectedOut string
expectedErr string
expectedCode int
}{
{
name: "basic info",
args: []string{"dev-1"},
expectedOut: dev1Output,
expectedCode: 0,
},
{
name: "basic info by prefix",
args: []string{"dev"},
expectedOut: dev1Output,
expectedCode: 0,
},
{
name: "exact prefix match",
args: []string{"prod-1"},
expectedOut: `
Name = prod-1
Description = <none>
Node Identity TTL = 24h0m0s
Metadata
No metadata
Scheduler Configuration
No scheduler configuration`,
expectedCode: 0,
},
{
name: "json",
args: []string{"-json", "dev"},
expectedOut: dev1JsonOutput,
expectedCode: 0,
},
{
name: "template",
args: []string{
"-t", "{{.Name}} -> {{.Meta.env}}",
"dev-1",
},
expectedOut: "dev-1 -> test",
expectedCode: 0,
},
{
name: "fail because of missing node pool arg",
args: []string{},
expectedErr: "This command takes one argument",
expectedCode: 1,
},
{
name: "fail because no match",
args: []string{"invalid"},
expectedErr: `No node pool with prefix "invalid" found`,
expectedCode: 1,
},
{
name: "fail because of multiple matches",
args: []string{"de"}, // Matches default and dev-1.
expectedErr: "Prefix matched multiple node pools",
expectedCode: 1,
},
{
name: "fail because of invalid template",
args: []string{
"-t", "{{.NotValid}}",
"dev-1",
},
expectedErr: "Error formatting the data",
expectedCode: 1,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Initialize UI and command.
ui := cli.NewMockUi()
cmd := &NodePoolInfoCommand{Meta: Meta{Ui: ui}}
// Run command.
args := []string{"-address", url}
args = append(args, tc.args...)
code := cmd.Run(args)
gotStdout := ui.OutputWriter.String()
gotStdout = jsonOutputRaftIndexes.ReplaceAllString(gotStdout, "")
test.Eq(t, tc.expectedCode, code)
test.StrContains(t, gotStdout, strings.TrimSpace(tc.expectedOut))
test.StrContains(t, ui.ErrorWriter.String(), strings.TrimSpace(tc.expectedErr))
})
}
}