backport of commit 71ea1deda707b706c36da7567caba7721e51db19 (#19444)

Co-authored-by: James Rasell <jrasell@users.noreply.github.com>
This commit is contained in:
hc-github-team-nomad-core 2023-12-12 02:48:17 -06:00 committed by GitHub
parent 2e8c36e698
commit ede76a85e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 0 deletions

3
.changelog/19423.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: Fix a bug in the `var put` command which prevented combining items as CLI arguments and other parameters as flags
```

View File

@ -393,6 +393,13 @@ func (c *VarPutCommand) makeVariable(path string) (*api.Variable, error) {
return nil, fmt.Errorf("unknown format flag value") return nil, fmt.Errorf("unknown format flag value")
} }
// It is possible a specification file was used which did not declare any
// items. Therefore, default the entry to avoid panics and ensure this type
// of use is valid.
if out.Items == nil {
out.Items = make(map[string]string)
}
// Handle cases where values are provided by CLI flags that modify the // Handle cases where values are provided by CLI flags that modify the
// the created variable. Typical of a "copy" operation, it is a convenience // the created variable. Typical of a "copy" operation, it is a convenience
// to reset the Create and Modify metadata to zero. // to reset the Create and Modify metadata to zero.

View File

@ -6,6 +6,7 @@ package command
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
@ -107,6 +108,40 @@ func TestVarPutCommand_GoodJson(t *testing.T) {
require.Equal(t, api.VariableItems{"k1": "v1", "k2": "v2"}, outVar.Items) require.Equal(t, api.VariableItems{"k1": "v1", "k2": "v2"}, outVar.Items)
} }
func TestVarPutCommand_FlagsWithSpec(t *testing.T) {
ci.Parallel(t)
// Create a server
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &VarPutCommand{Meta: Meta{Ui: ui}}
// Create a temporary file and ensure the proper cleanup is run once the
// test ends.
osFile, err := os.CreateTemp("", "nomad-cli-var-put-test-*.hcl")
must.NoError(t, err)
t.Cleanup(func() {
_ = osFile.Close()
_ = os.Remove(osFile.Name())
})
// Write out a partial spec that includes the namespace and variable path.
_, err = osFile.Write([]byte("path = \"path/to/variable\"\nnamespace = \"default\""))
must.NoError(t, err)
// Create the variables, ensure we clean it up, and check the command
// response.
code := cmd.Run([]string{"-address=" + url, "@" + osFile.Name(), "k1=v1", "k2=v2"})
must.Zero(t, code)
must.StrContains(t, ui.OutputWriter.String(), "path/to/variable")
must.StrContains(t, ui.OutputWriter.String(), "\"k1\": \"v1\"")
must.StrContains(t, ui.OutputWriter.String(), "\"k2\": \"v2\"")
}
func TestVarPutCommand_AutocompleteArgs(t *testing.T) { func TestVarPutCommand_AutocompleteArgs(t *testing.T) {
ci.Parallel(t) ci.Parallel(t)
srv, client, url := testServer(t, true, nil) srv, client, url := testServer(t, true, nil)