File: packed_binaries_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (45 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (4)
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
package gitaly

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
)

func TestUnpackAuxiliaryBinaries_success(t *testing.T) {
	destinationDir := t.TempDir()
	require.NoError(t, UnpackAuxiliaryBinaries(destinationDir))

	entries, err := os.ReadDir(destinationDir)
	require.NoError(t, err)

	require.Greater(t, len(entries), 1, "expected multiple packed binaries present")

	for _, entry := range entries {
		fileInfo, err := entry.Info()
		require.NoError(t, err)
		require.Equal(t, fileInfo.Mode(), perm.PrivateExecutable, "expected the owner to have rwx permissions on the unpacked binary")

		sourceBinary, err := os.ReadFile(filepath.Join(buildDir, fileInfo.Name()))
		require.NoError(t, err)

		unpackedBinary, err := os.ReadFile(filepath.Join(destinationDir, fileInfo.Name()))
		require.NoError(t, err)

		require.Equal(t, sourceBinary, unpackedBinary, "unpacked binary does not match the source binary")
	}
}

func TestUnpackAuxiliaryBinaries_alreadyExists(t *testing.T) {
	destinationDir := t.TempDir()

	existingFile := filepath.Join(destinationDir, "gitaly-hooks")
	require.NoError(t, os.WriteFile(existingFile, []byte("existing file"), perm.PublicFile))

	err := UnpackAuxiliaryBinaries(destinationDir)
	require.EqualError(t, err, fmt.Sprintf(`open %s: file exists`, existingFile), "expected unpacking to fail if destination binary already existed")
}