github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/security/groups/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" 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/security-groups", 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, SecurityGroupListResponse) 27 }) 28 29 count := 0 30 31 err := groups.List(fake.ServiceClient(), groups.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 32 count++ 33 actual, err := groups.ExtractGroups(page) 34 if err != nil { 35 t.Errorf("Failed to extract secgroups: %v", err) 36 return false, err 37 } 38 39 expected := []groups.SecGroup{SecurityGroup1} 40 th.CheckDeepEquals(t, expected, actual) 41 42 return true, nil 43 }) 44 45 th.AssertNoErr(t, err) 46 47 if count != 1 { 48 t.Errorf("Expected 1 page, got %d", count) 49 } 50 } 51 52 func TestCreate(t *testing.T) { 53 th.SetupHTTP() 54 defer th.TeardownHTTP() 55 56 th.Mux.HandleFunc("/v2.0/security-groups", func(w http.ResponseWriter, r *http.Request) { 57 th.TestMethod(t, r, "POST") 58 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 59 th.TestHeader(t, r, "Content-Type", "application/json") 60 th.TestHeader(t, r, "Accept", "application/json") 61 th.TestJSONRequest(t, r, SecurityGroupCreateRequest) 62 63 w.Header().Add("Content-Type", "application/json") 64 w.WriteHeader(http.StatusCreated) 65 66 fmt.Fprintf(w, SecurityGroupCreateResponse) 67 }) 68 69 opts := groups.CreateOpts{Name: "new-webservers", Description: "security group for webservers"} 70 _, err := groups.Create(fake.ServiceClient(), opts).Extract() 71 th.AssertNoErr(t, err) 72 } 73 74 func TestUpdate(t *testing.T) { 75 th.SetupHTTP() 76 defer th.TeardownHTTP() 77 78 th.Mux.HandleFunc("/v2.0/security-groups/2076db17-a522-4506-91de-c6dd8e837028", 79 func(w http.ResponseWriter, r *http.Request) { 80 th.TestMethod(t, r, "PUT") 81 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 82 th.TestHeader(t, r, "Content-Type", "application/json") 83 th.TestHeader(t, r, "Accept", "application/json") 84 th.TestJSONRequest(t, r, SecurityGroupUpdateRequest) 85 86 w.Header().Add("Content-Type", "application/json") 87 w.WriteHeader(http.StatusOK) 88 89 fmt.Fprintf(w, SecurityGroupUpdateResponse) 90 }) 91 92 opts := groups.UpdateOpts{Name: "newer-webservers"} 93 sg, err := groups.Update(fake.ServiceClient(), "2076db17-a522-4506-91de-c6dd8e837028", opts).Extract() 94 th.AssertNoErr(t, err) 95 96 th.AssertEquals(t, "newer-webservers", sg.Name) 97 th.AssertEquals(t, "security group for webservers", sg.Description) 98 th.AssertEquals(t, "2076db17-a522-4506-91de-c6dd8e837028", sg.ID) 99 th.AssertEquals(t, "2019-06-30T04:15:37Z", sg.CreatedAt.Format(time.RFC3339)) 100 th.AssertEquals(t, "2019-06-30T05:18:49Z", sg.UpdatedAt.Format(time.RFC3339)) 101 } 102 103 func TestGet(t *testing.T) { 104 th.SetupHTTP() 105 defer th.TeardownHTTP() 106 107 th.Mux.HandleFunc("/v2.0/security-groups/85cc3048-abc3-43cc-89b3-377341426ac5", func(w http.ResponseWriter, r *http.Request) { 108 th.TestMethod(t, r, "GET") 109 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 110 111 w.Header().Add("Content-Type", "application/json") 112 w.WriteHeader(http.StatusOK) 113 114 fmt.Fprintf(w, SecurityGroupGetResponse) 115 }) 116 117 sg, err := groups.Get(fake.ServiceClient(), "85cc3048-abc3-43cc-89b3-377341426ac5").Extract() 118 th.AssertNoErr(t, err) 119 120 th.AssertEquals(t, "default", sg.Description) 121 th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sg.ID) 122 th.AssertEquals(t, "default", sg.Name) 123 th.AssertEquals(t, 2, len(sg.Rules)) 124 th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sg.TenantID) 125 th.AssertEquals(t, "2019-06-30T04:15:37Z", sg.CreatedAt.Format(time.RFC3339)) 126 th.AssertEquals(t, "2019-06-30T05:18:49Z", sg.UpdatedAt.Format(time.RFC3339)) 127 } 128 129 func TestDelete(t *testing.T) { 130 th.SetupHTTP() 131 defer th.TeardownHTTP() 132 133 th.Mux.HandleFunc("/v2.0/security-groups/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { 134 th.TestMethod(t, r, "DELETE") 135 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 136 w.WriteHeader(http.StatusNoContent) 137 }) 138 139 res := groups.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304") 140 th.AssertNoErr(t, res.Err) 141 }