~james/core

core/spindle/logview/logview_test.go -rw-r--r-- 2.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package logview

import (
	"context"
	"fmt"
	"io"
	"strings"
	"testing"

	"tangled.org/core/spindle/db"
	"tangled.org/core/spindle/models"
)

type recordingReader struct {
	contents map[string]string
	opens    []string
}

func (r *recordingReader) Open(_ context.Context, ref string) (io.ReadCloser, error) {
	r.opens = append(r.opens, ref)
	content, ok := r.contents[ref]
	if !ok {
		return nil, fmt.Errorf("missing artifact %q", ref)
	}
	return io.NopCloser(strings.NewReader(content)), nil
}

func TestFollowDoesNotUseAnotherPipelineArtifact(t *testing.T) {
	d, err := db.Make(context.Background(), t.TempDir()+"/spindle.db")
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })

	target := models.WorkflowId{PipelineId: models.PipelineId("target"), Name: "build.yml"}
	other := models.WorkflowId{PipelineId: models.PipelineId("other"), Name: target.Name}
	if err := d.SaveArtifactRef("other-lease", "did:plc:other", other, "other.log", "hash"); err != nil {
		t.Fatal(err)
	}

	reader := &recordingReader{contents: map[string]string{"other.log": "wrong log\n"}}
	if _, _, err := Follow(context.Background(), d, reader, t.TempDir(), target, true); err == nil {
		t.Fatal("Follow succeeded without an artifact for the target pipeline")
	}
	if len(reader.opens) != 0 {
		t.Fatalf("opened unrelated artifacts: %v", reader.opens)
	}
}

func TestFollowReadsExactPipelineArtifact(t *testing.T) {
	d, err := db.Make(context.Background(), t.TempDir()+"/spindle.db")
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })

	target := models.WorkflowId{PipelineId: models.PipelineId("target"), Name: "build.yml"}
	other := models.WorkflowId{PipelineId: models.PipelineId("other"), Name: target.Name}
	if err := d.SaveArtifactRef("other-lease", "did:plc:other", other, "other.log", "hash"); err != nil {
		t.Fatal(err)
	}
	if err := d.SaveArtifactRef("target-lease", "did:plc:target", target, "target.log", "hash"); err != nil {
		t.Fatal(err)
	}

	reader := &recordingReader{contents: map[string]string{
		"other.log":  "wrong log\n",
		"target.log": "target log\n",
	}}
	lines, stop, err := Follow(context.Background(), d, reader, t.TempDir(), target, true)
	if err != nil {
		t.Fatal(err)
	}
	defer stop()
	line := <-lines
	if line == nil || line.Text != "target log" {
		t.Fatalf("line = %#v, want target log", line)
	}
	if len(reader.opens) != 1 || reader.opens[0] != "target.log" {
		t.Fatalf("opened artifacts = %v, want target.log", reader.opens)
	}
}