Skip to content

Commit be5d10e

Browse files
authored
Merge pull request #403 from fluxcd/bucket-revision-path
Take relative paths in account for Bucket revision
2 parents dbaf8bf + f5cb441 commit be5d10e

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

controllers/bucket_controller.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,12 @@ func (r *BucketReconciler) auth(ctx context.Context, bucket sourcev1.Bucket) (*m
356356
return minio.New(bucket.Spec.Endpoint, &opt)
357357
}
358358

359+
// checksum calculates the SHA1 checksum of the given root directory.
360+
// It traverses the given root directory and calculates the checksum for any found file, and returns the SHA1 sum of the
361+
// list with relative file paths and their checksums.
359362
func (r *BucketReconciler) checksum(root string) (string, error) {
360-
checksum := ""
361-
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
363+
sum := sha1.New()
364+
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
362365
if err != nil {
363366
return err
364367
}
@@ -369,14 +372,16 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
369372
if err != nil {
370373
return err
371374
}
372-
checksum += fmt.Sprintf("%x", sha1.Sum(data))
375+
relPath, err := filepath.Rel(root, path)
376+
if err != nil {
377+
return err
378+
}
379+
sum.Write([]byte(fmt.Sprintf("%x %s\n", sha1.Sum(data), relPath)))
373380
return nil
374-
})
375-
if err != nil {
381+
}); err != nil {
376382
return "", err
377383
}
378-
379-
return fmt.Sprintf("%x", sha1.Sum([]byte(checksum))), nil
384+
return fmt.Sprintf("%x", sum.Sum(nil)), nil
380385
}
381386

382387
// resetStatus returns a modified v1beta1.Bucket and a boolean indicating

controllers/bucket_controller_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2021 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
)
25+
26+
func TestBucketReconciler_checksum(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
beforeFunc func(root string)
30+
want string
31+
wantErr bool
32+
}{
33+
{
34+
name: "empty root",
35+
want: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
36+
},
37+
{
38+
name: "with file",
39+
beforeFunc: func(root string) {
40+
mockFile(root, "a/b/c.txt", "a dummy string")
41+
},
42+
want: "309a5e6e96b4a7eea0d1cfaabf1be8ec1c063fa0",
43+
},
44+
{
45+
name: "with file in different path",
46+
beforeFunc: func(root string) {
47+
mockFile(root, "a/b.txt", "a dummy string")
48+
},
49+
want: "e28c62b5cc488849950c4355dddc5523712616d4",
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
root, err := ioutil.TempDir("", "bucket-checksum-")
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
defer os.RemoveAll(root)
59+
if tt.beforeFunc != nil {
60+
tt.beforeFunc(root)
61+
}
62+
got, err := (&BucketReconciler{}).checksum(root)
63+
if (err != nil) != tt.wantErr {
64+
t.Errorf("checksum() error = %v, wantErr %v", err, tt.wantErr)
65+
return
66+
}
67+
if got != tt.want {
68+
t.Errorf("checksum() got = %v, want %v", got, tt.want)
69+
}
70+
})
71+
}
72+
}
73+
74+
func mockFile(root, path, content string) error {
75+
filePath := filepath.Join(root, path)
76+
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
77+
panic(err)
78+
}
79+
if err := ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {
80+
panic(err)
81+
}
82+
return nil
83+
}

0 commit comments

Comments
 (0)