carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package activator
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestInstallMarkerRoundTrips(t *testing.T) {
11 dir := t.TempDir()
12 if _, found, err := ReadInstallMarker(dir, "shop.test"); found || err != nil {
13 t.Fatalf("before any write: found=%v err=%v", found, err)
14 }
15 m := InstallMarker{Incarnation: "inc1", Generation: "g-op7", Stamp: 41, DBMarker: "dbm", FilesSeq: 1882}
16 if err := WriteInstallMarker(dir, "shop.test", m); err != nil {
17 t.Fatal(err)
18 }
19 got, found, err := ReadInstallMarker(dir, "shop.test")
20 if err != nil || !found || got != m {
21 t.Fatalf("got %+v found=%v err=%v", got, found, err)
22 }
23 m.Stamp = 57
24 if err := WriteInstallMarker(dir, "shop.test", m); err != nil {
25 t.Fatal(err)
26 }
27 if got, _, _ := ReadInstallMarker(dir, "shop.test"); got.Stamp != 57 {
28 t.Fatalf("a second write did not replace the first: %+v", got)
29 }
30 if err := RemoveInstallMarker(dir, "shop.test"); err != nil {
31 t.Fatal(err)
32 }
33 if err := RemoveInstallMarker(dir, "shop.test"); err != nil {
34 t.Fatalf("removing an absent marker: %v", err)
35 }
36 if _, found, _ := ReadInstallMarker(dir, "shop.test"); found {
37 t.Fatal("marker still there")
38 }
39}
40
41func TestInstallMarkerLeavesNoTemporaryFile(t *testing.T) {
42 dir := t.TempDir()
43 if err := WriteInstallMarker(dir, "shop.test", InstallMarker{Stamp: 1}); err != nil {
44 t.Fatal(err)
45 }
46 entries, err := os.ReadDir(filepath.Join(dir, ".installed"))
47 if err != nil {
48 t.Fatal(err)
49 }
50 if len(entries) != 1 {
51 t.Fatalf("%d files in .installed, want 1", len(entries))
52 }
53}
54
55func TestInstallMarkerForALongHostHasAShortFileName(t *testing.T) {
56 host := strings.Repeat("a", 200) + ".example.com"
57 p := InstallMarkerPath(t.TempDir(), host)
58 if n := len(filepath.Base(p)); n > 64 {
59 t.Fatalf("file name is %d bytes", n)
60 }
61}
62
63func TestADamagedMarkerIsAnErrorNotAbsence(t *testing.T) {
64 dir := t.TempDir()
65 if err := WriteInstallMarker(dir, "shop.test", InstallMarker{Stamp: 1}); err != nil {
66 t.Fatal(err)
67 }
68 if err := os.WriteFile(InstallMarkerPath(dir, "shop.test"), []byte("{half"), 0o600); err != nil {
69 t.Fatal(err)
70 }
71 if _, found, err := ReadInstallMarker(dir, "shop.test"); err == nil || found {
72 t.Fatalf("found=%v err=%v: a damaged marker must be an error", found, err)
73 }
74}
75