github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/lbserverpool_test.go (about)

     1  //go:build lb || lbServerPool || nsxv || functional || ALL
     2  
     3  /*
     4   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	. "gopkg.in/check.v1"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  )
    14  
    15  // Test_LBServerPool tests CRUD methods for load balancer server pool.
    16  // The following things are tested if prerequisite Edge Gateway exists:
    17  // 1. Creation of load balancer server pool
    18  // 2. Get load balancer server pool by both ID and Name (server pool name must be unique in single edge gateway)
    19  // 3. Update - change a single field and compare that configuration and result objects are deeply equal
    20  // 4. Update - try and fail to update without mandatory field
    21  // 5. Delete
    22  func (vcd *TestVCD) Test_LBServerPool(check *C) {
    23  	if vcd.config.VCD.EdgeGateway == "" {
    24  		check.Skip("Skipping test because no edge gateway given")
    25  	}
    26  	edge, err := vcd.vdc.GetEdgeGatewayByName(vcd.config.VCD.EdgeGateway, false)
    27  	check.Assert(err, IsNil)
    28  	check.Assert(edge.EdgeGateway.Name, Equals, vcd.config.VCD.EdgeGateway)
    29  
    30  	if !edge.HasAdvancedNetworking() {
    31  		check.Skip("Skipping test because the edge gateway does not have advanced networking enabled")
    32  	}
    33  
    34  	// Establish prerequisite - service monitor
    35  	lbMon := &types.LbMonitor{
    36  		Name:       check.TestName(),
    37  		Interval:   10,
    38  		Timeout:    10,
    39  		MaxRetries: 3,
    40  		Type:       "http",
    41  	}
    42  	err = deleteLbServiceMonitorIfExists(*edge, lbMon.Name)
    43  	check.Assert(err, IsNil)
    44  	lbMonitor, err := edge.CreateLbServiceMonitor(lbMon)
    45  	check.Assert(err, IsNil)
    46  	check.Assert(lbMonitor.ID, NotNil)
    47  
    48  	// Add service monitor to cleanup
    49  	parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway
    50  	AddToCleanupList(check.TestName(), "lbServiceMonitor", parentEntity, check.TestName())
    51  
    52  	// Configure creation object including reference to service monitor
    53  	lbPoolConfig := &types.LbPool{
    54  		Name:        TestLbServerPool,
    55  		Transparent: false,
    56  		Algorithm:   "round-robin",
    57  		MonitorId:   lbMonitor.ID,
    58  		Members: types.LbPoolMembers{
    59  			types.LbPoolMember{
    60  				Name:      "Server_one",
    61  				IpAddress: "1.1.1.1",
    62  				Port:      8443,
    63  				Weight:    1,
    64  				Condition: "enabled",
    65  			},
    66  			types.LbPoolMember{
    67  				Name:      "Server_two",
    68  				IpAddress: "2.2.2.2",
    69  				Port:      8443,
    70  				Weight:    2,
    71  				Condition: "enabled",
    72  			},
    73  		},
    74  	}
    75  
    76  	err = deleteLbServerPoolIfExists(*edge, lbMon.Name)
    77  	check.Assert(err, IsNil)
    78  	createdLbPool, err := edge.CreateLbServerPool(lbPoolConfig)
    79  	check.Assert(err, IsNil)
    80  	check.Assert(createdLbPool.ID, Not(IsNil))
    81  	check.Assert(createdLbPool.Transparent, Equals, lbPoolConfig.Transparent)
    82  	check.Assert(createdLbPool.MonitorId, Equals, lbMonitor.ID)
    83  	check.Assert(len(createdLbPool.Members), Equals, 2)
    84  	check.Assert(createdLbPool.Members[0].Condition, Equals, "enabled")
    85  	check.Assert(createdLbPool.Members[1].Condition, Equals, "enabled")
    86  	check.Assert(createdLbPool.Members[0].Weight, Equals, 1)
    87  	check.Assert(createdLbPool.Members[1].Weight, Equals, 2)
    88  	check.Assert(createdLbPool.Members[0].Name, Equals, "Server_one")
    89  	check.Assert(createdLbPool.Members[1].Name, Equals, "Server_two")
    90  
    91  	// We created server pool successfully therefore let's add it to cleanup list
    92  	AddToCleanupList(TestLbServerPool, "lbServerPool", parentEntity, check.TestName())
    93  
    94  	// Try to delete used service monitor and expect it to fail with nice error
    95  	err = edge.DeleteLbServiceMonitor(lbMon)
    96  	check.Assert(err, ErrorMatches, `.*Fail to delete objectId .*\S+.* for it is used by .*`)
    97  
    98  	// Lookup by both name and ID and compare that these are equal values
    99  	lbPoolByID, err := edge.getLbServerPool(&types.LbPool{ID: createdLbPool.ID})
   100  	check.Assert(err, IsNil)
   101  	check.Assert(lbPoolByID, Not(IsNil))
   102  
   103  	lbPoolByName, err := edge.getLbServerPool(&types.LbPool{Name: createdLbPool.Name})
   104  	check.Assert(err, IsNil)
   105  	check.Assert(lbPoolByName, Not(IsNil))
   106  	check.Assert(createdLbPool.ID, Equals, lbPoolByName.ID)
   107  	check.Assert(lbPoolByID.ID, Equals, lbPoolByName.ID)
   108  	check.Assert(lbPoolByID.Name, Equals, lbPoolByName.Name)
   109  
   110  	check.Assert(createdLbPool.Algorithm, Equals, lbPoolConfig.Algorithm)
   111  
   112  	// GetLbServerPools should return at least one pool which is ours.
   113  	pools, err := edge.GetLbServerPools()
   114  	check.Assert(err, IsNil)
   115  	check.Assert(pools, Not(HasLen), 0)
   116  
   117  	// Test updating fields
   118  	// Update algorithm
   119  	lbPoolByID.Algorithm = "ip-hash"
   120  	updatedLBPool, err := edge.UpdateLbServerPool(lbPoolByID)
   121  	check.Assert(err, IsNil)
   122  	check.Assert(updatedLBPool.Algorithm, Equals, lbPoolByID.Algorithm)
   123  
   124  	// Update boolean value fields
   125  	lbPoolByID.Transparent = true
   126  	updatedLBPool, err = edge.UpdateLbServerPool(lbPoolByID)
   127  	check.Assert(err, IsNil)
   128  	check.Assert(updatedLBPool.Transparent, Equals, lbPoolByID.Transparent)
   129  
   130  	// Verify that updated pool and its configuration are identical
   131  	check.Assert(updatedLBPool, DeepEquals, lbPoolByID)
   132  
   133  	// Try to set invalid algorithm hash and expect API to return error
   134  	// Invalid algorithm hash. Valid algorithms are: IP-HASH|ROUND-ROBIN|URI|LEASTCONN|URL|HTTP-HEADER.
   135  	lbPoolByID.Algorithm = "invalid_algorithm"
   136  	updatedLBPool, err = edge.UpdateLbServerPool(lbPoolByID)
   137  	check.Assert(updatedLBPool, IsNil)
   138  	check.Assert(err, ErrorMatches, ".*Invalid algorithm.*Valid algorithms are:.*")
   139  
   140  	// Update should fail without name
   141  	lbPoolByID.Name = ""
   142  	_, err = edge.UpdateLbServerPool(lbPoolByID)
   143  	check.Assert(err.Error(), Equals, "load balancer server pool Name cannot be empty")
   144  
   145  	// Delete / cleanup
   146  	err = edge.DeleteLbServerPool(&types.LbPool{ID: createdLbPool.ID})
   147  	check.Assert(err, IsNil)
   148  
   149  	_, err = edge.GetLbServerPoolById(createdLbPool.ID)
   150  	check.Assert(IsNotFound(err), Equals, true)
   151  }