Skip to content

Commit

Permalink
feat(testing): add basic tests for most modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Dec 20, 2024
1 parent 7bc4ebe commit f592125
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 105 deletions.
4 changes: 1 addition & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func Update(cmd *cobra.Command, args []string) {

if systemOutdated {
const OUTDATED_WARNING = "There hasn't been an update in over a month. Consider rebooting or running updates manually"
err := session.Notify("System Warning", OUTDATED_WARNING)
err := session.Notify(users, "System Warning", OUTDATED_WARNING)
if err != nil {
slog.Error("Failed showing warning notification")
}
Expand Down Expand Up @@ -167,7 +167,6 @@ func Update(cmd *cobra.Command, args []string) {

if flatpakUpdater.Config.Enabled {
slog.Debug(fmt.Sprintf("%s module", flatpakUpdater.Config.Title), slog.String("module_name", flatpakUpdater.Config.Title), slog.Any("module_configuration", flatpakUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: flatpakUpdater.Config.Title, Description: flatpakUpdater.Config.Description})
var out *[]drv.CommandOutput
out, err = flatpakUpdater.Update()
outputs = append(outputs, *out...)
Expand All @@ -176,7 +175,6 @@ func Update(cmd *cobra.Command, args []string) {

if distroboxUpdater.Config.Enabled {
slog.Debug(fmt.Sprintf("%s module", distroboxUpdater.Config.Title), slog.String("module_name", distroboxUpdater.Config.Title), slog.Any("module_configuration", distroboxUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: distroboxUpdater.Config.Title, Description: distroboxUpdater.Config.Description})
var out *[]drv.CommandOutput
out, err = distroboxUpdater.Update()
outputs = append(outputs, *out...)
Expand Down
35 changes: 35 additions & 0 deletions drv/brew/brew_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package brew_test

import (
"testing"

"github.com/ublue-os/uupd/drv/brew"
"github.com/ublue-os/uupd/drv/generic"
appLogging "github.com/ublue-os/uupd/pkg/logging"
)

func InitBaseConfig() brew.BrewUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
Ci: false,
Verbose: false,
Environment: nil,
Logger: appLogging.NewMuteLogger(),
}
driv, _ := brew.BrewUpdater{}.New(initConfiguration)
return driv
}

func TestProperSteps(t *testing.T) {
brewUpdater := InitBaseConfig()
brewUpdater.Config.Enabled = false

if brewUpdater.Steps() != 0 {
t.Fatalf("Expected no steps when module is disabled")
}

brewUpdater.Config.Enabled = true
if brewUpdater.Steps() == 0 {
t.Fatalf("Expected steps to be added")
}
}
7 changes: 1 addition & 6 deletions drv/distrobox/distrobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ func (up DistroboxUpdater) New(config UpdaterInitConfiguration) (DistroboxUpdate
up.usersEnabled = false
up.Tracker = nil

binaryPath, exists := up.Config.Environment["UUPD_DISTROBOX_BINARY"]
if !exists || binaryPath == "" {
up.binaryPath = "/usr/bin/distrobox"
} else {
up.binaryPath = binaryPath
}
up.binaryPath = EnvOrFallback(up.Config.Environment, "UUPD_DISTROBOX_BINARY", "/usr/bin/distrobox")

return up, nil
}
Expand Down
56 changes: 56 additions & 0 deletions drv/distrobox/distrobox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package distrobox_test

import (
"log"
"testing"

"github.com/ublue-os/uupd/drv/distrobox"
"github.com/ublue-os/uupd/drv/generic"
appLogging "github.com/ublue-os/uupd/pkg/logging"
"github.com/ublue-os/uupd/pkg/session"
)

func InitBaseConfig() distrobox.DistroboxUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
Ci: false,
Verbose: false,
Environment: nil,
Logger: appLogging.NewMuteLogger(),
}
driv, _ := distrobox.DistroboxUpdater{}.New(initConfiguration)
return driv
}

func TestProperSteps(t *testing.T) {
updater := InitBaseConfig()
updater.Config.Enabled = false

if updater.Steps() != 0 {
t.Fatalf("Expected no steps when module is disabled")
}

updater.Config.Enabled = true
if updater.Steps() == 0 {
t.Fatalf("Expected steps to be added")
}
}

func TestProperUserSteps(t *testing.T) {
updater := InitBaseConfig()

mockUser := []session.User{
{UID: 0, Name: "root"},
{UID: 1, Name: "roote"},
{UID: 2, Name: "rooto"},
}
updater.SetUsers(mockUser)

if reported := updater.Steps(); reported != 1+len(mockUser) {
log.Fatalf("Incorrect number of steps for users: %d", reported)
}
updater.Config.Enabled = false
if reported := updater.Steps(); reported != 0 {
log.Fatalf("Incorrect number of steps for users: %d", reported)
}
}
7 changes: 1 addition & 6 deletions drv/flatpak/flatpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ func (up FlatpakUpdater) New(config UpdaterInitConfiguration) (FlatpakUpdater, e
up.usersEnabled = false
up.Tracker = nil

binaryPath, exists := up.Config.Environment["UUPD_FLATPAK_BINARY"]
if !exists || binaryPath == "" {
up.binaryPath = "/usr/bin/flatpak"
} else {
up.binaryPath = binaryPath
}
up.binaryPath = EnvOrFallback(up.Config.Environment, "UUPD_FLATPAK_BINARY", "/usr/bin/flatpak")

return up, nil
}
Expand Down
56 changes: 56 additions & 0 deletions drv/flatpak/flatpak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package flatpak_test

import (
"log"
"testing"

"github.com/ublue-os/uupd/drv/flatpak"
"github.com/ublue-os/uupd/drv/generic"
appLogging "github.com/ublue-os/uupd/pkg/logging"
"github.com/ublue-os/uupd/pkg/session"
)

func InitBaseConfig() flatpak.FlatpakUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
Ci: false,
Verbose: false,
Environment: nil,
Logger: appLogging.NewMuteLogger(),
}
driv, _ := flatpak.FlatpakUpdater{}.New(initConfiguration)
return driv
}

