github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/workflow/v2/workflows/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "net/url" 8 "reflect" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/vnpaycloud-console/gophercloud/v2/openstack/workflow/v2/workflows" 14 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 15 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 16 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 17 ) 18 19 func TestCreateWorkflow(t *testing.T) { 20 th.SetupHTTP() 21 defer th.TeardownHTTP() 22 23 definition := `--- 24 version: '2.0' 25 26 workflow_echo: 27 description: Simple workflow example 28 type: direct 29 input: 30 - msg 31 32 tasks: 33 test: 34 action: std.echo output="<% $.msg %>"` 35 36 th.Mux.HandleFunc("/workflows", func(w http.ResponseWriter, r *http.Request) { 37 th.TestMethod(t, r, "POST") 38 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 39 th.TestHeader(t, r, "Content-Type", "text/plain") 40 th.TestFormValues(t, r, map[string]string{ 41 "namespace": "some-namespace", 42 "scope": "private", 43 }) 44 th.TestBody(t, r, definition) 45 46 w.WriteHeader(http.StatusCreated) 47 w.Header().Add("Content-Type", "application/json") 48 49 fmt.Fprint(w, `{ 50 "workflows": [ 51 { 52 "created_at": "2018-09-12 15:48:17", 53 "definition": "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<% $.msg %>\"", 54 "id": "604a3a1e-94e3-4066-a34a-aa56873ef236", 55 "input": "msg", 56 "name": "workflow_echo", 57 "namespace": "some-namespace", 58 "project_id": "778c0f25df0d492a9a868ee9e2fbb513", 59 "scope": "private", 60 "tags": [], 61 "updated_at": "2018-09-12 15:48:17" 62 } 63 ] 64 }`) 65 }) 66 67 opts := &workflows.CreateOpts{ 68 Namespace: "some-namespace", 69 Scope: "private", 70 Definition: strings.NewReader(definition), 71 } 72 73 actual, err := workflows.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 74 if err != nil { 75 t.Fatalf("Unable to create workflow: %v", err) 76 } 77 78 updated := time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC) 79 expected := []workflows.Workflow{ 80 { 81 ID: "604a3a1e-94e3-4066-a34a-aa56873ef236", 82 Definition: "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<% $.msg %>\"", 83 Name: "workflow_echo", 84 Namespace: "some-namespace", 85 Input: "msg", 86 ProjectID: "778c0f25df0d492a9a868ee9e2fbb513", 87 Scope: "private", 88 Tags: []string{}, 89 CreatedAt: time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC), 90 UpdatedAt: &updated, 91 }, 92 } 93 94 if !reflect.DeepEqual(expected, actual) { 95 t.Errorf("Expected %#v, but was %#v", expected, actual) 96 } 97 } 98 99 func TestDeleteWorkflow(t *testing.T) { 100 th.SetupHTTP() 101 defer th.TeardownHTTP() 102 103 th.Mux.HandleFunc("/workflows/604a3a1e-94e3-4066-a34a-aa56873ef236", func(w http.ResponseWriter, r *http.Request) { 104 th.TestMethod(t, r, "DELETE") 105 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 106 107 w.WriteHeader(http.StatusAccepted) 108 }) 109 110 res := workflows.Delete(context.TODO(), fake.ServiceClient(), "604a3a1e-94e3-4066-a34a-aa56873ef236") 111 th.AssertNoErr(t, res.Err) 112 } 113 114 func TestGetWorkflow(t *testing.T) { 115 th.SetupHTTP() 116 defer th.TeardownHTTP() 117 th.Mux.HandleFunc("/workflows/1", func(w http.ResponseWriter, r *http.Request) { 118 th.TestMethod(t, r, "GET") 119 th.TestHeader(t, r, "X-Auth-token", fake.TokenID) 120 w.Header().Add("Content-Type", "application/json") 121 fmt.Fprint(w, ` 122 { 123 "created_at": "2018-09-12 15:48:17", 124 "definition": "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<% $.msg %>\"", 125 "id": "604a3a1e-94e3-4066-a34a-aa56873ef236", 126 "input": "msg", 127 "name": "workflow_echo", 128 "namespace": "some-namespace", 129 "project_id": "778c0f25df0d492a9a868ee9e2fbb513", 130 "scope": "private", 131 "tags": [], 132 "updated_at": "2018-09-12 15:48:17" 133 } 134 `) 135 }) 136 actual, err := workflows.Get(context.TODO(), fake.ServiceClient(), "1").Extract() 137 if err != nil { 138 t.Fatalf("Unable to get workflow: %v", err) 139 } 140 141 updated := time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC) 142 expected := &workflows.Workflow{ 143 ID: "604a3a1e-94e3-4066-a34a-aa56873ef236", 144 Definition: "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<% $.msg %>\"", 145 Name: "workflow_echo", 146 Namespace: "some-namespace", 147 Input: "msg", 148 ProjectID: "778c0f25df0d492a9a868ee9e2fbb513", 149 Scope: "private", 150 Tags: []string{}, 151 CreatedAt: time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC), 152 UpdatedAt: &updated, 153 } 154 if !reflect.DeepEqual(expected, actual) { 155 t.Errorf("Expected %#v, but was %#v", expected, actual) 156 } 157 } 158 159 func TestListWorkflows(t *testing.T) { 160 th.SetupHTTP() 161 defer th.TeardownHTTP() 162 th.Mux.HandleFunc("/workflows", func(w http.ResponseWriter, r *http.Request) { 163 th.TestMethod(t, r, "GET") 164 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 165 w.Header().Add("Content-Type", "application/json") 166 if err := r.ParseForm(); err != nil { 167 t.Errorf("Failed to parse request form %v", err) 168 } 169 marker := r.Form.Get("marker") 170 switch marker { 171 case "": 172 fmt.Fprintf(w, `{ 173 "next": "%s/workflows?marker=604a3a1e-94e3-4066-a34a-aa56873ef236", 174 "workflows": [ 175 { 176 "created_at": "2018-09-12 15:48:17", 177 "definition": "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<%% $.msg %%>\"", 178 "id": "604a3a1e-94e3-4066-a34a-aa56873ef236", 179 "input": "msg", 180 "name": "workflow_echo", 181 "namespace": "some-namespace", 182 "project_id": "778c0f25df0d492a9a868ee9e2fbb513", 183 "scope": "private", 184 "tags": [], 185 "updated_at": "2018-09-12 15:48:17" 186 } 187 ] 188 }`, th.Server.URL) 189 case "604a3a1e-94e3-4066-a34a-aa56873ef236": 190 fmt.Fprint(w, `{ "workflows": [] }`) 191 default: 192 t.Fatalf("Unexpected marker: [%s]", marker) 193 } 194 }) 195 pages := 0 196 // Get all workflows 197 err := workflows.List(fake.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 198 pages++ 199 actual, err := workflows.ExtractWorkflows(page) 200 if err != nil { 201 return false, err 202 } 203 204 updated := time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC) 205 expected := []workflows.Workflow{ 206 { 207 ID: "604a3a1e-94e3-4066-a34a-aa56873ef236", 208 Definition: "---\nversion: '2.0'\n\nworkflow_echo:\n description: Simple workflow example\n type: direct\n\n input:\n - msg\n\n tasks:\n test:\n action: std.echo output=\"<% $.msg %>\"", 209 Name: "workflow_echo", 210 Namespace: "some-namespace", 211 Input: "msg", 212 ProjectID: "778c0f25df0d492a9a868ee9e2fbb513", 213 Scope: "private", 214 Tags: []string{}, 215 CreatedAt: time.Date(2018, time.September, 12, 15, 48, 17, 0, time.UTC), 216 UpdatedAt: &updated, 217 }, 218 } 219 220 if !reflect.DeepEqual(expected, actual) { 221 t.Errorf("Expected %#v, but was %#v", expected, actual) 222 } 223 return true, nil 224 }) 225 if err != nil { 226 t.Fatal(err) 227 } 228 if pages != 1 { 229 t.Errorf("Expected one page, got %d", pages) 230 } 231 } 232 233 func TestToWorkflowListQuery(t *testing.T) { 234 for expected, opts := range map[string]*workflows.ListOpts{ 235 newValue("tags", `tag1,tag2`): { 236 Tags: []string{"tag1", "tag2"}, 237 }, 238 newValue("name", `neq:invalid_name`): { 239 Name: &workflows.ListFilter{ 240 Filter: workflows.FilterNEQ, 241 Value: "invalid_name", 242 }, 243 }, 244 newValue("created_at", `gt:2018-01-01 00:00:00`): { 245 CreatedAt: &workflows.ListDateFilter{ 246 Filter: workflows.FilterGT, 247 Value: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), 248 }, 249 }, 250 } { 251 actual, _ := opts.ToWorkflowListQuery() 252 th.AssertEquals(t, expected, actual) 253 } 254 } 255 func newValue(param, value string) string { 256 v := url.Values{} 257 v.Add(param, value) 258 return "?" + v.Encode() 259 }