package slurm import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "testing" ) func TestSlurmdbAssocsService_GetAssociations(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") fmt.Fprint(w, `{"associations": []}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) resp, _, err := client.SlurmdbAssocs.GetAssociations(context.Background(), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_GetAssociations_WithOptions(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") q := r.URL.Query() if q.Get("account") != "testacct" { t.Errorf("expected account=testacct, got %s", q.Get("account")) } if q.Get("cluster") != "testcluster" { t.Errorf("expected cluster=testcluster, got %s", q.Get("cluster")) } if q.Get("user") != "testuser" { t.Errorf("expected user=testuser, got %s", q.Get("user")) } if q.Get("with_deleted") != "true" { t.Errorf("expected with_deleted=true, got %s", q.Get("with_deleted")) } if q.Get("with_usage") != "true" { t.Errorf("expected with_usage=true, got %s", q.Get("with_usage")) } fmt.Fprint(w, `{"associations": []}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetAssocsOptions{ Account: Ptr("testacct"), Cluster: Ptr("testcluster"), User: Ptr("testuser"), WithDeleted: Ptr("true"), WithUsage: Ptr("true"), } resp, _, err := client.SlurmdbAssocs.GetAssociations(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_GetAssociation(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/association", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") fmt.Fprint(w, `{"associations": [{"account": "testacct"}]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetAssocsOptions{ Account: Ptr("testacct"), } resp, _, err := client.SlurmdbAssocs.GetAssociation(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_GetAssociation_AllQueryParams(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/association", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") q := r.URL.Query() expectedParams := []string{ "account", "cluster", "default_qos", "format", "id", "only_defaults", "parent_account", "partition", "qos", "usage_end", "usage_start", "user", "with_usage", "with_deleted", "with_raw_qos", "with_sub_accts", "without_parent_info", "without_parent_limits", } for _, p := range expectedParams { if q.Get(p) == "" { t.Errorf("expected query param %s to be set", p) } } fmt.Fprint(w, `{"associations": []}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetAssocsOptions{ Account: Ptr("acct"), Cluster: Ptr("clust"), DefaultQos: Ptr("dqos"), Format: Ptr("fmt"), ID: Ptr("1"), OnlyDefaults: Ptr("1"), ParentAccount: Ptr("parent"), Partition: Ptr("part"), Qos: Ptr("qos"), UsageEnd: Ptr("9999"), UsageStart: Ptr("0000"), User: Ptr("usr"), WithUsage: Ptr("1"), WithDeleted: Ptr("1"), WithRawQos: Ptr("1"), WithSubAccts: Ptr("1"), WithoutParentInfo: Ptr("1"), WithoutParentLimits: Ptr("1"), } resp, _, err := client.SlurmdbAssocs.GetAssociation(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_PostAssociations(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") if ct := r.Header.Get("Content-Type"); ct != "application/json" { t.Errorf("expected Content-Type application/json, got %s", ct) } fmt.Fprint(w, `{"meta": {"plugin": {"type": "openapi/v0.0.40"}}}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) body := &OpenapiAssocsResp{ Associations: AssocList{}, } resp, _, err := client.SlurmdbAssocs.PostAssociations(context.Background(), body) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_DeleteAssociations(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") q := r.URL.Query() if q.Get("account") != "delacct" { t.Errorf("expected account=delacct, got %s", q.Get("account")) } if q.Get("cluster") != "delcluster" { t.Errorf("expected cluster=delcluster, got %s", q.Get("cluster")) } fmt.Fprint(w, `{"removed_associations": ["acct1"]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetAssocsOptions{ Account: Ptr("delacct"), Cluster: Ptr("delcluster"), } resp, _, err := client.SlurmdbAssocs.DeleteAssociations(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_DeleteAssociation(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/association", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") q := r.URL.Query() if q.Get("user") != "deluser" { t.Errorf("expected user=deluser, got %s", q.Get("user")) } if q.Get("partition") != "delpart" { t.Errorf("expected partition=delpart, got %s", q.Get("partition")) } fmt.Fprint(w, `{"removed_associations": ["assoc1"]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) opts := &GetAssocsOptions{ User: Ptr("deluser"), Partition: Ptr("delpart"), } resp, _, err := client.SlurmdbAssocs.DeleteAssociation(context.Background(), opts) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_DeleteAssociation_NoOptions(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/association", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") q := r.URL.Query() if len(q) != 0 { t.Errorf("expected no query params, got %v", q) } fmt.Fprint(w, `{"removed_associations": []}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) resp, _, err := client.SlurmdbAssocs.DeleteAssociation(context.Background(), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } if resp == nil { t.Fatal("expected non-nil response") } } func TestSlurmdbAssocsService_GetAssociations_Error(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) fmt.Fprint(w, `{"errors": [{"error": "internal error"}]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) _, _, err := client.SlurmdbAssocs.GetAssociations(context.Background(), nil) if err == nil { t.Fatal("expected error for 500 response") } if !strings.Contains(err.Error(), "500") { t.Errorf("expected error to contain 500, got %v", err) } } func TestSlurmdbAssocsService_DeleteAssociations_Error(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprint(w, `{"errors": [{"error": "not found"}]}`) }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) _, _, err := client.SlurmdbAssocs.DeleteAssociations(context.Background(), nil) if err == nil { t.Fatal("expected error for 404 response") } } func TestSlurmdbAssocsService_PathCorrectness(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slurmdb/v0.0.40/associations", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { fmt.Fprint(w, `{"associations": [], "path": "plural"}`) } else if r.Method == "DELETE" { fmt.Fprint(w, `{"removed_associations": [], "path": "plural"}`) } else { fmt.Fprint(w, `{"meta": {}}`) } }) mux.HandleFunc("/slurmdb/v0.0.40/association", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { fmt.Fprint(w, `{"associations": [], "path": "singular"}`) } else if r.Method == "DELETE" { fmt.Fprint(w, `{"removed_associations": [], "path": "singular"}`) } }) server := httptest.NewServer(mux) defer server.Close() client, _ := NewClient(server.URL, nil) resp, _, err := client.SlurmdbAssocs.GetAssociations(context.Background(), nil) if err != nil { t.Fatalf("GetAssociations: unexpected error: %v", err) } if resp == nil { t.Fatal("GetAssociations: expected non-nil response") } resp, _, err = client.SlurmdbAssocs.GetAssociation(context.Background(), nil) if err != nil { t.Fatalf("GetAssociation: unexpected error: %v", err) } if resp == nil { t.Fatal("GetAssociation: expected non-nil response") } delResp, _, err := client.SlurmdbAssocs.DeleteAssociations(context.Background(), nil) if err != nil { t.Fatalf("DeleteAssociations: unexpected error: %v", err) } if delResp == nil { t.Fatal("DeleteAssociations: expected non-nil response") } delResp, _, err = client.SlurmdbAssocs.DeleteAssociation(context.Background(), nil) if err != nil { t.Fatalf("DeleteAssociation: unexpected error: %v", err) } if delResp == nil { t.Fatal("DeleteAssociation: expected non-nil response") } postResp, _, err := client.SlurmdbAssocs.PostAssociations(context.Background(), &OpenapiAssocsResp{}) if err != nil { t.Fatalf("PostAssociations: unexpected error: %v", err) } if postResp == nil { t.Fatal("PostAssociations: expected non-nil response") } }