|
| 1 | +package testutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gitpod-io/leeway/pkg/leeway" |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 15 | +) |
| 16 | + |
| 17 | +func TestLoadFromYAML(t *testing.T) { |
| 18 | + ignores := cmpopts.IgnoreUnexported( |
| 19 | + leeway.Workspace{}, |
| 20 | + leeway.Component{}, |
| 21 | + leeway.Package{}, |
| 22 | + leeway.WorkspaceProvenance{}, |
| 23 | + leeway.GitInfo{}, |
| 24 | + ) |
| 25 | + |
| 26 | + tests := []struct { |
| 27 | + Name string |
| 28 | + Content string |
| 29 | + Expectation *Setup |
| 30 | + }{ |
| 31 | + { |
| 32 | + Name: "empty", |
| 33 | + Expectation: &Setup{}, |
| 34 | + }, |
| 35 | + { |
| 36 | + Name: "single", |
| 37 | + Expectation: &Setup{ |
| 38 | + Components: []Component{ |
| 39 | + { |
| 40 | + Location: "comp1", |
| 41 | + Packages: []leeway.Package{ |
| 42 | + { |
| 43 | + PackageInternal: leeway.PackageInternal{ |
| 44 | + Name: "pkg1", |
| 45 | + Type: leeway.GenericPackage, |
| 46 | + Sources: []string{"**"}, |
| 47 | + }, |
| 48 | + Config: leeway.GenericPkgConfig{ |
| 49 | + Commands: [][]string{{"foo"}}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + Content: `components: |
| 57 | + - location: comp1 |
| 58 | + packages: |
| 59 | + - name: pkg1 |
| 60 | + type: generic |
| 61 | + srcs: |
| 62 | + - "**" |
| 63 | + config: |
| 64 | + commands: [["foo"]]`, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, test := range tests { |
| 69 | + t.Run(test.Name, func(t *testing.T) { |
| 70 | + act, err := LoadFromYAML(bytes.NewBufferString(test.Content)) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + if diff := cmp.Diff(test.Expectation, act, ignores); diff != "" { |
| 76 | + t.Errorf("LoadFromYAML() mismatch (-want +got):\n%s", diff) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestMaterialise(t *testing.T) { |
| 83 | + type File struct { |
| 84 | + Path string |
| 85 | + Size int64 |
| 86 | + SHA256 string |
| 87 | + } |
| 88 | + tests := []struct { |
| 89 | + Name string |
| 90 | + Setup Setup |
| 91 | + Expectation []File |
| 92 | + }{ |
| 93 | + { |
| 94 | + Name: "simple", |
| 95 | + Setup: Setup{ |
| 96 | + Components: []Component{ |
| 97 | + { |
| 98 | + Location: "comp1", |
| 99 | + Files: map[string]string{ |
| 100 | + "someFile": "content", |
| 101 | + "some/other/file": "more content", |
| 102 | + }, |
| 103 | + Comp: leeway.Component{ |
| 104 | + Constants: leeway.Arguments{ |
| 105 | + "hello": "world", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Packages: []leeway.Package{ |
| 109 | + { |
| 110 | + PackageInternal: leeway.PackageInternal{ |
| 111 | + Name: "pkg1", |
| 112 | + Type: leeway.GenericPackage, |
| 113 | + Sources: []string{"**"}, |
| 114 | + }, |
| 115 | + Config: leeway.GenericPkgConfig{ |
| 116 | + Commands: [][]string{{"bla"}}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + Expectation: []File{ |
| 124 | + {Path: "WORKSPACE.yaml"}, |
| 125 | + {Path: "comp1/BUILD.yaml", SHA256: "6cc73a81aa8f00851c4738947d0d22ae296d8c626af4d1f4fe60fbabb09f906f"}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + for _, test := range tests { |
| 131 | + t.Run(test.Name, func(t *testing.T) { |
| 132 | + loc, err := test.Setup.Materialize() |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + t.Cleanup(func() { os.RemoveAll(loc) }) |
| 137 | + t.Logf("materialized at %s", loc) |
| 138 | + |
| 139 | + for _, f := range test.Expectation { |
| 140 | + fn := filepath.Join(loc, f.Path) |
| 141 | + stat, err := os.Stat(fn) |
| 142 | + if err != nil { |
| 143 | + t.Errorf("expected file mismatch: %s: %v", f.Path, err) |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + if f.Size > 0 && stat.Size() != f.Size { |
| 148 | + t.Errorf("expected file size mismatch: %s: expected %d, got %d", f.Path, f.Size, stat.Size()) |
| 149 | + } |
| 150 | + |
| 151 | + if f.SHA256 == "" { |
| 152 | + continue |
| 153 | + } |
| 154 | + hash := sha256.New() |
| 155 | + fp, err := os.Open(fn) |
| 156 | + if err != nil { |
| 157 | + t.Errorf("cannot hash %s: %v", fn, err) |
| 158 | + continue |
| 159 | + } |
| 160 | + _, err = io.Copy(hash, fp) |
| 161 | + fp.Close() |
| 162 | + if err != nil { |
| 163 | + t.Errorf("cannot hash %s: %v", fn, err) |
| 164 | + continue |
| 165 | + } |
| 166 | + |
| 167 | + sum := fmt.Sprintf("%x", hash.Sum(nil)) |
| 168 | + if f.SHA256 != sum { |
| 169 | + t.Errorf("file hash mismatch: %s: expected %s, got %s", f.Path, f.SHA256, sum) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
0 commit comments