github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/monitors/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" 7 fake "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/testhelper" 8 "github.com/gophercloud/gophercloud/pagination" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 ) 11 12 func TestListHealthmonitors(t *testing.T) { 13 th.SetupHTTP() 14 defer th.TeardownHTTP() 15 HandleHealthmonitorListSuccessfully(t) 16 17 pages := 0 18 err := monitors.List(fake.ServiceClient(), monitors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 19 pages++ 20 21 actual, err := monitors.ExtractMonitors(page) 22 if err != nil { 23 return false, err 24 } 25 26 if len(actual) != 2 { 27 t.Fatalf("Expected 2 healthmonitors, got %d", len(actual)) 28 } 29 th.CheckDeepEquals(t, HealthmonitorWeb, actual[0]) 30 th.CheckDeepEquals(t, HealthmonitorDb, actual[1]) 31 32 return true, nil 33 }) 34 35 th.AssertNoErr(t, err) 36 37 if pages != 1 { 38 t.Errorf("Expected 1 page, saw %d", pages) 39 } 40 } 41 42 func TestListAllHealthmonitors(t *testing.T) { 43 th.SetupHTTP() 44 defer th.TeardownHTTP() 45 HandleHealthmonitorListSuccessfully(t) 46 47 allPages, err := monitors.List(fake.ServiceClient(), monitors.ListOpts{}).AllPages() 48 th.AssertNoErr(t, err) 49 actual, err := monitors.ExtractMonitors(allPages) 50 th.AssertNoErr(t, err) 51 th.CheckDeepEquals(t, HealthmonitorWeb, actual[0]) 52 th.CheckDeepEquals(t, HealthmonitorDb, actual[1]) 53 } 54 55 func TestCreateHealthmonitor(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 HandleHealthmonitorCreationSuccessfully(t, SingleHealthmonitorBody) 59 60 actual, err := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{ 61 Type: "HTTP", 62 Name: "db", 63 PoolID: "84f1b61f-58c4-45bf-a8a9-2dafb9e5214d", 64 ProjectID: "453105b9-1754-413f-aab1-55f1af620750", 65 Delay: 20, 66 Timeout: 10, 67 MaxRetries: 5, 68 MaxRetriesDown: 4, 69 URLPath: "/check", 70 ExpectedCodes: "200-299", 71 }).Extract() 72 th.AssertNoErr(t, err) 73 74 th.CheckDeepEquals(t, HealthmonitorDb, *actual) 75 } 76 77 func TestRequiredCreateOpts(t *testing.T) { 78 res := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{}) 79 if res.Err == nil { 80 t.Fatalf("Expected error, got none") 81 } 82 res = monitors.Create(fake.ServiceClient(), monitors.CreateOpts{Type: monitors.TypeHTTP}) 83 if res.Err == nil { 84 t.Fatalf("Expected error, got none") 85 } 86 } 87 88 func TestGetHealthmonitor(t *testing.T) { 89 th.SetupHTTP() 90 defer th.TeardownHTTP() 91 HandleHealthmonitorGetSuccessfully(t) 92 93 client := fake.ServiceClient() 94 actual, err := monitors.Get(client, "5d4b5228-33b0-4e60-b225-9b727c1a20e7").Extract() 95 if err != nil { 96 t.Fatalf("Unexpected Get error: %v", err) 97 } 98 99 th.CheckDeepEquals(t, HealthmonitorDb, *actual) 100 } 101 102 func TestDeleteHealthmonitor(t *testing.T) { 103 th.SetupHTTP() 104 defer th.TeardownHTTP() 105 HandleHealthmonitorDeletionSuccessfully(t) 106 107 res := monitors.Delete(fake.ServiceClient(), "5d4b5228-33b0-4e60-b225-9b727c1a20e7") 108 th.AssertNoErr(t, res.Err) 109 } 110 111 func TestUpdateHealthmonitor(t *testing.T) { 112 th.SetupHTTP() 113 defer th.TeardownHTTP() 114 HandleHealthmonitorUpdateSuccessfully(t) 115 116 client := fake.ServiceClient() 117 name := "NewHealthmonitorName" 118 actual, err := monitors.Update(client, "5d4b5228-33b0-4e60-b225-9b727c1a20e7", monitors.UpdateOpts{ 119 Name: &name, 120 Delay: 3, 121 Timeout: 20, 122 MaxRetries: 10, 123 MaxRetriesDown: 8, 124 URLPath: "/another_check", 125 ExpectedCodes: "301", 126 }).Extract() 127 if err != nil { 128 t.Fatalf("Unexpected Update error: %v", err) 129 } 130 131 th.CheckDeepEquals(t, HealthmonitorUpdated, *actual) 132 } 133 134 func TestDelayMustBeGreaterOrEqualThanTimeout(t *testing.T) { 135 _, err := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{ 136 Type: "HTTP", 137 PoolID: "d459f7d8-c6ee-439d-8713-d3fc08aeed8d", 138 Delay: 1, 139 Timeout: 10, 140 MaxRetries: 5, 141 URLPath: "/check", 142 ExpectedCodes: "200-299", 143 }).Extract() 144 145 if err == nil { 146 t.Fatalf("Expected error, got none") 147 } 148 149 _, err = monitors.Update(fake.ServiceClient(), "453105b9-1754-413f-aab1-55f1af620750", monitors.UpdateOpts{ 150 Delay: 1, 151 Timeout: 10, 152 }).Extract() 153 154 if err == nil { 155 t.Fatalf("Expected error, got none") 156 } 157 }