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

     1  //go:build lb || lbServiceMonitor || 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_LBServiceMonitor tests CRUD methods for load balancer service monitor.
    16  // The following things are tested if prerequisite Edge Gateway exists:
    17  // 1. Creation of load balancer service monitor
    18  // 2. Get load balancer by both ID and Name (service monitor 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_LBServiceMonitor(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  	// Used for creating
    35  	lbMon := &types.LbMonitor{
    36  		Name:       check.TestName(),
    37  		Interval:   10,
    38  		Timeout:    10,
    39  		MaxRetries: 3,
    40  		Type:       "http",
    41  	}
    42  
    43  	err = deleteLbServiceMonitorIfExists(*edge, lbMon.Name)
    44  	check.Assert(err, IsNil)
    45  	lbMonitor, err := edge.CreateLbServiceMonitor(lbMon)
    46  	check.Assert(err, IsNil)
    47  	check.Assert(lbMonitor.ID, Not(IsNil))
    48  
    49  	// We created monitor successfully therefore let's add it to cleanup list
    50  	parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway
    51  	AddToCleanupList(check.TestName(), "lbServiceMonitor", parentEntity, check.TestName())
    52  
    53  	// Lookup by both name and ID and compare that these are equal values
    54  	lbMonitorByID, err := edge.getLbServiceMonitor(&types.LbMonitor{ID: lbMonitor.ID})
    55  	check.Assert(err, IsNil)
    56  	check.Assert(lbMonitorByID, Not(IsNil))
    57  
    58  	lbMonitorByName, err := edge.getLbServiceMonitor(&types.LbMonitor{Name: lbMonitor.Name})
    59  	check.Assert(err, IsNil)
    60  	check.Assert(lbMonitorByName, Not(IsNil))
    61  	check.Assert(lbMonitor.ID, Equals, lbMonitorByName.ID)
    62  	check.Assert(lbMonitorByID.ID, Equals, lbMonitorByName.ID)
    63  	check.Assert(lbMonitorByID.Name, Equals, lbMonitorByName.Name)
    64  
    65  	check.Assert(lbMonitor.ID, Equals, lbMonitorByID.ID)
    66  	check.Assert(lbMonitor.Interval, Equals, lbMonitorByID.Interval)
    67  	check.Assert(lbMonitor.Timeout, Equals, lbMonitorByID.Timeout)
    68  	check.Assert(lbMonitor.MaxRetries, Equals, lbMonitorByID.MaxRetries)
    69  
    70  	// GetLbServiceMonitors should return at least one vs which is ours.
    71  	lbMonitors, err := edge.GetLbServiceMonitors()
    72  	check.Assert(err, IsNil)
    73  	check.Assert(lbMonitors, Not(HasLen), 0)
    74  
    75  	// Test updating fields
    76  	// Update timeout
    77  	lbMonitorByID.Timeout = 35
    78  	updatedLBMonitor, err := edge.UpdateLbServiceMonitor(lbMonitorByID)
    79  	check.Assert(err, IsNil)
    80  	check.Assert(updatedLBMonitor.Timeout, Equals, 35)
    81  
    82  	// Verify that updated monitor and its configuration are identical
    83  	check.Assert(updatedLBMonitor, DeepEquals, lbMonitorByID)
    84  
    85  	// Update should fail without name
    86  	lbMonitorByID.Name = ""
    87  	_, err = edge.UpdateLbServiceMonitor(lbMonitorByID)
    88  	check.Assert(err.Error(), Equals, "load balancer monitor Name cannot be empty")
    89  
    90  	// Delete / cleanup
    91  	err = edge.DeleteLbServiceMonitor(&types.LbMonitor{ID: lbMonitorByID.ID})
    92  	check.Assert(err, IsNil)
    93  
    94  	_, err = edge.GetLbServiceMonitorById(lbMonitorByID.ID)
    95  	check.Assert(IsNotFound(err), Equals, true)
    96  }