github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/defsecrules/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/defsecrules"
     7  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/secgroups"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  const ruleID = "{ruleID}"
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	mockListRulesResponse(t)
    20  
    21  	count := 0
    22  
    23  	err := defsecrules.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
    24  		count++
    25  		actual, err := defsecrules.ExtractDefaultRules(page)
    26  		th.AssertNoErr(t, err)
    27  
    28  		expected := []defsecrules.DefaultRule{
    29  			{
    30  				FromPort:   80,
    31  				ID:         ruleID,
    32  				IPProtocol: "TCP",
    33  				IPRange:    secgroups.IPRange{CIDR: "10.10.10.0/24"},
    34  				ToPort:     80,
    35  			},
    36  		}
    37  
    38  		th.CheckDeepEquals(t, expected, actual)
    39  
    40  		return true, nil
    41  	})
    42  
    43  	th.AssertNoErr(t, err)
    44  	th.AssertEquals(t, 1, count)
    45  }
    46  
    47  func TestCreate(t *testing.T) {
    48  	th.SetupHTTP()
    49  	defer th.TeardownHTTP()
    50  
    51  	mockCreateRuleResponse(t)
    52  
    53  	opts := defsecrules.CreateOpts{
    54  		IPProtocol: "TCP",
    55  		FromPort:   80,
    56  		ToPort:     80,
    57  		CIDR:       "10.10.12.0/24",
    58  	}
    59  
    60  	group, err := defsecrules.Create(client.ServiceClient(), opts).Extract()
    61  	th.AssertNoErr(t, err)
    62  
    63  	expected := &defsecrules.DefaultRule{
    64  		ID:         ruleID,
    65  		FromPort:   80,
    66  		ToPort:     80,
    67  		IPProtocol: "TCP",
    68  		IPRange:    secgroups.IPRange{CIDR: "10.10.12.0/24"},
    69  	}
    70  	th.AssertDeepEquals(t, expected, group)
    71  }
    72  
    73  func TestCreateICMPZero(t *testing.T) {
    74  	th.SetupHTTP()
    75  	defer th.TeardownHTTP()
    76  
    77  	mockCreateRuleResponseICMPZero(t)
    78  
    79  	opts := defsecrules.CreateOpts{
    80  		IPProtocol: "ICMP",
    81  		FromPort:   0,
    82  		ToPort:     0,
    83  		CIDR:       "10.10.12.0/24",
    84  	}
    85  
    86  	group, err := defsecrules.Create(client.ServiceClient(), opts).Extract()
    87  	th.AssertNoErr(t, err)
    88  
    89  	expected := &defsecrules.DefaultRule{
    90  		ID:         ruleID,
    91  		FromPort:   0,
    92  		ToPort:     0,
    93  		IPProtocol: "ICMP",
    94  		IPRange:    secgroups.IPRange{CIDR: "10.10.12.0/24"},
    95  	}
    96  	th.AssertDeepEquals(t, expected, group)
    97  }
    98  
    99  func TestGet(t *testing.T) {
   100  	th.SetupHTTP()
   101  	defer th.TeardownHTTP()
   102  
   103  	mockGetRuleResponse(t, ruleID)
   104  
   105  	group, err := defsecrules.Get(client.ServiceClient(), ruleID).Extract()
   106  	th.AssertNoErr(t, err)
   107  
   108  	expected := &defsecrules.DefaultRule{
   109  		ID:         ruleID,
   110  		FromPort:   80,
   111  		ToPort:     80,
   112  		IPProtocol: "TCP",
   113  		IPRange:    secgroups.IPRange{CIDR: "10.10.12.0/24"},
   114  	}
   115  
   116  	th.AssertDeepEquals(t, expected, group)
   117  }
   118  
   119  func TestDelete(t *testing.T) {
   120  	th.SetupHTTP()
   121  	defer th.TeardownHTTP()
   122  
   123  	mockDeleteRuleResponse(t, ruleID)
   124  
   125  	err := defsecrules.Delete(client.ServiceClient(), ruleID).ExtractErr()
   126  	th.AssertNoErr(t, err)
   127  }