gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/common/config_test.go (about) 1 package common 2 3 import ( 4 "bytes" 5 "os" 6 "testing" 7 8 "github.com/BurntSushi/toml" 9 "github.com/sirupsen/logrus/hooks/test" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "github.com/urfave/cli" 13 "gitlab.com/ayufan/golang-cli-helpers" 14 ) 15 16 func getLogrusOutput(t *testing.T, hook *test.Hook) string { 17 buf := &bytes.Buffer{} 18 for _, entry := range hook.AllEntries() { 19 message, err := entry.String() 20 require.NoError(t, err) 21 buf.WriteString(message) 22 } 23 24 return buf.String() 25 } 26 27 // TODO: Remove in 12.0 28 func TestCacheConfig_DeprecatedSupport_TOML(t *testing.T) { 29 hook := test.NewGlobal() 30 content := ` 31 [cache] 32 ServerAddress = "server_address" 33 AccessKey = "access_key" 34 SecretKey = "secret_key" 35 BucketName = "bucket_name" 36 BucketLocation = "bucket_location" 37 Insecure = true 38 ` 39 test := struct { 40 Cache CacheConfig `toml:"cache,omitempty"` 41 }{} 42 43 _, err := toml.Decode(content, &test) 44 require.NoError(t, err) 45 46 assert.Equal(t, "server_address", test.Cache.GetServerAddress()) 47 assert.Equal(t, "access_key", test.Cache.GetAccessKey()) 48 assert.Equal(t, "secret_key", test.Cache.GetSecretKey()) 49 assert.Equal(t, "bucket_name", test.Cache.GetBucketName()) 50 assert.Equal(t, "bucket_location", test.Cache.GetBucketLocation()) 51 assert.True(t, test.Cache.GetInsecure()) 52 53 output := getLogrusOutput(t, hook) 54 assert.Contains(t, output, "[runners.cache] ServerAddress setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] ServerAddress instead") 55 assert.Contains(t, output, "[runners.cache] AccessKey setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] AccessKey instead") 56 assert.Contains(t, output, "[runners.cache] SecretKey setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] SecretKey instead") 57 assert.Contains(t, output, "[runners.cache] BucketName setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] BucketName instead") 58 assert.Contains(t, output, "[runners.cache] BucketLocation setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] BucketLocation instead") 59 assert.Contains(t, output, "[runners.cache] Insecure setting is deprecated and will be removed in GitLab Runner 12.0. Please use [runners.cache.s3] Insecure instead") 60 } 61 62 type cacheConfigDeprecatedCommand struct { 63 Cache CacheConfig `namespace:"cache"` 64 65 test func(c *cacheConfigDeprecatedCommand) 66 } 67 68 func (c *cacheConfigDeprecatedCommand) Execute(cliCtx *cli.Context) { 69 c.test(c) 70 } 71 72 func testCacheConfigDeprecatedSupportRunCommand(test func(c *cacheConfigDeprecatedCommand), args ...string) { 73 cmd := &cacheConfigDeprecatedCommand{test: test} 74 75 app := cli.NewApp() 76 app.Commands = []cli.Command{ 77 { 78 Name: "test", 79 Action: cmd.Execute, 80 Flags: clihelpers.GetFlagsFromStruct(cmd), 81 }, 82 } 83 84 args = append([]string{"binary", "test"}, args...) 85 app.Run(args) 86 } 87 88 // TODO: Remove in 12.0 89 func TestCacheConfig_DeprecatedSupport_CliOptions(t *testing.T) { 90 hook := test.NewGlobal() 91 test := func(c *cacheConfigDeprecatedCommand) { 92 assert.Equal(t, "test_path", c.Cache.GetPath()) 93 assert.True(t, c.Cache.GetShared()) 94 } 95 96 testCacheConfigDeprecatedSupportRunCommand(test, "--cache-s3-cache-path", "test_path", "--cache-cache-shared") 97 98 output := getLogrusOutput(t, hook) 99 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") 100 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") 101 } 102 103 func mockEnv(t *testing.T, key string, value string) func() { 104 err := os.Setenv(key, value) 105 require.NoError(t, err, "Variable %q not set properly", key) 106 107 return func() { 108 err := os.Unsetenv(key) 109 assert.NoError(t, err, "Variable %q not unset properly", key) 110 } 111 } 112 113 // TODO: Remove in 12.0 114 func TestCacheConfig_DeprecatedSupport_EnvVariables(t *testing.T) { 115 hook := test.NewGlobal() 116 test := func(c *cacheConfigDeprecatedCommand) { 117 assert.Equal(t, "test_path", c.Cache.GetPath()) 118 assert.Equal(t, "server_address", c.Cache.GetServerAddress()) 119 assert.Equal(t, "access_key", c.Cache.GetAccessKey()) 120 assert.Equal(t, "secret_key", c.Cache.GetSecretKey()) 121 assert.Equal(t, "bucket_name", c.Cache.GetBucketName()) 122 assert.Equal(t, "bucket_location", c.Cache.GetBucketLocation()) 123 assert.True(t, c.Cache.GetInsecure()) 124 } 125 126 defer mockEnv(t, "S3_CACHE_PATH", "test_path")() 127 defer mockEnv(t, "S3_SERVER_ADDRESS", "server_address")() 128 defer mockEnv(t, "S3_ACCESS_KEY", "access_key")() 129 defer mockEnv(t, "S3_SECRET_KEY", "secret_key")() 130 defer mockEnv(t, "S3_BUCKET_NAME", "bucket_name")() 131 defer mockEnv(t, "S3_BUCKET_LOCATION", "bucket_location")() 132 defer mockEnv(t, "S3_CACHE_INSECURE", "1")() 133 134 testCacheConfigDeprecatedSupportRunCommand(test) 135 136 output := getLogrusOutput(t, hook) 137 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") 138 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") 139 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") 140 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") 141 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") 142 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") 143 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") 144 }