From 842a5610c1a5eb0e6407b23f2cc3aa178a28b7e3 Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Thu, 4 Jul 2024 19:00:41 +0200 Subject: [PATCH] fix String method on Options struct to return verbosity properly --- pkg/adhoc/ansibleAdhocOptions.go | 5 +- pkg/adhoc/ansibleAdhocOptions_test.go | 126 +++++++++++++------- pkg/playbook/ansiblePlaybookOptions.go | 5 +- pkg/playbook/ansiblePlaybookOptions_test.go | 28 +++++ 4 files changed, 116 insertions(+), 48 deletions(-) diff --git a/pkg/adhoc/ansibleAdhocOptions.go b/pkg/adhoc/ansibleAdhocOptions.go index e95d8d3..197d75f 100644 --- a/pkg/adhoc/ansibleAdhocOptions.go +++ b/pkg/adhoc/ansibleAdhocOptions.go @@ -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 { diff --git a/pkg/adhoc/ansibleAdhocOptions_test.go b/pkg/adhoc/ansibleAdhocOptions_test.go index 9e55ab2..7a29d50 100644 --- a/pkg/adhoc/ansibleAdhocOptions_test.go +++ b/pkg/adhoc/ansibleAdhocOptions_test.go @@ -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) { diff --git a/pkg/playbook/ansiblePlaybookOptions.go b/pkg/playbook/ansiblePlaybookOptions.go index abb2acb..5737040 100644 --- a/pkg/playbook/ansiblePlaybookOptions.go +++ b/pkg/playbook/ansiblePlaybookOptions.go @@ -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 { diff --git a/pkg/playbook/ansiblePlaybookOptions_test.go b/pkg/playbook/ansiblePlaybookOptions_test.go index 05771ac..b7eb057 100644 --- a/pkg/playbook/ansiblePlaybookOptions_test.go +++ b/pkg/playbook/ansiblePlaybookOptions_test.go @@ -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 {