File: requests_test.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (144 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download | duplicates (2)
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
package testing

import (
	"testing"

	"github.com/gophercloud/gophercloud/openstack/baremetal/v1/ports"
	"github.com/gophercloud/gophercloud/pagination"
	th "github.com/gophercloud/gophercloud/testhelper"
	"github.com/gophercloud/gophercloud/testhelper/client"
)

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

	pages := 0
	err := ports.ListDetail(client.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		pages++

		actual, err := ports.ExtractPorts(page)
		if err != nil {
			return false, err
		}

		if len(actual) != 2 {
			t.Fatalf("Expected 2 ports, got %d", len(actual))
		}
		th.CheckDeepEquals(t, PortBar, actual[0])
		th.CheckDeepEquals(t, PortFoo, actual[1])

		return true, nil
	})

	th.AssertNoErr(t, err)

	if pages != 1 {
		t.Errorf("Expected 1 page, saw %d", pages)
	}
}

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

	pages := 0
	err := ports.List(client.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		pages++

		actual, err := ports.ExtractPorts(page)
		if err != nil {
			return false, err
		}

		if len(actual) != 2 {
			t.Fatalf("Expected 2 ports, got %d", len(actual))
		}
		th.AssertEquals(t, "3abe3f36-9708-4e9f-b07e-0f898061d3a7", actual[0].UUID)
		th.AssertEquals(t, "f2845e11-dbd4-4728-a8c0-30d19f48924a", actual[1].UUID)

		return true, nil
	})

	th.AssertNoErr(t, err)

	if pages != 1 {
		t.Errorf("Expected 1 page, saw %d", pages)
	}
}

func TestListOpts(t *testing.T) {
	// Detail cannot take Fields
	opts := ports.ListOpts{
		Fields: []string{"uuid", "address"},
	}

	_, err := opts.ToPortListDetailQuery()
	th.AssertEquals(t, err.Error(), "fields is not a valid option when getting a detailed listing of ports")

	// Regular ListOpts can
	query, err := opts.ToPortListQuery()
	th.AssertEquals(t, query, "?fields=uuid&fields=address")
	th.AssertNoErr(t, err)
}

func TestCreatePort(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandlePortCreationSuccessfully(t, SinglePortBody)

	iTrue := true
	actual, err := ports.Create(client.ServiceClient(), ports.CreateOpts{
		NodeUUID:   "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
		Address:    "52:54:00:4d:87:e6",
		PXEEnabled: &iTrue,
	}).Extract()
	th.AssertNoErr(t, err)

	th.CheckDeepEquals(t, PortFoo, *actual)
}

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

	res := ports.Delete(client.ServiceClient(), "3abe3f36-9708-4e9f-b07e-0f898061d3a7")
	th.AssertNoErr(t, res.Err)
}

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

	c := client.ServiceClient()
	actual, err := ports.Get(c, "f2845e11-dbd4-4728-a8c0-30d19f48924a").Extract()
	if err != nil {
		t.Fatalf("Unexpected Get error: %v", err)
	}

	th.CheckDeepEquals(t, PortFoo, *actual)
}

func TestUpdatePort(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandlePortUpdateSuccessfully(t, SinglePortBody)

	c := client.ServiceClient()
	actual, err := ports.Update(c, "f2845e11-dbd4-4728-a8c0-30d19f48924a", ports.UpdateOpts{
		ports.UpdateOperation{
			Op:    ports.ReplaceOp,
			Path:  "/address",
			Value: "22:22:22:22:22:22",
		},
	}).Extract()
	if err != nil {
		t.Fatalf("Unexpected Update error: %v", err)
	}

	th.CheckDeepEquals(t, PortFoo, *actual)
}