File: requests_test.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (148 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package testing

import (
	"testing"

	"github.com/gophercloud/gophercloud/openstack/clustering/v1/policies"
	"github.com/gophercloud/gophercloud/pagination"
	th "github.com/gophercloud/gophercloud/testhelper"
	fake "github.com/gophercloud/gophercloud/testhelper/client"
)

func TestListPolicies(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyList(t)

	listOpts := policies.ListOpts{
		Limit: 1,
	}

	count := 0
	err := policies.List(fake.ServiceClient(), listOpts).EachPage(func(page pagination.Page) (bool, error) {
		actual, err := policies.ExtractPolicies(page)
		if err != nil {
			t.Errorf("Failed to extract policies: %v", err)
			return false, err
		}

		th.AssertDeepEquals(t, ExpectedPolicies[count], actual)
		count++

		return true, nil
	})

	th.AssertNoErr(t, err)

	if count != 2 {
		t.Errorf("Expected 2 pages, got %d", count)
	}
}

func TestCreatePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyCreate(t)

	expected := ExpectedCreatePolicy

	opts := policies.CreateOpts{
		Name: ExpectedCreatePolicy.Name,
		Spec: ExpectedCreatePolicy.Spec,
	}

	actual, err := policies.Create(fake.ServiceClient(), opts).Extract()
	th.AssertNoErr(t, err)

	th.AssertDeepEquals(t, &expected, actual)
}

func TestDeletePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyDelete(t)

	res := policies.Delete(fake.ServiceClient(), PolicyIDtoDelete)
	th.AssertNoErr(t, res.ExtractErr())

	requestID := res.Header["X-Openstack-Request-Id"][0]
	th.AssertEquals(t, PolicyDeleteRequestID, requestID)
}

func TestUpdatePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyUpdate(t)

	expected := ExpectedUpdatePolicy

	opts := policies.UpdateOpts{
		Name: ExpectedUpdatePolicy.Name,
	}

	actual, err := policies.Update(fake.ServiceClient(), PolicyIDtoUpdate, opts).Extract()
	th.AssertNoErr(t, err)

	th.AssertDeepEquals(t, &expected, actual)
}

func TestBadUpdatePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandleBadPolicyUpdate(t)

	opts := policies.UpdateOpts{
		Name: ExpectedUpdatePolicy.Name,
	}

	_, err := policies.Update(fake.ServiceClient(), PolicyIDtoUpdate, opts).Extract()
	th.AssertEquals(t, false, err == nil)
}

func TestValidatePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyValidate(t)

	expected := ExpectedValidatePolicy

	opts := policies.ValidateOpts{
		Spec: ExpectedValidatePolicy.Spec,
	}

	actual, err := policies.Validate(fake.ServiceClient(), opts).Extract()
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, &expected, actual)
}

func TestBadValidatePolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandleBadPolicyValidate(t)

	opts := policies.ValidateOpts{
		Spec: ExpectedValidatePolicy.Spec,
	}

	_, err := policies.Validate(fake.ServiceClient(), opts).Extract()
	th.AssertEquals(t, false, err == nil)
}

func TestGetPolicy(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandlePolicyGet(t)

	actual, err := policies.Get(fake.ServiceClient(), PolicyIDtoGet).Extract()
	th.AssertNoErr(t, err)

	th.AssertDeepEquals(t, ExpectedGetPolicy, *actual)
}