Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit ba52e9b

Browse files
committed
206-doublestar-mount Fixed test cases, added config_test
1 parent ec29e86 commit ba52e9b

File tree

4 files changed

+135
-32
lines changed

4 files changed

+135
-32
lines changed

internal/plugin/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package plugin
22

33
import (
4+
"fmt"
5+
"strings"
46
"time"
57

8+
"github.com/bmatcuk/doublestar"
69
"github.com/meltwater/drone-cache/storage/backend/azure"
710
"github.com/meltwater/drone-cache/storage/backend/filesystem"
811
"github.com/meltwater/drone-cache/storage/backend/gcs"
@@ -38,3 +41,25 @@ type Config struct {
3841
Azure azure.Config
3942
GCS gcs.Config
4043
}
44+
45+
func (c *Config) HandleMount() error {
46+
mountLen := len(c.Mount)
47+
if mountLen > 0 {
48+
for i, mount := range c.Mount {
49+
if strings.Contains(mount, "**") {
50+
// Remove the glob from the original mount list
51+
c.Mount[i] = c.Mount[mountLen-1]
52+
c.Mount = c.Mount[:mountLen-1]
53+
54+
globMounts, err := doublestar.Glob(mount)
55+
if err != nil {
56+
return fmt.Errorf("glob handle mount error <%s>, %w", mount, err)
57+
}
58+
59+
c.Mount = append(c.Mount, globMounts...)
60+
}
61+
}
62+
}
63+
64+
return nil
65+
}

internal/plugin/config_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package plugin
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/meltwater/drone-cache/archive"
12+
"github.com/meltwater/drone-cache/test"
13+
)
14+
15+
const (
16+
testRoot = "testdata"
17+
defaultStorageOperationTimeout = 5 * time.Second
18+
)
19+
20+
func TestHandleMount(t *testing.T) {
21+
test.Ok(t, os.Mkdir(testRoot, 0755))
22+
t.Cleanup(func() {
23+
os.RemoveAll(testRoot)
24+
})
25+
cases := []struct {
26+
name string
27+
mounts []string
28+
expectedMounts []string
29+
makeFiles func()
30+
}{
31+
{
32+
name: "handle-mount-single",
33+
mounts: []string{"test/single"},
34+
expectedMounts: []string{"test/single"},
35+
makeFiles: func() {},
36+
},
37+
{
38+
name: "handle-mount-nested",
39+
mounts: []string{"test/a", "test/b"},
40+
expectedMounts: []string{"test/a", "test/b"},
41+
makeFiles: func() {},
42+
},
43+
{
44+
name: "handle-mount-glob-empty",
45+
mounts: []string{"test/**", "test/b"},
46+
expectedMounts: []string{"test/b"},
47+
makeFiles: func() {},
48+
},
49+
{
50+
name: "handle-mount-glob-notempty",
51+
mounts: []string{fmt.Sprintf("%s/%s", testRoot, "test/**")},
52+
expectedMounts: []string{
53+
fmt.Sprintf("%s/%s", testRoot, "test/nestedA"),
54+
fmt.Sprintf("%s/%s", testRoot, "test/nestedB"),
55+
},
56+
makeFiles: func() {
57+
// Make test directories for glob to work properly
58+
os.MkdirAll(fmt.Sprintf("%s/%s", testRoot, "test/nestedA"), 0755)
59+
os.MkdirAll(fmt.Sprintf("%s/%s", testRoot, "test/nestedB"), 0755)
60+
},
61+
},
62+
}
63+
64+
for _, tc := range cases {
65+
c := defaultConfig()
66+
c.Mount = tc.mounts
67+
68+
tc.makeFiles()
69+
test.Ok(t, c.HandleMount())
70+
71+
test.Assert(t, reflect.DeepEqual(c.Mount, tc.expectedMounts),
72+
"expected mount differs from handled mount result:\nexpected: %v\ngot:%v", tc.expectedMounts, c.Mount)
73+
}
74+
}
75+
76+
// Config plugin configuration
77+
78+
func defaultConfig() *Config {
79+
return &Config{
80+
CompressionLevel: archive.DefaultCompressionLevel,
81+
StorageOperationTimeout: defaultStorageOperationTimeout,
82+
Override: true,
83+
}
84+
}
85+
86+
func containsGlob(a []string) bool {
87+
for _, v := range a {
88+
if strings.Contains(v, "**") {
89+
return true
90+
}
91+
}
92+
93+
return false
94+
}

internal/plugin/plugin.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9-
"strings"
109

