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