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