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

Commit 6c2fcb4

Browse files
authored
Merge pull request #867 from laverya/we-were-unable-to-apply-your-patch
clean paths before comparison
2 parents ce0e98a + b8722d4 commit 6c2fcb4

File tree

2 files changed

+168
-2
lines changed

2 files changed

+168
-2
lines changed

pkg/patch/patcher.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package patch
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
"path"
78
"path/filepath"
@@ -232,7 +233,7 @@ func (p *ShipPatcher) writeTempKustomization(step api.Kustomize, resource string
232233
return errors.Wrap(err, "failed to get relative path")
233234
}
234235

235-
if targetPath == resource {
236+
if filepath.Clean(targetPath) == filepath.Clean(resource) {
236237
tempBaseKustomization.Resources = append(tempBaseKustomization.Resources, relativePath)
237238
}
238239
return nil
@@ -243,7 +244,7 @@ func (p *ShipPatcher) writeTempKustomization(step api.Kustomize, resource string
243244

244245
if len(tempBaseKustomization.Resources) == 0 {
245246
level.Error(p.Logger).Log("event", "unable to find", "resource", resource)
246-
return errors.New("Temp base directory is empty - base resource not found")
247+
return fmt.Errorf("temp base directory is empty - base resource %s not found in %s", resource, step.Base)
247248
}
248249

249250
marshalled, err := yaml.Marshal(tempBaseKustomization)

pkg/patch/patcher_test.go

+165
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66
"path"
77
"testing"
88

9+
"github.com/ghodss/yaml"
10+
"github.com/go-kit/kit/log"
911
. "github.com/onsi/ginkgo"
1012
. "github.com/onsi/gomega"
1113
"github.com/replicatedhq/ship/pkg/api"
1214
"github.com/replicatedhq/ship/pkg/testing/logger"
1315
"github.com/spf13/afero"
16+
"github.com/stretchr/testify/require"
17+
k8stypes "sigs.k8s.io/kustomize/pkg/types"
1418
)
1519

1620
func TestShipPatcher(t *testing.T) {
@@ -140,3 +144,164 @@ var _ = Describe("ShipPatcher", func() {
140144
})
141145
})
142146
})
147+
148+
func TestShipPatcher_writeTempKustomization(t *testing.T) {
149+
type testFile struct {
150+
path string
151+
contents string
152+
}
153+
tests := []struct {
154+
name string
155+
step api.Kustomize
156+
resource string
157+
testFiles []testFile
158+
expectKustomization k8stypes.Kustomization
159+
expectErr bool
160+
}{
161+
{
162+
name: "no matching resource",
163+
step: api.Kustomize{Base: "base/"},
164+
resource: "./base/file.yaml",
165+
testFiles: []testFile{
166+
{
167+
path: "./base/strawberry.yaml",
168+
contents: `apiVersion: apps/v1beta2
169+
kind: Deployment
170+
metadata:
171+
labels:
172+
app: strawberry
173+
heritage: Tiller
174+
chart: strawberry-1.0.0
175+
name: strawberry`,
176+
},
177+
},
178+
expectErr: true,
179+
},
180+
{
181+
name: "matching resource",
182+
step: api.Kustomize{Base: "base/"},
183+
resource: "./base/strawberry.yaml",
184+
testFiles: []testFile{
185+
{
186+
path: "base/strawberry.yaml",
187+
contents: `apiVersion: apps/v1beta2
188+
kind: Deployment
189+
metadata:
190+
labels:
191+
app: strawberry
192+
heritage: Tiller
193+
chart: strawberry-1.0.0
194+
name: strawberry`,
195+
},
196+
},
197+
expectErr: false,
198+
expectKustomization: k8stypes.Kustomization{
199+
Resources: []string{"strawberry.yaml"},
200+
},
201+
},
202+
{
203+
name: "matching resource, unclean path",
204+
step: api.Kustomize{Base: "base/"},
205+
resource: "./base/strawberry.yaml",
206+
testFiles: []testFile{
207+
{
208+
path: "./base/strawberry.yaml",
209+
contents: `apiVersion: apps/v1beta2
210+
kind: Deployment
211+
metadata:
212+
labels:
213+
app: strawberry
214+
heritage: Tiller
215+
chart: strawberry-1.0.0
216+
name: strawberry`,
217+
},
218+
},
219+
expectErr: false,
220+
expectKustomization: k8stypes.Kustomization{
221+
Resources: []string{"strawberry.yaml"},
222+
},
223+
},
224+
{
225+
name: "matching resource, unclean path in subdir",
226+
step: api.Kustomize{Base: "base/"},
227+
resource: "./base/flowers/rose.yml",
228+
testFiles: []testFile{
229+
{
230+
path: "./base/strawberry.yaml",
231+
contents: `apiVersion: apps/v1beta2
232+
kind: Deployment
233+
metadata:
234+
labels:
235+
app: strawberry
236+
heritage: Tiller
237+
chart: strawberry-1.0.0
238+
name: strawberry`,
239+
},
240+
{
241+
path: "./base/flowers/rose.yml",
242+
contents: `apiVersion: v1
243+
kind: Service
244+
metadata:
245+
labels:
246+
app: rose
247+
name: rose`,
248+
},
249+
},
250+
expectErr: false,
251+
expectKustomization: k8stypes.Kustomization{
252+
Resources: []string{"flowers/rose.yml"},
253+
},
254+
},
255+
{
256+
name: "alternate base",
257+
step: api.Kustomize{Base: "another/base/path/"},
258+
resource: "another/base/path/raspberry.yaml",
259+
testFiles: []testFile{
260+
{
261+
path: "another/base/path/raspberry.yaml",
262+
contents: `apiVersion: apps/v1
263+
kind: Deployment
264+
metadata:
265+
labels:
266+
app: raspberry
267+
name: raspberry`,
268+
},
269+
},
270+
expectErr: false,
271+
expectKustomization: k8stypes.Kustomization{
272+
Resources: []string{"raspberry.yaml"},
273+
},
274+
},
275+
}
276+
for _, tt := range tests {
277+
t.Run(tt.name, func(t *testing.T) {
278+
req := require.New(t)
279+
280+
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
281+
for _, testFile := range tt.testFiles {
282+
err := mockFs.WriteFile(testFile.path, []byte(testFile.contents), 0755)
283+
req.NoError(err)
284+
}
285+
p := &ShipPatcher{
286+
Logger: log.NewNopLogger(),
287+
FS: mockFs,
288+
}
289+
290+
err := p.writeTempKustomization(tt.step, tt.resource)
291+
292+
if !tt.expectErr {
293+
req.NoError(err)
294+
295+
kustomizationB, err := mockFs.ReadFile(path.Join(tt.step.Base, "kustomization.yaml"))
296+
req.NoError(err)
297+
298+
kustomizationYaml := k8stypes.Kustomization{}
299+
err = yaml.Unmarshal(kustomizationB, &kustomizationYaml)
300+
req.NoError(err)
301+
req.Equal(tt.expectKustomization, kustomizationYaml)
302+
} else {
303+
req.Error(err)
304+
}
305+
})
306+
}
307+
}

0 commit comments

Comments
 (0)