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

     1  //go:build lb || lbAppProfile || 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_LBAppProfile tests CRUD methods for load balancer application profile.
    16  // The following things are tested if prerequisite Edge Gateway exists:
    17  // 1. Creation of load balancer application profile
    18  // 2. Get load balancer application profile by both ID and Name (application profile 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_LBAppProfile(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  	// Used for creating
    31  	lbAppProfileConfig := &types.LbAppProfile{
    32  		Name: TestLbAppProfile,
    33  		Persistence: &types.LbAppProfilePersistence{
    34  			Method: "sourceip",
    35  			Expire: 13,
    36  		},
    37  		Template:                      "https",
    38  		SslPassthrough:                false,
    39  		InsertXForwardedForHttpHeader: false,
    40  		ServerSslEnabled:              false,
    41  	}
    42  
    43  	err = deleteLbAppProfileIfExists(*edge, lbAppProfileConfig.Name)
    44  	check.Assert(err, IsNil)
    45  	createdLbAppProfile, err := edge.CreateLbAppProfile(lbAppProfileConfig)
    46  	check.Assert(err, IsNil)
    47  	check.Assert(createdLbAppProfile.ID, Not(IsNil))
    48  	check.Assert(createdLbAppProfile.Persistence.Method, Equals, lbAppProfileConfig.Persistence.Method)
    49  	check.Assert(createdLbAppProfile.Template, Equals, lbAppProfileConfig.Template)
    50  	check.Assert(createdLbAppProfile.Persistence.Expire, Equals, lbAppProfileConfig.Persistence.Expire)
    51  	check.Assert(createdLbAppProfile.SslPassthrough, Equals, lbAppProfileConfig.SslPassthrough)
    52  	check.Assert(createdLbAppProfile.InsertXForwardedForHttpHeader, Equals, lbAppProfileConfig.InsertXForwardedForHttpHeader)
    53  	check.Assert(createdLbAppProfile.ServerSslEnabled, Equals, lbAppProfileConfig.ServerSslEnabled)
    54  
    55  	// We created application profile successfully therefore let's add it to cleanup list
    56  	parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway
    57  	AddToCleanupList(TestLbAppProfile, "lbAppProfile", parentEntity, check.TestName())
    58  
    59  	// Lookup by both name and ID and compare that these are equal values
    60  	lbAppProfileByID, err := edge.GetLbAppProfileById(createdLbAppProfile.ID)
    61  	check.Assert(err, IsNil)
    62  	check.Assert(lbAppProfileByID, Not(IsNil))
    63  
    64  	lbAppProfileByName, err := edge.GetLbAppProfileByName(createdLbAppProfile.Name)
    65  	check.Assert(err, IsNil)
    66  	check.Assert(lbAppProfileByName, Not(IsNil))
    67  	check.Assert(createdLbAppProfile.ID, Equals, lbAppProfileByName.ID)
    68  	check.Assert(lbAppProfileByID.ID, Equals, lbAppProfileByName.ID)
    69  	check.Assert(lbAppProfileByID.Name, Equals, lbAppProfileByName.Name)
    70  	check.Assert(lbAppProfileByID.Persistence.Expire, Equals, lbAppProfileByName.Persistence.Expire)
    71  
    72  	check.Assert(createdLbAppProfile.Template, Equals, lbAppProfileConfig.Template)
    73  
    74  	// Test that we can extract a list of LB app profiles, and that one of them is the profile we have got when searching by name
    75  	lbAppProfiles, err := edge.GetLbAppProfiles()
    76  	check.Assert(err, IsNil)
    77  	check.Assert(lbAppProfiles, NotNil)
    78  	foundProfile := false
    79  	for _, profile := range lbAppProfiles {
    80  		if profile.Name == lbAppProfileByName.Name && profile.ID == lbAppProfileByName.ID {
    81  			foundProfile = true
    82  		}
    83  	}
    84  	check.Assert(foundProfile, Equals, true)
    85  
    86  	// Test updating fields
    87  	// Update persistence method
    88  	lbAppProfileByID.Persistence.Method = "sourceip"
    89  	updatedAppProfile, err := edge.UpdateLbAppProfile(lbAppProfileByID)
    90  	check.Assert(err, IsNil)
    91  	check.Assert(updatedAppProfile.Persistence.Method, Equals, lbAppProfileByID.Persistence.Method)
    92  
    93  	// Update boolean value fields
    94  	lbAppProfileByID.SslPassthrough = true
    95  	lbAppProfileByID.InsertXForwardedForHttpHeader = true
    96  	lbAppProfileByID.ServerSslEnabled = true
    97  	updatedAppProfile, err = edge.UpdateLbAppProfile(lbAppProfileByID)
    98  	check.Assert(err, IsNil)
    99  	check.Assert(updatedAppProfile.SslPassthrough, Equals, lbAppProfileByID.SslPassthrough)
   100  	check.Assert(updatedAppProfile.InsertXForwardedForHttpHeader, Equals, lbAppProfileByID.InsertXForwardedForHttpHeader)
   101  	check.Assert(updatedAppProfile.ServerSslEnabled, Equals, lbAppProfileByID.ServerSslEnabled)
   102  
   103  	// Verify that updated application profile and its configuration are identical
   104  	check.Assert(updatedAppProfile, DeepEquals, lbAppProfileByID)
   105  
   106  	// Try to set invalid algorithm hash and expect API to return error
   107  	// Invalid persistence method invalid_method. Valid methods are: COOKIE|SSL-SESSIONID|SOURCEIP.
   108  	lbAppProfileByID.Persistence.Method = "invalid_method"
   109  	updatedAppProfile, err = edge.UpdateLbAppProfile(lbAppProfileByID)
   110  	check.Assert(updatedAppProfile, IsNil)
   111  	check.Assert(err, ErrorMatches, ".*Invalid persistence method .*Valid methods are:.*")
   112  
   113  	// Update should fail without name
   114  	lbAppProfileByID.Name = ""
   115  	_, err = edge.UpdateLbAppProfile(lbAppProfileByID)
   116  	check.Assert(err.Error(), Equals, "load balancer application profile Name cannot be empty")
   117  
   118  	// Delete / cleanup
   119  	err = edge.DeleteLbAppProfile(&types.LbAppProfile{ID: createdLbAppProfile.ID})
   120  	check.Assert(err, IsNil)
   121  
   122  	// Ensure it is deleted
   123  	_, err = edge.GetLbAppProfileById(createdLbAppProfile.ID)
   124  	check.Assert(IsNotFound(err), Equals, true)
   125  }