github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/l7policies/testing/requests_test.go (about)

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