github.com/fastly/go-fastly/v5@v5.3.0/fastly/pool_test.go (about) 1 package fastly 2 3 import ( 4 "testing" 5 ) 6 7 func TestClient_Pools(t *testing.T) { 8 t.Parallel() 9 10 var err error 11 var tv *Version 12 record(t, "pools/version", func(c *Client) { 13 tv = testVersion(t, c) 14 }) 15 16 // Create 17 var p *Pool 18 record(t, "pools/create", func(c *Client) { 19 p, err = c.CreatePool(&CreatePoolInput{ 20 ServiceID: testServiceID, 21 ServiceVersion: tv.Number, 22 Name: "test_pool", 23 Comment: "test pool", 24 Quorum: 50, 25 UseTLS: true, 26 TLSCertHostname: "example.com", 27 Type: PoolTypeRandom, 28 }) 29 }) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 // Ensure deleted 35 defer func() { 36 record(t, "pools/cleanup", func(c *Client) { 37 c.DeletePool(&DeletePoolInput{ 38 ServiceID: testServiceID, 39 ServiceVersion: tv.Number, 40 Name: "test_pool", 41 }) 42 43 c.DeletePool(&DeletePoolInput{ 44 ServiceID: testServiceID, 45 ServiceVersion: tv.Number, 46 Name: "new_test_pool", 47 }) 48 }) 49 }() 50 51 if p.Name != "test_pool" { 52 t.Errorf("bad name: %q", p.Name) 53 } 54 55 if p.Quorum != 50 { 56 t.Errorf("bad quorum: %q", p.Quorum) 57 } 58 59 if p.UseTLS != true { 60 t.Errorf("bad use_tls: %t", p.UseTLS) 61 } 62 63 if p.TLSCertHostname != "example.com" { 64 t.Errorf("bad tls_cert_hostname: %q", p.TLSCertHostname) 65 } 66 67 if p.Type != PoolTypeRandom { 68 t.Errorf("bad type: %q", p.Type) 69 } 70 71 // List 72 var ps []*Pool 73 record(t, "pools/list", func(c *Client) { 74 ps, err = c.ListPools(&ListPoolsInput{ 75 ServiceID: testServiceID, 76 ServiceVersion: tv.Number, 77 }) 78 }) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if len(ps) < 1 { 83 t.Errorf("bad pools: %v", ps) 84 } 85 86 // Get 87 var np *Pool 88 record(t, "pools/get", func(c *Client) { 89 np, err = c.GetPool(&GetPoolInput{ 90 ServiceID: testServiceID, 91 ServiceVersion: tv.Number, 92 Name: "test_pool", 93 }) 94 }) 95 if err != nil { 96 t.Fatal(err) 97 } 98 if p.Name != np.Name { 99 t.Errorf("bad name: %q (%q)", p.Name, np.Name) 100 } 101 if p.Quorum != np.Quorum { 102 t.Errorf("bad quorum: %q (%q)", p.Quorum, np.Quorum) 103 } 104 if p.Type != np.Type { 105 t.Errorf("bad type: %q (%q)", p.Type, np.Type) 106 } 107 108 // Update 109 var up *Pool 110 record(t, "pools/update", func(c *Client) { 111 up, err = c.UpdatePool(&UpdatePoolInput{ 112 ServiceID: testServiceID, 113 ServiceVersion: tv.Number, 114 Name: "test_pool", 115 NewName: String("new_test_pool"), 116 Quorum: Uint(0), 117 Type: PPoolType(PoolTypeHash), 118 }) 119 }) 120 if err != nil { 121 t.Fatal(err) 122 } 123 if up.Name != "new_test_pool" { 124 t.Errorf("bad name: %q", up.Name) 125 } 126 if up.Quorum != 0 { 127 t.Errorf("bad quorum: %q", up.Quorum) 128 } 129 130 // Delete 131 record(t, "pools/delete", func(c *Client) { 132 err = c.DeletePool(&DeletePoolInput{ 133 ServiceID: testServiceID, 134 ServiceVersion: tv.Number, 135 Name: "new_test_pool", 136 }) 137 }) 138 if err != nil { 139 t.Fatal(err) 140 } 141 } 142 143 func TestClient_ListPools_validation(t *testing.T) { 144 var err error 145 _, err = testClient.ListPools(&ListPoolsInput{ 146 ServiceID: "", 147 }) 148 if err != ErrMissingServiceID { 149 t.Errorf("bad error: %s", err) 150 } 151 152 _, err = testClient.ListPools(&ListPoolsInput{ 153 ServiceID: "foo", 154 ServiceVersion: 0, 155 }) 156 if err != ErrMissingServiceVersion { 157 t.Errorf("bad error: %s", err) 158 } 159 } 160 161 func TestClient_CreatePool_validation(t *testing.T) { 162 var err error 163 _, err = testClient.CreatePool(&CreatePoolInput{ 164 ServiceID: "", 165 }) 166 if err != ErrMissingServiceID { 167 t.Errorf("bad error: %s", err) 168 } 169 170 _, err = testClient.CreatePool(&CreatePoolInput{ 171 ServiceID: "foo", 172 ServiceVersion: 0, 173 }) 174 if err != ErrMissingServiceVersion { 175 t.Errorf("bad error: %s", err) 176 } 177 178 _, err = testClient.CreatePool(&CreatePoolInput{ 179 ServiceID: "foo", 180 ServiceVersion: 1, 181 Name: "", 182 }) 183 if err != ErrMissingName { 184 t.Errorf("bad error: %s", err) 185 } 186 } 187 188 func TestClient_GetPool_validation(t *testing.T) { 189 var err error 190 _, err = testClient.GetPool(&GetPoolInput{ 191 ServiceID: "", 192 }) 193 if err != ErrMissingServiceID { 194 t.Errorf("bad error: %s", err) 195 } 196 197 _, err = testClient.GetPool(&GetPoolInput{ 198 ServiceID: "foo", 199 ServiceVersion: 0, 200 }) 201 if err != ErrMissingServiceVersion { 202 t.Errorf("bad error: %s", err) 203 } 204 205 _, err = testClient.GetPool(&GetPoolInput{ 206 ServiceID: "foo", 207 ServiceVersion: 1, 208 Name: "", 209 }) 210 if err != ErrMissingName { 211 t.Errorf("bad error: %s", err) 212 } 213 } 214 215 func TestClient_UpdatePool_validation(t *testing.T) { 216 var err error 217 _, err = testClient.UpdatePool(&UpdatePoolInput{ 218 ServiceID: "", 219 }) 220 if err != ErrMissingServiceID { 221 t.Errorf("bad error: %s", err) 222 } 223 224 _, err = testClient.UpdatePool(&UpdatePoolInput{ 225 ServiceID: "foo", 226 ServiceVersion: 0, 227 }) 228 if err != ErrMissingServiceVersion { 229 t.Errorf("bad error: %s", err) 230 } 231 232 _, err = testClient.UpdatePool(&UpdatePoolInput{ 233 ServiceID: "foo", 234 ServiceVersion: 1, 235 Name: "", 236 }) 237 if err != ErrMissingName { 238 t.Errorf("bad error: %s", err) 239 } 240 } 241 242 func TestClient_DeletePool_validation(t *testing.T) { 243 var err error 244 err = testClient.DeletePool(&DeletePoolInput{ 245 ServiceID: "", 246 }) 247 if err != ErrMissingServiceID { 248 t.Errorf("bad error: %s", err) 249 } 250 251 err = testClient.DeletePool(&DeletePoolInput{ 252 ServiceID: "foo", 253 ServiceVersion: 0, 254 }) 255 if err != ErrMissingServiceVersion { 256 t.Errorf("bad error: %s", err) 257 } 258 259 err = testClient.DeletePool(&DeletePoolInput{ 260 ServiceID: "foo", 261 ServiceVersion: 1, 262 Name: "", 263 }) 264 if err != ErrMissingName { 265 t.Errorf("bad error: %s", err) 266 } 267 }