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