github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cce/v3/nodes/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/huaweicloud/golangsdk/openstack/cce/v3/common"
     9  	"github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes"
    10  	th "github.com/huaweicloud/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/nodes", 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         {
    30              "kind": "Host",
    31              "apiVersion": "v3",
    32              "metadata": {
    33                  "name": "test-node-1234",
    34                  "uid": "b99acd73-5d7c-11e8-8e76-0255ac101929"               
    35              },
    36              "spec": {
    37                  "flavor": "s1.medium",
    38                  "az": "cn-east-2a",
    39                  "login": {
    40                      "sshKey": "test-keypair",
    41                      "userPassword": {}
    42                  },
    43                  "rootVolume": {
    44                      "volumetype": "SATA",
    45                      "size": 40
    46                  },
    47                  "dataVolumes": [
    48                      {
    49                          "volumetype": "SATA",
    50                          "size": 100
    51                      }
    52                  ],
    53                  "publicIP": {
    54                      "eip": {
    55                          "bandwidth": {}
    56                      }
    57                  },
    58                  "billingMode": 0
    59              },
    60              "status": {
    61                  "phase": "Active",
    62                  "serverId": "41748e56-33d4-46a1-aa57-2c8c29907995",
    63                  "privateIP": "192.168.0.3"
    64              }
    65          }
    66  	]
    67  }
    68  		`)
    69  	})
    70  
    71  	listNodes := nodes.ListOpts{Name: "test-node-1234"}
    72  	actual, err := nodes.List(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", listNodes)
    73  
    74  	if err != nil {
    75  		t.Errorf("Failed to extract nodes: %v", err)
    76  	}
    77  
    78  	expected := []nodes.Nodes{
    79  		{
    80  			Kind:       "Host",
    81  			Apiversion: "v3",
    82  			Metadata: nodes.Metadata{
    83  				Name: "test-node-1234",
    84  				Id:   "b99acd73-5d7c-11e8-8e76-0255ac101929",
    85  			},
    86  			Spec: nodes.Spec{
    87  				Az:          "cn-east-2a",
    88  				Login:       nodes.LoginSpec{SshKey: "test-keypair"},
    89  				RootVolume:  nodes.VolumeSpec{Size: 40, VolumeType: "SATA"},
    90  				BillingMode: 0,
    91  				DataVolumes: []nodes.VolumeSpec{
    92  					{
    93  						VolumeType: "SATA",
    94  						Size:       100,
    95  					}},
    96  				Flavor: "s1.medium",
    97  			},
    98  			Status: nodes.Status{
    99  				Phase:     "Active",
   100  				ServerID:  "41748e56-33d4-46a1-aa57-2c8c29907995",
   101  				PrivateIP: "192.168.0.3",
   102  			},
   103  		},
   104  	}
   105  
   106  	th.AssertDeepEquals(t, expected, actual)
   107  }
   108  
   109  func TestGetV3Node(t *testing.T) {
   110  	th.SetupHTTP()
   111  	defer th.TeardownHTTP()
   112  
   113  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) {
   114  		th.TestMethod(t, r, "GET")
   115  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   116  		w.Header().Add("Content-Type", "application/json")
   117  		w.WriteHeader(http.StatusOK)
   118  		fmt.Fprintf(w, Output)
   119  	})
   120  
   121  	actual, err := nodes.Get(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926").Extract()
   122  	th.AssertNoErr(t, err)
   123  	expected := Expected
   124  	th.AssertDeepEquals(t, expected, actual)
   125  
   126  }
   127  
   128  func TestCreateV3Node(t *testing.T) {
   129  	th.SetupHTTP()
   130  	defer th.TeardownHTTP()
   131  
   132  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes", func(w http.ResponseWriter, r *http.Request) {
   133  		th.TestMethod(t, r, "POST")
   134  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   135  		th.TestHeader(t, r, "Content-Type", "application/json")
   136  		th.TestHeader(t, r, "Accept", "application/json")
   137  
   138  		th.TestJSONRequest(t, r, `
   139  	{
   140  	  "apiversion": "v3",
   141  	  "kind": "Node",
   142  	  "metadata": {
   143  	    "name": "test-node"
   144  	  },
   145  	  "spec": {
   146  	    "az": "cn-east-2a",
   147  	    "count": 1,
   148  	    "flavor": "s3.large.2",
   149  	    "login": {
   150  	      "sshKey": "test-keypair",
   151  	      "userPassword": {
   152  			"password": "",
   153              "username": ""
   154  		  }
   155  		},
   156  		"rootVolume": {
   157  			"size": 40,
   158  			"volumetype": "SATA"
   159  		},
   160  	    "dataVolumes": [
   161  	      {
   162  	        "size": 100,
   163  	        "volumetype": "SATA"
   164  	      }
   165  	    ],
   166  		"nodeNicSpec": {
   167  		  "primaryNic": {}
   168  		},
   169  	    "publicIP": {
   170  		      "eip": {
   171  		        "bandwidth": {}
   172  		      }
   173  		 }
   174  	  }
   175  	}
   176  `)
   177  
   178  		w.Header().Set("Content-Type", "application/json")
   179  		w.WriteHeader(http.StatusCreated)
   180  		fmt.Fprintf(w, Output)
   181  	})
   182  	options := nodes.CreateOpts{
   183  		Kind:       "Node",
   184  		ApiVersion: "v3",
   185  		Metadata:   nodes.CreateMetaData{Name: "test-node"},
   186  		Spec: nodes.Spec{
   187  			Flavor:      "s3.large.2",
   188  			Az:          "cn-east-2a",
   189  			Login:       nodes.LoginSpec{SshKey: "test-keypair"},
   190  			RootVolume:  nodes.VolumeSpec{Size: 40, VolumeType: "SATA"},
   191  			DataVolumes: []nodes.VolumeSpec{{Size: 100, VolumeType: "SATA"}},
   192  			Count:       1,
   193  		},
   194  	}
   195  	actual, err := nodes.Create(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", options).Extract()
   196  	th.AssertNoErr(t, err)
   197  	expected := Expected
   198  	th.AssertDeepEquals(t, expected, actual)
   199  
   200  }
   201  
   202  func TestUpdateV3Node(t *testing.T) {
   203  	th.SetupHTTP()
   204  	defer th.TeardownHTTP()
   205  
   206  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) {
   207  		th.TestMethod(t, r, "PUT")
   208  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   209  		th.TestHeader(t, r, "Content-Type", "application/json")
   210  		th.TestHeader(t, r, "Accept", "application/json")
   211  		th.TestJSONRequest(t, r, `
   212  {
   213      "metadata": {
   214          "name": "test-node"
   215      }
   216  }			`)
   217  
   218  		w.Header().Add("Content-Type", "application/json")
   219  		w.WriteHeader(http.StatusOK)
   220  
   221  		fmt.Fprintf(w, Output)
   222  	})
   223  	options := nodes.UpdateOpts{Metadata: nodes.UpdateMetadata{Name: "test-node"}}
   224  	actual, err := nodes.Update(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926", options).Extract()
   225  	th.AssertNoErr(t, err)
   226  	expected := Expected
   227  	th.AssertDeepEquals(t, expected, actual)
   228  }
   229  
   230  func TestDeleteNode(t *testing.T) {
   231  	th.SetupHTTP()
   232  	defer th.TeardownHTTP()
   233  
   234  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) {
   235  		th.TestMethod(t, r, "DELETE")
   236  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   237  		w.WriteHeader(http.StatusOK)
   238  	})
   239  
   240  	err := nodes.Delete(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926").ExtractErr()
   241  	th.AssertNoErr(t, err)
   242  
   243  }
   244  
   245  func TestGetV3Job(t *testing.T) {
   246  	th.SetupHTTP()
   247  	defer th.TeardownHTTP()
   248  
   249  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/jobs/73ce03fd-8b1b-11e8-8f9d-0255ac10193f", func(w http.ResponseWriter, r *http.Request) {
   250  		th.TestMethod(t, r, "GET")
   251  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   252  		w.Header().Add("Content-Type", "application/json")
   253  		w.WriteHeader(http.StatusOK)
   254  		fmt.Fprintf(w, JobOutput)
   255  	})
   256  
   257  	actual, err := nodes.GetJobDetails(fake.ServiceClient(), "73ce03fd-8b1b-11e8-8f9d-0255ac10193f").ExtractJob()
   258  	th.AssertNoErr(t, err)
   259  	expected := ExpectedJob
   260  	th.AssertDeepEquals(t, expected, actual)
   261  
   262  }