implement volume mount for container job (#1101)
* implement volume mount for container job * Update pkg/runner/run_context.go Co-authored-by: Ryan <me@hackerc.at> * add tests for container volume mount options * remove unused code * prefer if-else instead of if-continue * remove continue * add some tests Co-authored-by: Ryan <me@hackerc.at> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
@@ -102,6 +102,21 @@ func (rc *RunContext) GetBindsAndMounts() ([]string, map[string]string) {
|
||||
name + "-env": ActPath,
|
||||
}
|
||||
|
||||
if job := rc.Run.Job(); job != nil {
|
||||
if container := job.Container(); container != nil {
|
||||
for _, v := range container.Volumes {
|
||||
if !strings.Contains(v, ":") || filepath.IsAbs(v) {
|
||||
// Bind anonymous volume or host file.
|
||||
binds = append(binds, v)
|
||||
} else {
|
||||
// Mount existing volume.
|
||||
paths := strings.SplitN(v, ":", 2)
|
||||
mounts[paths[0]] = paths[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if rc.Config.BindWorkdir {
|
||||
bindModifiers := ""
|
||||
if runtime.GOOS == "darwin" {
|
||||
|
@@ -293,6 +293,44 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("ContainerVolumeMountTest", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
volumes []string
|
||||
wantbind string
|
||||
wantmount map[string]string
|
||||
}{
|
||||
{"BindAnonymousVolume", []string{"/volume"}, "/volume", map[string]string{}},
|
||||
{"BindHostFile", []string{"/path/to/file/on/host:/volume"}, "/path/to/file/on/host:/volume", map[string]string{}},
|
||||
{"MountExistingVolume", []string{"volume-id:/volume"}, "", map[string]string{"volume-id": "/volume"}},
|
||||
}
|
||||
|
||||
for _, testcase := range tests {
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
job := &model.Job{}
|
||||
err := job.RawContainer.Encode(map[string][]string{
|
||||
"volumes": testcase.volumes,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
rc := rctemplate.Clone()
|
||||
rc.Run.JobID = "job1"
|
||||
rc.Run.Workflow.Jobs = map[string]*model.Job{"job1": job}
|
||||
|
||||
gotbind, gotmount := rc.GetBindsAndMounts()
|
||||
|
||||
if len(testcase.wantbind) > 0 {
|
||||
assert.Contains(t, gotbind, testcase.wantbind)
|
||||
}
|
||||
|
||||
for k, v := range testcase.wantmount {
|
||||
assert.Contains(t, gotmount, k)
|
||||
assert.Equal(t, gotmount[k], v)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetGitHubContext(t *testing.T) {
|
||||
|
@@ -141,6 +141,7 @@ func TestRunEvent(t *testing.T) {
|
||||
{"testdata", "evalmatrix-merge-array", "push", "", platforms, ""},
|
||||
{"../model/testdata", "strategy", "push", "", platforms, ""}, // TODO: move all testdata into pkg so we can validate it with planner and runner
|
||||
// {"testdata", "issue-228", "push", "", platforms, ""}, // TODO [igni]: Remove this once everything passes
|
||||
{"../model/testdata", "container-volumes", "push", "", platforms, ""},
|
||||
|
||||
// single test for different architecture: linux/arm64
|
||||
{"testdata", "basic", "push", "", platforms, "linux/arm64"},
|
||||
|
Reference in New Issue
Block a user