11-
"github.com/bmatcuk/doublestar"
1210
"github.com/go-kit/log"
1311
"github.com/go-kit/log/level"
1412
"github.com/meltwater/drone-cache/archive"
@@ -123,21 +121,8 @@ func (p *Plugin) Exec() error { // nolint: funlen,cyclop
123121
)
124122

125123
// 4. Glob match mounts if doublestar paths exist
126-
for i, mount := range p.Config.Mount {
127-
if strings.Contains(mount, "**") {
128-
mountLen := len(p.Config.Mount)
129-
130-
// Remove the glob from the original mount list
131-
p.Config.Mount[i] = p.Config.Mount[mountLen-1]
132-
p.Config.Mount = p.Config.Mount[:mountLen-1]
133-
134-
globMounts, err := doublestar.Glob(mount)
135-
if err != nil {
136-
return fmt.Errorf("glob mount error <%s>, %w", mount, err)
137-
}
138-
139-
p.Config.Mount = append(p.Config.Mount, globMounts...)
140-
}
124+
if err = p.Config.HandleMount(); err != nil {
125+
return fmt.Errorf("exec handle mount call, %w", err)
141126
}
142127

143128
// 5. Select mode

internal/plugin/plugin_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ func TestPlugin(t *testing.T) {
149149
c := defaultConfig()
150150
setup(t, c, name)
151151
paths := tc.mount(tc.name)
152-
mount(c, paths...)
152+
c = mount(c, paths...)
153+
test.Ok(t, c.HandleMount())
154+
153155
cacheKey(c, tc.cacheKey)
154156
format(c, f)
155157

@@ -168,7 +170,7 @@ func TestPlugin(t *testing.T) {
168170
restoreRoot, cleanup := test.CreateTempDir(t, sanitize(name), testRootMoved)
169171
t.Cleanup(cleanup)
170172

171-
for _, p := range paths {
173+
for _, p := range c.Mount {
172174
rel, err := filepath.Rel(testRootMounted, p)
173175
test.Ok(t, err)
174176
dst := filepath.Join(restoreRoot, rel)
@@ -189,7 +191,7 @@ func TestPlugin(t *testing.T) {
189191
}
190192

191193
// Compare
192-
test.EqualDirs(t, restoreRoot, testRootMounted, paths)
194+
test.EqualDirs(t, restoreRoot, testRootMounted, c.Mount)
193195
})
194196
}
195197
}
@@ -220,6 +222,7 @@ func restore(c *Config) *Config {
220222

221223
func mount(c *Config, mount ...string) *Config {
222224
c.Mount = mount
225+
223226
return c
224227
}
225228

@@ -321,25 +324,21 @@ func exampleFileTreeWithSymlinks(t *testing.T, name string, content []byte) []st
321324

322325
func exampleNestedFileTreeWithGlob(t *testing.T, name string, content []byte) []string {
323326
name = sanitize(name)
324-
name1 := fmt.Sprintf("%s1", name)
325327

326-
dir, cleanup := test.CreateTempDir(t, name, testRootMounted)
327-
t.Cleanup(cleanup)
328+
dir, dirClean := test.CreateTempFilesInDir(t, name, content, testRootMounted)
329+
t.Cleanup(dirClean)
328330

329-
nestedDir, nestedDirClean := test.CreateTempDir(t, name, dir)
331+
_, nestedDirClean := test.CreateTempFilesInDir(t, name, content, dir)
330332
t.Cleanup(nestedDirClean)
331333

332-
nestedFile, nestedFileClean := test.CreateTempFile(t, name, content, nestedDir)
333-
t.Cleanup(nestedFileClean)
334-
335-
nestedDir1, nestedDirClean1 := test.CreateTempDir(t, name1, dir)
334+
_, nestedDirClean1 := test.CreateTempFilesInDir(t, name, content, dir)
336335
t.Cleanup(nestedDirClean1)
337336

338-
_, nestedFileClean1 := test.CreateTempFile(t, name1, content, nestedDir1)
339-
t.Cleanup(nestedFileClean1)
337+
file, fileClean := test.CreateTempFile(t, name, content, dir)
338+
t.Cleanup(fileClean)
340339

341-
globPath := fmt.Sprintf("%s/**/%s", testRootMounted, nestedDir1)
342-
return []string{nestedDir, nestedFile, globPath}
340+
globPath := fmt.Sprintf("%s/**/", testRootMounted)
341+
return []string{file, globPath}
343342
}
344343

345344
// Setup

0 commit comments

Comments
 (0)