Refactored GetDevices and test to use release_version

pull/17/head
Paulo Truta 2021-06-15 14:17:35 +00:00
parent 10e19a1292
commit ae0d16b3de
3 changed files with 22 additions and 19 deletions

View File

@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/edgebox-iot/edgeboxctl/internal/diagnostics"
"github.com/edgebox-iot/edgeboxctl/internal/utils" "github.com/edgebox-iot/edgeboxctl/internal/utils"
"github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/disk"
) )
@ -68,7 +67,7 @@ type Partition struct {
} }
// GetDevices : Returns a list of all available sotrage devices in structs filled with information // GetDevices : Returns a list of all available sotrage devices in structs filled with information
func GetDevices() []Device { func GetDevices(release_version string) []Device {
var devices []Device var devices []Device
@ -83,11 +82,11 @@ func GetDevices() []Device {
mainDiskID := "sda" mainDiskID := "sda"
if diagnostics.Version == "dev" { if release_version == "dev" {
mainDiskID = "sda" mainDiskID = "sda"
} else if diagnostics.Version == "prod" { } else if release_version == "prod" {
mainDiskID = "mmcblk0" mainDiskID = "mmcblk0"
} else if diagnostics.Version == "cloud" { } else if release_version == "cloud" {
mainDiskID = "vda" mainDiskID = "vda"
} }

View File

@ -7,33 +7,37 @@ import (
) )
func TestGetDevices(t *testing.T) { func TestGetDevices(t *testing.T) {
result := GetDevices()
if len(result) == 0 { t.Log("Testing with release version dev")
assertGetDevices(GetDevices("dev"), t)
t.Log("Testing with release version prod")
assertGetDevices(GetDevices("prod"), t)
t.Log("Testing with release version cloud")
assertGetDevices(GetDevices("cloud"), t)
}
func assertGetDevices(devices []Device, t *testing.T) {
if len(devices) == 0 {
t.Log("Expecting at least 1 block device, 0 elements found in slice") t.Log("Expecting at least 1 block device, 0 elements found in slice")
t.Fail() t.Fail()
} }
foundMainDevice := false
foundDevice := false foundDevice := false
t.Log("Looking for a mmcblk0 or sda device") t.Log("Looking for a mmcblk0, sda or dva device")
for _, device := range result { for _, device := range devices {
if device.MainDevice { if device.ID == "mmcblk0" || device.ID == "sda" || device.ID == "vda" {
t.Log("Found target main device", device.ID)
foundMainDevice = true
}
if device.ID == "mmcblk0" || device.ID == "sda" {
t.Log("Found target device", device.ID) t.Log("Found target device", device.ID)
foundDevice = true foundDevice = true
} }
} }
if !foundDevice || !foundMainDevice { if !foundDevice {
t.Log("Expected to find device mmcblk0 but did not. Devices:", result) t.Log("Expected to find device mmcblk0, sda or dva but did not. Devices:", devices)
t.Fail() t.Fail()
} }
} }

View File

@ -472,7 +472,7 @@ func taskGetSystemUptime() string {
func taskGetStorageDevices() string { func taskGetStorageDevices() string {
fmt.Println("Executing taskGetStorageDevices") fmt.Println("Executing taskGetStorageDevices")
devices := storage.GetDevices() devices := storage.GetDevices(diagnostics.Version)
devicesJSON, _ := json.Marshal(devices) devicesJSON, _ := json.Marshal(devices)
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails()) db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())