github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v2/servers/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/bms/v2/servers"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  func TestListServers(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "GET")
    19  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    20  
    21  		w.Header().Add("Content-Type", "application/json")
    22  		w.WriteHeader(http.StatusOK)
    23  
    24  		fmt.Fprintf(w, `
    25  {
    26      "servers": [
    27  {
    28          "tenant_id": "17fbda95add24720a4038ba4b1c705ed",  
    29          "id": "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f",
    30          "user_id": "aec83183a5b54cf2bc28812d1dc5509e",
    31          "name": "BMS-1",  	
    32  "key_name": "KeyPair-click2cloud",
    33          "flavor": {
    34              "links": [
    35                  {
    36                      "rel": "bookmark",
    37                      "href": "https://ecs.eu-de.otc.t-systems.com:443/17fbda95add24720a4038ba4b1c705ed/flavors/physical.o2.medium"
    38                  }
    39              ],
    40              "id": "physical.o2.medium"
    41          },       
    42          "status": "ACTIVE"
    43  
    44      },
    45  {
    46          "tenant_id": "17fbda95add24720a4038ba4b1c705ed",  
    47          "id": "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764r",
    48          "user_id": "aec83183a5b54cf2bc28812d1dc5509e",
    49          "name": "BMS-2",    		
    50  "key_name": "KeyPair-click2cloud",
    51          "flavor": {
    52              "links": [
    53                  {
    54                      "rel": "bookmark",
    55                      "href": "https://ecs.eu-de.otc.t-systems.com:443/17fbda95add24720a4038ba4b1c705ed/flavors/physical.o2.medium"
    56                  }
    57              ],
    58              "id": "physical.o2.medium"
    59          },       
    60          "status": "ACTIVE"
    61      }
    62  ]
    63  }
    64  			`)
    65  	})
    66  
    67  	serverOpts := servers.ListOpts{}
    68  	actual, err := servers.List(fake.ServiceClient(), serverOpts)
    69  	if err != nil {
    70  		t.Errorf("Failed to extract server list: %v", err)
    71  	}
    72  
    73  	expected := []servers.Server{
    74  		{
    75  			ID:       "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f",
    76  			Status:   "ACTIVE",
    77  			UserID:   "aec83183a5b54cf2bc28812d1dc5509e",
    78  			Name:     "BMS-1",
    79  			TenantID: "17fbda95add24720a4038ba4b1c705ed",
    80  			KeyName:  "KeyPair-click2cloud",
    81  			Flavor: servers.Flavor{ID: "physical.o2.medium", Links: []servers.Links{{Rel: "bookmark",
    82  				Href: "https://ecs.eu-de.otc.t-systems.com:443/17fbda95add24720a4038ba4b1c705ed/flavors/physical.o2.medium"}}},
    83  		},
    84  		{
    85  			ID:       "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764r",
    86  			Status:   "ACTIVE",
    87  			UserID:   "aec83183a5b54cf2bc28812d1dc5509e",
    88  			Name:     "BMS-2",
    89  			TenantID: "17fbda95add24720a4038ba4b1c705ed",
    90  			KeyName:  "KeyPair-click2cloud",
    91  			Flavor: servers.Flavor{ID: "physical.o2.medium", Links: []servers.Links{{Rel: "bookmark",
    92  				Href: "https://ecs.eu-de.otc.t-systems.com:443/17fbda95add24720a4038ba4b1c705ed/flavors/physical.o2.medium"}}},
    93  		},
    94  	}
    95  
    96  	th.AssertDeepEquals(t, expected, actual)
    97  }
    98  
    99  func TestGetServer(t *testing.T) {
   100  	th.SetupHTTP()
   101  	defer th.TeardownHTTP()
   102  
   103  	th.Mux.HandleFunc("/servers/2bff7a8a-3934-4f79-b1d6-53dc5540f00e", func(w http.ResponseWriter, r *http.Request) {
   104  		th.TestMethod(t, r, "GET")
   105  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   106  
   107  		w.Header().Add("Content-Type", "application/json")
   108  		w.WriteHeader(http.StatusOK)
   109  
   110  		fmt.Fprintf(w, `
   111  {
   112      "server": {
   113          "tenant_id": "17fbda95add24720a4038ba4b1c705ed",  
   114          "id": "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f",
   115          "user_id": "aec83183a5b54cf2bc28812d1dc5509e",
   116          "name": "BMS-1",        
   117          "status": "ACTIVE"
   118      }
   119  }
   120  		`)
   121  	})
   122  
   123  	n, err := servers.Get(fake.ServiceClient(), "2bff7a8a-3934-4f79-b1d6-53dc5540f00e").Extract()
   124  	th.AssertNoErr(t, err)
   125  	th.AssertEquals(t, "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", n.ID)
   126  	th.AssertEquals(t, "ACTIVE", n.Status)
   127  	th.AssertEquals(t, "BMS-1", n.Name)
   128  	th.AssertEquals(t, "aec83183a5b54cf2bc28812d1dc5509e", n.UserID)
   129  	th.AssertEquals(t, "17fbda95add24720a4038ba4b1c705ed", n.TenantID)
   130  
   131  }