Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit fa5275c

Browse files
authored
Merge pull request #1044 from laverya/upstream-file-parsing
fix filenames when parsing upstream contents
2 parents 5517742 + c5a4d6d commit fa5275c

File tree

2 files changed

+158
-2
lines changed

2 files changed

+158
-2
lines changed

pkg/state/upstream_files.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ func writeUpstreamFiles(fs afero.Afero, state State) error {
2020

2121
contents := state.V1.UpstreamContents
2222

23-
err := fs.MkdirAll(constants.UpstreamContentsPath, 0755)
23+
err := fs.RemoveAll(constants.UpstreamContentsPath)
24+
if err != nil {
25+
return errors.Wrapf(err, "clear upstream contents path %s", constants.UpstreamContentsPath)
26+
}
27+
28+
err = fs.MkdirAll(constants.UpstreamContentsPath, 0755)
2429
if err != nil {
2530
return errors.Wrapf(err, "create upstream contents path %s", constants.UpstreamContentsPath)
2631
}
@@ -114,9 +119,14 @@ func readUpstreamFiles(fs afero.Afero, state State) (State, error) {
114119
return errors.Wrapf(err, "read file %s", path)
115120
}
116121

122+
relpath, err := filepath.Rel(constants.UpstreamContentsPath, path)
123+
if err != nil {
124+
return errors.Wrapf(err, "find relative path to file %s", path)
125+
}
126+
117127
encodedFile := base64.StdEncoding.EncodeToString(fileBytes)
118128
newUpstreamContents.UpstreamFiles = append(newUpstreamContents.UpstreamFiles, UpstreamFile{
119-
FilePath: path,
129+
FilePath: relpath,
120130
FileContents: encodedFile,
121131
})
122132
return nil

pkg/state/upstream_files_test.go

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package state
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/spf13/afero"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/replicatedhq/ship/pkg/constants"
12+
)
13+
14+
func Test_readUpstreamFiles(t *testing.T) {
15+
type fileStruct struct {
16+
name string
17+
data string
18+
}
19+
20+
tests := []struct {
21+
name string
22+
wantErr bool
23+
inputFiles []fileStruct
24+
wantUpstreamContents UpstreamContents
25+
}{
26+
{
27+
name: "basic",
28+
inputFiles: []fileStruct{
29+
{
30+
name: "abc.test",
31+
data: "abc.test",
32+
},
33+
},
34+
wantUpstreamContents: UpstreamContents{
35+
UpstreamFiles: []UpstreamFile{
36+
{
37+
FilePath: "abc.test",
38+
FileContents: "YWJjLnRlc3Q=",
39+
},
40+
},
41+
AppRelease: nil,
42+
},
43+
},
44+
{
45+
name: "app release",
46+
inputFiles: []fileStruct{
47+
{
48+
name: "abc.test",
49+
data: "abc.test",
50+
},
51+
{
52+
name: "appRelease.json",
53+
data: `{"id":"appRelease.json"}`,
54+
},
55+
},
56+
wantUpstreamContents: UpstreamContents{
57+
UpstreamFiles: nil,
58+
AppRelease: &ShipRelease{ID: "appRelease.json"},
59+
},
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
req := require.New(t)
66+
// setup input FS
67+
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
68+
req.NoError(mockFs.MkdirAll(constants.UpstreamContentsPath, os.FileMode(0644)))
69+
for _, inFile := range tt.inputFiles {
70+
req.NoError(mockFs.WriteFile(filepath.Join(constants.UpstreamContentsPath, inFile.name), []byte(inFile.data), os.FileMode(0644)))
71+
}
72+
73+
got, err := readUpstreamFiles(mockFs, State{})
74+
if (err != nil) != tt.wantErr {
75+
t.Errorf("readUpstreamFiles() error = %v, wantErr %v", err, tt.wantErr)
76+
return
77+
}
78+
79+
wantState := State{V1: &V1{UpstreamContents: &tt.wantUpstreamContents}}
80+
req.Equal(wantState, got)
81+
})
82+
}
83+
}
84+
85+
func Test_UpstreamFilesCycle(t *testing.T) {
86+
87+
tests := []struct {
88+
name string
89+
contents UpstreamContents
90+
}{
91+
{
92+
name: "basic",
93+
contents: UpstreamContents{
94+
UpstreamFiles: []UpstreamFile{
95+
{
96+
FilePath: "abc.test",
97+
FileContents: "YWJjLnRlc3Q=",
98+
},
99+
},
100+
AppRelease: nil,
101+
},
102+
},
103+
{
104+
name: "app release",
105+
contents: UpstreamContents{
106+
UpstreamFiles: nil,
107+
AppRelease: &ShipRelease{ID: "appRelease.json"},
108+
},
109+
},
110+
{
111+
name: "multiple files",
112+
contents: UpstreamContents{
113+
UpstreamFiles: []UpstreamFile{
114+
{
115+
FilePath: "abc.test",
116+
FileContents: "YWJjLnRlc3Q=",
117+
},
118+
{
119+
FilePath: "subdir/abc.test",
120+
FileContents: "YWJjLnRlc3Q=",
121+
},
122+
},
123+
AppRelease: nil,
124+
},
125+
},
126+
}
127+
128+
for _, tt := range tests {
129+
t.Run(tt.name, func(t *testing.T) {
130+
req := require.New(t)
131+
// setup input FS
132+
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
133+
req.NoError(mockFs.MkdirAll(constants.UpstreamContentsPath, os.FileMode(0644)))
134+
135+
wantState := State{V1: &V1{UpstreamContents: &tt.contents}}
136+
137+
err := writeUpstreamFiles(mockFs, wantState)
138+
req.NoError(err)
139+
140+
got, err := readUpstreamFiles(mockFs, State{})
141+
req.NoError(err)
142+
143+
req.Equal(wantState, got)
144+
})
145+
}
146+
}

0 commit comments

Comments
 (0)