| 1 | (* Verify: checks release selection with real git histories, and the |
| 2 | bundled builder's checksum: |
| 3 | |
| 4 | raz run site/Verify.raz |
| 5 | |
| 6 | Run from the repository root; it needs only the library, git and |
| 7 | Source's procedures. Every repository and download is temporary: no |
| 8 | release, working checkout or published file is changed. Each test |
| 9 | makes a small repository of three tagged commits and a record of a |
| 10 | fourth release in site/releases, and says its name, then what |
| 11 | fails, as Check does. It leaves out the rest of the site: Build and |
| 12 | Source are checked by building the site. *) |
| 13 | module Verify; |
| 14 | import Files, Buf, Str, Heap, Fmt, Check, Site, Releases, Source; |
| 15 | import Names := Vec([]const U8); |
| 16 | |
| 17 | const |
| 18 | Versions: [3][]const U8 = ["v0.7.1", "v0.9.0", "v0.10.0"]; (* the tagged releases *) |
| 19 | (* the settings of the git a test runs: a name, an address, no signing *) |
| 20 | Identity: [6][]const U8 = ["-c", "user.name=Test", "-c", "user.email=test@example.org", "-c", "commit.gpgSign=false"]; |
| 21 | |
| 22 | type |
| 23 | (* A repository for a test: where it is, and its three tagged commits. *) |
| 24 | Repo = record |
| 25 | dir: []const U8; (* the temporary directory that holds everything *) |
| 26 | path: []const U8; (* the repository, dir/repo *) |
| 27 | manifest: []const U8; (* its site/releases *) |
| 28 | commits: [3][]const U8; (* the commits of Versions *) |
| 29 | end; |
| 30 | |
| 31 | (* The text git writes for the command args in the repository at path, |
| 32 | run as a test user who signs nothing; the test stops if git fails. *) |
| 33 | proc git(path: []const U8; args: [][]const U8): []const U8; |
| 34 | var v: Names.Vec; out: Buf.Buf; i: INT; |
| 35 | begin |
| 36 | Names.init(v, Heap.heap); |
| 37 | Names.push(v, "git"); Names.push(v, "-C"); Names.push(v, path); |
| 38 | for i := 0 to LEN(Identity) - 1 do Names.push(v, Identity[i]); end; |
| 39 | for i := 0 to LEN(args) - 1 do Names.push(v, args[i]); end; |
| 40 | Buf.init(out, Heap.heap); |
| 41 | if Site.captureOut(Names.all(v), "", [], out) # 0 then Site.die("git {} failed", [Fmt.str(args[0])]); end; |
| 42 | return Str.trim(Buf.bytes(out)); |
| 43 | end; |
| 44 | |
| 45 | (* Makes the test repository: three commits tagged v0.7.1, v0.9.0 and |
| 46 | v0.10.0, then a commit that records v0.20.0, not tagged yet, at the |
| 47 | last of them. *) |
| 48 | proc setUp(var r: Repo); |
| 49 | var i: INT; number: [20]U8; len: INT; |
| 50 | begin |
| 51 | r.dir := Site.tempDir(); |
| 52 | r.path := Site.cat([r.dir, "/repo"]); |
| 53 | r.manifest := Site.cat([r.path, "/site/releases"]); |
| 54 | Site.makeDirs(Site.cat([r.path, "/site"])); |
| 55 | _ := git(r.path, ["init", "--quiet", "--initial-branch=main"]); |
| 56 | for i := 0 to 2 do |
| 57 | number, len := Str.fromInt(I64(i)); |
| 58 | Site.write(Site.cat([r.path, "/file"]), number[0..len]); |
| 59 | _ := git(r.path, ["add", "."]); |
| 60 | _ := git(r.path, ["commit", "--quiet", "-m", Versions[i]]); |
| 61 | r.commits[i] := git(r.path, ["rev-parse", "HEAD"]); |
| 62 | _ := git(r.path, ["tag", Versions[i]]); |
| 63 | end; |
| 64 | Site.write(r.manifest, Site.cat(["# not tagged yet\nv0.20.0 ", r.commits[2], "\n"])); |
| 65 | _ := git(r.path, ["add", "site/releases"]); |
| 66 | _ := git(r.path, ["commit", "--quiet", "-m", "record distribution"]); |
| 67 | end; |
| 68 | |
| 69 | (* Whether the two lists of releases are the same. *) |
| 70 | proc same(a, b: []const Releases.Release): BOOL; |
| 71 | var i: INT; |
| 72 | begin |
| 73 | if LEN(a) # LEN(b) then return false; end; |
| 74 | for i := 0 to LEN(a) - 1 do |
| 75 | if (a[i].tag # b[i].tag) or (a[i].commit # b[i].commit) then return false; end; |
| 76 | end; |
| 77 | return true; |
| 78 | end; |
| 79 | |
| 80 | (* The releases of the repository at path; the test stops if they are |
| 81 | a mistake. *) |
| 82 | proc releasesOf(path: []const U8): []Releases.Release; |
| 83 | var list: []Releases.Release; why: []const U8; |
| 84 | begin |
| 85 | list, why := Releases.refs(path, []); |
| 86 | Check.str("no mistake", why, ""); |
| 87 | return list; |
| 88 | end; |
| 89 | |
| 90 | (* Checks that refs of the repository at path is a mistake whose |
| 91 | message holds word. *) |
| 92 | proc refuses(name, path, word: []const U8); |
| 93 | var why: []const U8; |
| 94 | begin |
| 95 | _, why := Releases.refs(path, []); |
| 96 | Check.ok(Str.find(why, word) >= 0, Site.cat([name, ": a mistake saying ", word, ", not \"", why, "\""])); |
| 97 | end; |
| 98 | |
| 99 | (* Tags, in order, and a record, make the numeric order of releases: |
| 100 | v0.9.0 before v0.10.0, and the record last. *) |
| 101 | proc testNumericTagsAndManifest; |
| 102 | var r: Repo; want: [4]Releases.Release; |
| 103 | begin |
| 104 | setUp(r); |
| 105 | want := [{tag: "v0.7.1", commit: r.commits[0]}, {tag: "v0.9.0", commit: r.commits[1]}, |
| 106 | {tag: "v0.10.0", commit: r.commits[2]}, {tag: "v0.20.0", commit: r.commits[2]}]; |
| 107 | Check.ok(same(releasesOf(r.path), want), "numeric tags and manifest"); |
| 108 | _ := Files.removeTree(r.dir); |
| 109 | end; |
| 110 | |
| 111 | (* A record can't move a tag to another commit. *) |
| 112 | proc testManifestCannotMoveATag; |
| 113 | var r: Repo; |
| 114 | begin |
| 115 | setUp(r); |
| 116 | Site.write(r.manifest, Site.cat(["v0.9.0 ", r.commits[0], "\n"])); |
| 117 | refuses("manifest cannot move a tag", r.path, "disagree"); |
| 118 | _ := Files.removeTree(r.dir); |
| 119 | end; |
| 120 | |
| 121 | (* A record can't publish a commit of another line, and a tag added |
| 122 | later can't override a record. *) |
| 123 | proc testManifestCannotPublishAnotherLine; |
| 124 | var r: Repo; side: []const U8; |
| 125 | begin |
| 126 | setUp(r); |
| 127 | _ := git(r.path, ["checkout", "--quiet", "-b", "side", r.commits[0]]); |
| 128 | Site.write(Site.cat([r.path, "/side"]), "a development branch\n"); |
| 129 | _ := git(r.path, ["add", "side"]); |
| 130 | _ := git(r.path, ["commit", "--quiet", "-m", "side"]); |
| 131 | side := git(r.path, ["rev-parse", "HEAD"]); |
| 132 | _ := git(r.path, ["checkout", "--quiet", "main"]); |
| 133 | Site.write(r.manifest, Site.cat(["v0.20.0 ", side, "\n"])); |
| 134 | refuses("manifest cannot publish another line", r.path, "first-parent"); |
| 135 | (* A new tag must not override the recorded distribution, even when |
| 136 | that tag would otherwise be skipped as off the main line. *) |
| 137 | Site.write(r.manifest, Site.cat(["v0.20.0 ", r.commits[2], "\n"])); |
| 138 | _ := git(r.path, ["tag", "v0.20.0", side]); |
| 139 | refuses("a tag cannot override a record", r.path, "disagree"); |
| 140 | _ := Files.removeTree(r.dir); |
| 141 | end; |
| 142 | |
| 143 | (* A shallow clone, as a deployment service makes, gets its history and |
| 144 | tags back from its origin. A file URL uses git's transport, so --depth |
| 145 | really is shallow. *) |
| 146 | proc testShallowDeploy; |
| 147 | var r: Repo; clone: []const U8; |
| 148 | begin |
| 149 | setUp(r); |
| 150 | clone := Site.cat([r.dir, "/deploy"]); |
| 151 | _ := git(r.path, ["clone", "--quiet", "--depth=1", "--branch=main", Site.cat(["file://", r.path]), clone]); |
| 152 | Check.str("shallow before", git(clone, ["rev-parse", "--is-shallow-repository"]), "true"); |
| 153 | Check.ok(same(releasesOf(clone), releasesOf(r.path)), "shallow deploy recovers history and tags"); |
| 154 | Check.str("shallow after", git(clone, ["rev-parse", "--is-shallow-repository"]), "false"); |
| 155 | _ := Files.removeTree(r.dir); |
| 156 | end; |
| 157 | |
| 158 | (* A detached shallow checkout with no local main still has its releases, |
| 159 | and the published store names its commit main. *) |
| 160 | proc testDetachedShallowDeploy; |
| 161 | var r: Repo; clone, store: []const U8; |
| 162 | begin |
| 163 | setUp(r); |
| 164 | clone := Site.cat([r.dir, "/deploy"]); |
| 165 | _ := git(r.path, ["clone", "--quiet", "--depth=1", "--branch=main", Site.cat(["file://", r.path]), clone]); |
| 166 | _ := git(clone, ["checkout", "--quiet", "--detach"]); |
| 167 | _ := git(clone, ["branch", "-D", "main"]); |
| 168 | Check.ok(same(releasesOf(clone), releasesOf(r.path)), "detached shallow deploy needs no local main"); |
| 169 | store := Source.prepareStore(clone, r.dir); |
| 170 | Check.str("store's commit", git(store, ["rev-parse", "HEAD"]), git(clone, ["rev-parse", "HEAD"])); |
| 171 | Check.str("store's branch", git(store, ["symbolic-ref", "HEAD"]), "refs/heads/main"); |
| 172 | _ := git(store, ["fsck", "--strict"]); |
| 173 | Check.str("clone stays detached", git(clone, ["rev-parse", "--abbrev-ref", "HEAD"]), "HEAD"); |
| 174 | _ := Files.removeTree(r.dir); |
| 175 | end; |
| 176 | |
| 177 | (* A builder whose bytes are not the ones recorded is rejected before it |
| 178 | is unpacked. *) |
| 179 | proc testModifiedBuilderIsRejected; |
| 180 | var dir, archive, why: []const U8; |
| 181 | begin |
| 182 | dir := Site.tempDir(); |
| 183 | archive := Site.cat([dir, "/ladle.gz"]); |
| 184 | Site.write(archive, "not the published executable"); |
| 185 | Site.write(Site.cat([archive, ".sha256"]), Site.cat(["0000000000000000000000000000000000000000000000000000000000000000", " ladle.gz\n"])); |
| 186 | _, why := Source.pinnedLadle(archive, Site.cat([dir, "/ladle"])); |
| 187 | Check.ok(Str.find(why, "recorded SHA-256") >= 0, "modified builder is rejected"); |
| 188 | Check.ok(not Site.exists(Site.cat([dir, "/ladle"])), "nothing is unpacked"); |
| 189 | _ := Files.removeTree(dir); |
| 190 | end; |
| 191 | |
| 192 | (* Says that the test t, which is called name, is running, and runs it: |
| 193 | Check says what fails, and at the end how many checks did. *) |
| 194 | proc run(name: []const U8; t: proc); |
| 195 | begin |
| 196 | Fmt.print("{}\n", [Fmt.str(name)]); |
| 197 | t(); |
| 198 | end; |
| 199 | |
| 200 | proc main*; |
| 201 | begin |
| 202 | run("numeric tags and manifest", testNumericTagsAndManifest); |
| 203 | run("manifest cannot move a tag", testManifestCannotMoveATag); |
| 204 | run("manifest cannot publish another line", testManifestCannotPublishAnotherLine); |
| 205 | run("shallow deploy recovers history and tags", testShallowDeploy); |
| 206 | run("detached shallow deploy needs no local main", testDetachedShallowDeploy); |
| 207 | run("modified builder is rejected before unpacking", testModifiedBuilderIsRejected); |
| 208 | end; |
| 209 | end Verify. |