github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/policies/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud/openstack/clustering/v1/policies"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  	th "github.com/gophercloud/gophercloud/testhelper"
     9  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    10  )
    11  
    12  func TestListPolicies(t *testing.T) {
    13  	th.SetupHTTP()
    14  	defer th.TeardownHTTP()
    15  
    16  	HandlePolicyList(t)
    17  
    18  	listOpts := policies.ListOpts{
    19  		Limit: 1,
    20  	}
    21  
    22  	count := 0
    23  	err := policies.List(fake.ServiceClient(), listOpts).EachPage(func(page pagination.Page) (bool, error) {
    24  		actual, err := policies.ExtractPolicies(page)
    25  		if err != nil {
    26  			t.Errorf("Failed to extract policies: %v", err)
    27  			return false, err
    28  		}
    29  
    30  		th.AssertDeepEquals(t, ExpectedPolicies[count], actual)
    31  		count++
    32  
    33  		return true, nil
    34  	})
    35  
    36  	th.AssertNoErr(t, err)
    37  
    38  	if count != 2 {
    39  		t.Errorf("Expected 2 pages, got %d", count)
    40  	}
    41  }
    42  
    43  func TestCreatePolicy(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  
    47  	HandlePolicyCreate(t)
    48  
    49  	expected := ExpectedCreatePolicy
    50  
    51  	opts := policies.CreateOpts{
    52  		Name: ExpectedCreatePolicy.Name,
    53  		Spec: ExpectedCreatePolicy.Spec,
    54  	}
    55  
    56  	actual, err := policies.Create(fake.ServiceClient(), opts).Extract()
    57  	th.AssertNoErr(t, err)
    58  
    59  	th.AssertDeepEquals(t, &expected, actual)
    60  }
    61  
    62  func TestDeletePolicy(t *testing.T) {
    63  	th.SetupHTTP()
    64  	defer th.TeardownHTTP()
    65  
    66  	HandlePolicyDelete(t)
    67  
    68  	res := policies.Delete(fake.ServiceClient(), PolicyIDtoDelete)
    69  	th.AssertNoErr(t, res.ExtractErr())
    70  
    71  	requestID := res.Header["X-Openstack-Request-Id"][0]
    72  	th.AssertEquals(t, PolicyDeleteRequestID, requestID)
    73  }
    74  
    75  func TestUpdatePolicy(t *testing.T) {
    76  	th.SetupHTTP()
    77  	defer th.TeardownHTTP()
    78  
    79  	HandlePolicyUpdate(t)
    80  
    81  	expected := ExpectedUpdatePolicy
    82  
    83  	opts := policies.UpdateOpts{
    84  		Name: ExpectedUpdatePolicy.Name,
    85  	}
    86  
    87  	actual, err := policies.Update(fake.ServiceClient(), PolicyIDtoUpdate, opts).Extract()
    88  	th.AssertNoErr(t, err)
    89  
    90  	th.AssertDeepEquals(t, &expected, actual)
    91  }
    92  
    93  func TestBadUpdatePolicy(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  
    97  	HandleBadPolicyUpdate(t)
    98  
    99  	opts := policies.UpdateOpts{
   100  		Name: ExpectedUpdatePolicy.Name,
   101  	}
   102  
   103  	_, err := policies.Update(fake.ServiceClient(), PolicyIDtoUpdate, opts).Extract()
   104  	th.AssertEquals(t, false, err == nil)
   105  }
   106  
   107  func TestValidatePolicy(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  
   111  	HandlePolicyValidate(t)
   112  
   113  	expected := ExpectedValidatePolicy
   114  
   115  	opts := policies.ValidateOpts{
   116  		Spec: ExpectedValidatePolicy.Spec,
   117  	}
   118  
   119  	actual, err := policies.Validate(fake.ServiceClient(), opts).Extract()
   120  	th.AssertNoErr(t, err)
   121  	th.AssertDeepEquals(t, &expected, actual)
   122  }
   123  
   124  func TestBadValidatePolicy(t *testing.T) {
   125  	th.SetupHTTP()
   126  	defer th.TeardownHTTP()
   127  
   128  	HandleBadPolicyValidate(t)
   129  
   130  	opts := policies.ValidateOpts{
   131  		Spec: ExpectedValidatePolicy.Spec,
   132  	}
   133  
   134  	_, err := policies.Validate(fake.ServiceClient(), opts).Extract()
   135  	th.AssertEquals(t, false, err == nil)
   136  }
   137  
   138  func TestGetPolicy(t *testing.T) {
   139  	th.SetupHTTP()
   140  	defer th.TeardownHTTP()
   141  
   142  	HandlePolicyGet(t)
   143  
   144  	actual, err := policies.Get(fake.ServiceClient(), PolicyIDtoGet).Extract()
   145  	th.AssertNoErr(t, err)
   146  
   147  	th.AssertDeepEquals(t, ExpectedGetPolicy, *actual)
   148  }