From 5ec62f4327084d7e017e5c00fb78e8bd0ff6d0ce Mon Sep 17 00:00:00 2001 From: Trevor Dawe Date: Tue, 17 Dec 2024 12:41:10 -0400 Subject: [PATCH] Update test coverage for system_limit (#163) --- system_limit_test.go | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/system_limit_test.go b/system_limit_test.go index 64f676b..a6a6777 100644 --- a/system_limit_test.go +++ b/system_limit_test.go @@ -143,3 +143,127 @@ func TestGetSystemLimits(t *testing.T) { }) } } + +func TestGetMaxVol(t *testing.T) { + type checkFn func(*testing.T, string, error) + check := func(fns ...checkFn) []checkFn { return fns } + + hasNoError := func(t *testing.T, _ string, err error) { + if err != nil { + t.Fatalf("expected no error") + } + } + + hasError := func(t *testing.T, _ string, err error) { + if err == nil { + t.Fatalf("expected error") + } + } + + checkLimitMaxVal := func(expectedMaxVal string) func(t *testing.T, maxvol string, err error) { + return func(t *testing.T, maxvol string, err error) { + if err == nil { + assert.Equal(t, expectedMaxVal, maxvol) + } + } + } + + tests := map[string]func(t *testing.T) (*httptest.Server, []checkFn){ + "success": func(t *testing.T) (*httptest.Server, []checkFn) { + href := "/api/instances/System/action/querySystemLimits" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodPost, r.Method)) + } + + if r.URL.Path != href { + t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path)) + } + + // Simulate a successful response for GetSystemLimits. + resp := types.QuerySystemLimitsResponse{ + SystemLimitEntryList: []types.SystemLimits{ + { + Type: "volumeSizeGb", + Description: "Maximum volume size in GB", + MaxVal: "1024", + }, + }, + } + + respData, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + fmt.Fprintln(w, string(respData)) + })) + + return ts, check(hasNoError, checkLimitMaxVal("1024")) + }, + "not found": func(t *testing.T) (*httptest.Server, []checkFn) { + href := "/api/instances/System/action/querySystemLimits" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodPost, r.Method)) + } + + if r.URL.Path != href { + t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path)) + } + + http.Error(w, "nas not found", http.StatusNotFound) + })) + + return ts, check(hasError) + }, + "couldn't get max vol size": func(t *testing.T) (*httptest.Server, []checkFn) { + href := "/api/instances/System/action/querySystemLimits" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodPost, r.Method)) + } + + if r.URL.Path != href { + t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path)) + } + + // Empty response without any valid payload. + resp := types.QuerySystemLimitsResponse{} + + respData, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + fmt.Fprintln(w, string(respData)) + })) + + return ts, check(hasError) + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + ts, checkFns := tc(t) + defer ts.Close() + + // Create a test client and call GetMaxVol. + client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false) + client.configConnect.Version = "4.0" + if err != nil { + t.Fatal(err) + } + + sys := System{ + client: client, + } + + resp, err := sys.client.GetMaxVol() + for _, checkFn := range checkFns { + checkFn(t, resp, err) + } + }) + } +}