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