Skip to content

Commit

Permalink
more tests and more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
presbrey committed Aug 12, 2024
1 parent 0bba9f4 commit ab78f28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
16 changes: 6 additions & 10 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,18 @@ User=nobody
WantedBy=multi-user.target
`, exePath)

if err := os.WriteFile(systemdPath, []byte(serviceContent), 0644); err != nil {
if err = os.WriteFile(systemdPath, []byte(serviceContent), 0644); err != nil {
return err
}

// Enable and start the service
if err := runCommand("systemctl", "daemon-reload"); err != nil {
return err
}
if err := runCommand("systemctl", "enable", serviceName); err != nil {
return err
}
if err := runCommand("systemctl", "start", serviceName); err != nil {
return err
if err = runCommand("systemctl", "daemon-reload"); err == nil {
if err = runCommand("systemctl", "enable", serviceName); err == nil {
err = runCommand("systemctl", "start", serviceName)
}
}

return nil
return err
}

var run = func(cmd *exec.Cmd) error {
Expand Down
7 changes: 7 additions & 0 deletions install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os/exec"
"reflect"
"testing"

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

func TestInstallDefaultConfig(t *testing.T) {
Expand Down Expand Up @@ -63,6 +65,9 @@ func TestGetMachineID(t *testing.T) {

// Replace the machine-id file path
oldMachineIDPath := "/etc/machine-id"
pathMachineID = "/"
assert.Equal(t, "", getMachineID())

pathMachineID = tempFile.Name()
defer func() { pathMachineID = oldMachineIDPath }()

Expand All @@ -75,6 +80,8 @@ func TestGetMachineID(t *testing.T) {
}

func TestInstallSystemdService(t *testing.T) {
assert.NoError(t, run(exec.Command("ls")))

// Create a temporary file for the systemd service
tempFile, err := os.CreateTemp("", "test_systemd_service")
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ func TestRun(t *testing.T) {
err = do()
assert.NoError(t, err)

var lastServer *socks5.Server
oldListenAndServe := listenAndServe
defer func() {
listenAndServe = oldListenAndServe
}()
*flagInstall = false
*flagDaemon = true
listenAndServe = func(_ *socks5.Server, network, addr string) error {
listenAndServe = func(server *socks5.Server, network, addr string) error {
lastServer = server
return fmt.Errorf("testing without starting server (%v:%v)", network, addr)
}
assert.Error(t, oldListenAndServe(lastServer, "", ""))

err = do()
assert.Error(t, err, "failed to start server: testing without starting server (tcp:0.0.0.0:1080)")
assert.Error(t, err)
listenAndServe = func(_ *socks5.Server, _, _ string) error {
return nil
}
Expand Down

0 comments on commit ab78f28

Please sign in to comment.