func TestProperSteps(t *testing.T) {
updater := InitBaseConfig()
updater.Config.Enabled = false

if updater.Steps() != 0 {
t.Fatalf("Expected no steps when module is disabled")
}

updater.Config.Enabled = true
if updater.Steps() == 0 {
t.Fatalf("Expected steps to be added")
}
}

func TestProperUserSteps(t *testing.T) {
updater := InitBaseConfig()

mockUser := []session.User{
{UID: 0, Name: "root"},
{UID: 1, Name: "roote"},
{UID: 2, Name: "rooto"},
}
updater.SetUsers(mockUser)

if reported := updater.Steps(); reported != 1+len(mockUser) {
log.Fatalf("Incorrect number of steps for users: %d", reported)
}
updater.Config.Enabled = false
if reported := updater.Steps(); reported != 0 {
log.Fatalf("Incorrect number of steps for users: %d", reported)
}
}
13 changes: 4 additions & 9 deletions drv/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@ func EnvOrFallback(environment EnvironmentMap, key string, fallback string) stri
return fallback
}

func GetEnvironment(data []string, getkeyval func(item string) (key, val string)) map[string]string {
func GetEnvironment(data []string) map[string]string {
items := make(map[string]string)
for _, item := range data {
key, val := getkeyval(item)
items[key] = val
splits := strings.Split(item, "=")
items[splits[0]] = splits[1]
}
return items
}

func (up UpdaterInitConfiguration) New() *UpdaterInitConfiguration {
up.DryRun = false
up.Ci = false
up.Environment = GetEnvironment(os.Environ(), func(item string) (key, val string) {
splits := strings.Split(item, "=")
key = splits[0]
val = splits[1]
return
})
up.Environment = GetEnvironment(os.Environ())
up.Logger = slog.Default()

return &up
Expand Down
21 changes: 21 additions & 0 deletions drv/generic/generic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic_test

import (
"log"
"testing"

"github.com/ublue-os/uupd/drv/generic"
Expand All @@ -17,3 +18,23 @@ func TestFallBack(t *testing.T) {
t.Fatalf("Getting the fallback fails, %s", value)
}
}

func TestProperEnvironment(t *testing.T) {
valuesExpected := map[string]string{
"SIGMA": "true",
"CHUD": "false",
"AMOGUS": "sus",
"NOTHING": "",
}

for key, value := range valuesExpected {
testmap := generic.GetEnvironment([]string{key + "=" + value})
valuegot, exists := testmap[key]
if !exists {
log.Fatalf("Could not get environment variable at all: %s", key)
}
if valuegot != value {
log.Fatalf("Value gotten from variable was not expected: Got %s, Expected %s", valuegot, value)
}
}
}
19 changes: 7 additions & 12 deletions drv/rpmostree/rpmostree.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ type RpmOstreeUpdater struct {
BinaryPath string
}

// Checks if it is at least a month old considering how that works
func IsOutdatedOneMonthTimestamp(current time.Time, target time.Time) bool {
return target.Before(current.AddDate(0, -1, 0))
}

func (up RpmOstreeUpdater) Outdated() (bool, error) {
if up.Config.DryRun {
return false, nil
}
oneMonthAgo := time.Now().AddDate(0, -1, 0)
var timestamp time.Time

cmd := exec.Command(up.BinaryPath, "status", "--json", "--booted")
Expand All @@ -44,7 +48,7 @@ func (up RpmOstreeUpdater) Outdated() (bool, error) {
}
timestamp = time.Unix(status.Deployments[0].Timestamp, 0).UTC()

return timestamp.Before(oneMonthAgo), nil
return IsOutdatedOneMonthTimestamp(time.Now(), timestamp), nil
}

func (up RpmOstreeUpdater) Update() (*[]CommandOutput, error) {
Expand Down Expand Up @@ -77,16 +81,7 @@ func (up RpmOstreeUpdater) New(config UpdaterInitConfiguration) (RpmOstreeUpdate
Environment: config.Environment,
}
up.Config.Logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title)))
if up.Config.DryRun {
return up, nil
}

