github.com/AbhinandanKurakure/podman/v3@v3.4.10/pkg/systemd/generate/common_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestFilterPodFlags(t *testing.T) {
    10  	tests := []struct {
    11  		input    []string
    12  		output   []string
    13  		argCount int
    14  	}{
    15  		{
    16  			[]string{"podman", "pod", "create"},
    17  			[]string{"podman", "pod", "create"},
    18  			0,
    19  		},
    20  		{
    21  			[]string{"podman", "pod", "create", "--name", "foo"},
    22  			[]string{"podman", "pod", "create", "--name", "foo"},
    23  			0,
    24  		},
    25  		{
    26  			[]string{"podman", "pod", "create", "--pod-id-file", "foo"},
    27  			[]string{"podman", "pod", "create"},
    28  			0,
    29  		},
    30  		{
    31  			[]string{"podman", "pod", "create", "--pod-id-file=foo"},
    32  			[]string{"podman", "pod", "create"},
    33  			0,
    34  		},
    35  		{
    36  			[]string{"podman", "pod", "create", "--pod-id-file", "foo", "--infra-conmon-pidfile", "foo"},
    37  			[]string{"podman", "pod", "create"},
    38  			0,
    39  		},
    40  		{
    41  			[]string{"podman", "pod", "create", "--pod-id-file", "foo", "--infra-conmon-pidfile=foo"},
    42  			[]string{"podman", "pod", "create"},
    43  			0,
    44  		},
    45  		{
    46  			[]string{"podman", "run", "--pod", "foo"},
    47  			[]string{"podman", "run"},
    48  			0,
    49  		},
    50  		{
    51  			[]string{"podman", "run", "--pod=foo"},
    52  			[]string{"podman", "run"},
    53  			0,
    54  		},
    55  		{
    56  			[]string{"podman", "run", "--pod=foo", "fedora", "podman", "run", "--pod=test", "alpine"},
    57  			[]string{"podman", "run", "fedora", "podman", "run", "--pod=test", "alpine"},
    58  			5,
    59  		},
    60  		{
    61  			[]string{"podman", "run", "--pod", "foo", "fedora", "podman", "run", "--pod", "test", "alpine"},
    62  			[]string{"podman", "run", "fedora", "podman", "run", "--pod", "test", "alpine"},
    63  			6,
    64  		},
    65  		{
    66  			[]string{"podman", "run", "--pod-id-file=foo", "fedora", "podman", "run", "--pod-id-file=test", "alpine"},
    67  			[]string{"podman", "run", "fedora", "podman", "run", "--pod-id-file=test", "alpine"},
    68  			5,
    69  		},
    70  		{
    71  			[]string{"podman", "run", "--pod-id-file", "foo", "fedora", "podman", "run", "--pod-id-file", "test", "alpine"},
    72  			[]string{"podman", "run", "fedora", "podman", "run", "--pod-id-file", "test", "alpine"},
    73  			6,
    74  		},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		processed := filterPodFlags(test.input, test.argCount)
    79  		assert.Equal(t, test.output, processed)
    80  	}
    81  }
    82  
    83  func TestFilterCommonContainerFlags(t *testing.T) {
    84  	tests := []struct {
    85  		input    []string
    86  		output   []string
    87  		argCount int
    88  	}{
    89  		{
    90  			[]string{"podman", "run", "alpine"},
    91  			[]string{"podman", "run", "alpine"},
    92  			1,
    93  		},
    94  		{
    95  			[]string{"podman", "run", "--conmon-pidfile", "foo", "alpine"},
    96  			[]string{"podman", "run", "--conmon-pidfile", "foo", "alpine"},
    97  			1,
    98  		},
    99  		{
   100  			[]string{"podman", "run", "--conmon-pidfile=foo", "alpine"},
   101  			[]string{"podman", "run", "--conmon-pidfile=foo", "alpine"},
   102  			1,
   103  		},
   104  		{
   105  			[]string{"podman", "run", "--cidfile", "foo", "alpine"},
   106  			[]string{"podman", "run", "alpine"},
   107  			1,
   108  		},
   109  		{
   110  			[]string{"podman", "run", "--cidfile=foo", "alpine"},
   111  			[]string{"podman", "run", "alpine"},
   112  			1,
   113  		},
   114  		{
   115  			[]string{"podman", "run", "--cgroups", "foo", "alpine"},
   116  			[]string{"podman", "run", "alpine"},
   117  			1,
   118  		},
   119  		{
   120  			[]string{"podman", "run", "--cgroups=foo", "--restart=foo", "alpine"},
   121  			[]string{"podman", "run", "alpine"},
   122  			1,
   123  		},
   124  		{
   125  			[]string{"podman", "run", "--cgroups=foo", "--rm", "--restart", "foo", "alpine"},
   126  			[]string{"podman", "run", "alpine"},
   127  			1,
   128  		},
   129  		{
   130  			[]string{"podman", "run", "--cgroups", "--rm=bogus", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo", "--rm"},
   131  			[]string{"podman", "run", "alpine", "--cgroups", "foo", "--conmon-pidfile", "foo", "--cidfile", "foo", "--rm"},
   132  			7,
   133  		},
   134  	}
   135  
   136  	for _, test := range tests {
   137  		processed := filterCommonContainerFlags(test.input, test.argCount)
   138  		assert.Equal(t, test.output, processed)
   139  	}
   140  }
   141  
   142  func TestEscapeSystemdArguments(t *testing.T) {
   143  	tests := []struct {
   144  		input  []string
   145  		output []string
   146  	}{
   147  		{
   148  			[]string{"foo", "bar=\"arg\""},
   149  			[]string{"foo", "bar=\"arg\""},
   150  		},
   151  		{
   152  			[]string{"foo", "bar=\"arg with space\""},
   153  			[]string{"foo", "\"bar=\\\"arg with space\\\"\""},
   154  		},
   155  		{
   156  			[]string{"foo", "bar=\"arg with\ttab\""},
   157  			[]string{"foo", "\"bar=\\\"arg with\\ttab\\\"\""},
   158  		},
   159  		{
   160  			[]string{"$"},
   161  			[]string{"$$"},
   162  		},
   163  		{
   164  			[]string{"foo", "command with dollar sign $"},
   165  			[]string{"foo", "\"command with dollar sign $$\""},
   166  		},
   167  		{
   168  			[]string{"foo", "command with two dollar signs $$"},
   169  			[]string{"foo", "\"command with two dollar signs $$$$\""},
   170  		},
   171  		{
   172  			[]string{"%"},
   173  			[]string{"%%"},
   174  		},
   175  		{
   176  			[]string{"foo", "command with percent sign %"},
   177  			[]string{"foo", "\"command with percent sign %%\""},
   178  		},
   179  		{
   180  			[]string{"foo", "command with two percent signs %%"},
   181  			[]string{"foo", "\"command with two percent signs %%%%\""},
   182  		},
   183  		{
   184  			[]string{`\`},
   185  			[]string{`\\`},
   186  		},
   187  		{
   188  			[]string{"foo", `command with backslash \`},
   189  			[]string{"foo", `"command with backslash \\"`},
   190  		},
   191  		{
   192  			[]string{"foo", `command with two backslashes \\`},
   193  			[]string{"foo", `"command with two backslashes \\\\"`},
   194  		},
   195  	}
   196  
   197  	for _, test := range tests {
   198  		quoted := escapeSystemdArguments(test.input)
   199  		assert.Equal(t, test.output, quoted)
   200  	}
   201  }