aboutsummaryrefslogtreecommitdiffsponsor
path: root/pkgtools_test.go
blob: cafd433b8a56dec87f73c1dfda7f2ecfe152a070 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package pkgtools

import (
	"encoding/json"
	"io"
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestPackageBase(t *testing.T) {
	tests := []struct {
		FileName string
		Exp      string
	}{
		{
			FileName: "kernel-firmware-20220124_eb8ea1b-noarch-1.txz",
			Exp:      "kernel-firmware-20220124_eb8ea1b-noarch-1",
		},
		{
			FileName: "tar-1.34-x86_64-1.txz",
			Exp:      "tar-1.34-x86_64-1",
		},
		{
			FileName: "tar-1.34-x86_64-1.other",
			Exp:      "tar-1.34-x86_64-1.other",
		},
	}
	for _, test := range tests {
		name := PackageBase(test.FileName)
		require.Equal(t, test.Exp, name)
	}
}

func TestPackageName(t *testing.T) {
	tests := []struct {
		FileName string
		Exp      string
	}{
		{
			FileName: "kernel-firmware-20220124_eb8ea1b-noarch-1.txz",
			Exp:      "kernel-firmware",
		},
		{
			FileName: "tar-1.34-x86_64-1.txz",
			Exp:      "tar",
		},
		{
			FileName: "tar-1.34-x86_64-1",
			Exp:      "tar",
		},
	}
	for _, test := range tests {
		name, err := PackageName(test.FileName)
		require.NoError(t, err)
		require.Equal(t, test.Exp, name)
	}
}

func TestReadPackageSoftLinks(t *testing.T) {
	tests := []struct {
		File string
		Exp  []string
	}{
		{
			File: "/var/lib/pkgtools/scripts/acl-2.3.1-x86_64-1",
			Exp: []string{
				"lib64/libacl.so.1",
				"usr/lib64/libacl.so",
				"usr/lib64/libacl.so.1",
				"usr/lib64/libacl.so.1.1.2301",
			},
		},
	}

	for _, test := range tests {
		links, err := ReadPackageSoftLinks(test.File)
		require.NoError(t, err)
		require.Equal(t, test.Exp, links)
	}
}

func getInfo(fp string) (*PackageInfo, error) {
	slackPkg, err := OpenSlackwarePkg("testdata/tar-1.34-x86_64-1.tar")
	if err != nil {
		return nil, err
	}
	defer slackPkg.Close()

	for {
		entry, err := slackPkg.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, err
		}
		if entry == nil {
			continue
		}

		io.Copy(io.Discard, slackPkg)
	}
	return slackPkg.PkgInfo(), nil
}

func jsonRead(obj interface{}, fp string) error {
	buf, err := os.ReadFile(fp)
	if err != nil {
		return err
	}
	return json.Unmarshal(buf, obj)
}

func TestReadPackageInfo(t *testing.T) {
	tarFile := "testdata/kernel-source-5.15.27-noarch-1"
	expFile := "testdata/kernel-source-5.15.27-noarch-1_exp.json"

	start := time.Now()
	info, err := ReadPackageInfo(tarFile)
	require.NoError(t, err)

	d := time.Now().Sub(start)
	require.Less(t, d, 5*time.Second)

	var exp PackageInfo
	require.NoError(t, jsonRead(&exp, expFile))
	_ = info
	require.Equal(t, &exp, info)

}