github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/clustering/v1/policies_test.go (about)

     1  //go:build acceptance || clustering || policies
     2  // +build acceptance clustering policies
     3  
     4  package v1
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/gophercloud/gophercloud/internal/acceptance/clients"
    10  	"github.com/gophercloud/gophercloud/internal/acceptance/tools"
    11  	"github.com/gophercloud/gophercloud/openstack/clustering/v1/policies"
    12  	th "github.com/gophercloud/gophercloud/testhelper"
    13  )
    14  
    15  func TestPoliciesCRUD(t *testing.T) {
    16  	client, err := clients.NewClusteringV1Client()
    17  	th.AssertNoErr(t, err)
    18  	client.Microversion = "1.5"
    19  
    20  	policy, err := CreatePolicy(t, client)
    21  	th.AssertNoErr(t, err)
    22  	defer DeletePolicy(t, client, policy.ID)
    23  
    24  	// Test listing policies
    25  	allPages, err := policies.List(client, nil).AllPages()
    26  	th.AssertNoErr(t, err)
    27  
    28  	allPolicies, err := policies.ExtractPolicies(allPages)
    29  	th.AssertNoErr(t, err)
    30  
    31  	var found bool
    32  	for _, v := range allPolicies {
    33  		if v.ID == policy.ID {
    34  			found = true
    35  		}
    36  	}
    37  
    38  	th.AssertEquals(t, found, true)
    39  
    40  	// Test Get policy
    41  	getPolicy, err := policies.Get(client, policy.ID).Extract()
    42  	th.AssertNoErr(t, err)
    43  	tools.PrintResource(t, getPolicy)
    44  
    45  	// Test updating policy
    46  	updateOpts := policies.UpdateOpts{
    47  		Name: policy.Name + "-UPDATE",
    48  	}
    49  
    50  	t.Logf("Attempting to update policy: %s", policy.ID)
    51  	updatePolicy, err := policies.Update(client, policy.ID, updateOpts).Extract()
    52  	th.AssertNoErr(t, err)
    53  
    54  	tools.PrintResource(t, updatePolicy)
    55  	tools.PrintResource(t, updatePolicy.UpdatedAt)
    56  
    57  	// Test validating policy
    58  	t.Logf("Attempting to validate policy: %s", policy.ID)
    59  	validateOpts := policies.ValidateOpts{
    60  		Spec: TestPolicySpec,
    61  	}
    62  
    63  	validatePolicy, err := policies.Validate(client, validateOpts).Extract()
    64  	th.AssertNoErr(t, err)
    65  
    66  	tools.PrintResource(t, validatePolicy)
    67  
    68  	th.AssertEquals(t, validatePolicy.Name, "validated_policy")
    69  	th.AssertEquals(t, validatePolicy.Spec.Version, TestPolicySpec.Version)
    70  }