gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/commands/register_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/sirupsen/logrus/hooks/test"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  	"github.com/stretchr/testify/require"
    14  	"github.com/urfave/cli"
    15  	"gitlab.com/ayufan/golang-cli-helpers"
    16  
    17  	"gitlab.com/gitlab-org/gitlab-runner/common"
    18  )
    19  
    20  func setupDockerRegisterCommand(dockerConfig *common.DockerConfig) *RegisterCommand {
    21  	fs := flag.NewFlagSet("", flag.ExitOnError)
    22  	ctx := cli.NewContext(cli.NewApp(), fs, nil)
    23  	fs.String("docker-image", "ruby:2.1", "")
    24  
    25  	s := &RegisterCommand{
    26  		context:        ctx,
    27  		NonInteractive: true,
    28  	}
    29  	s.Docker = dockerConfig
    30  
    31  	return s
    32  }
    33  
    34  func TestRegisterDefaultDockerCacheVolume(t *testing.T) {
    35  	s := setupDockerRegisterCommand(&common.DockerConfig{
    36  		Volumes: []string{},
    37  	})
    38  
    39  	s.askDocker()
    40  
    41  	assert.Equal(t, 1, len(s.Docker.Volumes))
    42  	assert.Equal(t, "/cache", s.Docker.Volumes[0])
    43  }
    44  
    45  func TestRegisterCustomDockerCacheVolume(t *testing.T) {
    46  	s := setupDockerRegisterCommand(&common.DockerConfig{
    47  		Volumes: []string{"/cache"},
    48  	})
    49  
    50  	s.askDocker()
    51  
    52  	assert.Equal(t, 1, len(s.Docker.Volumes))
    53  	assert.Equal(t, "/cache", s.Docker.Volumes[0])
    54  }
    55  
    56  func TestRegisterCustomMappedDockerCacheVolume(t *testing.T) {
    57  	s := setupDockerRegisterCommand(&common.DockerConfig{
    58  		Volumes: []string{"/my/cache:/cache"},
    59  	})
    60  
    61  	s.askDocker()
    62  
    63  	assert.Equal(t, 1, len(s.Docker.Volumes))
    64  	assert.Equal(t, "/my/cache:/cache", s.Docker.Volumes[0])
    65  }
    66  
    67  func getLogrusOutput(t *testing.T, hook *test.Hook) string {
    68  	buf := &bytes.Buffer{}
    69  	for _, entry := range hook.AllEntries() {
    70  		message, err := entry.String()
    71  		require.NoError(t, err)
    72  		buf.WriteString(message)
    73  	}
    74  
    75  	return buf.String()
    76  }
    77  
    78  func mockEnv(t *testing.T, key string, value string) func() {
    79  	err := os.Setenv(key, value)
    80  	require.NoError(t, err, "Variable %q not set properly", key)
    81  
    82  	return func() {
    83  		err := os.Unsetenv(key)
    84  		assert.NoError(t, err, "Variable %q not unset properly", key)
    85  	}
    86  }
    87  
    88  func testRegisterCommandRun(t *testing.T, network common.Network, args ...string) {
    89  	cmd := newRegisterCommand()
    90  	cmd.network = network
    91  
    92  	app := cli.NewApp()
    93  	app.Commands = []cli.Command{
    94  		{
    95  			Name:   "register",
    96  			Action: cmd.Execute,
    97  			Flags:  clihelpers.GetFlagsFromStruct(cmd),
    98  		},
    99  	}
   100  
   101  	args = append([]string{"binary", "register"}, args...)
   102  	app.Run(args)
   103  }
   104  
   105  func testRegisterCommandDeprecatedOptions(t *testing.T, args ...string) (string, string) {
   106  	hook := test.NewGlobal()
   107  
   108  	network := new(common.MockNetwork)
   109  	defer network.AssertExpectations(t)
   110  
   111  	network.On("RegisterRunner", mock.Anything, mock.Anything).Once().Return(&common.RegisterRunnerResponse{
   112  		Token: "test-runner-token",
   113  	})
   114  
   115  	configFile, err := ioutil.TempFile("", "config.toml")
   116  	require.NoError(t, err)
   117  
   118  	configFile.Close()
   119  	defer os.Remove(configFile.Name())
   120  
   121  	arguments := []string{
   122  		"-n",
   123  		"--config", configFile.Name(),
   124  		"--url", "http://gitlab.example.com/",
   125  		"--registration-token", "test-registration-token",
   126  		"--executor", "shell",
   127  		"--cache-type", "s3",
   128  	}
   129  	arguments = append(arguments, args...)
   130  
   131  	testRegisterCommandRun(t, network, arguments...)
   132  
   133  	content, err := ioutil.ReadFile(configFile.Name())
   134  	require.NoError(t, err)
   135  
   136  	return string(content), getLogrusOutput(t, hook)
   137  }
   138  
   139  // TODO: Remove in 12.0
   140  func TestRegisterCacheDeprecatedOptions_CLIOptions(t *testing.T) {
   141  	content, output := testRegisterCommandDeprecatedOptions(
   142  		t,
   143  		"--cache-s3-cache-path", "test_path",
   144  		"--cache-cache-shared",
   145  	)
   146  
   147  	assert.Contains(t, content, `
   148    [runners.cache]
   149      Type = "s3"
   150      Path = "test_path"
   151      Shared = true
   152  `)
   153  
   154  	assert.Contains(t, output, "'--cache-s3-cache-path' command line option and `$S3_CACHE_PATH` environment variables are deprecated and will be removed in GitLab Runner 12.0. Please use '--cache-path' or '$CACHE_PATH' instead")
   155  	assert.Contains(t, output, "'--cache-cache-shared' command line is deprecated and will be removed in GitLab Runner 12.0. Please use '--cache-shared' instead")
   156  }
   157  
   158  // TODO: Remove in 12.0
   159  func TestRegisterCacheDeprecatedOptions_EnvVariables(t *testing.T) {
   160  	defer mockEnv(t, "S3_CACHE_PATH", "test_path")()
   161  	defer mockEnv(t, "S3_SERVER_ADDRESS", "server_address")()
   162  	defer mockEnv(t, "S3_ACCESS_KEY", "access_key")()
   163  	defer mockEnv(t, "S3_SECRET_KEY", "secret_key")()
   164  	defer mockEnv(t, "S3_BUCKET_NAME", "bucket_name")()
   165  	defer mockEnv(t, "S3_BUCKET_LOCATION", "bucket_location")()
   166  	defer mockEnv(t, "S3_CACHE_INSECURE", "1")()
   167  
   168  	content, output := testRegisterCommandDeprecatedOptions(t)
   169  
   170  	assert.Contains(t, content, `
   171    [runners.cache]
   172      Type = "s3"
   173      Path = "test_path"
   174      [runners.cache.s3]
   175        ServerAddress = "server_address"
   176        AccessKey = "access_key"
   177        SecretKey = "secret_key"
   178        BucketName = "bucket_name"
   179        BucketLocation = "bucket_location"
   180        Insecure = true
   181      [runners.cache.gcs]
   182  `)
   183  
   184  	assert.Contains(t, output, "'--cache-s3-cache-path' command line option and `$S3_CACHE_PATH` environment variables are deprecated and will be removed in GitLab Runner 12.0. Please use '--cache-path' or '$CACHE_PATH' instead")
   185  	assert.Contains(t, output, "S3_SERVER_ADDRESS environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_SERVER_ADDRESS instead")
   186  	assert.Contains(t, output, "S3_ACCESS_KEY environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_ACCESS_KEY instead")
   187  	assert.Contains(t, output, "S3_SECRET_KEY environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_SECRET_KEY instead")
   188  	assert.Contains(t, output, "S3_BUCKET_NAME environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_BUCKET_NAME instead")
   189  	assert.Contains(t, output, "S3_BUCKET_LOCATION environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_BUCKET_LOCATION instead")
   190  	assert.Contains(t, output, "S3_CACHE_INSECURE environment variables is deprecated and will be removed in GitLab Runner 12.0. Please use CACHE_S3_INSECURE instead")
   191  }