diff --git a/webapp/backend/pkg/models/testdata/smart-nvme2.json b/webapp/backend/pkg/models/testdata/smart-nvme2.json new file mode 100644 index 0000000..4f47c1a --- /dev/null +++ b/webapp/backend/pkg/models/testdata/smart-nvme2.json @@ -0,0 +1,105 @@ +{ + "json_format_version": [ + 1, + 0 + ], + "smartctl": { + "version": [ + 7, + 0 + ], + "svn_revision": "4883", + "platform_info": "x86_64-linux-4.19.107-Unraid", + "build_info": "(local build)", + "argv": [ + "smartctl", + "-a", + "-j", + "-d", + "nvme", + "/dev/nvme0" + ], + "exit_status": 0 + }, + "device": { + "name": "/dev/nvme0", + "info_name": "/dev/nvme0", + "type": "nvme", + "protocol": "NVMe" + }, + "model_name": "Force MP510", + "serial_number": "yes", + "firmware_version": "ECFM12.3", + "nvme_pci_vendor": { + "id": 6535, + "subsystem_id": 6535 + }, + "nvme_ieee_oui_identifier": 6584743, + "nvme_total_capacity": 480103981056, + "nvme_unallocated_capacity": 0, + "nvme_controller_id": 1, + "nvme_number_of_namespaces": 1, + "nvme_namespaces": [ + { + "id": 1, + "size": { + "blocks": 937703088, + "bytes": 480103981056 + }, + "capacity": { + "blocks": 937703088, + "bytes": 480103981056 + }, + "utilization": { + "blocks": 937703088, + "bytes": 480103981056 + }, + "formatted_lba_size": 512, + "eui64": { + "oui": 6584743, + "ext_id": 171819811633 + } + } + ], + "user_capacity": { + "blocks": 937703088, + "bytes": 480103981056 + }, + "logical_block_size": 512, + "local_time": { + "time_t": 1600619090, + "asctime": "Sun Sep 20 16:24:50 2020 Europe" + }, + "smart_status": { + "passed": true, + "nvme": { + "value": 0 + } + }, + "nvme_smart_health_information_log": { + "critical_warning": 0, + "temperature": 38, + "available_spare": 100, + "available_spare_threshold": 5, + "percentage_used": 1, + "data_units_read": 6932144, + "data_units_written": 16093122, + "host_reads": 29878811, + "host_writes": 17533252, + "controller_busy_time": 305, + "power_cycles": 4, + "power_on_hours": 6487, + "unsafe_shutdowns": 4, + "media_errors": 0, + "num_err_log_entries": 8382, + "warning_temp_time": 0, + "critical_comp_time": 0 + }, + "temperature": { + "current": 38 + }, + "power_cycle_count": 4, + "power_on_time": { + "hours": 6487 + } +} diff --git a/webapp/backend/pkg/web/server_test.go b/webapp/backend/pkg/web/server_test.go index 785c9ed..c1949c5 100644 --- a/webapp/backend/pkg/web/server_test.go +++ b/webapp/backend/pkg/web/server_test.go @@ -1,24 +1,30 @@ package web_test import ( + "encoding/json" mock_config "github.com/analogj/scrutiny/webapp/backend/pkg/config/mock" + dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db" "github.com/analogj/scrutiny/webapp/backend/pkg/web" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "io/ioutil" "net/http" "net/http/httptest" "os" + "path" "strings" "testing" ) func TestHealthRoute(t *testing.T) { //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() fakeConfig := mock_config.NewMockInterface(mockCtrl) - fakeConfig.EXPECT().GetString("web.database.location").Return("testdata/scrutiny_test.db") - fakeConfig.EXPECT().GetString("web.src.frontend.path").Return("testdata") + fakeConfig.EXPECT().GetString("web.database.location").Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").Return(parentPath) ae := web.AppEngine{ Config: fakeConfig, @@ -38,11 +44,13 @@ func TestHealthRoute(t *testing.T) { func TestRegisterDevicesRoute(t *testing.T) { //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() fakeConfig := mock_config.NewMockInterface(mockCtrl) - fakeConfig.EXPECT().GetString("web.database.location").Return("testdata/scrutiny_test.db") - fakeConfig.EXPECT().GetString("web.src.frontend.path").Return("testdata") + fakeConfig.EXPECT().GetString("web.database.location").Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").Return(parentPath) ae := web.AppEngine{ Config: fakeConfig, } @@ -61,11 +69,13 @@ func TestRegisterDevicesRoute(t *testing.T) { func TestUploadDeviceMetricsRoute(t *testing.T) { //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() fakeConfig := mock_config.NewMockInterface(mockCtrl) - fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return("testdata/scrutiny_test.db") - fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return("testdata") + fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) ae := web.AppEngine{ Config: fakeConfig, } @@ -92,11 +102,14 @@ func TestUploadDeviceMetricsRoute(t *testing.T) { func TestPopulateMultiple(t *testing.T) { //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() fakeConfig := mock_config.NewMockInterface(mockCtrl) - fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return("testdata/scrutiny_test.db") - fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return("testdata") + //fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return("testdata/scrutiny_test.db") + fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) ae := web.AppEngine{ Config: fakeConfig, } @@ -151,11 +164,13 @@ func TestPopulateMultiple(t *testing.T) { func TestSendTestNotificationRoute(t *testing.T) { //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() fakeConfig := mock_config.NewMockInterface(mockCtrl) - fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return("testdata/scrutiny_test.db") - fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return("testdata") + fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{"https://scrutiny.requestcatcher.com/test"}) ae := web.AppEngine{ Config: fakeConfig, @@ -170,3 +185,45 @@ func TestSendTestNotificationRoute(t *testing.T) { //assert require.Equal(t, 200, wr.Code) } + +func TestGetDevicesSummaryRoute_Nvme(t *testing.T) { + //setup + parentPath, _ := ioutil.TempDir("", "") + defer os.RemoveAll(parentPath) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) + fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) + ae := web.AppEngine{ + Config: fakeConfig, + } + router := ae.Setup() + devicesfile, err := os.Open("testdata/register-devices-req-2.json") + require.NoError(t, err) + + metricsfile, err := os.Open("../models/testdata/smart-nvme2.json") + require.NoError(t, err) + + //test + wr := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/api/devices/register", devicesfile) + router.ServeHTTP(wr, req) + require.Equal(t, 200, wr.Code) + + mr := httptest.NewRecorder() + req, _ = http.NewRequest("POST", "/api/device/a4c8e8ed-11a0-4c97-9bba-306440f1b944/smart", metricsfile) + router.ServeHTTP(mr, req) + require.Equal(t, 200, mr.Code) + + sr := httptest.NewRecorder() + req, _ = http.NewRequest("GET", "/api/summary", nil) + router.ServeHTTP(sr, req) + require.Equal(t, 200, sr.Code) + var device dbModels.DeviceWrapper + json.Unmarshal(sr.Body.Bytes(), &device) + + //assert + require.Equal(t, "a4c8e8ed-11a0-4c97-9bba-306440f1b944", device.Data[0].WWN) + require.Equal(t, "passed", device.Data[0].SmartResults[0].SmartStatus) +} diff --git a/webapp/backend/pkg/web/testdata/register-devices-req-2.json b/webapp/backend/pkg/web/testdata/register-devices-req-2.json new file mode 100644 index 0000000..eb300b9 --- /dev/null +++ b/webapp/backend/pkg/web/testdata/register-devices-req-2.json @@ -0,0 +1,20 @@ +{ + "data": [ + { + "wwn": "a4c8e8ed-11a0-4c97-9bba-306440f1b944", + "device_name": "nvme0", + "manufacturer": "", + "model_name": "Force MP510", + "interface_type": "", + "interface_speed": "", + "serial_number": "a4c8e8ed-11a0-4c97-9bba-306440f1b944", + "firmware": "ECFM12.3", + "rotational_speed": 0, + "capacity": 480103981056, + "form_factor": "", + "smart_support": false, + "device_protocol": "NVMe", + "device_type": "nvme" + } + ] +}