github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/security/rules/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
     9  	fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/common"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/security/rules"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "GET")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  
    23  		w.Header().Add("Content-Type", "application/json")
    24  		w.WriteHeader(http.StatusOK)
    25  
    26  		_, _ = fmt.Fprint(w, `
    27  {
    28      "security_group_rules": [
    29          {
    30              "direction": "egress",
    31              "ethertype": "IPv6",
    32              "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
    33              "port_range_max": 0,
    34              "port_range_min": 0,
    35              "protocol": null,
    36              "remote_group_id": null,
    37              "remote_ip_prefix": null,
    38              "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
    39              "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
    40          },
    41          {
    42              "direction": "egress",
    43              "ethertype": "IPv4",
    44              "id": "93aa42e5-80db-4581-9391-3a608bd0e448",
    45              "port_range_max": null,
    46              "port_range_min": null,
    47              "protocol": null,
    48              "remote_group_id": null,
    49              "remote_ip_prefix": null,
    50              "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
    51              "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
    52          }
    53      ]
    54  }
    55        `)
    56  	})
    57  
    58  	count := 0
    59  
    60  	_ = rules.List(fake.ServiceClient(), rules.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    61  		count++
    62  		actual, err := rules.ExtractRules(page)
    63  		if err != nil {
    64  			t.Errorf("Failed to extract secrules: %v", err)
    65  			return false, err
    66  		}
    67  
    68  		expected := []rules.SecGroupRule{
    69  			{
    70  				Direction:      "egress",
    71  				EtherType:      "IPv6",
    72  				ID:             "3c0e45ff-adaf-4124-b083-bf390e5482ff",
    73  				PortRangeMax:   pointerto.Int(0),
    74  				PortRangeMin:   pointerto.Int(0),
    75  				Protocol:       "",
    76  				RemoteGroupID:  "",
    77  				RemoteIPPrefix: "",
    78  				SecGroupID:     "85cc3048-abc3-43cc-89b3-377341426ac5",
    79  				TenantID:       "e4f50856753b4dc6afee5fa6b9b6c550",
    80  			},
    81  			{
    82  				Direction:      "egress",
    83  				EtherType:      "IPv4",
    84  				ID:             "93aa42e5-80db-4581-9391-3a608bd0e448",
    85  				PortRangeMax:   (*int)(nil),
    86  				PortRangeMin:   (*int)(nil),
    87  				Protocol:       "",
    88  				RemoteGroupID:  "",
    89  				RemoteIPPrefix: "",
    90  				SecGroupID:     "85cc3048-abc3-43cc-89b3-377341426ac5",
    91  				TenantID:       "e4f50856753b4dc6afee5fa6b9b6c550",
    92  			},
    93  		}
    94  
    95  		th.CheckDeepEquals(t, expected, actual)
    96  
    97  		return true, nil
    98  	})
    99  
   100  	if count != 1 {
   101  		t.Errorf("Expected 1 page, got %d", count)
   102  	}
   103  }
   104  
   105  func TestCreate(t *testing.T) {
   106  	th.SetupHTTP()
   107  	defer th.TeardownHTTP()
   108  
   109  	th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) {
   110  		th.TestMethod(t, r, "POST")
   111  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   112  		th.TestHeader(t, r, "Content-Type", "application/json")
   113  		th.TestHeader(t, r, "Accept", "application/json")
   114  		th.TestJSONRequest(t, r, `
   115  {
   116      "security_group_rule": {
   117          "direction": "ingress",
   118          "port_range_min": 80,
   119          "ethertype": "IPv4",
   120          "port_range_max": 80,
   121          "protocol": "tcp",
   122          "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
   123          "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a"
   124      }
   125  }
   126        `)
   127  
   128  		w.Header().Add("Content-Type", "application/json")
   129  		w.WriteHeader(http.StatusCreated)
   130  
   131  		_, _ = fmt.Fprint(w, `
   132  {
   133      "security_group_rule": {
   134          "direction": "ingress",
   135          "ethertype": "IPv4",
   136          "id": "2bc0accf-312e-429a-956e-e4407625eb62",
   137          "port_range_max": 80,
   138          "port_range_min": 80,
   139          "protocol": "tcp",
   140          "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
   141          "remote_ip_prefix": null,
   142          "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a",
   143          "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
   144      }
   145  }
   146      `)
   147  	})
   148  
   149  	portRangeMin := 80
   150  	portRangeMax := 80
   151  	opts := rules.CreateOpts{
   152  		Direction:     "ingress",
   153  		PortRangeMin:  &portRangeMin,
   154  		EtherType:     rules.EtherType4,
   155  		PortRangeMax:  &portRangeMax,
   156  		Protocol:      "tcp",
   157  		RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
   158  		SecGroupID:    "a7734e61-b545-452d-a3cd-0189cbd9747a",
   159  	}
   160  	_, err := rules.Create(fake.ServiceClient(), opts).Extract()
   161  	th.AssertNoErr(t, err)
   162  }
   163  
   164  func TestRequiredCreateOpts(t *testing.T) {
   165  	res := rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress})
   166  	if res.Err == nil {
   167  		t.Fatalf("Expected error, got none")
   168  	}
   169  	res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4})
   170  	if res.Err == nil {
   171  		t.Fatalf("Expected error, got none")
   172  	}
   173  	res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4})
   174  	if res.Err == nil {
   175  		t.Fatalf("Expected error, got none")
   176  	}
   177  	res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4, SecGroupID: "something", Protocol: "foo"})
   178  	if res.Err == nil {
   179  		t.Fatalf("Expected error, got none")
   180  	}
   181  }
   182  
   183  func TestGet(t *testing.T) {
   184  	th.SetupHTTP()
   185  	defer th.TeardownHTTP()
   186  
   187  	th.Mux.HandleFunc("/v2.0/security-group-rules/3c0e45ff-adaf-4124-b083-bf390e5482ff", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "GET")
   189  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   190  
   191  		w.Header().Add("Content-Type", "application/json")
   192  		w.WriteHeader(http.StatusOK)
   193  
   194  		_, _ = fmt.Fprint(w, `
   195  {
   196      "security_group_rule": {
   197          "direction": "egress",
   198          "ethertype": "IPv6",
   199          "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
   200          "port_range_max": null,
   201          "port_range_min": null,
   202          "protocol": null,
   203          "remote_group_id": null,
   204          "remote_ip_prefix": null,
   205          "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
   206          "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
   207      }
   208  }
   209        `)
   210  	})
   211  
   212  	sr, err := rules.Get(fake.ServiceClient(), "3c0e45ff-adaf-4124-b083-bf390e5482ff").Extract()
   213  	th.AssertNoErr(t, err)
   214  
   215  	th.AssertEquals(t, "egress", sr.Direction)
   216  	th.AssertEquals(t, "IPv6", sr.EtherType)
   217  	th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID)
   218  	th.AssertEquals(t, (*int)(nil), sr.PortRangeMax)
   219  	th.AssertEquals(t, (*int)(nil), sr.PortRangeMin)
   220  	th.AssertEquals(t, "", sr.Protocol)
   221  	th.AssertEquals(t, "", sr.RemoteGroupID)
   222  	th.AssertEquals(t, "", sr.RemoteIPPrefix)
   223  	th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID)
   224  	th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID)
   225  }
   226  
   227  func TestDelete(t *testing.T) {
   228  	th.SetupHTTP()
   229  	defer th.TeardownHTTP()
   230  
   231  	th.Mux.HandleFunc("/v2.0/security-group-rules/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   232  		th.TestMethod(t, r, "DELETE")
   233  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   234  		w.WriteHeader(http.StatusNoContent)
   235  	})
   236  
   237  	res := rules.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
   238  	th.AssertNoErr(t, res.Err)
   239  }