Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 75fd80e

Browse files
Federico Guerinonipippolo84
authored andcommitted
Add test for checkInstallNetworkManager()
Signed-off-by: Federico Guerinoni <guerra@develer.com>
1 parent 0d1fbe7 commit 75fd80e

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

handlers_stats.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os/exec"
25-
"strings"
2625

2726
apt "github.com/arduino/go-apt-client"
2827
"github.com/arduino/go-system-stats/disk"
@@ -70,25 +69,23 @@ func checkAndInstallNetworkManager() {
7069
if err == nil {
7170
return
7271
}
73-
if strings.Contains(err.Error(), "NetworkManager") {
74-
// dpkg --configure -a for prevent block of installation
75-
dpkgCmd := exec.Command("dpkg", "--configure", "-a")
76-
if out, err := dpkgCmd.CombinedOutput(); err != nil {
77-
fmt.Println("Failed to dpkg configure all:")
78-
fmt.Println(string(out))
79-
}
80-
toInstall := &apt.Package{Name: "network-manager"}
81-
if out, err := apt.Install(toInstall); err != nil {
82-
fmt.Println("Failed to install network-manager:")
83-
fmt.Println(string(out))
84-
return
85-
}
86-
cmd := exec.Command("/etc/init.d/network-manager", "start")
87-
if out, err := cmd.CombinedOutput(); err != nil {
88-
fmt.Println("Failed to start network-manager:")
89-
fmt.Println(string(out))
90-
}
9172

73+
dpkgCmd := exec.Command("dpkg", "--configure", "-a")
74+
if out, err := dpkgCmd.CombinedOutput(); err != nil {
75+
fmt.Println("Failed to dpkg configure all:")
76+
fmt.Println(string(out))
77+
}
78+
79+
toInstall := &apt.Package{Name: "network-manager"}
80+
if out, err := apt.Install(toInstall); err != nil {
81+
fmt.Println("Failed to install network-manager:")
82+
fmt.Println(string(out))
83+
return
84+
}
85+
cmd := exec.Command("/etc/init.d/network-manager", "start")
86+
if out, err := cmd.CombinedOutput(); err != nil {
87+
fmt.Println("Failed to start network-manager:")
88+
fmt.Println(string(out))
9289
}
9390
}
9491

install.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"net"
3636
"net/http"
3737
"os"
38-
"os/exec"
3938
"path/filepath"
4039
"strings"
4140
"time"
@@ -68,15 +67,6 @@ func createConfigFolder() error {
6867
return nil
6968
}
7069

71-
func isDockerInstalled() (bool, error) {
72-
_, err := exec.LookPath("docker")
73-
if err == nil {
74-
return true, nil
75-
}
76-
77-
return false, nil
78-
}
79-
8070
// Register creates the necessary certificates and configuration files
8171
func register(config Config, configFile, token string) {
8272
// Request token

install_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/http"
1818
"net/http/httptest"
1919
"os"
20+
"os/exec"
2021
"path/filepath"
2122
"strings"
2223
"sync"
@@ -363,9 +364,37 @@ func connectTestClient(crtPath, keyPath string) (mqtt.Client, error) {
363364
return mqttClient, nil
364365
}
365366

367+
func isDockerInstalled() (bool, error) {
368+
_, err := exec.LookPath("docker")
369+
if err == nil {
370+
return true, nil
371+
}
372+
373+
return false, nil
374+
}
375+
366376
func TestInstallDocker(t *testing.T) {
367377
checkAndInstallDocker()
368378
installed, err := isDockerInstalled()
369379
assert.True(t, err == nil)
370380
assert.True(t, installed)
371381
}
382+
383+
func isNetManagerInstalled() bool {
384+
cmd := exec.Command("bash", "-c", "dpkg --get-selections | grep network-manager")
385+
out, _ := cmd.CombinedOutput()
386+
return !strings.Contains(string(out), "deinstall") && len(out) != 0
387+
}
388+
389+
func TestInstallNetworkManager(t *testing.T) {
390+
assert.False(t, isNetManagerInstalled())
391+
checkAndInstallNetworkManager()
392+
defer func() {
393+
c := exec.Command("bash", "-c", "apt-get remove -y network-manager")
394+
_, err := c.CombinedOutput()
395+
assert.True(t, err == nil)
396+
397+
assert.False(t, isNetManagerInstalled())
398+
}()
399+
assert.True(t, isNetManagerInstalled())
400+
}

0 commit comments

Comments
 (0)