Read custom transfer configurations & test

This commit is contained in:
Steve Streeting 2016-07-08 12:46:04 +01:00
parent 5fe21cc9ed
commit 357703e2b3
2 changed files with 240 additions and 0 deletions

75
transfer/custom.go Normal file

@ -0,0 +1,75 @@
package transfer
import (
"fmt"
"regexp"
"strings"
"github.com/github/git-lfs/config"
)
// Adapter for custom transfer via external process
type customAdapter struct {
*adapterBase
path string
args string
concurrent bool
}
func (a *customAdapter) ClearTempStorage() error {
// no action requred
return nil
}
func (a *customAdapter) DoTransfer(t *Transfer, cb TransferProgressCallback, authOkFunc func()) error {
// TODO
return nil
}
func newCustomAdapter(name string, dir Direction, path, args string, concurrent bool) *customAdapter {
c := &customAdapter{newAdapterBase(name, dir, nil), path, args, concurrent}
// self implements impl
c.transferImpl = c
return c
}
// Initialise custom adapters based on current config
func ConfigureCustomAdapters() {
pathRegex := regexp.MustCompile(`lfs.customtransfer.([^.]+).path`)
for k, v := range config.Config.AllGitConfig() {
if match := pathRegex.FindStringSubmatch(k); match != nil {
name := match[1]
path := v
var args string
var concurrent bool
var direction string
// retrieve other values
args, _ = config.Config.GitConfig(fmt.Sprintf("lfs.customtransfer.%s.args", name))
concurrent = config.Config.GitConfigBool(fmt.Sprintf("lfs.customtransfer.%s.concurrent", name), true)
direction, _ = config.Config.GitConfig(fmt.Sprintf("lfs.customtransfer.%s.direction", name))
if len(direction) == 0 {
direction = "both"
} else {
direction = strings.ToLower(direction)
}
// Separate closure for each since we need to capture vars above
newfunc := func(name string, dir Direction) TransferAdapter {
return newCustomAdapter(name, dir, path, args, concurrent)
}
if direction == "download" || direction == "both" {
RegisterNewTransferAdapterFunc(name, Download, newfunc)
}
if direction == "upload" || direction == "both" {
RegisterNewTransferAdapterFunc(name, Upload, newfunc)
}
}
}
}
func init() {
ConfigureCustomAdapters()
}

165
transfer/custom_test.go Normal file

