From ae0d16b3defc015e911743f18d71c48cf5e242a6 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Tue, 15 Jun 2021 14:17:35 +0000 Subject: [PATCH] Refactored GetDevices and test to use release_version --- internal/storage/storage.go | 9 ++++----- internal/storage/storage_test.go | 30 +++++++++++++++++------------- internal/tasks/tasks.go | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 58a0744..8633f62 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -6,7 +6,6 @@ import ( "path/filepath" "strings" - "github.com/edgebox-iot/edgeboxctl/internal/diagnostics" "github.com/edgebox-iot/edgeboxctl/internal/utils" "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 -func GetDevices() []Device { +func GetDevices(release_version string) []Device { var devices []Device @@ -83,11 +82,11 @@ func GetDevices() []Device { mainDiskID := "sda" - if diagnostics.Version == "dev" { + if release_version == "dev" { mainDiskID = "sda" - } else if diagnostics.Version == "prod" { + } else if release_version == "prod" { mainDiskID = "mmcblk0" - } else if diagnostics.Version == "cloud" { + } else if release_version == "cloud" { mainDiskID = "vda" } diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index a28912c..05b5dee 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -7,33 +7,37 @@ import ( ) 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.Fail() } - foundMainDevice := false foundDevice := false - t.Log("Looking for a mmcblk0 or sda device") - for _, device := range result { + t.Log("Looking for a mmcblk0, sda or dva device") + for _, device := range devices { - if device.MainDevice { - t.Log("Found target main device", device.ID) - foundMainDevice = true - } - - if device.ID == "mmcblk0" || device.ID == "sda" { + if device.ID == "mmcblk0" || device.ID == "sda" || device.ID == "vda" { t.Log("Found target device", device.ID) foundDevice = true } } - if !foundDevice || !foundMainDevice { - t.Log("Expected to find device mmcblk0 but did not. Devices:", result) + if !foundDevice { + t.Log("Expected to find device mmcblk0, sda or dva but did not. Devices:", devices) t.Fail() } } diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 59dc4ca..235bc83 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -472,7 +472,7 @@ func taskGetSystemUptime() string { func taskGetStorageDevices() string { fmt.Println("Executing taskGetStorageDevices") - devices := storage.GetDevices() + devices := storage.GetDevices(diagnostics.Version) devicesJSON, _ := json.Marshal(devices) db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())