github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v1/vpcs/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/networking/v1/common"
     9  	"github.com/huaweicloud/golangsdk/openstack/networking/v1/vpcs"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestListVpc(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs", 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      "vpcs": [
    27          {
    28              "id": "14ece7d0-a8d4-4317-982a-041e4f10f442",
    29              "name": "vpc-elb-l00379969",
    30              "cidr": "192.168.0.0/16",
    31              "status": "OK",
    32              "routes": [],
    33              "enable_shared_snat": false
    34          },
    35          {
    36              "id": "1e5618c3-89f0-4f58-a14e-33536074ec88",
    37              "name": "vpc-ops",
    38              "cidr": "192.168.0.0/16",
    39              "status": "OK",
    40              "routes": [],
    41              "enable_shared_snat": false
    42          },
    43          {
    44              "id": "2140264c-d313-4363-9874-9a5e18aeb516",
    45              "name": "test",
    46              "cidr": "192.168.0.0/16",
    47              "status": "OK",
    48              "routes": [],
    49              "enable_shared_snat": false
    50          }
    51      ]
    52  }
    53  			`)
    54  	})
    55  
    56  	//count := 0
    57  
    58  	actual, err := vpcs.List(fake.ServiceClient(), vpcs.ListOpts{})
    59  	if err != nil {
    60  		t.Errorf("Failed to extract vpcs: %v", err)
    61  	}
    62  
    63  	expected := []vpcs.Vpc{
    64  		{
    65  			Status:           "OK",
    66  			CIDR:             "192.168.0.0/16",
    67  			EnableSharedSnat: false,
    68  			Name:             "vpc-elb-l00379969",
    69  			ID:               "14ece7d0-a8d4-4317-982a-041e4f10f442",
    70  			Routes:           []vpcs.Route{},
    71  		},
    72  		{
    73  			Status:           "OK",
    74  			CIDR:             "192.168.0.0/16",
    75  			EnableSharedSnat: false,
    76  			Name:             "vpc-ops",
    77  			ID:               "1e5618c3-89f0-4f58-a14e-33536074ec88",
    78  			Routes:           []vpcs.Route{},
    79  		},
    80  		{
    81  			Status:           "OK",
    82  			CIDR:             "192.168.0.0/16",
    83  			EnableSharedSnat: false,
    84  			Name:             "test",
    85  			ID:               "2140264c-d313-4363-9874-9a5e18aeb516",
    86  			Routes:           []vpcs.Route{},
    87  		},
    88  	}
    89  
    90  	th.AssertDeepEquals(t, expected, actual)
    91  }
    92  
    93  func TestGetVpc(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  
    97  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/abda1f6e-ae7c-4ff5-8d06-53425dc11f34", func(w http.ResponseWriter, r *http.Request) {
    98  		th.TestMethod(t, r, "GET")
    99  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   100  
   101  		w.Header().Add("Content-Type", "application/json")
   102  		w.WriteHeader(http.StatusOK)
   103  
   104  		fmt.Fprintf(w, `
   105  {
   106      "vpc": {
   107          "id": "abda1f6e-ae7c-4ff5-8d06-53425dc11f34",
   108          "name": "terraform-provider-test-l90006937",
   109          "cidr": "192.168.0.0/16",
   110          "status": "OK",
   111          "routes": [
   112              {
   113                  "destination": "0.0.0.0/0",
   114                  "nexthop": "192.168.0.5"
   115              }
   116          ],
   117          "enable_shared_snat": false
   118      }
   119  }
   120  		`)
   121  	})
   122  
   123  	n, err := vpcs.Get(fake.ServiceClient(), "abda1f6e-ae7c-4ff5-8d06-53425dc11f34").Extract()
   124  	th.AssertNoErr(t, err)
   125  	th.AssertEquals(t, "abda1f6e-ae7c-4ff5-8d06-53425dc11f34", n.ID)
   126  	th.AssertEquals(t, "terraform-provider-test-l90006937", n.Name)
   127  	th.AssertEquals(t, "192.168.0.0/16", n.CIDR)
   128  	th.AssertEquals(t, "OK", n.Status)
   129  	th.AssertDeepEquals(t, []vpcs.Route{{DestinationCIDR: "0.0.0.0/0", NextHop: "192.168.0.5"}}, n.Routes)
   130  	th.AssertEquals(t, false, n.EnableSharedSnat)
   131  
   132  }
   133  
   134  func TestCreateVpc(t *testing.T) {
   135  	th.SetupHTTP()
   136  	defer th.TeardownHTTP()
   137  
   138  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs", func(w http.ResponseWriter, r *http.Request) {
   139  		th.TestMethod(t, r, "POST")
   140  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   141  		th.TestHeader(t, r, "Content-Type", "application/json")
   142  		th.TestHeader(t, r, "Accept", "application/json")
   143  		th.TestJSONRequest(t, r, `
   144  {
   145   "vpc":
   146       {
   147       "name": "terraform-provider-vpctestcreate",
   148       "cidr": "192.168.0.0/16"
   149       }
   150  }
   151  			`)
   152  
   153  		w.Header().Add("Content-Type", "application/json")
   154  		w.WriteHeader(http.StatusOK)
   155  
   156  		fmt.Fprintf(w, `
   157  {
   158      "vpc": {
   159          "id": "97e01fc2-e39e-4cfc-abf6-1d0886d120af",
   160          "name": "terraform-provider-vpctestcreate",
   161          "cidr": "192.168.0.0/16",
   162          "status": "CREATING"
   163      }
   164  }		`)
   165  	})
   166  
   167  	options := vpcs.CreateOpts{
   168  		Name: "terraform-provider-vpctestcreate",
   169  		CIDR: "192.168.0.0/16",
   170  	}
   171  	n, err := vpcs.Create(fake.ServiceClient(), options).Extract()
   172  	th.AssertNoErr(t, err)
   173  	th.AssertEquals(t, "terraform-provider-vpctestcreate", n.Name)
   174  	th.AssertEquals(t, "97e01fc2-e39e-4cfc-abf6-1d0886d120af", n.ID)
   175  	th.AssertEquals(t, "192.168.0.0/16", n.CIDR)
   176  	th.AssertEquals(t, "CREATING", n.Status)
   177  	th.AssertEquals(t, false, n.EnableSharedSnat)
   178  }
   179  
   180  func TestUpdateVpc(t *testing.T) {
   181  	th.SetupHTTP()
   182  	defer th.TeardownHTTP()
   183  
   184  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/97e01fc2-e39e-4cfc-abf6-1d0886d120af", func(w http.ResponseWriter, r *http.Request) {
   185  		th.TestMethod(t, r, "PUT")
   186  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   187  		th.TestHeader(t, r, "Content-Type", "application/json")
   188  		th.TestHeader(t, r, "Accept", "application/json")
   189  		th.TestJSONRequest(t, r, `
   190  {
   191  "vpc":
   192      {
   193      "name": "terraform-provider-new-name",
   194      "cidr": "192.168.0.0/16"
   195  
   196      }
   197  }
   198  			`)
   199  
   200  		w.Header().Add("Content-Type", "application/json")
   201  		w.WriteHeader(http.StatusOK)
   202  
   203  		fmt.Fprintf(w, `
   204  {
   205      "vpc": {
   206          "id": "97e01fc2-e39e-4cfc-abf6-1d0886d120af",
   207          "name": "terraform-provider-new-name",
   208          "cidr": "192.168.0.0/16",
   209          "status": "OK",
   210          "routes": [
   211              {
   212                  "destination": "0.0.0.0/4",
   213                  "nexthop": "192.168.0.4"
   214              }
   215          ],
   216          "enable_shared_snat": false
   217      }
   218  }
   219  		`)
   220  	})
   221  
   222  	options := vpcs.UpdateOpts{Name: "terraform-provider-new-name", CIDR: "192.168.0.0/16"}
   223  
   224  	n, err := vpcs.Update(fake.ServiceClient(), "97e01fc2-e39e-4cfc-abf6-1d0886d120af", options).Extract()
   225  	th.AssertNoErr(t, err)
   226  	th.AssertEquals(t, "terraform-provider-new-name", n.Name)
   227  	th.AssertEquals(t, "97e01fc2-e39e-4cfc-abf6-1d0886d120af", n.ID)
   228  	th.AssertEquals(t, "192.168.0.0/16", n.CIDR)
   229  	th.AssertEquals(t, "OK", n.Status)
   230  	th.AssertDeepEquals(t, []vpcs.Route{{DestinationCIDR: "0.0.0.0/4", NextHop: "192.168.0.4"}}, n.Routes)
   231  	th.AssertEquals(t, false, n.EnableSharedSnat)
   232  }
   233  
   234  func TestDeleteVpc(t *testing.T) {
   235  	th.SetupHTTP()
   236  	defer th.TeardownHTTP()
   237  
   238  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/abda1f6e-ae7c-4ff5-8d06-53425dc11f34", func(w http.ResponseWriter, r *http.Request) {
   239  		th.TestMethod(t, r, "DELETE")
   240  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   241  		w.WriteHeader(http.StatusNoContent)
   242  	})
   243  
   244  	res := vpcs.Delete(fake.ServiceClient(), "abda1f6e-ae7c-4ff5-8d06-53425dc11f34")
   245  	th.AssertNoErr(t, res.Err)
   246  }