github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/trunks/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/trunks"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    12  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    13  )
    14  
    15  func TestCreate(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/trunks", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "POST")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  		th.TestHeader(t, r, "Content-Type", "application/json")
    23  		th.TestHeader(t, r, "Accept", "application/json")
    24  		th.TestJSONRequest(t, r, CreateRequest)
    25  		w.Header().Add("Content-Type", "application/json")
    26  		w.WriteHeader(http.StatusCreated)
    27  
    28  		fmt.Fprint(w, CreateResponse)
    29  	})
    30  
    31  	iTrue := true
    32  	options := trunks.CreateOpts{
    33  		Name:         "gophertrunk",
    34  		Description:  "Trunk created by gophercloud",
    35  		AdminStateUp: &iTrue,
    36  		Subports: []trunks.Subport{
    37  			{
    38  				SegmentationID:   1,
    39  				SegmentationType: "vlan",
    40  				PortID:           "28e452d7-4f8a-4be4-b1e6-7f3db4c0430b",
    41  			},
    42  			{
    43  				SegmentationID:   2,
    44  				SegmentationType: "vlan",
    45  				PortID:           "4c8b2bff-9824-4d4c-9b60-b3f6621b2bab",
    46  			},
    47  		},
    48  	}
    49  	_, err := trunks.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    50  	if err == nil {
    51  		t.Fatalf("Failed to detect missing parent PortID field")
    52  	}
    53  	options.PortID = "c373d2fa-3d3b-4492-924c-aff54dea19b6"
    54  	n, err := trunks.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    55  	th.AssertNoErr(t, err)
    56  
    57  	th.AssertEquals(t, n.Status, "ACTIVE")
    58  	expectedTrunks, err := ExpectedTrunkSlice()
    59  	th.AssertNoErr(t, err)
    60  	th.AssertDeepEquals(t, &expectedTrunks[1], n)
    61  }
    62  
    63  func TestCreateNoSubports(t *testing.T) {
    64  	th.SetupHTTP()
    65  	defer th.TeardownHTTP()
    66  
    67  	th.Mux.HandleFunc("/v2.0/trunks", func(w http.ResponseWriter, r *http.Request) {
    68  		th.TestMethod(t, r, "POST")
    69  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    70  		th.TestHeader(t, r, "Content-Type", "application/json")
    71  		th.TestHeader(t, r, "Accept", "application/json")
    72  		th.TestJSONRequest(t, r, CreateNoSubportsRequest)
    73  		w.Header().Add("Content-Type", "application/json")
    74  		w.WriteHeader(http.StatusCreated)
    75  
    76  		fmt.Fprint(w, CreateNoSubportsResponse)
    77  	})
    78  
    79  	iTrue := true
    80  	options := trunks.CreateOpts{
    81  		Name:         "gophertrunk",
    82  		Description:  "Trunk created by gophercloud",
    83  		AdminStateUp: &iTrue,
    84  		PortID:       "c373d2fa-3d3b-4492-924c-aff54dea19b6",
    85  	}
    86  	n, err := trunks.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    87  	th.AssertNoErr(t, err)
    88  
    89  	th.AssertEquals(t, n.Status, "ACTIVE")
    90  	th.AssertEquals(t, 0, len(n.Subports))
    91  }
    92  
    93  func TestDelete(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  
    97  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c", func(w http.ResponseWriter, r *http.Request) {
    98  		th.TestMethod(t, r, "DELETE")
    99  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   100  		w.WriteHeader(http.StatusNoContent)
   101  	})
   102  
   103  	res := trunks.Delete(context.TODO(), fake.ServiceClient(), "f6a9718c-5a64-43e3-944f-4deccad8e78c")
   104  	th.AssertNoErr(t, res.Err)
   105  }
   106  
   107  func TestList(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  
   111  	th.Mux.HandleFunc("/v2.0/trunks", func(w http.ResponseWriter, r *http.Request) {
   112  		th.TestMethod(t, r, "GET")
   113  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   114  
   115  		w.Header().Add("Content-Type", "application/json")
   116  		w.WriteHeader(http.StatusOK)
   117  
   118  		fmt.Fprint(w, ListResponse)
   119  	})
   120  
   121  	client := fake.ServiceClient()
   122  	count := 0
   123  
   124  	err := trunks.List(client, trunks.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   125  		count++
   126  		actual, err := trunks.ExtractTrunks(page)
   127  		if err != nil {
   128  			t.Errorf("Failed to extract trunks: %v", err)
   129  			return false, err
   130  		}
   131  
   132  		expected, err := ExpectedTrunkSlice()
   133  		th.AssertNoErr(t, err)
   134  		th.CheckDeepEquals(t, expected, actual)
   135  
   136  		return true, nil
   137  	})
   138  	th.AssertNoErr(t, err)
   139  
   140  	if count != 1 {
   141  		t.Errorf("Expected 1 page, got %d", count)
   142  	}
   143  }
   144  
   145  func TestGet(t *testing.T) {
   146  	th.SetupHTTP()
   147  	defer th.TeardownHTTP()
   148  
   149  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c", func(w http.ResponseWriter, r *http.Request) {
   150  		th.TestMethod(t, r, "GET")
   151  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   152  
   153  		w.Header().Add("Content-Type", "application/json")
   154  		w.WriteHeader(http.StatusOK)
   155  
   156  		fmt.Fprint(w, GetResponse)
   157  	})
   158  
   159  	n, err := trunks.Get(context.TODO(), fake.ServiceClient(), "f6a9718c-5a64-43e3-944f-4deccad8e78c").Extract()
   160  	th.AssertNoErr(t, err)
   161  	expectedTrunks, err := ExpectedTrunkSlice()
   162  	th.AssertNoErr(t, err)
   163  	th.CheckDeepEquals(t, &expectedTrunks[1], n)
   164  }
   165  
   166  func TestUpdate(t *testing.T) {
   167  	th.SetupHTTP()
   168  	defer th.TeardownHTTP()
   169  
   170  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c", func(w http.ResponseWriter, r *http.Request) {
   171  		th.TestMethod(t, r, "PUT")
   172  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   173  		th.TestHeader(t, r, "Content-Type", "application/json")
   174  		th.TestHeader(t, r, "Accept", "application/json")
   175  		th.TestJSONRequest(t, r, UpdateRequest)
   176  
   177  		w.Header().Add("Content-Type", "application/json")
   178  		w.WriteHeader(http.StatusOK)
   179  
   180  		fmt.Fprint(w, UpdateResponse)
   181  	})
   182  
   183  	iFalse := false
   184  	name := "updated_gophertrunk"
   185  	description := "gophertrunk updated by gophercloud"
   186  	options := trunks.UpdateOpts{
   187  		Name:         &name,
   188  		AdminStateUp: &iFalse,
   189  		Description:  &description,
   190  	}
   191  	n, err := trunks.Update(context.TODO(), fake.ServiceClient(), "f6a9718c-5a64-43e3-944f-4deccad8e78c", options).Extract()
   192  	th.AssertNoErr(t, err)
   193  
   194  	th.AssertEquals(t, n.Name, name)
   195  	th.AssertEquals(t, n.AdminStateUp, iFalse)
   196  	th.AssertEquals(t, n.Description, description)
   197  }
   198  
   199  func TestGetSubports(t *testing.T) {
   200  	th.SetupHTTP()
   201  	defer th.TeardownHTTP()
   202  
   203  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c/get_subports", func(w http.ResponseWriter, r *http.Request) {
   204  		th.TestMethod(t, r, "GET")
   205  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   206  
   207  		w.Header().Add("Content-Type", "application/json")
   208  		w.WriteHeader(http.StatusOK)
   209  
   210  		fmt.Fprint(w, ListSubportsResponse)
   211  	})
   212  
   213  	client := fake.ServiceClient()
   214  
   215  	subports, err := trunks.GetSubports(context.TODO(), client, "f6a9718c-5a64-43e3-944f-4deccad8e78c").Extract()
   216  	th.AssertNoErr(t, err)
   217  	th.CheckDeepEquals(t, ExpectedSubports, subports)
   218  }
   219  
   220  func TestMissingFields(t *testing.T) {
   221  	iTrue := true
   222  	opts := trunks.CreateOpts{
   223  		Name:         "gophertrunk",
   224  		PortID:       "c373d2fa-3d3b-4492-924c-aff54dea19b6",
   225  		Description:  "Trunk created by gophercloud",
   226  		AdminStateUp: &iTrue,
   227  		Subports: []trunks.Subport{
   228  			{
   229  				SegmentationID:   1,
   230  				SegmentationType: "vlan",
   231  				PortID:           "28e452d7-4f8a-4be4-b1e6-7f3db4c0430b",
   232  			},
   233  			{
   234  				SegmentationID:   2,
   235  				SegmentationType: "vlan",
   236  				PortID:           "4c8b2bff-9824-4d4c-9b60-b3f6621b2bab",
   237  			},
   238  			{
   239  				PortID: "4c8b2bff-9824-4d4c-9b60-b3f6621b2bab",
   240  			},
   241  		},
   242  	}
   243  
   244  	_, err := opts.ToTrunkCreateMap()
   245  	if err == nil {
   246  		t.Fatalf("Failed to detect missing subport fields")
   247  	}
   248  }
   249  
   250  func TestAddSubports(t *testing.T) {
   251  	th.SetupHTTP()
   252  	defer th.TeardownHTTP()
   253  
   254  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c/add_subports", func(w http.ResponseWriter, r *http.Request) {
   255  		th.TestMethod(t, r, "PUT")
   256  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   257  		th.TestHeader(t, r, "Content-Type", "application/json")
   258  		th.TestHeader(t, r, "Accept", "application/json")
   259  		th.TestJSONRequest(t, r, AddSubportsRequest)
   260  		w.WriteHeader(http.StatusOK)
   261  
   262  		fmt.Fprint(w, AddSubportsResponse)
   263  	})
   264  
   265  	client := fake.ServiceClient()
   266  
   267  	opts := trunks.AddSubportsOpts{
   268  		Subports: ExpectedSubports,
   269  	}
   270  
   271  	trunk, err := trunks.AddSubports(context.TODO(), client, "f6a9718c-5a64-43e3-944f-4deccad8e78c", opts).Extract()
   272  	th.AssertNoErr(t, err)
   273  	expectedTrunk, err := ExpectedSubportsAddedTrunk()
   274  	th.AssertNoErr(t, err)
   275  	th.CheckDeepEquals(t, &expectedTrunk, trunk)
   276  }
   277  
   278  func TestRemoveSubports(t *testing.T) {
   279  	th.SetupHTTP()
   280  	defer th.TeardownHTTP()
   281  
   282  	th.Mux.HandleFunc("/v2.0/trunks/f6a9718c-5a64-43e3-944f-4deccad8e78c/remove_subports", func(w http.ResponseWriter, r *http.Request) {
   283  		th.TestMethod(t, r, "PUT")
   284  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   285  		th.TestHeader(t, r, "Content-Type", "application/json")
   286  		th.TestHeader(t, r, "Accept", "application/json")
   287  		th.TestJSONRequest(t, r, RemoveSubportsRequest)
   288  		w.WriteHeader(http.StatusOK)
   289  
   290  		fmt.Fprint(w, RemoveSubportsResponse)
   291  	})
   292  
   293  	client := fake.ServiceClient()
   294  
   295  	opts := trunks.RemoveSubportsOpts{
   296  		Subports: []trunks.RemoveSubport{
   297  			{PortID: "28e452d7-4f8a-4be4-b1e6-7f3db4c0430b"},
   298  			{PortID: "4c8b2bff-9824-4d4c-9b60-b3f6621b2bab"},
   299  		},
   300  	}
   301  	trunk, err := trunks.RemoveSubports(context.TODO(), client, "f6a9718c-5a64-43e3-944f-4deccad8e78c", opts).Extract()
   302  
   303  	th.AssertNoErr(t, err)
   304  	expectedTrunk, err := ExpectedSubportsRemovedTrunk()
   305  	th.AssertNoErr(t, err)
   306  	th.CheckDeepEquals(t, &expectedTrunk, trunk)
   307  }