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