github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v1/subnets/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/subnets"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestListSubnet(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets", 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      "subnets": [
    27          {
    28              "id": "0345a6ef-9404-487b-87c8-212557a1160d",
    29              "name": "openlab-subnet",
    30              "cidr": "192.168.200.0/24",
    31              "status": "ACTIVE",
    32              "vpc_id": "58c24204-170e-4ff0-9b42-c53cdea9239a",
    33              "gateway_ip": "192.168.200.1",
    34              "dhcp_enable": true,
    35  			"primary_dns": "114.114.114.114",
    36  		    "secondary_dns": "114.114.115.115",
    37  		    "dnsList": [
    38  			  "114.114.114.114",
    39  			  "114.114.115.115"
    40  		    ],
    41              "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578"
    42          },
    43          {
    44              "id": "134ca339-24dc-44f5-ae6a-cf0404216ed2",
    45              "name": "openlab-subnet",
    46              "cidr": "192.168.200.0/24",
    47              "status": "ACTIVE",
    48              "vpc_id": "58c24204-170e-4ff0-9b42-c53cdea9239a",
    49              "gateway_ip": "192.168.200.1",
    50              "dhcp_enable": true,
    51  			"primary_dns": "114.114.114.114",
    52  		    "secondary_dns": "114.114.115.115",
    53  		    "dnsList": [
    54  			  "114.114.114.114",
    55  			  "114.114.115.115"
    56  		    ],
    57              "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578"
    58          }
    59      ]
    60  }
    61  
    62  		`)
    63  	})
    64  
    65  	actual, err := subnets.List(fake.ServiceClient(), subnets.ListOpts{})
    66  	if err != nil {
    67  		t.Errorf("Failed to extract subnets: %v", err)
    68  	}
    69  
    70  	expected := []subnets.Subnet{
    71  		{
    72  			Status:        "ACTIVE",
    73  			CIDR:          "192.168.200.0/24",
    74  			EnableDHCP:    true,
    75  			Name:          "openlab-subnet",
    76  			ID:            "0345a6ef-9404-487b-87c8-212557a1160d",
    77  			GatewayIP:     "192.168.200.1",
    78  			VPC_ID:        "58c24204-170e-4ff0-9b42-c53cdea9239a",
    79  			PRIMARY_DNS:   "114.114.114.114",
    80  			SECONDARY_DNS: "114.114.115.115",
    81  			DnsList:       []string{"114.114.114.114", "114.114.115.115"},
    82  			SubnetId:      "3d543273-31c3-41f8-b887-ed8c2c837578",
    83  		},
    84  		{
    85  			Status:        "ACTIVE",
    86  			CIDR:          "192.168.200.0/24",
    87  			EnableDHCP:    true,
    88  			Name:          "openlab-subnet",
    89  			ID:            "134ca339-24dc-44f5-ae6a-cf0404216ed2",
    90  			GatewayIP:     "192.168.200.1",
    91  			VPC_ID:        "58c24204-170e-4ff0-9b42-c53cdea9239a",
    92  			PRIMARY_DNS:   "114.114.114.114",
    93  			SECONDARY_DNS: "114.114.115.115",
    94  			DnsList:       []string{"114.114.114.114", "114.114.115.115"},
    95  			SubnetId:      "3d543273-31c3-41f8-b887-ed8c2c837578",
    96  		},
    97  	}
    98  	th.AssertDeepEquals(t, expected, actual)
    99  }
   100  
   101  func TestGetSubnet(t *testing.T) {
   102  	th.SetupHTTP()
   103  	defer th.TeardownHTTP()
   104  
   105  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets/aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb", func(w http.ResponseWriter, r *http.Request) {
   106  		th.TestMethod(t, r, "GET")
   107  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   108  
   109  		w.Header().Add("Content-Type", "application/json")
   110  
   111  		w.WriteHeader(http.StatusOK)
   112  
   113  		fmt.Fprintf(w, `
   114  {
   115      "subnet": {
   116          "id": "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb",
   117          "name": "subnet-mgmt",
   118          "cidr": "10.0.0.0/24",
   119          "dnsList": [
   120              "100.125.4.25",
   121              "8.8.8.8"
   122          ],
   123          "status": "ACTIVE",
   124          "vpc_id": "d4f2c817-d5df-4a66-994a-6571312b470e",
   125          "gateway_ip": "10.0.0.1",
   126          "dhcp_enable": true,
   127          "primary_dns": "100.125.4.25",
   128          "secondary_dns": "8.8.8.8",
   129          "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578"
   130      }
   131  }
   132  		`)
   133  	})
   134  
   135  	n, err := subnets.Get(fake.ServiceClient(), "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb").Extract()
   136  	th.AssertNoErr(t, err)
   137  	th.AssertEquals(t, "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb", n.ID)
   138  	th.AssertEquals(t, "subnet-mgmt", n.Name)
   139  	th.AssertEquals(t, "10.0.0.0/24", n.CIDR)
   140  	th.AssertEquals(t, "ACTIVE", n.Status)
   141  	th.AssertEquals(t, "d4f2c817-d5df-4a66-994a-6571312b470e", n.VPC_ID)
   142  	th.AssertEquals(t, "3d543273-31c3-41f8-b887-ed8c2c837578", n.SubnetId)
   143  	th.AssertEquals(t, "10.0.0.1", n.GatewayIP)
   144  	th.AssertEquals(t, "100.125.4.25", n.PRIMARY_DNS)
   145  	th.AssertEquals(t, "8.8.8.8", n.SECONDARY_DNS)
   146  	th.AssertEquals(t, true, n.EnableDHCP)
   147  	th.AssertEquals(t, "100.125.4.25", n.DnsList[0])
   148  	th.AssertEquals(t, "8.8.8.8", n.DnsList[1])
   149  
   150  }
   151  
   152  func TestCreateSubnet(t *testing.T) {
   153  	th.SetupHTTP()
   154  	defer th.TeardownHTTP()
   155  
   156  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets", func(w http.ResponseWriter, r *http.Request) {
   157  		th.TestMethod(t, r, "POST")
   158  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   159  		th.TestHeader(t, r, "Content-Type", "application/json")
   160  		th.TestHeader(t, r, "Accept", "application/json")
   161  		th.TestJSONRequest(t, r, `
   162  {
   163    "subnet":
   164           {
   165            "name": "test_subnets",
   166            "cidr": "192.168.0.0/16",
   167            "gateway_ip": "192.168.0.1",
   168  		  "dhcp_enable": true,
   169            "primary_dns": "8.8.8.8",
   170            "secondary_dns": "8.8.4.4",
   171            "availability_zone":"eu-de-02",
   172            "vpc_id":"3b9740a0-b44d-48f0-84ee-42eb166e54f7",
   173  		  "dnsList": [
   174               "8.8.8.8",
   175              "8.8.4.4"
   176            ]
   177            }
   178  }
   179  			`)
   180  
   181  		w.Header().Add("Content-Type", "application/json")
   182  		w.WriteHeader(http.StatusOK)
   183  
   184  		fmt.Fprintf(w, `
   185  {
   186      "subnet": {
   187          "id": "6b0cf733-f496-4159-9df1-d74c3584a9f7",
   188          "name": "test_subnets",
   189          "cidr": "192.168.0.0/16",
   190          "dnsList": [
   191              "8.8.8.8",
   192              "8.8.4.4"
   193          ],
   194          "status": "UNKNOWN",
   195          "vpc_id": "3b9740a0-b44d-48f0-84ee-42eb166e54f7",
   196          "gateway_ip": "192.168.0.1",
   197          "dhcp_enable": true,
   198          "primary_dns": "8.8.8.8",
   199          "secondary_dns": "8.8.4.4",
   200          "availability_zone": "eu-de-02",
   201          "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578"
   202      }
   203  }	`)
   204  	})
   205  
   206  	options := subnets.CreateOpts{
   207  		Name:             "test_subnets",
   208  		CIDR:             "192.168.0.0/16",
   209  		GatewayIP:        "192.168.0.1",
   210  		PRIMARY_DNS:      "8.8.8.8",
   211  		SECONDARY_DNS:    "8.8.4.4",
   212  		AvailabilityZone: "eu-de-02",
   213  		VPC_ID:           "3b9740a0-b44d-48f0-84ee-42eb166e54f7",
   214  		DnsList:          []string{"8.8.8.8", "8.8.4.4"},
   215  		EnableDHCP:       true,
   216  	}
   217  	n, err := subnets.Create(fake.ServiceClient(), options).Extract()
   218  	th.AssertNoErr(t, err)
   219  	th.AssertEquals(t, "test_subnets", n.Name)
   220  	th.AssertEquals(t, "192.168.0.1", n.GatewayIP)
   221  	th.AssertEquals(t, "192.168.0.0/16", n.CIDR)
   222  	th.AssertEquals(t, true, n.EnableDHCP)
   223  	th.AssertEquals(t, "8.8.8.8", n.PRIMARY_DNS)
   224  	th.AssertEquals(t, "8.8.4.4", n.SECONDARY_DNS)
   225  	th.AssertEquals(t, "eu-de-02", n.AvailabilityZone)
   226  	th.AssertEquals(t, "6b0cf733-f496-4159-9df1-d74c3584a9f7", n.ID)
   227  	th.AssertEquals(t, "UNKNOWN", n.Status)
   228  	th.AssertEquals(t, "3b9740a0-b44d-48f0-84ee-42eb166e54f7", n.VPC_ID)
   229  	th.AssertEquals(t, "3d543273-31c3-41f8-b887-ed8c2c837578", n.SubnetId)
   230  	th.AssertEquals(t, "8.8.8.8", n.DnsList[0])
   231  	th.AssertEquals(t, "8.8.4.4", n.DnsList[1])
   232  
   233  }
   234  
   235  func TestUpdateSubnet(t *testing.T) {
   236  	th.SetupHTTP()
   237  	defer th.TeardownHTTP()
   238  
   239  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/8f794f06-2275-4d82-9f5a-6d68fbe21a75/subnets/83e3bddc-b9ed-4614-a0dc-8a997095a86c", func(w http.ResponseWriter, r *http.Request) {
   240  		th.TestMethod(t, r, "PUT")
   241  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   242  		th.TestHeader(t, r, "Content-Type", "application/json")
   243  		th.TestHeader(t, r, "Accept", "application/json")
   244  		th.TestJSONRequest(t, r, `
   245  {
   246  "subnet":
   247      {
   248          "name": "testsubnet",
   249          "dnsList": [
   250              "114.114.114.114",
   251              "8.8.8.8"
   252          ],
   253          "dhcp_enable": false
   254      }
   255  }
   256  `)
   257  
   258  		w.Header().Add("Content-Type", "application/json")
   259  		w.WriteHeader(http.StatusOK)
   260  
   261  		fmt.Fprintf(w, `
   262  {
   263      "subnet": {
   264          "id": "83e3bddc-b9ed-4614-a0dc-8a997095a86c",
   265          "name": "testsubnet",
   266          "status": "ACTIVE"
   267      }
   268  }
   269  		`)
   270  	})
   271  
   272  	options := subnets.UpdateOpts{
   273  		Name:    "testsubnet",
   274  		DnsList: &[]string{"114.114.114.114", "8.8.8.8"},
   275  	}
   276  
   277  	n, err := subnets.Update(fake.ServiceClient(), "8f794f06-2275-4d82-9f5a-6d68fbe21a75", "83e3bddc-b9ed-4614-a0dc-8a997095a86c", options).Extract()
   278  	th.AssertNoErr(t, err)
   279  	th.AssertEquals(t, "testsubnet", n.Name)
   280  	th.AssertEquals(t, "83e3bddc-b9ed-4614-a0dc-8a997095a86c", n.ID)
   281  	th.AssertEquals(t, "ACTIVE", n.Status)
   282  }
   283  
   284  func TestDeleteSubnet(t *testing.T) {
   285  	th.SetupHTTP()
   286  	defer th.TeardownHTTP()
   287  
   288  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/8f794f06-2275-4d82-9f5a-6d68fbe21a75/subnets/83e3bddc-b9ed-4614-a0dc-8a997095a86c", func(w http.ResponseWriter, r *http.Request) {
   289  		th.TestMethod(t, r, "DELETE")
   290  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   291  		w.WriteHeader(http.StatusNoContent)
   292  	})
   293  
   294  	res := subnets.Delete(fake.ServiceClient(), "8f794f06-2275-4d82-9f5a-6d68fbe21a75", "83e3bddc-b9ed-4614-a0dc-8a997095a86c")
   295  	th.AssertNoErr(t, res.Err)
   296  }