|
| 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