github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cce/v3/partitions/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/chnsz/golangsdk/openstack/cce/v3/common"
     9  	"github.com/chnsz/golangsdk/openstack/cce/v3/partitions"
    10  	th "github.com/chnsz/golangsdk/testhelper"
    11  )
    12  
    13  func TestListNode(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/partitions", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "GET")
    19  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    20  		w.Header().Add("Content-Type", "application/json")
    21  		w.WriteHeader(http.StatusOK)
    22  
    23  		fmt.Fprintf(w, `
    24  {
    25      "kind":"List",
    26      "apiVersion":"v3",
    27      "items":[
    28          {
    29              "kind":"Partition",
    30              "apiVersion":"v3",
    31              "metadata":{
    32                  "name":"center",
    33                  "creationTimestamp":"2023-03-21 07:58:56.617633 +0000 UTC"
    34              },
    35              "spec":{
    36                  "hostNetwork":{
    37                      "subnetID":"82f94fd8-a10a-4088-a514-3198b44fa1b6"
    38                  },
    39                  "containerNetwork":[
    40                      {
    41                          "subnetID":"86e5f9b8-53ec-4a58-a6f3-594b341f30e9"
    42                      }
    43                  ],
    44                  "publicBorderGroup":"center",
    45                  "category":"Default"
    46              }
    47          },
    48          {
    49              "kind":"Partition",
    50              "apiVersion":"v3",
    51              "metadata":{
    52                  "name":"cn-south-1-ies-fstxz",
    53                  "creationTimestamp":"2023-03-21 12:12:10.269567 +0000 UTC"
    54              },
    55              "spec":{
    56                  "hostNetwork":{
    57                      "subnetID":"d7131ed5-f813-4dbc-86f8-bcbdc07dce6f"
    58                  },
    59                  "containerNetwork":[
    60                      {
    61                          "subnetID":"b2f23c46-edaa-4e66-b82f-50edafa638f5"
    62                      },
    63                      {
    64                          "subnetID":"dee746d5-6c78-43fb-bc36-ac26c581a3ec"
    65                      }
    66                  ],
    67                  "publicBorderGroup":"cn-south-1-ies-fstxz",
    68                  "category":"IES"
    69              }
    70          }
    71      ]
    72  }
    73  		`)
    74  	})
    75  
    76  	listPartitions := partitions.ListOpts{Name: "cn-south-1-ies-fstxz"}
    77  	actual, err := partitions.List(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", listPartitions)
    78  
    79  	if err != nil {
    80  		t.Errorf("Failed to extract partitions: %v", err)
    81  	}
    82  
    83  	expected := []partitions.Partitions{
    84  		{
    85  			Kind:       "Partition",
    86  			Apiversion: "v3",
    87  			Metadata: partitions.Metadata{
    88  				Name: "cn-south-1-ies-fstxz",
    89  			},
    90  			Spec: partitions.Spec{
    91  				Category:          "IES",
    92  				PublicBorderGroup: "cn-south-1-ies-fstxz",
    93  				HostNetwork: partitions.HostNetwork{
    94  					SubnetID: "d7131ed5-f813-4dbc-86f8-bcbdc07dce6f",
    95  				},
    96  				ContainerNetwork: []partitions.ContainerNetwork{
    97  					{
    98  						SubnetID: "b2f23c46-edaa-4e66-b82f-50edafa638f5",
    99  					},
   100  					{
   101  						SubnetID: "dee746d5-6c78-43fb-bc36-ac26c581a3ec",
   102  					},
   103  				},
   104  			},
   105  		},
   106  	}
   107  
   108  	th.AssertDeepEquals(t, expected, actual)
   109  }
   110  
   111  func TestGetV3Partition(t *testing.T) {
   112  	th.SetupHTTP()
   113  	defer th.TeardownHTTP()
   114  
   115  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/partitions/cn-south-1-ies-fstxz", func(w http.ResponseWriter, r *http.Request) {
   116  		th.TestMethod(t, r, "GET")
   117  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   118  		w.Header().Add("Content-Type", "application/json")
   119  		w.WriteHeader(http.StatusOK)
   120  		fmt.Fprintf(w, Output)
   121  	})
   122  
   123  	actual, err := partitions.Get(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cn-south-1-ies-fstxz").Extract()
   124  	th.AssertNoErr(t, err)
   125  	expected := Expected
   126  	th.AssertDeepEquals(t, expected, actual)
   127  }