Skip to content

ci: refactor tests #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/test-go-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ jobs:
- name: Run tests
env:
GO_MODULE_PATH: ${{ matrix.module.path }}
# run: task go:test ## TODO: refactor the tests
run: task go:test:docker
run: task go:test

- name: Send unit tests coverage to Codecov
if: runner.os == 'Linux'
Expand Down
6 changes: 0 additions & 6 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,3 @@ tasks:
-coverprofile=coverage_unit.txt \
{{.TEST_LDFLAGS}} \
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

go:test:docker:
desc: Run the tests inside a docker image
cmds:
- docker build -f testdata/Dockerfile -t go-apt-test:latest .
- docker run --rm go-apt-test
14 changes: 12 additions & 2 deletions apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"regexp"
"strconv"
Expand Down Expand Up @@ -85,6 +86,7 @@ func parseDpkgQueryOutput(out []byte) []*Package {

// CheckForUpdates runs an apt update to retrieve new packages available
// from the repositories
// NOTE: it requires root privileges to run
func CheckForUpdates() (output []byte, err error) {
cmd := exec.Command("apt-get", "update", "-q")
return cmd.CombinedOutput()
Expand All @@ -98,10 +100,18 @@ func ListUpgradable() ([]*Package, error) {
if err != nil {
return nil, fmt.Errorf("running apt list: %s", err)
}

res := parseListUpgradableOutput(bytes.NewReader(out))
return res, nil
}

func parseListUpgradableOutput(r io.Reader) []*Package {
// Example of matched line:
// xserver-xorg-core/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
re := regexp.MustCompile(`^([^ ]+) ([^ ]+) ([^ ]+)( \[upgradable from: [^\[\]]*\])?`)

res := []*Package{}
scanner := bufio.NewScanner(bytes.NewReader(out))
scanner := bufio.NewScanner(r)
for scanner.Scan() {
matches := re.FindAllStringSubmatch(scanner.Text(), -1)
if len(matches) == 0 {
Expand All @@ -120,7 +130,7 @@ func ListUpgradable() ([]*Package, error) {
Architecture: matches[0][3],
})
}
return res, nil
return res
}

// Upgrade runs the upgrade for a set of packages
Expand Down
71 changes: 65 additions & 6 deletions apt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"

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

func TestList(t *testing.T) {
func TestParseDpkgQueryOutput(t *testing.T) {
out, err := os.ReadFile("testdata/dpkg-query-output-1.txt")
require.NoError(t, err, "Reading test input data")
list := parseDpkgQueryOutput(out)
Expand Down Expand Up @@ -62,9 +63,67 @@ func TestListUpgradable(t *testing.T) {
require.NoError(t, err, "running List command")
}

func TestCheckForUpdates(t *testing.T) {
out, err := CheckForUpdates()
require.NoError(t, err, "running CheckForUpdate command")
fmt.Printf(">>>\n%s\n<<<\n", string(out))
fmt.Println("ERR:", err)
func TestParseListUpgradableOutput(t *testing.T) {
t.Run("edges cases", func(t *testing.T) {
tests := []struct {
name string
input string
expected []*Package
}{
{
name: "empty input",
input: "",
expected: []*Package{},
},
{
name: "line not matching regex",
input: "this-is-not a-valid-line\n",
expected: []*Package{},
},
{
name: "upgradable package without [upgradable from]",
input: "nano/bionic-updates 2.9.3-2 amd64\n",
expected: []*Package{
{
Name: "nano",
Status: "upgradable",
Version: "2.9.3-2",
Architecture: "amd64",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := parseListUpgradableOutput(strings.NewReader(tt.input))
require.Equal(t, tt.expected, res)
})
}
})

t.Run("golden file: list-upgradable.golden", func(t *testing.T) {
data, err := os.ReadFile("testdata/apt-list-upgradable.golden")
require.NoError(t, err, "Reading golden file")
result := parseListUpgradableOutput(strings.NewReader(string(data)))

want := []*Package{
{Name: "apt-transport-https", Status: "upgradable", Version: "2.0.11", Architecture: "all"},
{Name: "apt-utils", Status: "upgradable", Version: "2.0.11", Architecture: "amd64"},
{Name: "apt", Status: "upgradable", Version: "2.0.11", Architecture: "amd64"},
{Name: "code-insiders", Status: "upgradable", Version: "1.101.0-1749657374", Architecture: "amd64"},
{Name: "code", Status: "upgradable", Version: "1.100.3-1748872405", Architecture: "amd64"},
{Name: "containerd.io", Status: "upgradable", Version: "1.7.27-1", Architecture: "amd64"},
{Name: "distro-info-data", Status: "upgradable", Version: "0.43ubuntu1.18", Architecture: "all"},
{Name: "docker-ce-cli", Status: "upgradable", Version: "5:28.1.1-1~ubuntu.20.04~focal", Architecture: "amd64"},
{Name: "python3.12", Status: "upgradable", Version: "3.12.11-1+focal1", Architecture: "amd64"},
{Name: "xdg-desktop-portal", Status: "upgradable", Version: "1.14.3-1~flatpak1~20.04", Architecture: "amd64"},
{Name: "xserver-common", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "all"},
{Name: "xserver-xephyr", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
{Name: "xserver-xorg-core", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
{Name: "xserver-xorg-legacy", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
{Name: "xwayland", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
}
require.NotNil(t, result)
require.Equal(t, want, result, "Parsed result should match expected from golden file")
})
}
19 changes: 0 additions & 19 deletions testdata/Dockerfile

This file was deleted.

16 changes: 16 additions & 0 deletions testdata/apt-list-upgradable.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Listing... Done
apt-transport-https/focal-updates,focal-updates 2.0.11 all [upgradable from: 2.0.10]
apt-utils/focal-updates 2.0.11 amd64 [upgradable from: 2.0.10]
apt/focal-updates 2.0.11 amd64 [upgradable from: 2.0.10]
code-insiders/stable 1.101.0-1749657374 amd64 [upgradable from: 1.100.0-1743745333]
code/stable 1.100.3-1748872405 amd64 [upgradable from: 1.100.2-1747260578]
containerd.io/focal 1.7.27-1 amd64 [upgradable from: 1.7.25-1]
distro-info-data/focal-updates,focal-updates 0.43ubuntu1.18 all [upgradable from: 0.43ubuntu1.16]
docker-ce-cli/focal 5:28.1.1-1~ubuntu.20.04~focal amd64 [upgradable from: 5:28.0.1-1~ubuntu.20.04~focal]
python3.12/focal 3.12.11-1+focal1 amd64 [upgradable from: 3.12.5-1+focal1]
xdg-desktop-portal/focal 1.14.3-1~flatpak1~20.04 amd64 [upgradable from: 1.6.0-1ubuntu2]
xserver-common/focal-updates,focal-updates 2:1.20.13-1ubuntu1~20.04.20 all [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
xserver-xephyr/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
xserver-xorg-core/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
xserver-xorg-legacy/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
xwayland/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]