binaryPath, exists := up.Config.Environment["UUPD_RPMOSTREE_BINARY"]
if !exists || binaryPath == "" {
up.BinaryPath = "/usr/bin/rpm-ostree"
} else {
up.BinaryPath = binaryPath
}
up.BinaryPath = EnvOrFallback(up.Config.Environment, "UUPD_RPMOSTREE_BINARY", "/usr/bin/rpm-ostree")

return up, nil
}
Expand Down
35 changes: 35 additions & 0 deletions drv/rpmostree/rpmostree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package rpmostree_test

import (
"testing"

"github.com/ublue-os/uupd/drv/generic"
"github.com/ublue-os/uupd/drv/rpmostree"
appLogging "github.com/ublue-os/uupd/pkg/logging"
)

func InitBaseConfig() rpmostree.RpmOstreeUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
Ci: false,
Verbose: false,
Environment: nil,
Logger: appLogging.NewMuteLogger(),
}
driv, _ := rpmostree.RpmOstreeUpdater{}.New(initConfiguration)
return driv
}

func TestProperSteps(t *testing.T) {
rpmostreeUpdater := InitBaseConfig()
rpmostreeUpdater.Config.Enabled = false

if rpmostreeUpdater.Steps() != 0 {
t.Fatalf("Expected no steps when module is disabled")
}

rpmostreeUpdater.Config.Enabled = true
if rpmostreeUpdater.Steps() == 0 {
t.Fatalf("Expected steps to be added")
}
}
7 changes: 1 addition & 6 deletions drv/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ type SystemUpdater struct {
BinaryPath string
}

// Checks if it is at least a month old considering how that works
func IsOutdatedOneMonthTimestamp(current time.Time, target time.Time) bool {
return target.Before(current.AddDate(0, -1, 0))
}

func (up SystemUpdater) Outdated() (bool, error) {
if up.Config.DryRun {
return false, nil
Expand All @@ -69,7 +64,7 @@ func (up SystemUpdater) Outdated() (bool, error) {
if err != nil {
return false, nil
}
return IsOutdatedOneMonthTimestamp(time.Now(), timestamp), nil
return rpmostree.IsOutdatedOneMonthTimestamp(time.Now(), timestamp), nil
}

func (up SystemUpdater) Update() (*[]CommandOutput, error) {
Expand Down
18 changes: 14 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,20 @@ lint:
release:
goreleaser

test:
go test -v -cover ./...
test directory="":
#!/usr/bin/env bash
if [ "{{directory}}" != "" ] ; then
go test -v -cover ./{{directory}}/...
else
go test -v -cover ./...
fi
test-interactive:
test-coverage directory="":
#!/usr/bin/env bash
t="/tmp/go-cover.$$.tmp"
go test -v -coverprofile=$t ./... $@ && go tool cover -html=$t && unlink $t
if [ "{{directory}}" != "" ] ; then
go test -v -coverprofile=$t ./{{directory}}/... $@ && go tool cover -html=$t && unlink $t
else
go test -v -coverprofile=$t ./... $@ && go tool cover -html=$t && unlink $t
fi
Loading

0 comments on commit f592125

Please sign in to comment.