Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9ee5faf
Replace interface{} with any
kolyshkin Aug 6, 2025
07efb51
Use for range for integers
kolyshkin Aug 6, 2025
6c64a8d
sdjournal/journal_test.go: use slices.Contains
kolyshkin Aug 6, 2025
376e0cd
login1: remove unused method
kolyshkin Aug 6, 2025
476279d
sdjournal,machine1: don't use rand.Seed in test
kolyshkin Aug 6, 2025
1f212c8
Remove io/ioutil usage
kolyshkin Aug 6, 2025
50a6630
dbus: simplify if condition in a test
kolyshkin Aug 6, 2025
d168ac7
dbus: fix some godoc comments
kolyshkin Aug 6, 2025
30abda1
daemon,journal: use t.Setenv in tests
kolyshkin Aug 6, 2025
3cbff4d
journal,sdjournal: use %q in tests
kolyshkin Aug 6, 2025
d8d5995
sdjournal: simplify/fix tests
kolyshkin Aug 6, 2025
d997b93
dbus: fix SA5001 linter warning in tests
kolyshkin Aug 7, 2025
d8964ac
activation: fix SA1006 staticcheck warnings in tests
kolyshkin Aug 7, 2025
adf9320
activation: simplify/unify connectStringWritten
kolyshkin Aug 7, 2025
4dcd007
unit: fix SA4001 staticcheck warning
kolyshkin Aug 12, 2025
5586a97
machine1: fix some godoc strings
kolyshkin Aug 12, 2025
a196f27
dbus: fix ST1016 staticcheck warning
kolyshkin Aug 12, 2025
9317ea5
unit: add package doc
kolyshkin Aug 12, 2025
832c0c0
unit: fix SA4004 staticcheck warning
kolyshkin Aug 12, 2025
b83d8a7
sdjournal: fix SA1024 staticcheck warning
kolyshkin Aug 12, 2025
456ef98
journal: apply De Morgan law
kolyshkin Aug 12, 2025
d38e4b7
machine1: fix SA1019 staticcheck warnings in tests
kolyshkin Aug 12, 2025
1e2eee7
activation: Files: fix ercheck warnings
kolyshkin Aug 12, 2025
ba4d799
dbus: deprecate KillUnitContext
kolyshkin Aug 13, 2025
1282df7
dbus: ignore a few errcheck warnings
kolyshkin Aug 12, 2025
1151292
activation: fix errcheck warnings in tests
kolyshkin Aug 12, 2025
9102adf
dbus: avoid zombies in test
kolyshkin Aug 13, 2025
f82fcb4
dbus: TestFreezer: check runStopUnit error
kolyshkin Aug 13, 2025
1313182
dbus: fix stopUnit test helper
kolyshkin Aug 13, 2025
b9c5324
Fix some "error value not checked" linter warnings
kolyshkin Aug 13, 2025
c6dae46
Gofumpt the code
kolyshkin Aug 13, 2025
c763162
Remove obsoleted build tags
kolyshkin Aug 13, 2025
08292ea
ci: enable golangci-lint
kolyshkin Aug 13, 2025
60c4c55
scripts/ci-runner: less Fedora packages
kolyshkin Aug 13, 2025
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
18 changes: 14 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: Go fmt
run: ./scripts/ci-runner.sh go_fmt
- name: Go build (source)
run: ./scripts/ci-runner.sh build_source
- name: Go build (tests)
run: ./scripts/ci-runner.sh build_tests
- name: Go vet
run: ./scripts/ci-runner.sh go_vet
lint:
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: |
sudo apt-get -qq update
sudo apt-get -qq install libsystemd-dev
- uses: actions/checkout@v5
- uses: actions/setup-go@v5
with:
go-version: stable
- uses: golangci/golangci-lint-action@v8
with:
version: v2.3

all-done:
needs:
Expand Down
19 changes: 19 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"

formatters:
enable:
- gofumpt

