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