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

     1  //go:build lb || lbAppRule || 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_LBAppRule tests CRUD methods for load balancer application rule.
    16  // The following things are tested if prerequisite Edge Gateway exists:
    17  // 1. Creation of load balancer application rule
    18  // 2. Get load balancer application rule by both ID and Name (application rule name must be unique in single edge gateway)
    19  // 3. Update - change a 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_LBAppRule(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  	lbAppRuleConfig := &types.LbAppRule{
    32  		Name:   TestLbAppRule,
    33  		Script: "acl vmware_page url_beg / vmware redirect location https://www.vmware.com/ ifvmware_page",
    34  	}
    35  
    36  	err = deleteLbAppRuleIfExists(*edge, lbAppRuleConfig.Name)
    37  	check.Assert(err, IsNil)
    38  	createdLbAppRule, err := edge.CreateLbAppRule(lbAppRuleConfig)
    39  	check.Assert(err, IsNil)
    40  	check.Assert(createdLbAppRule.ID, Not(IsNil))
    41  
    42  	// // We created application rule successfully therefore let's add it to cleanup list
    43  	parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway
    44  	AddToCleanupList(TestLbAppRule, "lbAppRule", parentEntity, check.TestName())
    45  
    46  	// // Lookup by both name and ID and compare that these are equal values
    47  	lbAppRuleByID, err := edge.getLbAppRule(&types.LbAppRule{ID: createdLbAppRule.ID})
    48  	check.Assert(err, IsNil)
    49  	check.Assert(lbAppRuleByID, Not(IsNil))
    50  
    51  	lbAppRuleByName, err := edge.getLbAppRule(&types.LbAppRule{Name: createdLbAppRule.Name})
    52  	check.Assert(err, IsNil)
    53  	check.Assert(lbAppRuleByName, Not(IsNil))
    54  	check.Assert(createdLbAppRule.ID, Equals, lbAppRuleByName.ID)
    55  	check.Assert(lbAppRuleByID.ID, Equals, lbAppRuleByName.ID)
    56  	check.Assert(lbAppRuleByID.Name, Equals, lbAppRuleByName.Name)
    57  
    58  	check.Assert(createdLbAppRule.Script, Equals, lbAppRuleConfig.Script)
    59  
    60  	// Test that we can extract a list of LB app rules, and that one of them is the rule we have got when searching by name
    61  	lbAppRules, err := edge.GetLbAppRules()
    62  	check.Assert(err, IsNil)
    63  	check.Assert(lbAppRules, NotNil)
    64  	foundRule := false
    65  	for _, rule := range lbAppRules {
    66  		if rule.Name == lbAppRuleByName.Name && rule.ID == lbAppRuleByName.ID {
    67  			foundRule = true
    68  		}
    69  	}
    70  	check.Assert(foundRule, Equals, true)
    71  
    72  	// Test updating fields
    73  	// Update script to be multi-line
    74  	lbAppRuleByID.Script = "acl other_page url_beg / other redirect location https://www.other.com/ ifother_page\n" +
    75  		"acl other_page2 url_beg / other2 redirect location https://www.other2.com/ ifother_page2"
    76  	updatedAppProfile, err := edge.UpdateLbAppRule(lbAppRuleByID)
    77  	check.Assert(err, IsNil)
    78  	check.Assert(updatedAppProfile.Script, Equals, lbAppRuleByID.Script)
    79  
    80  	// Verify that updated pool and its configuration are identical
    81  	check.Assert(updatedAppProfile, DeepEquals, lbAppRuleByID)
    82  
    83  	// Try to set invalid script expect API to return error
    84  	// invalid applicationRule script, invalid script line : invalid_script, error details :
    85  	// Unknown keyword 'invalid_script'
    86  	lbAppRuleByID.Script = "invalid_script"
    87  	updatedAppProfile, err = edge.UpdateLbAppRule(lbAppRuleByID)
    88  	check.Assert(updatedAppProfile, IsNil)
    89  	check.Assert(err, NotNil)
    90  
    91  	// Update should fail without name
    92  	lbAppRuleByID.Name = ""
    93  	_, err = edge.UpdateLbAppRule(lbAppRuleByID)
    94  	check.Assert(err.Error(), Equals, "load balancer application rule Name cannot be empty")
    95  
    96  	// Delete / cleanup
    97  	err = edge.DeleteLbAppRule(&types.LbAppRule{ID: createdLbAppRule.ID})
    98  	check.Assert(err, IsNil)
    99  
   100  	// Ensure it is deleted
   101  	_, err = edge.GetLbAppRuleById(createdLbAppRule.ID)
   102  	check.Assert(IsNotFound(err), Equals, true)
   103  }