github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/l7policies/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/l7policies"
     8  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    10  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    11  )
    12  
    13  func TestCreateL7Policy(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleL7PolicyCreationSuccessfully(t, SingleL7PolicyBody)
    17  
    18  	actual, err := l7policies.Create(context.TODO(), fake.ServiceClient(), l7policies.CreateOpts{
    19  		Name:        "redirect-example.com",
    20  		ListenerID:  "023f2e34-7806-443b-bfae-16c324569a3d",
    21  		Action:      l7policies.ActionRedirectToURL,
    22  		RedirectURL: "http://www.example.com",
    23  	}).Extract()
    24  
    25  	th.AssertNoErr(t, err)
    26  	th.CheckDeepEquals(t, L7PolicyToURL, *actual)
    27  }
    28  
    29  func TestRequiredL7PolicyCreateOpts(t *testing.T) {
    30  	// no param specified.
    31  	res := l7policies.Create(context.TODO(), fake.ServiceClient(), l7policies.CreateOpts{})
    32  	if res.Err == nil {
    33  		t.Fatalf("Expected error, got none")
    34  	}
    35  
    36  	// Action is invalid.
    37  	res = l7policies.Create(context.TODO(), fake.ServiceClient(), l7policies.CreateOpts{
    38  		ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d",
    39  		Action:     l7policies.Action("invalid"),
    40  	})
    41  	if res.Err == nil {
    42  		t.Fatalf("Expected error, but got none")
    43  	}
    44  }
    45  
    46  func TestListL7Policies(t *testing.T) {
    47  	th.SetupHTTP()
    48  	defer th.TeardownHTTP()
    49  	HandleL7PolicyListSuccessfully(t)
    50  
    51  	pages := 0
    52  	err := l7policies.List(fake.ServiceClient(), l7policies.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    53  		pages++
    54  
    55  		actual, err := l7policies.ExtractL7Policies(page)
    56  		if err != nil {
    57  			return false, err
    58  		}
    59  
    60  		if len(actual) != 2 {
    61  			t.Fatalf("Expected 2 l7policies, got %d", len(actual))
    62  		}
    63  		th.CheckDeepEquals(t, L7PolicyToURL, actual[0])
    64  		th.CheckDeepEquals(t, L7PolicyToPool, actual[1])
    65  
    66  		return true, nil
    67  	})
    68  
    69  	th.AssertNoErr(t, err)
    70  
    71  	if pages != 1 {
    72  		t.Errorf("Expected 1 page, saw %d", pages)
    73  	}
    74  }
    75  
    76  func TestListAllL7Policies(t *testing.T) {
    77  	th.SetupHTTP()
    78  	defer th.TeardownHTTP()
    79  	HandleL7PolicyListSuccessfully(t)
    80  
    81  	allPages, err := l7policies.List(fake.ServiceClient(), l7policies.ListOpts{}).AllPages(context.TODO())
    82  	th.AssertNoErr(t, err)
    83  	actual, err := l7policies.ExtractL7Policies(allPages)
    84  	th.AssertNoErr(t, err)
    85  	th.CheckDeepEquals(t, L7PolicyToURL, actual[0])
    86  	th.CheckDeepEquals(t, L7PolicyToPool, actual[1])
    87  }
    88  
    89  func TestGetL7Policy(t *testing.T) {
    90  	th.SetupHTTP()
    91  	defer th.TeardownHTTP()
    92  	HandleL7PolicyGetSuccessfully(t)
    93  
    94  	client := fake.ServiceClient()
    95  	actual, err := l7policies.Get(context.TODO(), client, "8a1412f0-4c32-4257-8b07-af4770b604fd").Extract()
    96  	if err != nil {
    97  		t.Fatalf("Unexpected Get error: %v", err)
    98  	}
    99  
   100  	th.CheckDeepEquals(t, L7PolicyToURL, *actual)
   101  }
   102  
   103  func TestDeleteL7Policy(t *testing.T) {
   104  	th.SetupHTTP()
   105  	defer th.TeardownHTTP()
   106  	HandleL7PolicyDeletionSuccessfully(t)
   107  
   108  	res := l7policies.Delete(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd")
   109  	th.AssertNoErr(t, res.Err)
   110  }
   111  
   112  func TestUpdateL7Policy(t *testing.T) {
   113  	th.SetupHTTP()
   114  	defer th.TeardownHTTP()
   115  	HandleL7PolicyUpdateSuccessfully(t)
   116  
   117  	client := fake.ServiceClient()
   118  	newName := "NewL7PolicyName"
   119  	redirectURL := "http://www.new-example.com"
   120  	actual, err := l7policies.Update(context.TODO(), client, "8a1412f0-4c32-4257-8b07-af4770b604fd",
   121  		l7policies.UpdateOpts{
   122  			Name:        &newName,
   123  			Action:      l7policies.ActionRedirectToURL,
   124  			RedirectURL: &redirectURL,
   125  		}).Extract()
   126  	if err != nil {
   127  		t.Fatalf("Unexpected Update error: %v", err)
   128  	}
   129  
   130  	th.CheckDeepEquals(t, L7PolicyUpdated, *actual)
   131  }
   132  
   133  func TestUpdateL7PolicyNullRedirectURL(t *testing.T) {
   134  	th.SetupHTTP()
   135  	defer th.TeardownHTTP()
   136  	HandleL7PolicyUpdateNullRedirectURLSuccessfully(t)
   137  
   138  	client := fake.ServiceClient()
   139  	newName := "NewL7PolicyName"
   140  	redirectURL := ""
   141  	actual, err := l7policies.Update(context.TODO(), client, "8a1412f0-4c32-4257-8b07-af4770b604fd",
   142  		l7policies.UpdateOpts{
   143  			Name:        &newName,
   144  			RedirectURL: &redirectURL,
   145  		}).Extract()
   146  	if err != nil {
   147  		t.Fatalf("Unexpected Update error: %v", err)
   148  	}
   149  
   150  	th.CheckDeepEquals(t, L7PolicyNullRedirectURLUpdated, *actual)
   151  }
   152  
   153  func TestUpdateL7PolicyWithInvalidOpts(t *testing.T) {
   154  	th.SetupHTTP()
   155  	defer th.TeardownHTTP()
   156  
   157  	res := l7policies.Update(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.UpdateOpts{
   158  		Action: l7policies.Action("invalid"),
   159  	})
   160  	if res.Err == nil {
   161  		t.Fatalf("Expected error, got none")
   162  	}
   163  }
   164  
   165  func TestCreateRule(t *testing.T) {
   166  	th.SetupHTTP()
   167  	defer th.TeardownHTTP()
   168  	HandleRuleCreationSuccessfully(t, SingleRuleBody)
   169  
   170  	actual, err := l7policies.CreateRule(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{
   171  		RuleType:    l7policies.TypePath,
   172  		CompareType: l7policies.CompareTypeRegex,
   173  		Value:       "/images*",
   174  	}).Extract()
   175  	th.AssertNoErr(t, err)
   176  
   177  	th.CheckDeepEquals(t, RulePath, *actual)
   178  }
   179  
   180  func TestRequiredRuleCreateOpts(t *testing.T) {
   181  	th.SetupHTTP()
   182  	defer th.TeardownHTTP()
   183  
   184  	res := l7policies.CreateRule(context.TODO(), fake.ServiceClient(), "", l7policies.CreateRuleOpts{})
   185  	if res.Err == nil {
   186  		t.Fatalf("Expected error, got none")
   187  	}
   188  	res = l7policies.CreateRule(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{
   189  		RuleType: l7policies.TypePath,
   190  	})
   191  	if res.Err == nil {
   192  		t.Fatalf("Expected error, but got none")
   193  	}
   194  	res = l7policies.CreateRule(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{
   195  		RuleType:    l7policies.RuleType("invalid"),
   196  		CompareType: l7policies.CompareTypeRegex,
   197  		Value:       "/images*",
   198  	})
   199  	if res.Err == nil {
   200  		t.Fatalf("Expected error, but got none")
   201  	}
   202  	res = l7policies.CreateRule(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{
   203  		RuleType:    l7policies.TypePath,
   204  		CompareType: l7policies.CompareType("invalid"),
   205  		Value:       "/images*",
   206  	})
   207  	if res.Err == nil {
   208  		t.Fatalf("Expected error, but got none")
   209  	}
   210  }
   211  
   212  func TestListRules(t *testing.T) {
   213  	th.SetupHTTP()
   214  	defer th.TeardownHTTP()
   215  	HandleRuleListSuccessfully(t)
   216  
   217  	pages := 0
   218  	err := l7policies.ListRules(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.ListRulesOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   219  		pages++
   220  
   221  		actual, err := l7policies.ExtractRules(page)
   222  		if err != nil {
   223  			return false, err
   224  		}
   225  
   226  		if len(actual) != 2 {
   227  			t.Fatalf("Expected 2 rules, got %d", len(actual))
   228  		}
   229  		th.CheckDeepEquals(t, RulePath, actual[0])
   230  		th.CheckDeepEquals(t, RuleHostName, actual[1])
   231  
   232  		return true, nil
   233  	})
   234  
   235  	th.AssertNoErr(t, err)
   236  
   237  	if pages != 1 {
   238  		t.Errorf("Expected 1 page, saw %d", pages)
   239  	}
   240  }
   241  
   242  func TestListAllRules(t *testing.T) {
   243  	th.SetupHTTP()
   244  	defer th.TeardownHTTP()
   245  	HandleRuleListSuccessfully(t)
   246  
   247  	allPages, err := l7policies.ListRules(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.ListRulesOpts{}).AllPages(context.TODO())
   248  	th.AssertNoErr(t, err)
   249  
   250  	actual, err := l7policies.ExtractRules(allPages)
   251  	th.AssertNoErr(t, err)
   252  	th.CheckDeepEquals(t, RulePath, actual[0])
   253  	th.CheckDeepEquals(t, RuleHostName, actual[1])
   254  }
   255  
   256  func TestGetRule(t *testing.T) {
   257  	th.SetupHTTP()
   258  	defer th.TeardownHTTP()
   259  	HandleRuleGetSuccessfully(t)
   260  
   261  	client := fake.ServiceClient()
   262  	actual, err := l7policies.GetRule(context.TODO(), client, "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c").Extract()
   263  	if err != nil {
   264  		t.Fatalf("Unexpected Get error: %v", err)
   265  	}
   266  
   267  	th.CheckDeepEquals(t, RulePath, *actual)
   268  }
   269  
   270  func TestDeleteRule(t *testing.T) {
   271  	th.SetupHTTP()
   272  	defer th.TeardownHTTP()
   273  	HandleRuleDeletionSuccessfully(t)
   274  
   275  	res := l7policies.DeleteRule(context.TODO(), fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c")
   276  	th.AssertNoErr(t, res.Err)
   277  }
   278  
   279  func TestUpdateRule(t *testing.T) {
   280  	th.SetupHTTP()
   281  	defer th.TeardownHTTP()
   282  	HandleRuleUpdateSuccessfully(t)
   283  
   284  	client := fake.ServiceClient()
   285  	invert := false
   286  	key := ""
   287  	actual, err := l7policies.UpdateRule(context.TODO(), client, "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c", l7policies.UpdateRuleOpts{
   288  		RuleType:    l7policies.TypePath,
   289  		CompareType: l7policies.CompareTypeRegex,
   290  		Value:       "/images/special*",
   291  		Key:         &key,
   292  		Invert:      &invert,
   293  	}).Extract()
   294  	if err != nil {
   295  		t.Fatalf("Unexpected Update error: %v", err)
   296  	}
   297  
   298  	th.CheckDeepEquals(t, RuleUpdated, *actual)
   299  }
   300  
   301  func TestUpdateRuleWithInvalidOpts(t *testing.T) {
   302  	th.SetupHTTP()
   303  	defer th.TeardownHTTP()
   304  
   305  	res := l7policies.UpdateRule(context.TODO(), fake.ServiceClient(), "", "", l7policies.UpdateRuleOpts{
   306  		RuleType: l7policies.RuleType("invalid"),
   307  	})
   308  	if res.Err == nil {
   309  		t.Fatalf("Expected error, got none")
   310  	}
   311  
   312  	res = l7policies.UpdateRule(context.TODO(), fake.ServiceClient(), "", "", l7policies.UpdateRuleOpts{
   313  		CompareType: l7policies.CompareType("invalid"),
   314  	})
   315  	if res.Err == nil {
   316  		t.Fatalf("Expected error, got none")
   317  	}
   318  }