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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk"
     9  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas/firewalls"
    11  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas/routerinsertion"
    12  	"github.com/huaweicloud/golangsdk/pagination"
    13  	th "github.com/huaweicloud/golangsdk/testhelper"
    14  )
    15  
    16  func TestList(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "GET")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  
    24  		w.Header().Add("Content-Type", "application/json")
    25  		w.WriteHeader(http.StatusOK)
    26  
    27  		fmt.Fprintf(w, `
    28  {
    29     "firewalls":[
    30          {
    31             "status": "ACTIVE",
    32             "name": "fw1",
    33             "admin_state_up": false,
    34             "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    35             "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
    36             "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
    37             "description": "OpenStack firewall 1"
    38          },
    39          {
    40             "status": "PENDING_UPDATE",
    41             "name": "fw2",
    42             "admin_state_up": true,
    43             "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    44             "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e299",
    45             "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f99",
    46             "description": "OpenStack firewall 2"
    47          }
    48     ]
    49  }
    50        `)
    51  	})
    52  
    53  	count := 0
    54  
    55  	firewalls.List(fake.ServiceClient(), firewalls.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    56  		count++
    57  		actual, err := firewalls.ExtractFirewalls(page)
    58  		if err != nil {
    59  			t.Errorf("Failed to extract members: %v", err)
    60  			return false, err
    61  		}
    62  
    63  		expected := []firewalls.Firewall{
    64  			{
    65  				Status:       "ACTIVE",
    66  				Name:         "fw1",
    67  				AdminStateUp: false,
    68  				TenantID:     "b4eedccc6fb74fa8a7ad6b08382b852b",
    69  				PolicyID:     "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
    70  				ID:           "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
    71  				Description:  "OpenStack firewall 1",
    72  			},
    73  			{
    74  				Status:       "PENDING_UPDATE",
    75  				Name:         "fw2",
    76  				AdminStateUp: true,
    77  				TenantID:     "b4eedccc6fb74fa8a7ad6b08382b852b",
    78  				PolicyID:     "34be8c83-4d42-4dca-a74e-b77fffb8e299",
    79  				ID:           "fb5b5315-64f6-4ea3-8e58-981cc37c6f99",
    80  				Description:  "OpenStack firewall 2",
    81  			},
    82  		}
    83  
    84  		th.CheckDeepEquals(t, expected, actual)
    85  
    86  		return true, nil
    87  	})
    88  
    89  	if count != 1 {
    90  		t.Errorf("Expected 1 page, got %d", count)
    91  	}
    92  }
    93  
    94  func TestListWithExtensions(t *testing.T) {
    95  	th.SetupHTTP()
    96  	defer th.TeardownHTTP()
    97  
    98  	th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
    99  		th.TestMethod(t, r, "GET")
   100  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   101  
   102  		w.Header().Add("Content-Type", "application/json")
   103  		w.WriteHeader(http.StatusOK)
   104  
   105  		fmt.Fprintf(w, `
   106  {
   107     "firewalls":[
   108          {
   109             "status": "ACTIVE",
   110             "name": "fw1",
   111             "admin_state_up": false,
   112             "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   113             "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
   114             "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
   115             "description": "OpenStack firewall 1",
   116             "router_ids": ["abcd1234"]
   117          },
   118          {
   119             "status": "PENDING_UPDATE",
   120             "name": "fw2",
   121             "admin_state_up": true,
   122             "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   123             "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e299",
   124             "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f99",
   125             "description": "OpenStack firewall 2"
   126          }
   127     ]
   128  }
   129        `)
   130  	})
   131  
   132  	type FirewallsWithExt struct {
   133  		firewalls.Firewall
   134  		routerinsertion.FirewallExt
   135  	}
   136  
   137  	allPages, err := firewalls.List(fake.ServiceClient(), nil).AllPages()
   138  	th.AssertNoErr(t, err)
   139  
   140  	var actual []FirewallsWithExt
   141  	err = firewalls.ExtractFirewallsInto(allPages, &actual)
   142  	th.AssertNoErr(t, err)
   143  	th.AssertEquals(t, 2, len(actual))
   144  	th.AssertEquals(t, "fb5b5315-64f6-4ea3-8e58-981cc37c6f61", actual[0].ID)
   145  	th.AssertEquals(t, "abcd1234", actual[0].RouterIDs[0])
   146  }
   147  
   148  func TestCreate(t *testing.T) {
   149  	th.SetupHTTP()
   150  	defer th.TeardownHTTP()
   151  
   152  	th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
   153  		th.TestMethod(t, r, "POST")
   154  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   155  		th.TestHeader(t, r, "Content-Type", "application/json")
   156  		th.TestHeader(t, r, "Accept", "application/json")
   157  		th.TestJSONRequest(t, r, `
   158  {
   159      "firewall":{
   160          "name": "fw",
   161          "description": "OpenStack firewall",
   162          "admin_state_up": true,
   163          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   164          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b"
   165      }
   166  }
   167        `)
   168  
   169  		w.Header().Add("Content-Type", "application/json")
   170  		w.WriteHeader(http.StatusCreated)
   171  
   172  		fmt.Fprintf(w, `
   173  {
   174      "firewall":{
   175          "status": "PENDING_CREATE",
   176          "name": "fw",
   177          "description": "OpenStack firewall",
   178          "admin_state_up": true,
   179          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   180          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
   181      }
   182  }
   183      `)
   184  	})
   185  
   186  	options := firewalls.CreateOpts{
   187  		TenantID:     "b4eedccc6fb74fa8a7ad6b08382b852b",
   188  		Name:         "fw",
   189  		Description:  "OpenStack firewall",
   190  		AdminStateUp: golangsdk.Enabled,
   191  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   192  	}
   193  	_, err := firewalls.Create(fake.ServiceClient(), options).Extract()
   194  	th.AssertNoErr(t, err)
   195  }
   196  
   197  func TestGet(t *testing.T) {
   198  	th.SetupHTTP()
   199  	defer th.TeardownHTTP()
   200  
   201  	th.Mux.HandleFunc("/v2.0/fw/firewalls/fb5b5315-64f6-4ea3-8e58-981cc37c6f61", func(w http.ResponseWriter, r *http.Request) {
   202  		th.TestMethod(t, r, "GET")
   203  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   204  
   205  		w.Header().Add("Content-Type", "application/json")
   206  		w.WriteHeader(http.StatusOK)
   207  
   208  		fmt.Fprintf(w, `
   209  {
   210      "firewall": {
   211          "status": "ACTIVE",
   212          "name": "fw",
   213          "admin_state_up": true,
   214          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   215          "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
   216          "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
   217          "description": "OpenStack firewall"
   218      }
   219  }
   220          `)
   221  	})
   222  
   223  	fw, err := firewalls.Get(fake.ServiceClient(), "fb5b5315-64f6-4ea3-8e58-981cc37c6f61").Extract()
   224  	th.AssertNoErr(t, err)
   225  
   226  	th.AssertEquals(t, "ACTIVE", fw.Status)
   227  	th.AssertEquals(t, "fw", fw.Name)
   228  	th.AssertEquals(t, "OpenStack firewall", fw.Description)
   229  	th.AssertEquals(t, true, fw.AdminStateUp)
   230  	th.AssertEquals(t, "34be8c83-4d42-4dca-a74e-b77fffb8e28a", fw.PolicyID)
   231  	th.AssertEquals(t, "fb5b5315-64f6-4ea3-8e58-981cc37c6f61", fw.ID)
   232  	th.AssertEquals(t, "b4eedccc6fb74fa8a7ad6b08382b852b", fw.TenantID)
   233  }
   234  
   235  func TestGetWithExtensions(t *testing.T) {
   236  	th.SetupHTTP()
   237  	defer th.TeardownHTTP()
   238  
   239  	th.Mux.HandleFunc("/v2.0/fw/firewalls/fb5b5315-64f6-4ea3-8e58-981cc37c6f61", func(w http.ResponseWriter, r *http.Request) {
   240  		th.TestMethod(t, r, "GET")
   241  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   242  
   243  		w.Header().Add("Content-Type", "application/json")
   244  		w.WriteHeader(http.StatusOK)
   245  
   246  		fmt.Fprintf(w, `
   247  {
   248      "firewall": {
   249          "status": "ACTIVE",
   250          "name": "fw",
   251          "admin_state_up": true,
   252          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   253          "firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
   254          "id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
   255          "description": "OpenStack firewall",
   256          "router_ids": ["abcd1234"]
   257      }
   258  }
   259          `)
   260  	})
   261  
   262  	var fw struct {
   263  		firewalls.Firewall
   264  		routerinsertion.FirewallExt
   265  	}
   266  
   267  	err := firewalls.Get(fake.ServiceClient(), "fb5b5315-64f6-4ea3-8e58-981cc37c6f61").ExtractInto(&fw)
   268  	th.AssertNoErr(t, err)
   269  
   270  	th.AssertEquals(t, "ACTIVE", fw.Status)
   271  	th.AssertEquals(t, "fw", fw.Name)
   272  	th.AssertEquals(t, "OpenStack firewall", fw.Description)
   273  	th.AssertEquals(t, true, fw.AdminStateUp)
   274  	th.AssertEquals(t, "34be8c83-4d42-4dca-a74e-b77fffb8e28a", fw.PolicyID)
   275  	th.AssertEquals(t, "fb5b5315-64f6-4ea3-8e58-981cc37c6f61", fw.ID)
   276  	th.AssertEquals(t, "b4eedccc6fb74fa8a7ad6b08382b852b", fw.TenantID)
   277  	th.AssertEquals(t, "abcd1234", fw.RouterIDs[0])
   278  }
   279  
   280  func TestUpdate(t *testing.T) {
   281  	th.SetupHTTP()
   282  	defer th.TeardownHTTP()
   283  
   284  	th.Mux.HandleFunc("/v2.0/fw/firewalls/ea5b5315-64f6-4ea3-8e58-981cc37c6576", func(w http.ResponseWriter, r *http.Request) {
   285  		th.TestMethod(t, r, "PUT")
   286  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   287  		th.TestHeader(t, r, "Content-Type", "application/json")
   288  		th.TestHeader(t, r, "Accept", "application/json")
   289  		th.TestJSONRequest(t, r, `
   290  {
   291      "firewall":{
   292          "name": "fw",
   293          "description": "updated fw",
   294          "admin_state_up":false,
   295          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
   296      }
   297  }
   298        `)
   299  
   300  		w.Header().Add("Content-Type", "application/json")
   301  		w.WriteHeader(http.StatusOK)
   302  
   303  		fmt.Fprintf(w, `
   304  {
   305      "firewall": {
   306          "status": "ACTIVE",
   307          "name": "fw",
   308          "admin_state_up": false,
   309          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   310          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   311          "id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
   312          "description": "OpenStack firewall"
   313      }
   314  }
   315      `)
   316  	})
   317  
   318  	options := firewalls.UpdateOpts{
   319  		Name:         "fw",
   320  		Description:  "updated fw",
   321  		AdminStateUp: golangsdk.Disabled,
   322  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   323  	}
   324  
   325  	_, err := firewalls.Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", options).Extract()
   326  	th.AssertNoErr(t, err)
   327  }
   328  
   329  func TestDelete(t *testing.T) {
   330  	th.SetupHTTP()
   331  	defer th.TeardownHTTP()
   332  
   333  	th.Mux.HandleFunc("/v2.0/fw/firewalls/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   334  		th.TestMethod(t, r, "DELETE")
   335  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   336  		w.WriteHeader(http.StatusNoContent)
   337  	})
   338  
   339  	res := firewalls.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
   340  	th.AssertNoErr(t, res.Err)
   341  }