github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/trunks/testing/requests_test.go (about)

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