github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/deh/v1/hosts/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/deh/v1/common"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/deh/v1/hosts"
    10  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/testhelper/client"
    12  )
    13  
    14  func TestGet(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  
    18  	th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) {
    19  		th.TestMethod(t, r, "GET")
    20  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    21  
    22  		w.Header().Add("Content-Type", "application/json")
    23  		w.WriteHeader(http.StatusOK)
    24  
    25  		_, _ = fmt.Fprint(w, getResponse)
    26  	})
    27  
    28  	s, err := hosts.Get(client.ServiceClient(), HostID).Extract()
    29  	th.AssertNoErr(t, err)
    30  	th.AssertEquals(t, "66156a61-27c2-4169-936b-910dd9c73da3", s.ID)
    31  	th.AssertEquals(t, "test-aj2", s.Name)
    32  	th.AssertEquals(t, "eu-de-02", s.Az)
    33  	th.AssertEquals(t, "available", s.State)
    34  	th.AssertDeepEquals(t, hosts.HostPropertiesOpts{
    35  		HostTypeName: "High performance",
    36  		HostType:     "h1",
    37  		Vcpus:        36,
    38  		Memory:       270336,
    39  		Cores:        12,
    40  		Sockets:      2,
    41  	}, s.HostProperties)
    42  }
    43  
    44  func TestList(t *testing.T) {
    45  	th.SetupHTTP()
    46  	defer th.TeardownHTTP()
    47  
    48  	th.Mux.HandleFunc(dehEndpoint, func(w http.ResponseWriter, r *http.Request) {
    49  		th.TestMethod(t, r, "GET")
    50  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    51  
    52  		w.Header().Add("Content-Type", "application/json")
    53  		w.WriteHeader(http.StatusOK)
    54  
    55  		_, _ = fmt.Fprint(w, listResponse)
    56  	})
    57  
    58  	// count := 0
    59  
    60  	host, err := hosts.List(client.ServiceClient(), hosts.ListOpts{}).AllPages()
    61  	th.AssertNoErr(t, err)
    62  
    63  	actual, err := hosts.ExtractHosts(host)
    64  
    65  	if err != nil {
    66  		t.Errorf("Failed to extract hosts: %v", err)
    67  	}
    68  
    69  	expected := []hosts.Host{
    70  		{
    71  			Az:              "eu-de-01",
    72  			Name:            "c2c-deh-test",
    73  			AvailableMemory: 262144,
    74  			AvailableVcpus:  70,
    75  			ID:              "671611d2-b45c-4648-9e78-06eb24522291",
    76  			State:           "available",
    77  			InstanceTotal:   2,
    78  			AutoPlacement:   "off",
    79  			TenantId:        "17fbda95add24720a4038ba4b1c705ed",
    80  			HostProperties: hosts.HostPropertiesOpts{
    81  				HostType:     "general",
    82  				Vcpus:        72,
    83  				Memory:       270336,
    84  				Cores:        12,
    85  				Sockets:      2,
    86  				HostTypeName: "General computing",
    87  			},
    88  			InstanceUuids: []string{"3de1ce75-2550-4a46-a689-dd33ca2b62d6",
    89  				"885dc71d-905d-48b5-bae7-db66801dc175"},
    90  		},
    91  	}
    92  
    93  	th.AssertDeepEquals(t, expected, actual)
    94  }
    95  
    96  func TestListServer(t *testing.T) {
    97  	th.SetupHTTP()
    98  	defer th.TeardownHTTP()
    99  
   100  	th.Mux.HandleFunc(dehEndpoint+"/"+HostID+"/servers", func(w http.ResponseWriter, r *http.Request) {
   101  		th.TestMethod(t, r, "GET")
   102  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   103  
   104  		w.Header().Add("Content-Type", "application/json")
   105  		w.WriteHeader(http.StatusOK)
   106  
   107  		_, _ = fmt.Fprint(w, listserverResponse)
   108  	})
   109  
   110  	actual, err := hosts.ListServer(client.ServiceClient(), HostID, hosts.ListServerOpts{})
   111  	th.AssertNoErr(t, err)
   112  
   113  	expected := []hosts.Server{
   114  		{
   115  			Status: "ACTIVE",
   116  			Addresses: map[string]interface{}{
   117  				"0b98c646-617f-4d90-9ca5-385f0cd73ea7": []interface{}{
   118  					map[string]interface{}{
   119  						"version": float64(4),
   120  						"addr":    "192.168.3.133",
   121  					},
   122  				},
   123  			},
   124  			Flavor: map[string]interface{}{
   125  				"id": "normal1",
   126  			},
   127  			ID:       "3de1ce75-2550-4a46-a689-dd33ca2b62d6",
   128  			UserID:   "6d78fa8550ae45d6932a1fadfb1fa552",
   129  			Name:     "c2c-ecs-test-2",
   130  			TenantID: "17fbda95add24720a4038ba4b1c705ed",
   131  			Metadata: map[string]string{
   132  				"metering.image_id":         "c0ea3ff1-432e-4650-8a1b-372a80b2d2be",
   133  				"metering.imagetype":        "gold",
   134  				"metering.resourcespeccode": "deh.linux",
   135  				"metering.cloudServiceType": "sys.service.type.ec2",
   136  				"image_name":                "Standard_CentOS_7_latest",
   137  				"metering.resourcetype":     "1",
   138  				"os_bit":                    "64",
   139  				"vpc_id":                    "0b98c646-617f-4d90-9ca5-385f0cd73ea7",
   140  				"os_type":                   "Linux",
   141  				"charging_mode":             "0",
   142  			},
   143  		},
   144  	}
   145  	th.AssertDeepEquals(t, expected, actual)
   146  }
   147  
   148  func TestAllocateDeH(t *testing.T) {
   149  	th.SetupHTTP()
   150  	defer th.TeardownHTTP()
   151  	th.Mux.HandleFunc(dehEndpoint, func(w http.ResponseWriter, r *http.Request) {
   152  		th.TestMethod(t, r, "POST")
   153  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   154  		th.TestHeader(t, r, "Content-Type", "application/json")
   155  		th.TestHeader(t, r, "Accept", "application/json")
   156  		th.TestJSONRequest(t, r, allocateRequest)
   157  		w.Header().Add("Content-Type", "application/json")
   158  		w.WriteHeader(http.StatusOK)
   159  		_, _ = fmt.Fprint(w, allocateResponse)
   160  	})
   161  
   162  	c := client.ServiceClient()
   163  	allocateOpts := hosts.AllocateOpts{Name: "Test-1",
   164  		Az:            "eu-de-02",
   165  		HostType:      "h1",
   166  		AutoPlacement: "off",
   167  		Quantity:      2}
   168  	s, err := hosts.Allocate(c, allocateOpts).ExtractHost()
   169  
   170  	th.AssertNoErr(t, err)
   171  	th.AssertDeepEquals(t, s, &hosts.AllocatedHosts{
   172  		AllocatedHostIds: []string{"fb4733fd-70a3-44e1-a1cb-0311f028d7e5",
   173  			"7408f985-047d-4313-b3c8-8e12bef01d12"},
   174  	})
   175  }
   176  
   177  func TestUpdateDeH(t *testing.T) {
   178  	th.SetupHTTP()
   179  	defer th.TeardownHTTP()
   180  	th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) {
   181  		th.TestMethod(t, r, "PUT")
   182  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   183  		th.TestJSONRequest(t, r, updateRequest)
   184  		w.WriteHeader(http.StatusNoContent)
   185  	})
   186  
   187  	c := client.ServiceClient()
   188  	updateOpts := hosts.UpdateOpts{Name: "Test-2",
   189  		AutoPlacement: "off",
   190  	}
   191  	s := hosts.Update(c, HostID, updateOpts)
   192  	th.AssertNoErr(t, s.Err)
   193  }
   194  
   195  func TestDeleteDeH(t *testing.T) {
   196  	th.SetupHTTP()
   197  	defer th.TeardownHTTP()
   198  
   199  	th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) {
   200  		th.TestMethod(t, r, "DELETE")
   201  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   202  		w.WriteHeader(http.StatusAccepted)
   203  	})
   204  
   205  	result := hosts.Delete(client.ServiceClient(), HostID)
   206  	th.AssertNoErr(t, result.Err)
   207  }