github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/monitors/testing/requests_test.go (about)

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