Commit 7546b65e authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch...

Merge branch '3755-helper-image-for-windows-container-execution-bash-gitalb-runner-cache' into 'master'

Migrate cache bash script to Go for helper image

See merge request gitlab-org/gitlab-runner!1201
parents 37667b07 617995a2
Pipeline #49097243 passed with stages
in 26 minutes and 35 seconds
package helpers
import (
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
)
// CacheInitCommand will take a single directory/file path and initialize it
// correctly for it to be used for cache. This command tries to support spaces
// in directories name by using the the flags to specify which entries you want
// to initialize.
type CacheInitCommand struct{}
func (c *CacheInitCommand) Execute(ctx *cli.Context) {
if ctx.NArg() == 0 {
logrus.Fatal("No arguments passed, at least 1 path is required.")
}
for _, path := range ctx.Args() {
err := os.Chmod(path, os.ModePerm)
if err != nil {
logrus.WithError(err).Error("failed to chmod path")
}
}
}
func init() {
common.RegisterCommand2("cache-init", "changed permissions for cache paths (internal)", &CacheInitCommand{})
}
package helpers
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
func newCacheInitTestApp() *cli.App {
cmd := &CacheInitCommand{}
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Commands = append(app.Commands, cli.Command{
Name: "cache-init",
Action: cmd.Execute,
})
return app
}
func TestCacheInit(t *testing.T) {
// Specifically test a file name with spaces.
file, err := ioutil.TempFile("", "Test Cache Chmod")
require.NoError(t, err)
file.Close()
defer os.Remove(file.Name())
// Make sure that the mode is not the expect 0777.
err = os.Chmod(file.Name(), 0600)
require.NoError(t, err)
// Start a new cli with the arguments for the command.
args := os.Args[0:1]
args = append(args, "cache-init", file.Name())
err = newCacheInitTestApp().Run(args)
require.NoError(t, err)
info, err := os.Stat(file.Name())
require.NoError(t, err)
assert.Equal(t, os.ModePerm, info.Mode())
}
func TestCacheInit_NoArguments(t *testing.T) {
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
args := os.Args[0:1]
args = append(args, "cache-init")
assert.Panics(t, func() {
newCacheInitTestApp().Run(args)
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment