Skip to content

Commit e1f47c5

Browse files
committed
Add test for json mode
1 parent 4ca16e7 commit e1f47c5

1 file changed

Lines changed: 59 additions & 14 deletions

File tree

pkg/fssync/walk_test.go

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import (
2222
"context"
2323
"crypto/sha256"
2424
"encoding/hex"
25+
"encoding/json"
2526
gofs "io/fs"
2627
"testing"
2728
"time"
2829

2930
"github.com/apple/container-builder-shim/pkg/api"
3031
"github.com/apple/container-builder-shim/pkg/stream"
31-
"google.golang.org/grpc/metadata"
3232
)
3333

3434
func (p *FSSyncProxy) RegisterDemux(id string, d *stream.Demultiplexer) {
@@ -74,6 +74,29 @@ func makeNestedTarHeaderAndBody() (checksum string, full []byte) {
7474
func (p *FSSyncProxy) Send(s *api.ServerStream) error {
7575
id := s.BuildId
7676
d := demuxes[id]
77+
78+
bt := s.GetBuildTransfer()
79+
if bt != nil && bt.Metadata["mode"] == string(ModeJSON) {
80+
files := []RawFileInfo{
81+
{Name: "dir", Mode: 0o755, IsDir: true, ModTime: time.Now().UTC().Format(time.RFC3339)},
82+
{Name: "dir/file.txt", Size: 42, Mode: 0o644, IsDir: false, ModTime: time.Now().UTC().Format(time.RFC3339)},
83+
}
84+
data, _ := json.Marshal(files)
85+
go func() {
86+
_ = d.Accept(&api.ClientStream{
87+
BuildId: id,
88+
PacketType: &api.ClientStream_BuildTransfer{
89+
BuildTransfer: &api.BuildTransfer{
90+
Id: id,
91+
Complete: true,
92+
Data: data,
93+
},
94+
},
95+
})
96+
}()
97+
return nil
98+
}
99+
77100
checksum, full := makeNestedTarHeaderAndBody()
78101
go func() {
79102
_ = d.Accept(&api.ClientStream{
@@ -113,7 +136,7 @@ func (p *FSSyncProxy) Send(s *api.ServerStream) error {
113136
}
114137

115138
func TestUnmarshalWalkMetadata_Defaults(t *testing.T) {
116-
md, err := unmarshalWalkMetadata(context.Background())
139+
md, err := unmarshalWalkMetadata(context.Background(), ModeTAR)
117140
if err != nil {
118141
t.Fatalf("unexpected err: %v", err)
119142
}
@@ -122,19 +145,11 @@ func TestUnmarshalWalkMetadata_Defaults(t *testing.T) {
122145
}
123146
}
124147

125-
func TestUnmarshalWalkMetadata_InvalidMode(t *testing.T) {
126-
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("mode", "json"))
127-
_, err := unmarshalWalkMetadata(ctx)
128-
if err == nil {
129-
t.Fatal("expected error for unsupported mode 'json', got nil")
130-
}
131-
}
132-
133148
func TestWalk_UnsupportedMode(t *testing.T) {
134-
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("mode", "json"))
135-
fs := NewFS(ctx, &FSSyncProxy{}, "/", t.TempDir()) // proxy never used
149+
// A zero-value FSSyncProxy has mode="" which is not a recognised TransferMode.
150+
fs := NewFS(context.Background(), &FSSyncProxy{}, "/", t.TempDir())
136151
var fn gofs.WalkDirFunc = func(string, gofs.DirEntry, error) error { return nil }
137-
err := fs.Walk(ctx, "", fn)
152+
err := fs.Walk(context.Background(), "", fn)
138153
if err == nil {
139154
t.Fatal("Walk returned nil error, want unsupported-mode error")
140155
}
@@ -145,7 +160,7 @@ func TestWalk_TarModeSuccess(t *testing.T) {
145160

146161
_, full := makeNestedTarHeaderAndBody()
147162

148-
fs := NewFS(context.Background(), &FSSyncProxy{}, "/", tmp)
163+
fs := NewFS(context.Background(), &FSSyncProxy{mode: ModeTAR}, "/", tmp)
149164

150165
var walked []string
151166
err := fs.Walk(context.Background(), "", func(path string, _ gofs.DirEntry, _ error) error {
@@ -164,3 +179,33 @@ func TestWalk_TarModeSuccess(t *testing.T) {
164179
t.Errorf("walk callback not invoked")
165180
}
166181
}
182+
183+
func TestWalk_JSONModeSuccess(t *testing.T) {
184+
fs := NewFS(context.Background(), &FSSyncProxy{mode: ModeJSON}, "/", t.TempDir())
185+
186+
type result struct {
187+
path string
188+
isDir bool
189+
mode gofs.FileMode
190+
}
191+
var walked []result
192+
err := fs.Walk(context.Background(), "", func(path string, d gofs.DirEntry, _ error) error {
193+
info, _ := d.Info()
194+
walked = append(walked, result{path: path, isDir: d.IsDir(), mode: info.Mode()})
195+
return nil
196+
})
197+
if err != nil {
198+
t.Fatalf("Walk returned err=%v", err)
199+
}
200+
if len(walked) != 2 {
201+
t.Fatalf("got %d entries, want 2", len(walked))
202+
}
203+
if walked[0].path != "dir" || !walked[0].isDir || walked[0].mode&gofs.ModeDir == 0 {
204+
t.Errorf("entry[0]: got path=%q isDir=%v mode=%v, want dir entry with ModeDir set",
205+
walked[0].path, walked[0].isDir, walked[0].mode)
206+
}
207+
if walked[1].path != "dir/file.txt" || walked[1].isDir {
208+
t.Errorf("entry[1]: got path=%q isDir=%v, want dir/file.txt regular file",
209+
walked[1].path, walked[1].isDir)
210+
}
211+
}

0 commit comments

Comments
 (0)