github.com/secure-build/gitlab-runner@v12.5.0+incompatible/commands/service_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/urfave/cli"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    11  )
    12  
    13  func newTestGetServiceArgumentsCommand(t *testing.T, expectedArgs []string) func(*cli.Context) {
    14  	return func(c *cli.Context) {
    15  		arguments := getServiceArguments(c)
    16  
    17  		for _, arg := range expectedArgs {
    18  			assert.Contains(t, arguments, arg)
    19  		}
    20  	}
    21  }
    22  
    23  func testServiceCommandRun(t *testing.T, command func(*cli.Context), args ...string) {
    24  	app := cli.NewApp()
    25  	app.Commands = []cli.Command{
    26  		{
    27  			Name:   "test-command",
    28  			Action: command,
    29  			Flags:  getInstallFlags(),
    30  		},
    31  	}
    32  
    33  	args = append([]string{"binary", "test-command"}, args...)
    34  	app.Run(args)
    35  }
    36  
    37  type getServiceArgumentsTestCase struct {
    38  	cliFlags     []string
    39  	expectedArgs []string
    40  }
    41  
    42  func TestGetServiceArguments(t *testing.T) {
    43  	tests := []getServiceArgumentsTestCase{
    44  		{
    45  			expectedArgs: []string{
    46  				"--working-directory", helpers.GetCurrentWorkingDirectory(),
    47  				"--config", getDefaultConfigFile(),
    48  				"--service", "gitlab-runner",
    49  				"--syslog",
    50  			},
    51  		},
    52  		{
    53  			cliFlags: []string{
    54  				"--config", "/tmp/config.toml",
    55  			},
    56  			expectedArgs: []string{
    57  				"--working-directory", helpers.GetCurrentWorkingDirectory(),
    58  				"--config", "/tmp/config.toml",
    59  				"--service", "gitlab-runner",
    60  				"--syslog",
    61  			},
    62  		},
    63  		{
    64  			cliFlags: []string{
    65  				"--working-directory", "/tmp",
    66  			},
    67  			expectedArgs: []string{
    68  				"--working-directory", "/tmp",
    69  				"--config", getDefaultConfigFile(),
    70  				"--service", "gitlab-runner",
    71  				"--syslog",
    72  			},
    73  		},
    74  		{
    75  			cliFlags: []string{
    76  				"--service", "gitlab-runner-service-name",
    77  			},
    78  			expectedArgs: []string{
    79  				"--working-directory", helpers.GetCurrentWorkingDirectory(),
    80  				"--config", getDefaultConfigFile(),
    81  				"--service", "gitlab-runner-service-name",
    82  				"--syslog",
    83  			},
    84  		},
    85  		{
    86  			cliFlags: []string{
    87  				"--syslog=true",
    88  			},
    89  			expectedArgs: []string{
    90  				"--working-directory", helpers.GetCurrentWorkingDirectory(),
    91  				"--config", getDefaultConfigFile(),
    92  				"--service", "gitlab-runner",
    93  				"--syslog",
    94  			},
    95  		},
    96  		{
    97  			cliFlags: []string{
    98  				"--syslog=false",
    99  			},
   100  			expectedArgs: []string{
   101  				"--working-directory", helpers.GetCurrentWorkingDirectory(),
   102  				"--config", getDefaultConfigFile(),
   103  				"--service", "gitlab-runner",
   104  			},
   105  		},
   106  	}
   107  
   108  	for id, testCase := range tests {
   109  		t.Run(fmt.Sprintf("case-%d", id), func(t *testing.T) {
   110  			testServiceCommandRun(t, newTestGetServiceArgumentsCommand(t, testCase.expectedArgs), testCase.cliFlags...)
   111  		})
   112  	}
   113  }