github.com/leeclow-ops/gophercloud@v1.2.1/openstack/networking/v2/extensions/fwaas_v2/policies/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/policies"
    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_policies", 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_policies": [
    29          {
    30              "name": "policy1",
    31              "firewall_rules": [
    32                  "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
    33                  "c9e77ca0-1bc8-497d-904d-948107873dc6"
    34              ],
    35              "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    36              "audited": true,
    37  			"shared": false,
    38              "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    39              "description": "Firewall policy 1"
    40          },
    41          {
    42              "name": "policy2",
    43              "firewall_rules": [
    44                  "03d2a6ad-633f-431a-8463-4370d06a22c8"
    45              ],
    46              "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    47              "audited": false,
    48  			"shared": true,
    49              "id": "c854fab5-bdaf-4a86-9359-78de93e5df01",
    50              "description": "Firewall policy 2"
    51          }
    52      ]
    53  }
    54          `)
    55  	})
    56  
    57  	count := 0
    58  
    59  	policies.List(fake.ServiceClient(), policies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    60  		count++
    61  		actual, err := policies.ExtractPolicies(page)
    62  		if err != nil {
    63  			t.Errorf("Failed to extract members: %v", err)
    64  			return false, err
    65  		}
    66  
    67  		expected := []policies.Policy{
    68  			{
    69  				Name: "policy1",
    70  				Rules: []string{
    71  					"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
    72  					"c9e77ca0-1bc8-497d-904d-948107873dc6",
    73  				},
    74  				TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
    75  				Audited:     true,
    76  				Shared:      false,
    77  				ID:          "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    78  				Description: "Firewall policy 1",
    79  			},
    80  			{
    81  				Name: "policy2",
    82  				Rules: []string{
    83  					"03d2a6ad-633f-431a-8463-4370d06a22c8",
    84  				},
    85  				TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
    86  				Audited:     false,
    87  				Shared:      true,
    88  				ID:          "c854fab5-bdaf-4a86-9359-78de93e5df01",
    89  				Description: "Firewall policy 2",
    90  			},
    91  		}
    92  
    93  		th.CheckDeepEquals(t, expected, actual)
    94  
    95  		return true, nil
    96  	})
    97  
    98  	if count != 1 {
    99  		t.Errorf("Expected 1 page, got %d", count)
   100  	}
   101  }
   102  
   103  func TestCreate(t *testing.T) {
   104  	th.SetupHTTP()
   105  	defer th.TeardownHTTP()
   106  
   107  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies", func(w http.ResponseWriter, r *http.Request) {
   108  		th.TestMethod(t, r, "POST")
   109  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   110  		th.TestHeader(t, r, "Content-Type", "application/json")
   111  		th.TestHeader(t, r, "Accept", "application/json")
   112  		th.TestJSONRequest(t, r, `
   113  {
   114      "firewall_policy":{
   115          "name": "policy",
   116          "firewall_rules": [
   117              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   118              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   119          ],
   120          "description": "Firewall policy",
   121  		"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   122  		"audited": true,
   123  		"shared": false
   124      }
   125  }
   126        `)
   127  
   128  		w.Header().Add("Content-Type", "application/json")
   129  		w.WriteHeader(http.StatusCreated)
   130  
   131  		fmt.Fprintf(w, `
   132  {
   133      "firewall_policy":{
   134          "name": "policy",
   135          "firewall_rules": [
   136              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   137              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   138          ],
   139          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   140          "audited": false,
   141          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   142          "description": "Firewall policy"
   143      }
   144  }
   145          `)
   146  	})
   147  
   148  	options := policies.CreateOpts{
   149  		TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
   150  		Name:        "policy",
   151  		Description: "Firewall policy",
   152  		Shared:      gophercloud.Disabled,
   153  		Audited:     gophercloud.Enabled,
   154  		FirewallRules: []string{
   155  			"98a58c87-76be-ae7c-a74e-b77fffb88d95",
   156  			"11a58c87-76be-ae7c-a74e-b77fffb88a32",
   157  		},
   158  	}
   159  
   160  	_, err := policies.Create(fake.ServiceClient(), options).Extract()
   161  	th.AssertNoErr(t, err)
   162  }
   163  
   164  func TestInsertRule(t *testing.T) {
   165  	th.SetupHTTP()
   166  	defer th.TeardownHTTP()
   167  
   168  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/e3c78ab6-e827-4297-8d68-739063865a8b/insert_rule", func(w http.ResponseWriter, r *http.Request) {
   169  		th.TestMethod(t, r, "PUT")
   170  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   171  		th.TestHeader(t, r, "Content-Type", "application/json")
   172  		th.TestHeader(t, r, "Accept", "application/json")
   173  		th.TestJSONRequest(t, r, `
   174  {
   175      "firewall_rule_id": "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   176      "insert_before": "3062ed90-1fb0-4c25-af3d-318dff2143ae"
   177  }
   178          `)
   179  
   180  		w.Header().Add("Content-Type", "application/json")
   181  		w.WriteHeader(http.StatusOK)
   182  
   183  		fmt.Fprintf(w, `
   184  {
   185      "audited": false,
   186      "description": "TESTACC-DESC-8P12aLfW",
   187      "firewall_rules": [
   188          "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   189          "3062ed90-1fb0-4c25-af3d-318dff2143ae"
   190      ],
   191      "id": "e3c78ab6-e827-4297-8d68-739063865a8b",
   192      "name": "TESTACC-2LnMayeG",
   193      "project_id": "9f98fc0e5f944cd1b51798b668dc8778",
   194      "shared": false,
   195      "tenant_id": "9f98fc0e5f944cd1b51798b668dc8778"
   196  }
   197      `)
   198  	})
   199  
   200  	options := policies.InsertRuleOpts{
   201  		ID:           "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   202  		InsertBefore: "3062ed90-1fb0-4c25-af3d-318dff2143ae",
   203  	}
   204  
   205  	policy, err := policies.InsertRule(fake.ServiceClient(), "e3c78ab6-e827-4297-8d68-739063865a8b", options).Extract()
   206  	th.AssertNoErr(t, err)
   207  	th.AssertEquals(t, "TESTACC-2LnMayeG", policy.Name)
   208  	th.AssertEquals(t, 2, len(policy.Rules))
   209  	th.AssertEquals(t, "7d305689-6cb1-4e75-9f4d-517b9ba792b5", policy.Rules[0])
   210  	th.AssertEquals(t, "3062ed90-1fb0-4c25-af3d-318dff2143ae", policy.Rules[1])
   211  	th.AssertEquals(t, "e3c78ab6-e827-4297-8d68-739063865a8b", policy.ID)
   212  	th.AssertEquals(t, "TESTACC-DESC-8P12aLfW", policy.Description)
   213  	th.AssertEquals(t, "9f98fc0e5f944cd1b51798b668dc8778", policy.TenantID)
   214  
   215  }
   216  
   217  func TestInsertRuleWithInvalidParameters(t *testing.T) {
   218  	th.SetupHTTP()
   219  	defer th.TeardownHTTP()
   220  
   221  	//invalid opts, its not allowed to specify InsertBefore and InsertAfter together
   222  	options := policies.InsertRuleOpts{
   223  		ID:           "unknown",
   224  		InsertBefore: "1",
   225  		InsertAfter:  "2",
   226  	}
   227  
   228  	_, err := policies.InsertRule(fake.ServiceClient(), "0", options).Extract()
   229  
   230  	// expect to fail with an gophercloud error
   231  	th.AssertErr(t, err)
   232  	th.AssertEquals(t, "Exactly one of InsertBefore and InsertAfter must be provided", err.Error())
   233  }
   234  
   235  func TestGet(t *testing.T) {
   236  	th.SetupHTTP()
   237  	defer th.TeardownHTTP()
   238  
   239  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 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_policy":{
   249          "name": "www",
   250          "firewall_rules": [
   251              "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
   252              "c9e77ca0-1bc8-497d-904d-948107873dc6",
   253              "03d2a6ad-633f-431a-8463-4370d06a22c8"
   254          ],
   255          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   256          "audited": false,
   257          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   258          "description": "Firewall policy web"
   259      }
   260  }
   261          `)
   262  	})
   263  
   264  	policy, err := policies.Get(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c").Extract()
   265  	th.AssertNoErr(t, err)
   266  
   267  	th.AssertEquals(t, "www", policy.Name)
   268  	th.AssertEquals(t, "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", policy.ID)
   269  	th.AssertEquals(t, "Firewall policy web", policy.Description)
   270  	th.AssertEquals(t, 3, len(policy.Rules))
   271  	th.AssertEquals(t, "75452b36-268e-4e75-aaf4-f0e7ed50bc97", policy.Rules[0])
   272  	th.AssertEquals(t, "c9e77ca0-1bc8-497d-904d-948107873dc6", policy.Rules[1])
   273  	th.AssertEquals(t, "03d2a6ad-633f-431a-8463-4370d06a22c8", policy.Rules[2])
   274  	th.AssertEquals(t, "9145d91459d248b1b02fdaca97c6a75d", policy.TenantID)
   275  }
   276  
   277  func TestUpdate(t *testing.T) {
   278  	th.SetupHTTP()
   279  	defer th.TeardownHTTP()
   280  
   281  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", func(w http.ResponseWriter, r *http.Request) {
   282  		th.TestMethod(t, r, "PUT")
   283  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   284  		th.TestHeader(t, r, "Content-Type", "application/json")
   285  		th.TestHeader(t, r, "Accept", "application/json")
   286  		th.TestJSONRequest(t, r, `
   287  {
   288      "firewall_policy":{
   289          "name": "policy",
   290          "firewall_rules": [
   291              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   292              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   293          ],
   294          "description": "Firewall policy"
   295      }
   296  }
   297        `)
   298  
   299  		w.Header().Add("Content-Type", "application/json")
   300  		w.WriteHeader(http.StatusOK)
   301  
   302  		fmt.Fprintf(w, `
   303  {
   304      "firewall_policy":{
   305          "name": "policy",
   306          "firewall_rules": [
   307              "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
   308              "c9e77ca0-1bc8-497d-904d-948107873dc6",
   309              "03d2a6ad-633f-431a-8463-4370d06a22c8"
   310          ],
   311          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   312          "audited": false,
   313          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   314          "description": "Firewall policy"
   315      }
   316  }
   317      `)
   318  	})
   319  
   320  	name := "policy"
   321  	description := "Firewall policy"
   322  
   323  	options := policies.UpdateOpts{
   324  		Name:        &name,
   325  		Description: &description,
   326  		FirewallRules: &[]string{
   327  			"98a58c87-76be-ae7c-a74e-b77fffb88d95",
   328  			"11a58c87-76be-ae7c-a74e-b77fffb88a32",
   329  		},
   330  	}
   331  
   332  	_, err := policies.Update(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", options).Extract()
   333  	th.AssertNoErr(t, err)
   334  }
   335  
   336  func TestDelete(t *testing.T) {
   337  	th.SetupHTTP()
   338  	defer th.TeardownHTTP()
   339  
   340  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   341  		th.TestMethod(t, r, "DELETE")
   342  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   343  		w.WriteHeader(http.StatusNoContent)
   344  	})
   345  
   346  	res := policies.Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304")
   347  	th.AssertNoErr(t, res.Err)
   348  }
   349  
   350  func TestRemoveRule(t *testing.T) {
   351  	th.SetupHTTP()
   352  	defer th.TeardownHTTP()
   353  
   354  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/9fed8075-06ee-463f-83a6-d4118791b02f/remove_rule", func(w http.ResponseWriter, r *http.Request) {
   355  		th.TestMethod(t, r, "PUT")
   356  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   357  		th.TestHeader(t, r, "Content-Type", "application/json")
   358  		th.TestHeader(t, r, "Accept", "application/json")
   359  		th.TestJSONRequest(t, r, `
   360  {
   361      "firewall_rule_id": "9fed8075-06ee-463f-83a6-d4118791b02f"
   362  }
   363          `)
   364  
   365  		w.Header().Add("Content-Type", "application/json")
   366  		w.WriteHeader(http.StatusOK)
   367  
   368  		fmt.Fprintf(w, `
   369  {
   370      "audited": false,
   371      "description": "TESTACC-DESC-skno2e52",
   372      "firewall_rules": [
   373        "3ccc0f2b-4a04-4e7c-bb47-dd1701127a47"
   374      ],
   375      "id": "9fed8075-06ee-463f-83a6-d4118791b02f",
   376      "name": "TESTACC-Qf7pMSkq",
   377      "project_id": "TESTID-era34jkaslk",
   378      "shared": false,
   379      "tenant_id": "TESTID-334sdfassdf"
   380  }
   381      `)
   382  	})
   383  
   384  	policy, err := policies.RemoveRule(fake.ServiceClient(), "9fed8075-06ee-463f-83a6-d4118791b02f", "9fed8075-06ee-463f-83a6-d4118791b02f").Extract()
   385  	th.AssertNoErr(t, err)
   386  	th.AssertEquals(t, "9fed8075-06ee-463f-83a6-d4118791b02f", policy.ID)
   387  
   388  }