github.com/leeclow-ops/gophercloud@v1.2.1/openstack/networking/v2/extensions/fwaas_v2/rules/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/leeclow-ops/gophercloud"
     9  	fake "github.com/leeclow-ops/gophercloud/openstack/networking/v2/common"
    10  	"github.com/leeclow-ops/gophercloud/openstack/networking/v2/extensions/fwaas_v2/rules"
    11  	"github.com/leeclow-ops/gophercloud/pagination"
    12  	th "github.com/leeclow-ops/gophercloud/testhelper"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_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.Fprintf(w, `
    27  {
    28      "firewall_rules": [
    29          {
    30              "protocol": "tcp",
    31              "description": "ssh rule",
    32              "source_port": null,
    33              "source_ip_address": null,
    34              "destination_ip_address": "192.168.1.0/24",
    35              "firewall_policy_id": ["e2a5fb51-698c-4898-87e8-f1eee6b50919"],
    36              "destination_port": "22",
    37              "id": "f03bd950-6c56-4f5e-a307-45967078f507",
    38              "name": "ssh_form_any",
    39              "tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
    40              "enabled": true,
    41              "action": "allow",
    42              "ip_version": 4,
    43              "shared": false
    44          },
    45          {
    46              "protocol": "udp",
    47              "description": "udp rule",
    48              "source_port": null,
    49              "source_ip_address": null,
    50              "destination_ip_address": null,
    51              "firewall_policy_id": ["98d7fb51-698c-4123-87e8-f1eee6b5ab7e"],
    52              "destination_port": null,
    53              "id": "ab7bd950-6c56-4f5e-a307-45967078f890",
    54              "name": "deny_all_udp",
    55              "tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
    56              "enabled": true,
    57              "action": "deny",
    58              "ip_version": 4,
    59              "shared": false
    60          }
    61      ]
    62  }
    63          `)
    64  	})
    65  
    66  	count := 0
    67  
    68  	rules.List(fake.ServiceClient(), rules.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    69  		count++
    70  		actual, err := rules.ExtractRules(page)
    71  		if err != nil {
    72  			t.Errorf("Failed to extract members: %v", err)
    73  			return false, err
    74  		}
    75  
    76  		expected := []rules.Rule{
    77  			{
    78  				Protocol:             string(rules.ProtocolTCP),
    79  				Description:          "ssh rule",
    80  				SourcePort:           "",
    81  				SourceIPAddress:      "",
    82  				DestinationIPAddress: "192.168.1.0/24",
    83  				FirewallPolicyID:     []string{"e2a5fb51-698c-4898-87e8-f1eee6b50919"},
    84  				DestinationPort:      "22",
    85  				ID:                   "f03bd950-6c56-4f5e-a307-45967078f507",
    86  				Name:                 "ssh_form_any",
    87  				TenantID:             "80cf934d6ffb4ef5b244f1c512ad1e61",
    88  				Enabled:              true,
    89  				Action:               string(rules.ActionAllow),
    90  				IPVersion:            4,
    91  				Shared:               false,
    92  			},
    93  			{
    94  				Protocol:             "udp",
    95  				Description:          "udp rule",
    96  				SourcePort:           "",
    97  				SourceIPAddress:      "",
    98  				DestinationIPAddress: "",
    99  				FirewallPolicyID:     []string{"98d7fb51-698c-4123-87e8-f1eee6b5ab7e"},
   100  				DestinationPort:      "",
   101  				ID:                   "ab7bd950-6c56-4f5e-a307-45967078f890",
   102  				Name:                 "deny_all_udp",
   103  				TenantID:             "80cf934d6ffb4ef5b244f1c512ad1e61",
   104  				Enabled:              true,
   105  				Action:               "deny",
   106  				IPVersion:            4,
   107  				Shared:               false,
   108  			},
   109  		}
   110  
   111  		th.CheckDeepEquals(t, expected, actual)
   112  
   113  		return true, nil
   114  	})
   115  
   116  	if count != 1 {
   117  		t.Errorf("Expected 1 page, got %d", count)
   118  	}
   119  }
   120  func TestCreate(t *testing.T) {
   121  	th.SetupHTTP()
   122  	defer th.TeardownHTTP()
   123  
   124  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_rules", func(w http.ResponseWriter, r *http.Request) {
   125  		th.TestMethod(t, r, "POST")
   126  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   127  		th.TestHeader(t, r, "Content-Type", "application/json")
   128  		th.TestHeader(t, r, "Accept", "application/json")
   129  		th.TestJSONRequest(t, r, `
   130  {
   131  	"firewall_rule": {
   132  		"protocol": "tcp",
   133  		"description": "ssh rule",
   134  		"destination_ip_address": "192.168.1.0/24",
   135  		"destination_port": "22",
   136  		"name": "ssh_form_any",
   137  		"action": "allow",
   138  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61"
   139  	}
   140  }
   141        `)
   142  
   143  		w.Header().Add("Content-Type", "application/json")
   144  		w.WriteHeader(http.StatusCreated)
   145  
   146  		fmt.Fprintf(w, `
   147  {
   148  	"firewall_rule":{
   149  		"protocol": "tcp",
   150  		"description": "ssh rule",
   151  		"source_port": null,
   152  		"source_ip_address": null,
   153  		"destination_ip_address": "192.168.1.0/24",
   154  		"firewall_policy_id": ["e2a5fb51-698c-4898-87e8-f1eee6b50919"],
   155  		"position": 2,
   156  		"destination_port": "22",
   157  		"id": "f03bd950-6c56-4f5e-a307-45967078f507",
   158  		"name": "ssh_form_any",
   159  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
   160  		"enabled": true,
   161  		"action": "allow",
   162  		"ip_version": 4,
   163  		"shared": false
   164  	}
   165  }
   166          `)
   167  	})
   168  
   169  	options := rules.CreateOpts{
   170  		TenantID:             "80cf934d6ffb4ef5b244f1c512ad1e61",
   171  		Protocol:             rules.ProtocolTCP,
   172  		Description:          "ssh rule",
   173  		DestinationIPAddress: "192.168.1.0/24",
   174  		DestinationPort:      "22",
   175  		Name:                 "ssh_form_any",
   176  		Action:               "allow",
   177  	}
   178  
   179  	_, err := rules.Create(fake.ServiceClient(), options).Extract()
   180  	th.AssertNoErr(t, err)
   181  }
   182  
   183  func TestCreateAnyProtocol(t *testing.T) {
   184  	th.SetupHTTP()
   185  	defer th.TeardownHTTP()
   186  
   187  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_rules", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "POST")
   189  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   190  		th.TestHeader(t, r, "Content-Type", "application/json")
   191  		th.TestHeader(t, r, "Accept", "application/json")
   192  		th.TestJSONRequest(t, r, `
   193  {
   194  	"firewall_rule": {
   195  		"protocol": null,
   196  		"description": "any to 192.168.1.0/24",
   197  		"destination_ip_address": "192.168.1.0/24",
   198  		"name": "any_to_192.168.1.0/24",
   199  		"action": "allow",
   200  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61"
   201  	}
   202  }
   203        `)
   204  
   205  		w.Header().Add("Content-Type", "application/json")
   206  		w.WriteHeader(http.StatusCreated)
   207  
   208  		fmt.Fprintf(w, `
   209  {
   210  	"firewall_rule":{
   211  		"protocol": null,
   212  		"description": "any to 192.168.1.0/24",
   213  		"source_port": null,
   214  		"source_ip_address": null,
   215  		"destination_ip_address": "192.168.1.0/24",
   216  		"firewall_policy_id": ["e2a5fb51-698c-4898-87e8-f1eee6b50919"],
   217  		"position": 2,
   218  		"destination_port": null,
   219  		"id": "f03bd950-6c56-4f5e-a307-45967078f507",
   220  		"name": "any_to_192.168.1.0/24",
   221  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
   222  		"enabled": true,
   223  		"action": "allow",
   224  		"ip_version": 4,
   225  		"shared": false
   226  	}
   227  }
   228          `)
   229  	})
   230  
   231  	options := rules.CreateOpts{
   232  		TenantID:             "80cf934d6ffb4ef5b244f1c512ad1e61",
   233  		Protocol:             rules.ProtocolAny,
   234  		Description:          "any to 192.168.1.0/24",
   235  		DestinationIPAddress: "192.168.1.0/24",
   236  		Name:                 "any_to_192.168.1.0/24",
   237  		Action:               "allow",
   238  	}
   239  
   240  	_, err := rules.Create(fake.ServiceClient(), options).Extract()
   241  	th.AssertNoErr(t, err)
   242  }
   243  
   244  func TestGet(t *testing.T) {
   245  	th.SetupHTTP()
   246  	defer th.TeardownHTTP()
   247  
   248  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_rules/f03bd950-6c56-4f5e-a307-45967078f507", func(w http.ResponseWriter, r *http.Request) {
   249  		th.TestMethod(t, r, "GET")
   250  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   251  
   252  		w.Header().Add("Content-Type", "application/json")
   253  		w.WriteHeader(http.StatusOK)
   254  
   255  		fmt.Fprintf(w, `
   256  {
   257  	"firewall_rule":{
   258  		"protocol": "tcp",
   259  		"description": "ssh rule",
   260  		"source_port": null,
   261  		"source_ip_address": null,
   262  		"destination_ip_address": "192.168.1.0/24",
   263  		"firewall_policy_id": ["e2a5fb51-698c-4898-87e8-f1eee6b50919"],
   264  		"position": 2,
   265  		"destination_port": "22",
   266  		"id": "f03bd950-6c56-4f5e-a307-45967078f507",
   267  		"name": "ssh_form_any",
   268  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
   269  		"enabled": true,
   270  		"action": "allow",
   271  		"ip_version": 4,
   272  		"shared": false
   273  	}
   274  }
   275          `)
   276  	})
   277  
   278  	rule, err := rules.Get(fake.ServiceClient(), "f03bd950-6c56-4f5e-a307-45967078f507").Extract()
   279  	th.AssertNoErr(t, err)
   280  
   281  	th.AssertEquals(t, "tcp", rule.Protocol)
   282  	th.AssertEquals(t, "ssh rule", rule.Description)
   283  	th.AssertEquals(t, "192.168.1.0/24", rule.DestinationIPAddress)
   284  	th.AssertEquals(t, 1, len(rule.FirewallPolicyID))
   285  	th.AssertEquals(t, "e2a5fb51-698c-4898-87e8-f1eee6b50919", rule.FirewallPolicyID[0])
   286  	th.AssertEquals(t, "22", rule.DestinationPort)
   287  	th.AssertEquals(t, "f03bd950-6c56-4f5e-a307-45967078f507", rule.ID)
   288  	th.AssertEquals(t, "ssh_form_any", rule.Name)
   289  	th.AssertEquals(t, "80cf934d6ffb4ef5b244f1c512ad1e61", rule.TenantID)
   290  	th.AssertEquals(t, true, rule.Enabled)
   291  	th.AssertEquals(t, "allow", rule.Action)
   292  	th.AssertEquals(t, 4, rule.IPVersion)
   293  	th.AssertEquals(t, false, rule.Shared)
   294  }
   295  
   296  func TestUpdate(t *testing.T) {
   297  	th.SetupHTTP()
   298  	defer th.TeardownHTTP()
   299  
   300  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_rules/f03bd950-6c56-4f5e-a307-45967078f507", func(w http.ResponseWriter, r *http.Request) {
   301  		th.TestMethod(t, r, "PUT")
   302  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   303  		th.TestHeader(t, r, "Content-Type", "application/json")
   304  		th.TestHeader(t, r, "Accept", "application/json")
   305  		th.TestJSONRequest(t, r, `
   306  {
   307  	"firewall_rule":{
   308  		"protocol": "tcp",
   309  		"description": "ssh rule",
   310  		"destination_ip_address": "192.168.1.0/24",
   311  		"destination_port": "22",
   312  		"name": "ssh_form_any",
   313  		"action": "allow",
   314  		"enabled": false
   315  	}
   316  }
   317  	`)
   318  
   319  		w.Header().Add("Content-Type", "application/json")
   320  		w.WriteHeader(http.StatusOK)
   321  
   322  		fmt.Fprintf(w, `
   323  {
   324  	"firewall_rule":{
   325  		"protocol": "tcp",
   326  		"description": "ssh rule",
   327  		"destination_ip_address": "192.168.1.0/24",
   328  		"firewall_policy_id": ["e2a5fb51-698c-4898-87e8-f1eee6b50919"],
   329  		"position": 2,
   330  		"destination_port": "22",
   331  		"id": "f03bd950-6c56-4f5e-a307-45967078f507",
   332  		"name": "ssh_form_any",
   333  		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
   334  		"enabled": false,
   335  		"action": "allow",
   336  		"ip_version": 4,
   337  		"shared": false
   338  	}
   339  }
   340  		`)
   341  	})
   342  
   343  	newProtocol := rules.ProtocolTCP
   344  	newDescription := "ssh rule"
   345  	newDestinationIP := "192.168.1.0/24"
   346  	newDestintionPort := "22"
   347  	newName := "ssh_form_any"
   348  	newAction := rules.ActionAllow
   349  
   350  	options := rules.UpdateOpts{
   351  		Protocol:             &newProtocol,
   352  		Description:          &newDescription,
   353  		DestinationIPAddress: &newDestinationIP,
   354  		DestinationPort:      &newDestintionPort,
   355  		Name:                 &newName,
   356  		Action:               &newAction,
   357  		Enabled:              gophercloud.Disabled,
   358  	}
   359  
   360  	_, err := rules.Update(fake.ServiceClient(), "f03bd950-6c56-4f5e-a307-45967078f507", options).Extract()
   361  	th.AssertNoErr(t, err)
   362  }
   363  
   364  func TestDelete(t *testing.T) {
   365  	th.SetupHTTP()
   366  	defer th.TeardownHTTP()
   367  
   368  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_rules/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   369  		th.TestMethod(t, r, "DELETE")
   370  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   371  		w.WriteHeader(http.StatusNoContent)
   372  	})
   373  
   374  	res := rules.Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304")
   375  	th.AssertNoErr(t, res.Err)
   376  }