linters:
settings:
staticcheck:
checks:
- all
- -ST1003 # https://staticcheck.dev/docs/checks/#ST1003 Should not use underscores in Go names.
- -ST1005 # https://staticcheck.dev/docs/checks/#ST1005 Error strings should not be capitalized.
govet:
enable:
- nilness
exclusions:
presets:
- std-error-handling
17 changes: 10 additions & 7 deletions activation/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import (

// correctStringWritten fails the text if the correct string wasn't written
// to the other side of the pipe.
func correctStringWritten(t *testing.T, r *os.File, expected string) bool {
func correctStringWritten(t *testing.T, r io.Reader, expected string) {
t.Helper()

bytes := make([]byte, len(expected))
io.ReadAtLeast(r, bytes, len(expected))
_, err := io.ReadAtLeast(r, bytes, len(expected))
if err != nil {
t.Fatal(err)
}

if string(bytes) != expected {
t.Fatalf("Unexpected string %s", string(bytes))
}

return true
}

// TestActivation forks out a copy of activation.go example and reads back two
Expand All @@ -53,7 +56,7 @@ func TestActivation(t *testing.T) {

err := cmd.Run()
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

correctStringWritten(t, r1, "Hello world: fd1")
Expand All @@ -68,7 +71,7 @@ func TestActivationNoFix(t *testing.T) {

out, _ := cmd.CombinedOutput()
if !bytes.Contains(out, []byte("No files")) {
t.Fatalf("Child didn't error out as expected")
t.Fatal("Child didn't error out as expected")
}
}

Expand All @@ -80,6 +83,6 @@ func TestActivationNoFiles(t *testing.T) {

out, _ := cmd.CombinedOutput()
if !bytes.Contains(out, []byte("No files")) {
t.Fatalf("Child didn't error out as expected")
t.Fatal("Child didn't error out as expected")
}
}
10 changes: 6 additions & 4 deletions activation/files_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

//go:build !windows
// +build !windows

// Package activation implements primitives for systemd socket activation.
package activation
Expand All @@ -38,9 +37,12 @@ const (
// fd usage and to avoid leaking environment flags to child processes.
func Files(unsetEnv bool) []*os.File {
if unsetEnv {
defer os.Unsetenv("LISTEN_PID")
defer os.Unsetenv("LISTEN_FDS")
defer os.Unsetenv("LISTEN_FDNAMES")
defer func() {
// Unsetenv implementation for unix never returns an error.
_ = os.Unsetenv("LISTEN_PID")
_ = os.Unsetenv("LISTEN_FDS")
_ = os.Unsetenv("LISTEN_FDNAMES")
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling the error from os.Unsetenv() instead of discarding it, as this could indicate permission issues or other environment problems.

Suggested change
_ = os.Unsetenv("LISTEN_FDNAMES")
if err := os.Unsetenv("LISTEN_PID"); err != nil {
log.Printf("failed to unset LISTEN_PID: %v", err)
}
if err := os.Unsetenv("LISTEN_FDS"); err != nil {
log.Printf("failed to unset LISTEN_FDS: %v", err)
}
if err := os.Unsetenv("LISTEN_FDNAMES"); err != nil {
log.Printf("failed to unset LISTEN_FDNAMES: %v", err)
}

Copilot uses AI. Check for mistakes.

}()
}

pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
Expand Down
37 changes: 13 additions & 24 deletions activation/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,12 @@
package activation

import (
"io"
"net"
"os"
"os/exec"
"testing"
)

// correctStringWritten fails the text if the correct string wasn't written
// to the other side of the pipe.
func correctStringWrittenNet(t *testing.T, r net.Conn, expected string) bool {
bytes := make([]byte, len(expected))
io.ReadAtLeast(r, bytes, len(expected))

if string(bytes) != expected {
t.Fatalf("Unexpected string %s", string(bytes))
}

return true
}

// TestActivation forks out a copy of activation.go example and reads back two
// strings from the pipes that are passed in.
func TestListeners(t *testing.T) {
Expand All @@ -43,11 +29,11 @@ func TestListeners(t *testing.T) {

l1, err := net.Listen("tcp", ":9999")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
l2, err := net.Listen("tcp", ":1234")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

t1 := l1.(*net.TCPListener)
Expand All @@ -63,25 +49,28 @@ func TestListeners(t *testing.T) {

r1, err := net.Dial("tcp", "127.0.0.1:9999")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
if _, err := r1.Write([]byte("Hi")); err != nil {
t.Fatal(err)
}
r1.Write([]byte("Hi"))

r2, err := net.Dial("tcp", "127.0.0.1:1234")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
if _, err := r2.Write([]byte("Hi")); err != nil {
t.Fatal(err)
}
r2.Write([]byte("Hi"))

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")

out, err := cmd.CombinedOutput()
if err != nil {
println(string(out))
t.Fatalf(err.Error())
t.Fatalf("Unexpected error: %v (command output: %s)", err, out)
}

correctStringWrittenNet(t, r1, "Hello world: fd1")
correctStringWrittenNet(t, r2, "Goodbye world: fd2")
correctStringWritten(t, r1, "Hello world: fd1")
correctStringWritten(t, r2, "Goodbye world: fd2")
}
20 changes: 12 additions & 8 deletions activation/packetconns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func TestPacketConns(t *testing.T) {

u1, err := net.ListenUDP("udp", &net.UDPAddr{Port: 9999})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
u2, err := net.ListenUDP("udp", &net.UDPAddr{Port: 1234})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

f1, _ := u1.File()
Expand All @@ -46,15 +46,19 @@ func TestPacketConns(t *testing.T) {

r1, err := net.Dial("udp", "127.0.0.1:9999")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
if _, err := r1.Write([]byte("Hi")); err != nil {
t.Fatal(err)
}
r1.Write([]byte("Hi"))

r2, err := net.Dial("udp", "127.0.0.1:1234")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
if _, err := r2.Write([]byte("Hi")); err != nil {
t.Fatal(err)
}
r2.Write([]byte("Hi"))

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")
Expand All @@ -64,6 +68,6 @@ func TestPacketConns(t *testing.T) {
t.Fatalf("Cmd output '%s', err: '%s'\n", out, err)
}

correctStringWrittenNet(t, r1, "Hello world")
correctStringWrittenNet(t, r2, "Goodbye world")
correctStringWritten(t, r1, "Hello world")
correctStringWritten(t, r2, "Goodbye world")
}
20 changes: 5 additions & 15 deletions daemon/sdnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,22 @@ package daemon

import (
"fmt"
"io/ioutil"
"net"
"os"
"testing"
)

// TestSdNotify
func TestSdNotify(t *testing.T) {

testDir, e := ioutil.TempDir("/tmp/", "test-")
if e != nil {
panic(e)
}
defer os.RemoveAll(testDir)
testDir := t.TempDir()

notifySocket := testDir + "/notify-socket.sock"
laddr := net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}
_, e = net.ListenUnixgram("unixgram", &laddr)
if e != nil {
panic(e)
_, err := net.ListenUnixgram("unixgram", &laddr)
if err != nil {
t.Fatal(err)
}

tests := []struct {
Expand All @@ -57,10 +50,7 @@ func TestSdNotify(t *testing.T) {
}

for i, tt := range tests {
must(os.Unsetenv("NOTIFY_SOCKET"))
if tt.envSocket != "" {
must(os.Setenv("NOTIFY_SOCKET", tt.envSocket))
}
t.Setenv("NOTIFY_SOCKET", tt.envSocket)
sent, err := SdNotify(tt.unsetEnv, fmt.Sprintf("TestSdNotify test message #%d", i))

if sent != tt.wsent {
Expand Down
18 changes: 2 additions & 16 deletions daemon/watchdog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
"time"
)

func must(err error) {
if err != nil {
panic(err)
}
}

func TestSdWatchdogEnabled(t *testing.T) {
mypid := strconv.Itoa(os.Getpid())
tests := []struct {
Expand Down Expand Up @@ -57,16 +51,8 @@ func TestSdWatchdogEnabled(t *testing.T) {
}

for i, tt := range tests {
if tt.usec != "" {
must(os.Setenv("WATCHDOG_USEC", tt.usec))
} else {
must(os.Unsetenv("WATCHDOG_USEC"))
}
if tt.pid != "" {
must(os.Setenv("WATCHDOG_PID", tt.pid))
} else {
must(os.Unsetenv("WATCHDOG_PID"))
}
t.Setenv("WATCHDOG_USEC", tt.usec)
t.Setenv("WATCHDOG_PID", tt.pid)

delay, err := SdWatchdogEnabled(tt.unsetEnv)

Expand Down
3 changes: 2 additions & 1 deletion dbus/dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Integration with the systemd D-Bus API. See http://www.freedesktop.org/wiki/Software/systemd/dbus/
// Package dbus provides integration with the systemd D-Bus API.
// See http://www.freedesktop.org/wiki/Software/systemd/dbus/
package dbus

import (
Expand Down
11 changes: 4 additions & 7 deletions dbus/dbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
func TestNeedsEscape(t *testing.T) {
// Anything not 0-9a-zA-Z should always be escaped
for want, vals := range map[bool][]byte{
false: []byte{'a', 'b', 'z', 'A', 'Q', '1', '4', '9'},
true: []byte{'#', '%', '$', '!', '.', '_', '-', '%', '\\'},
false: {'a', 'b', 'z', 'A', 'Q', '1', '4', '9'},
true: {'#', '%', '$', '!', '.', '_', '-', '%', '\\'},
} {
for i := 1; i < 10; i++ {
for _, b := range vals {
Expand All @@ -36,8 +36,8 @@ func TestNeedsEscape(t *testing.T) {

// 0-9 in position 0 should be escaped
for want, vals := range map[bool][]byte{
false: []byte{'A', 'a', 'e', 'x', 'Q', 'Z'},
true: []byte{'0', '4', '5', '9'},
false: {'A', 'a', 'e', 'x', 'Q', 'Z'},
true: {'0', '4', '5', '9'},
} {
for _, b := range vals {
got := needsEscape(0, b)
Expand All @@ -46,7 +46,6 @@ func TestNeedsEscape(t *testing.T) {
}
}
}

}

func TestPathBusEscape(t *testing.T) {
Expand All @@ -64,7 +63,6 @@ func TestPathBusEscape(t *testing.T) {
t.Errorf("bad result for PathBusEscape(%s): got %q, want %q", in, got, want)
}
}

}

func TestPathBusUnescape(t *testing.T) {
Expand All @@ -89,7 +87,6 @@ func TestPathBusUnescape(t *testing.T) {
// TestNew ensures that New() works without errors.
func TestNew(t *testing.T) {
_, err := New()

if err != nil {
t.Fatal(err)
}
Expand Down
Loading