~james/core

core/spindle/mill/executor/cache_test.go -rw-r--r-- 2.1 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package executor

import (
	"testing"

	millv1 "tangled.org/core/spindle/mill/proto/gen"
	"tangled.org/core/spindle/models"
)

func TestCacheBindingsFromProtoUsesValidatedWorkflowEntries(t *testing.T) {
	const (
		repoDid   = "did:web:example.com"
		restoreID = "11111111-1111-4111-8111-111111111111"
		saveID    = "22222222-2222-4222-8222-222222222222"
	)
	entries := []models.CacheEntry{
		{Key: "deps", Paths: []string{"node_modules"}, CompressionLevel: 7, When: "always"},
	}
	bindings, err := cacheBindingsFromProto(repoDid, entries, []*millv1.CacheBinding{{
		EntryIndex: 0,
		RestoreId:  restoreID,
		RestoreKey: "objects/" + repoDid + "/" + restoreID,
		SaveId:     saveID,
		SaveKey:    "objects/" + repoDid + "/" + saveID,
	}})
	if err != nil {
		t.Fatal(err)
	}
	if len(bindings) != 1 {
		t.Fatalf("bindings = %+v", bindings)
	}
	got := bindings[0]
	if got.Key != "deps" || got.RestoreID != restoreID || got.SaveID != saveID || got.CompressionLevel != 7 || got.When != "always" {
		t.Fatalf("binding = %+v", got)
	}
}

func TestCacheBindingsFromProtoRejectsInvalidIndexes(t *testing.T) {
	entries := []models.CacheEntry{{Key: "deps", Paths: []string{"node_modules"}}}
	for _, bindings := range [][]*millv1.CacheBinding{
		{{EntryIndex: 1}},
		{{EntryIndex: 0}, {EntryIndex: 0}},
	} {
		if _, err := cacheBindingsFromProto("did:web:example.com", entries, bindings); err == nil {
			t.Fatalf("cacheBindingsFromProto(%+v) succeeded", bindings)
		}
	}
}

func TestCacheBindingsFromProtoRejectsForeignAndUnpairedObjects(t *testing.T) {
	const id = "11111111-1111-4111-8111-111111111111"
	entries := []models.CacheEntry{{Key: "deps", Paths: []string{"node_modules"}}}
	for _, binding := range []*millv1.CacheBinding{
		{EntryIndex: 0, SaveId: id},
		{EntryIndex: 0, SaveKey: "objects/did:web:example.com/" + id},
		{EntryIndex: 0, RestoreId: id, RestoreKey: "objects/did:web:other.example/" + id},
	} {
		if _, err := cacheBindingsFromProto(
			"did:web:example.com",
			entries,
			[]*millv1.CacheBinding{binding},
		); err == nil {
			t.Fatalf("foreign cache binding %+v succeeded", binding)
		}
	}
}