github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/workflow/v2/executions/testing/requests_test.go (about)

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