aboutsummaryrefslogtreecommitdiffsponsor
path: root/removepkg_integration_test.go
blob: 15801bd2ab91266db8033ca834e18a833f58fe7b (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
//go:build integration
// +build integration

package pkgtools

import (
	"fmt"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"sort"
	"testing"

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

func findSlackwarePackages(root string) ([]string, error) {
	var pkgs []string
	err := filepath.WalkDir(
		root,
		func(path string, d fs.DirEntry, err error) error {
			if err != nil {
				log.Fatal(err)
			}
			if IsSlackwarePkg(d.Name()) {
				pkgs = append(pkgs, filepath.Join(path))
			}
			return nil
		},
	)
	if err != nil {
		return nil, err
	}
	sort.Strings(pkgs)
	return pkgs, nil
}

func removePkg(pkgs []string, p func(str string) bool) []string {
	var ret []string
	for _, v := range pkgs {
		if p(v) {
			ret = append(ret, v)
		}
	}
	return ret
}

func saveError(t *testing.T, file string, repErr error) {
	f, err := os.Create(filepath.Join("error", file))
	require.NoError(t, err)
	defer f.Close()

	fmt.Fprintln(f, repErr)
}

// TODO: Adapt to test against official installpkg, removepkg

func TestRemoveInstalledPkg(t *testing.T) {
	require.NotEmpty(t, os.Getenv("ROOT"), "expected ROOT environment variable to be set")
	root := os.Getenv("ROOT")
	require.NotEqual(t, "/", root, "a test directory should be used for ROOT.  All files there will be deleted")
	pkgName := "bzip2"

	removeFlags := DefaultRemovePkgFlags
	removeFlags.Root = root
	err := RemovePkg(&removeFlags, pkgName)
	require.NoError(t, err)

}

func TestInstallAll(t *testing.T) {
	pkgsPerRound := 50

	require.NotEmpty(t, os.Getenv("SOURCE"), "expected SOURCE environment variable to be set")
	from := os.Getenv("SOURCE")
	pkgs, err := findSlackwarePackages(from)
	require.NoError(t, err)

	require.NotEmpty(t, os.Getenv("ROOT"), "expected ROOT environment variable to be set")
	root := os.Getenv("ROOT")
	require.NotEqual(t, "/", root, "a test directory should be used for ROOT.  All files there will be deleted")

	_ = os.RemoveAll(root)

	installFlags := DefaultInstallPkgFlags
	installFlags.chmod = false
	installFlags.chown = false
	installFlags.SetEnvValues()

	removeFlags := DefaultRemovePkgFlags
	removeFlags.SetEnvValues()

	for i := 0; i < len(pkgs); i += pkgsPerRound {
		end := (i + pkgsPerRound)
		if end >= len(pkgs) {
			end = len(pkgs) - 1
		}
		roundPkgs := pkgs[i:end]

		for _, pkg := range roundPkgs {
			name, err := PackageName(pkg)
			require.NoError(t, err)
			err = InstallPkg(&installFlags, pkg)
			if err != nil {
				saveError(t, "installpkg-"+name, err)
				continue
			}
			_, err = GetPackageInfo(installFlags.Root, PackageBase(pkg))
			require.NoError(t, err, "package must be installed")
		}

		for _, pkg := range roundPkgs {
			name, err := PackageName(pkg)
			require.NoError(t, err)
			err = RemovePkg(&removeFlags, name)
			if err != nil {
				saveError(t, "removepkg-"+name, err)
			}
		}
	}
}