Files
nfpm/files/files_test.go
Dj GilcreaseandClaude Fable 5.1 c953fe3995 feat: add msi support (#1097)
* chore(deps): add go-msi and go-pkcs12

go.digitalxero.dev/go-msi is the pure-Go Windows Installer database
writer, validator, reader and Authenticode signer behind the new msi
packager; go-pkcs12 becomes a direct dependency for the signing tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* feat: add msi support

Adds an `msi` packager that writes a real Windows Installer database with
the pure-Go go-msi library: contents are mapped onto the standard Windows
folders (everything else under the product's Program Files folder), root
metadata (description, homepage, license, vendor/maintainer, scripts) is
reused where MSI has a place for it, and advertised shortcuts, Windows
services, registry values, per-user installs and Authenticode signing are
exposed under `msi:`.

Every release replaces older installs of the same upgrade code in place:
the upgrade code and component GUIDs are derived from identities that
survive releases, while the product code follows the full version. A
version Windows Installer cannot represent is an error rather than a
silently clamped (and colliding) one.

Maintainer scripts run as elevated deferred custom actions embedded in the
package so they work before any file is installed and after every file is
removed; they are extracted to a randomly named, freshly created file
since the SYSTEM temp directory is world-writable.

Unit tests build real packages, run go-msi's ICE validator and assert on
the decoded tables; golden GUIDs pin the derivation contract. The
acceptance suite installs, exercises, upgrades and removes the package
with msiexec on a Windows runner and proves a failing script rolls back.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* ci: validate nfpm-signed MSI on real Windows

Build and Authenticode-sign the MSI on Linux with nfpm's pure-Go signer
and a throwaway openssl certificate, then validate the signature with the
real Windows trust stack (Get-AuthenticodeSignature, signtool verify) and
install, exercise and remove it with msiexec on a Windows runner. The
unsigned msi-windows-install job remains as the control.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* chore(deps): update go-msi to latest main

* Revert "chore(deps): update go-msi to latest main"

This reverts commit 05aaa91684.

* ci: integrate MSI validation with merge queue checks

* fix(msi): decode quoted maintainer names

* fix: preserve address-only maintainer fallback

* fix(msi): update go-msi and support explicit file versions

Pin the latest upstream fixes for string references, column limits, and PE version detection. Expose per-destination file version overrides so intentionally unversioned Go binaries can still be packaged, and update fixtures, docs, and schema.

* chore(deps): pin go-msi v0.5.1 release

* fix(msi): Correct downgrade removal and shortcut resources

Match Windows destinations without case sensitivity, reuse PE icon streams by source and target extension, and cover newer products in downgrade removal ranges.

Add table regressions, native icon extraction and ICE50 checks, and Windows downgrade acceptance with distinct payloads. Run MSI installation checks on pull requests.

See #1097

* test(msi): Correct native icon extraction assertions

Distinguish one RT_GROUP_ICON resource from the two extracted icon handles requested from the Windows shell. Preserve checks for both handles and cover malformed resource directories and extensionless icon targets.

See #1097

* fix(msi): Adopt go-msi v0.5.2 and reject install-path aliases

Use the released dependency fixes for downgrade sequencing, non-elevating per-user packages, and lossless non-ASCII summary metadata. Preserve the explicit removal rule for allowed downgrades.

Reject duplicate resolved installation targets while preserving GUID identities. Add downstream table regressions and native silent-downgrade and standard-user Unicode/script verification.

See #1097

* fix(msi): Adopt go-msi v0.5.3 and close authoring gaps

* test(msi): Normalize redirected native install paths

* fix(msi): Preserve upgrade targets and release artifacts

* fix(msi): Adopt go-msi v0.5.5 signing and AppSearch fixes

* fix(msi): expand remaining strings and prove hook rollback

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-11 02:25:17 +00:00

1380 lines
31 KiB
Go

package files_test
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/goreleaser/nfpm/v2/files"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v3"
)
var mtime = time.Date(2023, 11, 5, 23, 15, 17, 0, time.UTC)
type testStruct struct {
Contents files.Contents `yaml:"contents"`
}
func TestBasicDecode(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: a
dst: b
- src: a
dst: b
type: "config|noreplace"
packager: "rpm"
file_info:
mode: 0644
mtime: 2008-01-02T15:04:05Z
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
require.Len(t, config.Contents, 2)
for _, f := range config.Contents {
t.Logf("%+#v\n", f)
require.Equal(t, "a", f.Source)
require.Equal(t, "b", f.Destination)
}
}
func TestDeepPathsWithGlobAndUmask(t *testing.T) {
path := filepath.Join(t.TempDir(), "foo", "bar", "zaz", "file.txt")
// create a bunch of files with bad permissions
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o777))
require.NoError(t, os.WriteFile(path, nil, 0o777))
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/globtest/**/*
dst: /bla
file_info:
mode: 0644
mtime: 2008-01-02T15:04:05Z
- src: testdata/deep-paths/
dst: /bar
- src: ` + path + `
dst: /foo/file.txt
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
require.Len(t, config.Contents, 3)
parsedContents, err := files.PrepareForPackager(
config.Contents,
0o133,
"",
false,
mtime,
)
require.NoError(t, err)
for _, c := range parsedContents {
switch c.Source {
case "testdata/globtest/nested/b.txt":
require.Equal(t, "/bla/nested/b.txt", c.Destination)
require.Equal(t, "-rw-r--r--", c.Mode().String())
case "testdata/globtest/multi-nested/subdir/c.txt":
require.Equal(t, "/bla/multi-nested/subdir/c.txt", c.Destination)
require.Equal(t, "-rw-r--r--", c.Mode().String())
case "testdata/deep-paths/nested1/nested2/a.txt":
require.Equal(t, "/bar/nested1/nested2/a.txt", c.Destination)
require.Equal(t, "-rw-r--r--", c.Mode().String())
case path:
require.Equal(t, "/foo/file.txt", c.Destination)
require.Equal(t, "-rw-r--r--", c.Mode().String())
}
}
}
func TestDeepPathsWithoutGlob(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/deep-paths/
dst: /bla
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
require.Len(t, config.Contents, 1)
parsedContents, err := files.PrepareForPackager(
config.Contents,
0,
"",
true,
mtime,
)
require.NoError(t, err)
present := false
for _, f := range parsedContents {
switch f.Source {
case "testdata/deep-paths/nested1/nested2/a.txt":
present = true
require.Equal(t, "/bla/nested1/nested2/a.txt", f.Destination)
case "":
continue
default:
t.Errorf("unknown source %s for content %#v", f.Source, f)
}
}
require.True(t, present)
}
func TestFileInfoDefault(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: files_test.go
dst: b
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
config.Contents, err = files.PrepareForPackager(
config.Contents,
0,
"",
true,
mtime,
)
require.NoError(t, err)
require.Len(t, config.Contents, 1)
fi, err := os.Stat("files_test.go")
require.NoError(t, err)
f := config.Contents[0]
require.Equal(t, "files_test.go", f.Source)
require.Equal(t, "/b", f.Destination)
require.Equal(t, f.FileInfo.Mode, fi.Mode())
require.Equal(t, f.FileInfo.MTime, mtime)
}
func TestFileInfo(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: files_test.go
dst: b
type: "config|noreplace"
packager: "rpm"
file_info:
mode: 0123
mtime: 2008-01-02T15:04:05Z
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
config.Contents, err = files.PrepareForPackager(
config.Contents,
0,
"rpm",
true,
mtime,
)
require.NoError(t, err)
require.Len(t, config.Contents, 1)
ct, err := time.Parse(time.RFC3339, "2008-01-02T15:04:05Z")
require.NoError(t, err)
f := config.Contents[0]
require.Equal(t, "files_test.go", f.Source)
require.Equal(t, "/b", f.Destination)
require.Equal(t, f.FileInfo.Mode, os.FileMode(0o123))
require.Equal(t, f.FileInfo.MTime, ct)
}
func TestSymlinksInDirectory(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/symlinks/subdir
dst: /bla
- src: testdata/symlinks/link-1
dst: /
- src: testdata/symlinks/link-2
dst: /
- src: existent
dst: /bla/link-3
type: symlink
`))
dec.KnownFields(true)
err := dec.Decode(&config)
require.NoError(t, err)
config.Contents, err = files.PrepareForPackager(
config.Contents,
0,
"",
true,
mtime,
)
require.NoError(t, err)
config.Contents = withoutFileInfo(config.Contents)
expected := files.Contents{
{
Source: "",
Destination: "/bla/",
Type: files.TypeImplicitDir,
},
{
Source: "testdata/symlinks/subdir/existent",
Destination: "/bla/existent",
Type: files.TypeFile,
},
{
Source: "non-existent",
Destination: "/bla/link-1",
Type: files.TypeSymlink,
},
{
Source: "existent",
Destination: "/bla/link-2",
Type: files.TypeSymlink,
},
{
Source: "existent",
Destination: "/bla/link-3",
Type: files.TypeSymlink,
},
{
Source: "broken",
Destination: "/link-1",
Type: files.TypeSymlink,
},
{
Source: "bla",
Destination: "/link-2",
Type: files.TypeSymlink,
},
}
require.Equal(t, expected, config.Contents)
}
func TestRace(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: a
dst: b
type: symlink
`))
err := dec.Decode(&config)
require.NoError(t, err)
errs := make(chan error, 10)
t.Cleanup(func() { close(errs) })
var wg sync.WaitGroup
for range 10 {
wg.Go(func() {
_, err := files.PrepareForPackager(
config.Contents,
0,
"",
false,
mtime,
)
errs <- err
})
}
wg.Wait()
for range 10 {
require.NoError(t, <-errs)
}
}
func TestCollision(t *testing.T) {
t.Run("collision between files for all packagers", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination"},
}
_, err := files.PrepareForPackager(
configuredFiles,
0,
"",
true,
mtime,
)
require.ErrorIs(t, err, files.ErrContentCollision)
})
t.Run("no collision due to different per-file packagers", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination", Packager: "foo"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination", Packager: "bar"},
}
_, err := files.PrepareForPackager(
configuredFiles,
0,
"foo",
true,
mtime,
)
require.NoError(t, err)
})
t.Run("collision between file for all packagers and file for specific packager", func(t *testing.T) {
configuredFiles := []*files.Content{
{Source: "../testdata/whatever.conf", Destination: "/samedestination", Packager: "foo"},
{Source: "../testdata/whatever2.conf", Destination: "/samedestination", Packager: ""},
}
_, err := files.PrepareForPackager(
configuredFiles,
0,
"foo",
true,
mtime,
)
require.ErrorIs(t, err, files.ErrContentCollision)
})
}
func TestDisableGlobbing(t *testing.T) {
testCases := []files.Content{
{
Source: "testdata/{test}/bar",
Destination: "/etc/{test}/bar",
},
{
Source: "testdata/{test}/[f]oo",
Destination: "testdata/{test}/[f]oo",
},
{
Source: "testdata/globtest/a.txt",
Destination: "testdata/globtest/a.txt",
},
{
Source: "testdata/globtest/a.txt",
Destination: "/etc/a.txt",
},
}
disableGlobbing := true
for i, testCase := range testCases {
content := testCase
t.Run(strconv.Itoa(i), func(t *testing.T) {
result, err := files.PrepareForPackager(
files.Contents{&content},
0,
"",
disableGlobbing,
mtime,
)
if err != nil {
t.Fatalf("expand content globs: %v", err)
}
result = withoutImplicitDirs(result)
if len(result) != 1 {
t.Fatalf("unexpected result length: %d, expected one", len(result))
}
actualContent := result[0]
// we expect the result content to be identical to the input content
if actualContent.Source != content.Source {
t.Fatalf("unexpected content source: %q, expected %q", actualContent.Source, content.Source)
}
if strings.TrimLeft(actualContent.Destination, "./") != strings.TrimLeft(content.Destination, "/") {
t.Fatalf("unexpected content destination: %q, expected %q",
strings.TrimLeft(actualContent.Destination, "./"), strings.TrimLeft(content.Destination, "/"))
}
})
}
}
func withoutImplicitDirs(contents files.Contents) files.Contents {
filtered := make(files.Contents, 0, len(contents))
for _, c := range contents {
if c.Type != files.TypeImplicitDir {
filtered = append(filtered, c)
}
}
return filtered
}
func TestGlobbingWhenFilesHaveBrackets(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on windows")
}
result, err := files.PrepareForPackager(
files.Contents{
{
Source: "./testdata/\\{test\\}/",
Destination: ".",
},
},
0,
"",
false,
mtime,
)
if err != nil {
t.Fatalf("expand content globs: %v", err)
}
expected := files.Contents{
{
Source: "testdata/{test}/[f]oo",
Destination: "/[f]oo",
},
{
Source: "testdata/{test}/bar",
Destination: "/bar",
},
}
if len(result) != 2 {
t.Fatalf("unexpected result length: %d, expected one", len(result))
}
for i, r := range result {
ex := expected[i]
if ex.Source != r.Source {
t.Fatalf("unexpected content source: %q, expected %q", r.Source, ex.Source)
}
if ex.Destination != r.Destination {
t.Fatalf("unexpected content destination: %q, expected %q",
ex.Destination, r.Destination)
}
}
}
func TestGlobbingFilesWithDifferentSizesWithFileInfo(t *testing.T) {
result, err := files.PrepareForPackager(
files.Contents{
{
Source: "./testdata/globtest/different-sizes/**/*",
Destination: ".",
FileInfo: &files.ContentFileInfo{
Mode: 0o777,
},
},
},
0,
"",
false,
mtime,
)
if err != nil {
t.Fatalf("expand content globs: %v", err)
}
result = withoutImplicitDirs(result)
if len(result) != 2 {
t.Fatalf("unexpected result length: %d, expected 2", len(result))
}
if result[0].FileInfo.Size == result[1].FileInfo.Size {
t.Fatal("test FileInfos have the same size, expected different")
}
}
func TestDestEndsWithSlash(t *testing.T) {
result, err := files.PrepareForPackager(
files.Contents{
{
Source: "./testdata/globtest/a.txt",
Destination: "./foo/",
},
},
0,
"",
false,
mtime,
)
result = withoutImplicitDirs(result)
require.NoError(t, err)
require.Len(t, result, 1)
require.Equal(t, "/foo/a.txt", result[0].Destination)
}
func TestInvalidFileType(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/globtest/**/*
dst: /bla
type: filr
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
_, err := files.PrepareForPackager(
config.Contents,
0,
"",
false,
mtime,
)
require.EqualError(t, err, "invalid content type: filr")
}
func TestValidFileTypes(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/globtest/a.txt
dst: /f1.txt
- src: testdata/globtest/a.txt
dst: /f2.txt
type: file
- src: testdata/globtest/a.txt
dst: /f3.txt
type: config
- src: testdata/globtest/a.txt
dst: /f4.txt
type: config|noreplace
- src: testdata/globtest/a.txt
dst: /f5.txt
type: symlink
- src: testdata/globtest/a.txt
dst: /f6.txt
type: dir
- src: testdata/globtest/a.txt
dst: /f7.txt
type: ghost
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
_, err := files.PrepareForPackager(
config.Contents,
0,
"",
false,
mtime,
)
require.NoError(t, err)
}
func TestImplicitDirectories(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: "./testdata/globtest/a.txt",
Destination: "./foo/bar/baz",
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
expected := files.Contents{
{
Source: "",
Destination: "/foo/",
Type: files.TypeImplicitDir,
},
{
Source: "",
Destination: "/foo/bar/",
Type: files.TypeImplicitDir,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/foo/bar/baz",
Type: files.TypeFile,
},
}
require.Equal(t, expected, withoutFileInfo(results))
}
// TestDriveLetterDestinationTerminates guards against the parent-directory walk
// looping forever on a Windows drive-letter destination (filepath.Dir gets
// stuck at the volume root), which previously caused an out-of-memory crash.
func TestDriveLetterDestinationTerminates(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: "./testdata/globtest/a.txt",
Destination: "C:/Program Files/App/a.txt",
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
var file *files.Content
for _, c := range results {
if c.Type == files.TypeFile {
file = c
}
}
require.NotNil(t, file)
require.Equal(t, "testdata/globtest/a.txt", file.Source)
}
func TestRelevantFiles(t *testing.T) {
contents := files.Contents{
{
Source: "./testdata/globtest/a.txt",
Destination: "/1allpackagers",
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/2onlyrpm",
Packager: "rpm",
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/3onlydeb",
Packager: "deb",
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/4debchangelog",
Type: files.TypeDebChangelog,
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/5ghost",
Type: files.TypeRPMGhost,
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/6doc",
Type: files.TypeRPMDoc,
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/7licence",
Type: files.TypeRPMLicence,
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/8license",
Type: files.TypeRPMLicense,
},
{
Source: "./testdata/globtest/a.txt",
Destination: "/9readme",
Type: files.TypeRPMReadme,
},
}
t.Run("deb", func(t *testing.T) {
results, err := files.PrepareForPackager(
contents,
0,
"deb",
false,
mtime,
)
require.NoError(t, err)
require.Equal(t, files.Contents{
{
Source: "testdata/globtest/a.txt",
Destination: "/1allpackagers",
Type: files.TypeFile,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/3onlydeb",
Packager: "deb",
Type: files.TypeFile,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/4debchangelog",
Type: files.TypeDebChangelog,
},
}, withoutFileInfo(results))
})
t.Run("rpm", func(t *testing.T) {
results, err := files.PrepareForPackager(
contents,
0,
"rpm",
false,
mtime,
)
require.NoError(t, err)
require.Equal(t, files.Contents{
{
Source: "testdata/globtest/a.txt",
Destination: "/1allpackagers",
Type: files.TypeFile,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/2onlyrpm",
Packager: "rpm",
Type: files.TypeFile,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/5ghost",
Type: files.TypeRPMGhost,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/6doc",
Type: files.TypeRPMDoc,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/7licence",
Type: files.TypeRPMLicence,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/8license",
Type: files.TypeRPMLicense,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/9readme",
Type: files.TypeRPMReadme,
},
}, withoutFileInfo(results))
})
t.Run("apk", func(t *testing.T) {
results, err := files.PrepareForPackager(
contents,
0,
"apk",
false,
mtime,
)
require.NoError(t, err)
require.Equal(t, files.Contents{
{
Source: "testdata/globtest/a.txt",
Destination: "/1allpackagers",
Type: files.TypeFile,
},
}, withoutFileInfo(results))
})
t.Run("msi", func(t *testing.T) {
results, err := files.PrepareForPackager(
contents,
0,
"msi",
false,
mtime,
)
require.NoError(t, err)
require.Equal(t, files.Contents{
{
Source: "testdata/globtest/a.txt",
Destination: "/1allpackagers",
Type: files.TypeFile,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/7licence",
Type: files.TypeRPMLicence,
},
{
Source: "testdata/globtest/a.txt",
Destination: "/8license",
Type: files.TypeRPMLicense,
},
}, withoutFileInfo(results))
})
}
func TestTreeOwner(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "tree"),
Destination: "/usr/share/foo",
Type: files.TypeTree,
FileInfo: &files.ContentFileInfo{
Owner: "goreleaser",
Group: "goreleaser",
MTime: mtime,
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
for _, f := range results {
if f.Destination == "/usr/" || f.Destination == "/usr/share/" {
require.Equal(t, "root", f.FileInfo.Owner, f.Destination)
require.Equal(t, "root", f.FileInfo.Group, f.Destination)
continue
}
require.Equal(t, "goreleaser", f.FileInfo.Owner, f.Destination)
require.Equal(t, "goreleaser", f.FileInfo.Group, f.Destination)
}
require.Equal(t, files.Contents{
{
Source: "",
Destination: "/usr/",
Type: files.TypeImplicitDir,
},
{
Source: "",
Destination: "/usr/share/",
Type: files.TypeImplicitDir,
},
{
Source: "",
Destination: "/usr/share/foo/",
Type: files.TypeDir,
},
{
Source: "",
Destination: "/usr/share/foo/files/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "a"),
Destination: "/usr/share/foo/files/a",
Type: files.TypeFile,
},
{
Source: "",
Destination: "/usr/share/foo/files/b/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "b", "c"),
Destination: "/usr/share/foo/files/b/c",
Type: files.TypeFile,
},
{
Source: "",
Destination: "/usr/share/foo/symlinks/",
Type: files.TypeDir,
},
{
Source: "/etc/foo",
Destination: "/usr/share/foo/symlinks/link1",
Type: files.TypeSymlink,
},
{
Source: "../files/a",
Destination: "/usr/share/foo/symlinks/link2",
Type: files.TypeSymlink,
},
}, withoutFileInfo(results))
}
func TestTree(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "tree"),
Destination: "/base",
Type: files.TypeTree,
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
require.Equal(t, files.Contents{
{
Source: "",
Destination: "/base/",
Type: files.TypeDir,
},
{
Source: "",
Destination: "/base/files/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "a"),
Destination: "/base/files/a",
Type: files.TypeFile,
},
{
Source: "",
Destination: "/base/files/b/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "b", "c"),
Destination: "/base/files/b/c",
Type: files.TypeFile,
},
{
Source: "",
Destination: "/base/symlinks/",
Type: files.TypeDir,
},
{
Source: "/etc/foo",
Destination: "/base/symlinks/link1",
Type: files.TypeSymlink,
},
{
Source: "../files/a",
Destination: "/base/symlinks/link2",
Type: files.TypeSymlink,
},
}, withoutFileInfo(results))
}
func TestConfigTree(t *testing.T) {
for _, tt := range []struct {
treeType string
fileType string
}{
{files.TypeConfigTree, files.TypeConfig},
{files.TypeConfigNoReplaceTree, files.TypeConfigNoReplace},
{files.TypeConfigMissingOKTree, files.TypeConfigMissingOK},
} {
t.Run(tt.treeType, func(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "tree"),
Destination: "/base",
Type: tt.treeType,
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
// Only regular files become config files; directories and symlinks
// keep their natural type.
require.Equal(t, files.Contents{
{
Source: "",
Destination: "/base/",
Type: files.TypeDir,
},
{
Source: "",
Destination: "/base/files/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "a"),
Destination: "/base/files/a",
Type: tt.fileType,
},
{
Source: "",
Destination: "/base/files/b/",
Type: files.TypeDir,
},
{
Source: filepath.Join("testdata", "tree", "files", "b", "c"),
Destination: "/base/files/b/c",
Type: tt.fileType,
},
{
Source: "",
Destination: "/base/symlinks/",
Type: files.TypeDir,
},
{
Source: "/etc/foo",
Destination: "/base/symlinks/link1",
Type: files.TypeSymlink,
},
{
Source: "../files/a",
Destination: "/base/symlinks/link2",
Type: files.TypeSymlink,
},
}, withoutFileInfo(results))
})
}
}
func TestContentLang(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "globtest", "**", "*.txt"),
Destination: "/usr/share/locale",
FileInfo: &files.ContentFileInfo{
Lang: "en",
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
// The lang attribute must survive glob expansion on every resulting file.
var sawFile bool
for _, c := range results {
if c.Type == files.TypeFile {
sawFile = true
require.Equal(t, "en", c.FileInfo.Lang, c.Destination)
}
}
require.True(t, sawFile, "expected at least one globbed file")
}
func TestTreeMode(t *testing.T) {
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "tree"),
Destination: "/usr/share/foo",
Type: files.TypeTree,
FileInfo: &files.ContentFileInfo{
Mode: 0o777,
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
for _, f := range results {
if f.Destination == "/usr/" || f.Destination == "/usr/share/" || f.Type == files.TypeSymlink {
require.NotEqual(t, fs.FileMode(0o777).String(), f.FileInfo.Mode.String(), f.Destination)
continue
}
require.Equal(t, fs.FileMode(0o777).String(), f.FileInfo.Mode.String(), "%s: %s", f.Destination, f.Type)
}
}
func withoutFileInfo(contents files.Contents) files.Contents {
filtered := make(files.Contents, 0, len(contents))
for _, c := range contents {
cc := *c
cc.FileInfo = nil
filtered = append(filtered, &cc)
}
return filtered
}
func TestTreeOwnerFsOwnedDirs(t *testing.T) {
// When a tree maps to a destination that contains filesystem-owned
// directories (like /usr, /usr/local), those dirs must not inherit
// the tree's custom owner/group.
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "treefs"),
Destination: "/",
Type: files.TypeTree,
FileInfo: &files.ContentFileInfo{
Owner: "custom",
Group: "custom",
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
fsOwned := map[string]bool{
"/usr/": true,
"/usr/local/": true,
"/usr/local/bin/": true,
}
for _, f := range results {
if fsOwned[f.Destination] {
require.Equal(t, "root", f.FileInfo.Owner,
"%s should have root owner", f.Destination)
require.Equal(t, "root", f.FileInfo.Group,
"%s should have root group", f.Destination)
} else {
require.Equal(t, "custom", f.FileInfo.Owner,
"%s should have custom owner", f.Destination)
require.Equal(t, "custom", f.FileInfo.Group,
"%s should have custom group", f.Destination)
}
}
}
func TestTreeFileMode(t *testing.T) {
// Verify that regular files in a tree preserve their source permissions
// rather than getting a zero mode from d.Type().
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "tree"),
Destination: "/base",
Type: files.TypeTree,
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
for _, f := range results {
if f.Type != files.TypeFile {
continue
}
info, err := os.Stat(f.Source)
require.NoError(t, err)
require.Equal(t, info.Mode(), f.FileInfo.Mode,
"file %s mode should match source", f.Destination)
}
}
func TestAsRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "etc/foo/",
"./etc/foo": "etc/foo",
"./././foo/../bar/": "bar/",
sep: "",
sep + sep: "",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "foo/bar/zazz",
}
for input, expected := range testCases {
assert.Equal(t, expected, files.AsRelativePath(input))
}
}
func TestAsExplicitRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "./etc/foo/",
"./etc/foo": "./etc/foo",
"./././foo/../bar/": "./bar/",
sep: "./",
sep: "./",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "./foo/bar/zazz",
}
for input, expected := range testCases {
assert.Equal(t, expected, files.AsExplicitRelativePath(input))
}
}
func TestIssue954(t *testing.T) {
tmpDir := t.TempDir()
contentDir := filepath.Join(tmpDir, "content")
require.NoError(t, os.MkdirAll(filepath.Join(contentDir, "subdir1"), 0o755))
require.NoError(t, os.MkdirAll(filepath.Join(contentDir, "subdir2"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(contentDir, "subdir1", "file1.txt"), []byte("test"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(contentDir, "subdir2", "file2.txt"), []byte("test"), 0o644))
results, err := files.PrepareForPackager(
files.Contents{
{
Source: contentDir + "/",
Destination: "/srv/www",
FileInfo: &files.ContentFileInfo{
Owner: "www-data",
Group: "www-data",
Mode: 0o775,
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
found := []string{}
for _, f := range results {
found = append(found, f.Destination)
if f.Destination == "/srv/" {
require.Equal(t, "root", f.FileInfo.Owner, f.Destination)
require.Equal(t, "root", f.FileInfo.Group, f.Destination)
continue
}
require.Contains(t, f.Destination, "/srv/www")
require.Equal(t, "www-data", f.FileInfo.Owner, f.Destination)
require.Equal(t, "www-data", f.FileInfo.Group, f.Destination)
}
require.ElementsMatch(t, []string{
"/srv/",
"/srv/www/",
"/srv/www/subdir1/",
"/srv/www/subdir1/file1.txt",
"/srv/www/subdir2/",
"/srv/www/subdir2/file2.txt",
}, found)
}
func TestIssue952(t *testing.T) {
contents := files.Contents{
{
Source: "/source",
Destination: "/dest",
Type: files.TypeSymlink,
},
}
result, err := files.PrepareForPackager(contents, 0, "", false, mtime)
require.NoError(t, err)
require.Len(t, result, 1)
symlink := result[0]
require.Equal(t, files.TypeSymlink, symlink.Type)
require.Equal(t, "/dest", symlink.Destination)
require.Equal(t, mtime, symlink.FileInfo.MTime)
}
func TestIssue829(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
# Implementation.
- src: ./testdata/issue829/lib/
dst: /usr/lib/
type: tree
- src: ./testdata/issue829/usr/
dst: /usr/
type: tree
- src: ./testdata/issue829/var/
dst: /var/
type: tree
# Configuration.
- src: ./testdata/issue829/etc/logrotate.d/acme
dst: /etc/logrotate.d/acme
type: config|noreplace
- src: ./testdata/issue829/etc/sysconfig/acme
dst: /etc/sysconfig/acme
type: config|noreplace
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
files, err := files.PrepareForPackager(
config.Contents,
0,
"",
false,
mtime,
)
require.NoError(t, err)
expect := map[string]string{
"/etc/": "implicit dir",
"/etc/logrotate.d/": "implicit dir",
"/etc/logrotate.d/acme": "config|noreplace",
"/etc/sysconfig/": "implicit dir",
"/etc/sysconfig/acme": "config|noreplace",
"/usr/lib/": "implicit dir",
"/usr/lib/systemd/": "implicit dir",
"/usr/lib/systemd/system/": "implicit dir",
"/usr/lib/systemd/system/acme.service": "file",
"/usr/": "implicit dir",
"/usr/bin/": "implicit dir",
"/usr/bin/acme": "file",
"/usr/share/": "implicit dir",
"/usr/share/doc/": "implicit dir",
"/usr/share/doc/acme/": "dir",
"/usr/share/doc/acme/readme.md": "file",
"/usr/share/man/": "implicit dir",
"/usr/share/man/man1/": "implicit dir",
"/usr/share/man/man1/acme.1.gz": "file",
"/var/": "implicit dir",
"/var/log/": "implicit dir",
"/var/log/acme/": "dir",
"/var/log/acme/.gitkeep": "file",
}
for _, file := range files {
require.Equal(t, expect[file.Destination], file.Type, "invalid type for %s", file.Destination)
}
}
func TestTreeWithDisown(t *testing.T) {
files, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "disown"),
Type: files.TypeTree,
DisownSubtree: []string{
"/disowned-dir",
"*/nested-disowned",
"owned-dir/nested-disowned/",
"*matched-dir",
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
expect := map[string]string{
"/disowned-matched-dir/": "implicit dir",
"/disowned-matched-dir/file.txt": "file",
"/disowned-dir/": "implicit dir",
"/disowned-dir/nested-disowned/": "implicit dir",
"/disowned-dir/nested-disowned/file.txt": "file",
"/disowned-dir/nested-owned/": "dir",
"/disowned-dir/nested-owned/file.txt": "file",
"/owned-dir/": "dir",
"/owned-dir/nested-disowned/": "implicit dir",
"/owned-dir/nested-disowned/file.txt": "file",
"/owned-dir/file.txt": "file",
}
for _, file := range files {
require.Equal(t, expect[file.Destination], file.Type, "invalid type for %s", file.Destination)
}
}