@ -0,0 +1,165 @@
package transfer
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/github/git-lfs/config"
)
var (
savedDownloadAdapterFuncs map[string]NewTransferAdapterFunc
savedUploadAdapterFuncs map[string]NewTransferAdapterFunc
)
func copyFuncMap(to, from map[string]NewTransferAdapterFunc) {
for k, v := range from {
to[k] = v
}
}
func saveTransferSetupState() {
funcMutex.Lock()
defer funcMutex.Unlock()
savedDownloadAdapterFuncs = make(map[string]NewTransferAdapterFunc)
copyFuncMap(savedDownloadAdapterFuncs, downloadAdapterFuncs)
savedUploadAdapterFuncs = make(map[string]NewTransferAdapterFunc)
copyFuncMap(savedUploadAdapterFuncs, uploadAdapterFuncs)
}
func restoreTransferSetupState() {
funcMutex.Lock()
defer funcMutex.Unlock()
downloadAdapterFuncs = make(map[string]NewTransferAdapterFunc)
copyFuncMap(downloadAdapterFuncs, savedDownloadAdapterFuncs)
uploadAdapterFuncs = make(map[string]NewTransferAdapterFunc)
copyFuncMap(uploadAdapterFuncs, savedUploadAdapterFuncs)
}
func TestCustomTransferBasicConfig(t *testing.T) {
saveTransferSetupState()
defer func() {
config.Config.ResetConfig()
restoreTransferSetupState()
}()
path := "/path/to/binary"
config.Config.SetConfig("lfs.customtransfer.testsimple.path", path)
ConfigureCustomAdapters()
u := NewUploadAdapter("testsimple")
assert.NotNil(t, u, "Upload adapter should be present")
cu, _ := u.(*customAdapter)
assert.NotNil(t, cu, "Upload adapter should be customAdapter")
assert.Equal(t, cu.path, path, "Path should be correct")
assert.Equal(t, cu.args, "", "args should be blank")
assert.Equal(t, cu.concurrent, true, "concurrent should be defaulted")
d := NewDownloadAdapter("testsimple")
assert.NotNil(t, d, "Download adapter should be present")
cd, _ := u.(*customAdapter)
assert.NotNil(t, cd, "Download adapter should be customAdapter")
assert.Equal(t, cd.path, path, "Path should be correct")
assert.Equal(t, cd.args, "", "args should be blank")
assert.Equal(t, cd.concurrent, true, "concurrent should be defaulted")
}
func TestCustomTransferDownloadConfig(t *testing.T) {
saveTransferSetupState()
defer func() {
config.Config.ResetConfig()
restoreTransferSetupState()
}()
path := "/path/to/binary"
args := "-c 1 --whatever"
config.Config.SetConfig("lfs.customtransfer.testdownload.path", path)
config.Config.SetConfig("lfs.customtransfer.testdownload.args", args)
config.Config.SetConfig("lfs.customtransfer.testdownload.concurrent", "false")
config.Config.SetConfig("lfs.customtransfer.testdownload.direction", "download")
ConfigureCustomAdapters()
u := NewUploadAdapter("testdownload")
assert.NotNil(t, u, "Upload adapter should always be created")
cu, _ := u.(*customAdapter)
assert.Nil(t, cu, "Upload adapter should NOT be custom (default to basic)")
d := NewDownloadAdapter("testdownload")
assert.NotNil(t, d, "Download adapter should be present")
cd, _ := d.(*customAdapter)
assert.NotNil(t, cd, "Download adapter should be customAdapter")
assert.Equal(t, cd.path, path, "Path should be correct")
assert.Equal(t, cd.args, args, "args should be correct")
assert.Equal(t, cd.concurrent, false, "concurrent should be set")
}
func TestCustomTransferUploadConfig(t *testing.T) {
saveTransferSetupState()
defer func() {
config.Config.ResetConfig()
restoreTransferSetupState()
}()
path := "/path/to/binary"
args := "-c 1 --whatever"
config.Config.SetConfig("lfs.customtransfer.testupload.path", path)
config.Config.SetConfig("lfs.customtransfer.testupload.args", args)
config.Config.SetConfig("lfs.customtransfer.testupload.concurrent", "false")
config.Config.SetConfig("lfs.customtransfer.testupload.direction", "upload")
ConfigureCustomAdapters()
d := NewDownloadAdapter("testupload")
assert.NotNil(t, d, "Download adapter should always be created")
cd, _ := d.(*customAdapter)
assert.Nil(t, cd, "Download adapter should NOT be custom (default to basic)")
u := NewUploadAdapter("testupload")
assert.NotNil(t, u, "Upload adapter should be present")
cu, _ := u.(*customAdapter)
assert.NotNil(t, cu, "Upload adapter should be customAdapter")
assert.Equal(t, cu.path, path, "Path should be correct")
assert.Equal(t, cu.args, args, "args should be correct")
assert.Equal(t, cu.concurrent, false, "concurrent should be set")
}
func TestCustomTransferBothConfig(t *testing.T) {
saveTransferSetupState()
defer func() {
config.Config.ResetConfig()
restoreTransferSetupState()
}()
path := "/path/to/binary"
args := "-c 1 --whatever --yeah"
config.Config.SetConfig("lfs.customtransfer.testboth.path", path)
config.Config.SetConfig("lfs.customtransfer.testboth.args", args)
config.Config.SetConfig("lfs.customtransfer.testboth.concurrent", "yes")
config.Config.SetConfig("lfs.customtransfer.testboth.direction", "both")
ConfigureCustomAdapters()
d := NewDownloadAdapter("testboth")
assert.NotNil(t, d, "Download adapter should be present")
cd, _ := d.(*customAdapter)
assert.NotNil(t, cd, "Download adapter should be customAdapter")
assert.Equal(t, cd.path, path, "Path should be correct")
assert.Equal(t, cd.args, args, "args should be correct")
assert.Equal(t, cd.concurrent, true, "concurrent should be set")
u := NewUploadAdapter("testboth")
assert.NotNil(t, u, "Upload adapter should be present")
cu, _ := u.(*customAdapter)
assert.NotNil(t, cu, "Upload adapter should be customAdapter")
assert.Equal(t, cu.path, path, "Path should be correct")
assert.Equal(t, cu.args, args, "args should be correct")
assert.Equal(t, cu.concurrent, true, "concurrent should be set")
}