104 lines
3.7 KiB
Go
104 lines
3.7 KiB
Go
//go:build integration
|
|
|
|
package builder
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/novox/mesh-controller/internal/catalogue"
|
|
)
|
|
|
|
func TestABuildReadsEachTemplateBackFromItsArchive(t *testing.T) {
|
|
r, workspace := aRepository(t, templatedModule, templatedFiles)
|
|
got, err := Build(context.Background(), r.run, r, "https://forge.invalid/meshboard.git", "", "", workspace, nil,
|
|
Npmrc{}, GitCredential{}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if text := contentOf(t, got.Manifest, "conf"); text != templatedFiles["files/meshboard.conf"] {
|
|
t.Fatalf("the template's text is %q, not the file's", text)
|
|
}
|
|
if text := contentOf(t, got.Manifest, "deeper"); text != templatedFiles["files/a/deeper.conf"] {
|
|
t.Fatalf("the template's text is %q, not the file's", text)
|
|
}
|
|
if r.archives["meshboard/files"] == "" {
|
|
t.Fatalf("the templates archive was not published: %v", r.archives)
|
|
}
|
|
if problem := catalogue.HeldTemplateProblem(got.Manifest); problem != "" {
|
|
t.Fatal(problem)
|
|
}
|
|
}
|
|
|
|
func TestABuildWhoseTemplateIsMissingFailsNamingIt(t *testing.T) {
|
|
files := map[string]string{}
|
|
for k, v := range templatedFiles {
|
|
files[k] = v
|
|
}
|
|
delete(files, "files/a/deeper.conf")
|
|
r, workspace := aRepository(t, templatedModule, files)
|
|
_, err := Build(context.Background(), r.run, r, "https://forge.invalid/meshboard.git", "", "", workspace, nil,
|
|
Npmrc{}, GitCredential{}, nil)
|
|
if err == nil || !strings.Contains(err.Error(), "files/a/deeper.conf") {
|
|
t.Fatalf("a build whose template is missing did not fail naming it: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAWalkTakingAChecksBuildReadsTheTemplatesBackToo(t *testing.T) {
|
|
withVersion(t, "builder-1")
|
|
contents := map[string]string{"mod/" + ManifestName: templatedModule}
|
|
for k, v := range templatedFiles {
|
|
contents["mod/"+k] = v
|
|
}
|
|
scratch := newScratch()
|
|
r, run, workspace := aModuleWith(t, "tree-one", contents)
|
|
checked := aCheckBuiltBy(t, scratch, r, run, workspace)
|
|
|
|
r, run, workspace = aModuleWith(t, "tree-one", contents)
|
|
got, err := BuildReusing(t.Context(), run, r, Reuse{Scratch: scratch, Pulls: []PullRef{aPull}, Named: named(checked)},
|
|
"https://forge.invalid/meshboard.git", "mod", "main", workspace, nil, Npmrc{}, GitCredential{}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Reused == nil || len(r.archives) > 0 {
|
|
t.Fatalf("the walk built again: reused %+v, why %q, published %v", got.Reused, got.Why, r.archives)
|
|
}
|
|
if text := contentOf(t, got.Manifest, "conf"); text != templatedFiles["files/meshboard.conf"] {
|
|
t.Fatalf("the template's text is %q, not the file's", text)
|
|
}
|
|
}
|
|
|
|
func TestABuildReadsEachTemplateBackFromTheTemplatesDirectoryItsManifestNames(t *testing.T) {
|
|
files := map[string]string{"files/bin/meshboard-hook": "#!/bin/sh\n", "templates/meshboard.conf": "listen = 8080\n"}
|
|
r, workspace := aRepository(t, unpackingModule, files)
|
|
got, err := Build(context.Background(), r.run, r, "https://forge.invalid/meshboard.git", "", "", workspace, nil,
|
|
Npmrc{}, GitCredential{}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if text := contentOf(t, got.Manifest, "conf"); text != files["templates/meshboard.conf"] {
|
|
t.Fatalf("the template's text is %q, not the file's", text)
|
|
}
|
|
if got.Manifest.Templates != "" {
|
|
t.Fatalf("the built manifest still names its templates directory: %q", got.Manifest.Templates)
|
|
}
|
|
// files/ packs exactly as it would with no templates beside it.
|
|
alone, err := pack(writeTree(t, t.TempDir(), map[string]string{"bin/meshboard-hook": "#!/bin/sh\n"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(alone)
|
|
packed := ""
|
|
for _, b := range got.Built {
|
|
if b.Name == "scripts" {
|
|
packed = b.Digest
|
|
}
|
|
}
|
|
if packed != "sha256:"+hex.EncodeToString(sum[:]) {
|
|
t.Fatalf("files/ packed to %q, not to what it packs to alone", packed)
|
|
}
|
|
}
|