Skip to content

Commit

Permalink
fix String method on Options struct to return verbosity properly
Browse files Browse the repository at this point in the history
  • Loading branch information
apenella committed Jul 4, 2024
1 parent b65bb63 commit 842a561
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 48 deletions.
5 changes: 3 additions & 2 deletions pkg/adhoc/ansibleAdhocOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ func (o *AnsibleAdhocOptions) String() string {
str = fmt.Sprintf("%s %s %s", str, VaultPasswordFileFlag, o.VaultPasswordFile)
}

if o.Verbose {
str = fmt.Sprintf("%s %s", str, VerboseFlag)
verbosityString, _ := o.generateVerbosityFlag()
if verbosityString != "" {
str = fmt.Sprintf("%s %s", str, verbosityString)
}

if o.Version {
Expand Down
126 changes: 82 additions & 44 deletions pkg/adhoc/ansibleAdhocOptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,54 +197,92 @@ func TestGenerateExtraVarsCommand(t *testing.T) {

func TestAnsibleAdhocOptionsString(t *testing.T) {

t.Log("Testing generate ansible adhoc options string")

options := &AnsibleAdhocOptions{
Args: "args",
AskBecomePass: true,
AskPass: true,
AskVaultPassword: true,
Background: 11,
Become: true,
BecomeMethod: "become-method",
BecomeUser: "become-user",
Check: true,
Connection: "local",
Diff: true,
ExtraVars: map[string]interface{}{
"var1": "value1",
"var2": false,
tests := []struct {
desc string
options *AnsibleAdhocOptions
res string
}{
{
desc: "Testing generate ansible adhoc options string",
options: &AnsibleAdhocOptions{
Args: "args",
AskBecomePass: true,
AskPass: true,
AskVaultPassword: true,
Background: 11,
Become: true,
BecomeMethod: "become-method",
BecomeUser: "become-user",
Check: true,
Connection: "local",
Diff: true,
ExtraVars: map[string]interface{}{
"var1": "value1",
"var2": false,
},
ExtraVarsFile: []string{"@test/ansible/extra_vars.yml"},
Forks: "10",
Inventory: "127.0.0.1,",
Limit: "myhost",
ListHosts: true,
ModuleName: "module-name",
ModulePath: "/dev/null",
OneLine: true,
PlaybookDir: "playbook-dir",
Poll: 12,
PrivateKey: "pk",
SCPExtraArgs: "scp-extra-args",
SFTPExtraArgs: "sftp-extra-args",
SSHCommonArgs: "ssh-common-args",
SSHExtraArgs: "ssh-extra-args",
SyntaxCheck: true,
Timeout: 10,
Tree: "tree",
User: "user",
VaultID: "asdf",
VaultPasswordFile: "/dev/null",
Verbose: true,
Version: true,
},
res: " --args 'args' --ask-vault-password --background 11 --check --diff --extra-vars '{\"var1\":\"value1\",\"var2\":false}' --extra-vars @test/ansible/extra_vars.yml --forks 10 --inventory 127.0.0.1, --limit myhost --list-hosts --module-name module-name --module-path /dev/null --one-line --playbook-dir playbook-dir --poll 12 --syntax-check --tree tree --vault-id asdf --vault-password-file /dev/null -vvvv --version --ask-pass --connection local --private-key pk --scp-extra-args 'scp-extra-args' --sftp-extra-args 'sftp-extra-args' --ssh-common-args 'ssh-common-args' --ssh-extra-args 'ssh-extra-args' --timeout 10 --user user --ask-become-pass --become --become-method become-method --become-user become-user",
},
{
desc: "Testing AnsibleAdhocOptions setting the VerboseV flag as true",
options: &AnsibleAdhocOptions{
VerboseV: true,
},
res: " -v",
},
{
desc: "Testing AnsibleAdhocOptions setting the VerboseVV flag as true",
options: &AnsibleAdhocOptions{
VerboseVV: true,
},
res: " -vv",
},
{
desc: "Testing AnsibleAdhocOptions setting the VerboseVVV flag as true",
options: &AnsibleAdhocOptions{
VerboseVVV: true,
},
res: " -vvv",
},
{
desc: "Testing AnsibleAdhocOptions setting the VerboseVVVV flag as true",
options: &AnsibleAdhocOptions{
VerboseVVVV: true,
},
res: " -vvvv",
},
ExtraVarsFile: []string{"@test/ansible/extra_vars.yml"},
Forks: "10",
Inventory: "127.0.0.1,",
Limit: "myhost",
ListHosts: true,
ModuleName: "module-name",
ModulePath: "/dev/null",
OneLine: true,
PlaybookDir: "playbook-dir",
Poll: 12,
PrivateKey: "pk",
SCPExtraArgs: "scp-extra-args",
SFTPExtraArgs: "sftp-extra-args",
SSHCommonArgs: "ssh-common-args",
SSHExtraArgs: "ssh-extra-args",
SyntaxCheck: true,
Timeout: 10,
Tree: "tree",
User: "user",
VaultID: "asdf",
VaultPasswordFile: "/dev/null",
Verbose: true,
Version: true,
}

cmd := options.String()

expected := " --args 'args' --ask-vault-password --background 11 --check --diff --extra-vars '{\"var1\":\"value1\",\"var2\":false}' --extra-vars @test/ansible/extra_vars.yml --forks 10 --inventory 127.0.0.1, --limit myhost --list-hosts --module-name module-name --module-path /dev/null --one-line --playbook-dir playbook-dir --poll 12 --syntax-check --tree tree --vault-id asdf --vault-password-file /dev/null -vvvv --version --ask-pass --connection local --private-key pk --scp-extra-args 'scp-extra-args' --sftp-extra-args 'sftp-extra-args' --ssh-common-args 'ssh-common-args' --ssh-extra-args 'ssh-extra-args' --timeout 10 --user user --ask-become-pass --become --become-method become-method --become-user become-user"
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := test.options.String()
assert.Equal(t, test.res, res)
})
}

assert.Equal(t, expected, cmd)
}

func TestAddExtraVar(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/playbook/ansiblePlaybookOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,9 @@ func (o *AnsiblePlaybookOptions) String() string {
str = fmt.Sprintf("%s %s %s", str, VaultPasswordFileFlag, o.VaultPasswordFile)
}

if o.Verbose {
str = fmt.Sprintf("%s %s", str, VerboseFlag)
verbosityString, _ := o.generateVerbosityFlag()
if verbosityString != "" {
str = fmt.Sprintf("%s %s", str, verbosityString)
}

if o.Version {
Expand Down
28 changes: 28 additions & 0 deletions pkg/playbook/ansiblePlaybookOptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,34 @@ func TestAnsiblePlaybookOptionsString(t *testing.T) {
},
res: " --ask-vault-password --check --diff --extra-vars '{\"extra\":\"var\"}' --extra-vars @test.yml --flush-cache --force-handlers --forks 10 --inventory inventory --limit limit --list-hosts --list-tags --list-tasks --module-path module-path --skip-tags skip-tags --start-at-task start-at-task --step --syntax-check --tags tags --vault-id vault-ID --vault-password-file vault-password-file -vvvv --version --ask-pass --connection local --private-key private-key --scp-extra-args 'scp-extra-args' --sftp-extra-args 'sftp-extra-args' --ssh-common-args 'ssh-common-args' --ssh-extra-args 'ssh-extra-args' --timeout 11 --user user --ask-become-pass --become --become-method become-method --become-user become-user",
},
{
desc: "Testing AnsiblePlaybookOptions setting the VerboseV flag as true",
ansiblePlaybookOptions: &AnsiblePlaybookOptions{
VerboseV: true,
},
res: " -v",
},
{
desc: "Testing AnsiblePlaybookOptions setting the VerboseVV flag as true",
ansiblePlaybookOptions: &AnsiblePlaybookOptions{
VerboseVV: true,
},
res: " -vv",
},
{
desc: "Testing AnsiblePlaybookOptions setting the VerboseVVV flag as true",
ansiblePlaybookOptions: &AnsiblePlaybookOptions{
VerboseVVV: true,
},
res: " -vvv",
},
{
desc: "Testing AnsiblePlaybookOptions setting the VerboseVVVV flag as true",
ansiblePlaybookOptions: &AnsiblePlaybookOptions{
VerboseVVVV: true,
},
res: " -vvvv",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 842a561

Please sign in to comment.