github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/workflow/v2/crontriggers/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/crontriggers"
    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 TestCreateCronTrigger(t *testing.T) {
    19  	th.SetupHTTP()
    20  	defer th.TeardownHTTP()
    21  
    22  	th.Mux.HandleFunc("/cron_triggers", 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 15:48:18",
    31  				"first_execution_time": "2018-09-12 17:48:00",
    32  				"id": "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
    33  				"name": "crontrigger",
    34  				"next_execution_time": "2018-09-12 17:48:00",
    35  				"pattern": "0 0 1 1 *",
    36  				"project_id": "778c0f25df0d492a9a868ee9e2fbb513",
    37  				"remaining_executions": 42,
    38  				"scope": "private",
    39  				"updated_at": null,
    40  				"workflow_id": "604a3a1e-94e3-4066-a34a-aa56873ef236",
    41  				"workflow_input": "{\"msg\": \"hello\"}",
    42  				"workflow_name": "workflow_echo",
    43  				"workflow_params": "{\"msg\": \"world\"}"
    44  			}
    45  		`)
    46  	})
    47  
    48  	firstExecution := time.Date(2018, time.September, 12, 17, 48, 0, 0, time.UTC)
    49  	opts := &crontriggers.CreateOpts{
    50  		WorkflowID:         "604a3a1e-94e3-4066-a34a-aa56873ef236",
    51  		Name:               "trigger",
    52  		FirstExecutionTime: &firstExecution,
    53  		WorkflowParams: map[string]any{
    54  			"msg": "world",
    55  		},
    56  		WorkflowInput: map[string]any{
    57  			"msg": "hello",
    58  		},
    59  	}
    60  
    61  	actual, err := crontriggers.Create(context.TODO(), fake.ServiceClient(), opts).Extract()
    62  	if err != nil {
    63  		t.Fatalf("Unable to create cron trigger: %v", err)
    64  	}
    65  
    66  	expected := &crontriggers.CronTrigger{
    67  		ID:                  "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
    68  		Name:                "crontrigger",
    69  		Pattern:             "0 0 1 1 *",
    70  		ProjectID:           "778c0f25df0d492a9a868ee9e2fbb513",
    71  		RemainingExecutions: 42,
    72  		Scope:               "private",
    73  		WorkflowID:          "604a3a1e-94e3-4066-a34a-aa56873ef236",
    74  		WorkflowName:        "workflow_echo",
    75  		WorkflowParams: map[string]any{
    76  			"msg": "world",
    77  		},
    78  		WorkflowInput: map[string]any{
    79  			"msg": "hello",
    80  		},
    81  		CreatedAt:          time.Date(2018, time.September, 12, 15, 48, 18, 0, time.UTC),
    82  		FirstExecutionTime: &firstExecution,
    83  		NextExecutionTime:  &firstExecution,
    84  	}
    85  
    86  	if !reflect.DeepEqual(expected, actual) {
    87  		t.Errorf("Expected %#v, but was %#v", expected, actual)
    88  	}
    89  }
    90  
    91  func TestDeleteCronTrigger(t *testing.T) {
    92  	th.SetupHTTP()
    93  	defer th.TeardownHTTP()
    94  
    95  	th.Mux.HandleFunc("/cron_triggers/0520ffd8-f7f1-4f2e-845b-55d953a1cf46", func(w http.ResponseWriter, r *http.Request) {
    96  		th.TestMethod(t, r, "DELETE")
    97  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    98  
    99  		w.WriteHeader(http.StatusAccepted)
   100  	})
   101  
   102  	res := crontriggers.Delete(context.TODO(), fake.ServiceClient(), "0520ffd8-f7f1-4f2e-845b-55d953a1cf46")
   103  	th.AssertNoErr(t, res.Err)
   104  }
   105  
   106  func TestGetCronTrigger(t *testing.T) {
   107  	th.SetupHTTP()
   108  	defer th.TeardownHTTP()
   109  	th.Mux.HandleFunc("/cron_triggers/0520ffd8-f7f1-4f2e-845b-55d953a1cf46", func(w http.ResponseWriter, r *http.Request) {
   110  		th.TestMethod(t, r, "GET")
   111  		th.TestHeader(t, r, "X-Auth-token", fake.TokenID)
   112  		w.Header().Add("Content-Type", "application/json")
   113  		fmt.Fprint(w, `
   114  			{
   115  				"created_at": "2018-09-12 15:48:18",
   116  				"first_execution_time": "2018-09-12 17:48:00",
   117  				"id": "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
   118  				"name": "crontrigger",
   119  				"next_execution_time": "2018-09-12 17:48:00",
   120  				"pattern": "0 0 1 1 *",
   121  				"project_id": "778c0f25df0d492a9a868ee9e2fbb513",
   122  				"remaining_executions": 42,
   123  				"scope": "private",
   124  				"updated_at": null,
   125  				"workflow_id": "604a3a1e-94e3-4066-a34a-aa56873ef236",
   126  				"workflow_input": "{\"msg\": \"hello\"}",
   127  				"workflow_name": "workflow_echo",
   128  				"workflow_params": "{\"msg\": \"world\"}"
   129  			}
   130  		`)
   131  	})
   132  	actual, err := crontriggers.Get(context.TODO(), fake.ServiceClient(), "0520ffd8-f7f1-4f2e-845b-55d953a1cf46").Extract()
   133  	if err != nil {
   134  		t.Fatalf("Unable to get cron trigger: %v", err)
   135  	}
   136  
   137  	firstExecution := time.Date(2018, time.September, 12, 17, 48, 0, 0, time.UTC)
   138  
   139  	expected := &crontriggers.CronTrigger{
   140  		ID:                  "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
   141  		Name:                "crontrigger",
   142  		Pattern:             "0 0 1 1 *",
   143  		ProjectID:           "778c0f25df0d492a9a868ee9e2fbb513",
   144  		RemainingExecutions: 42,
   145  		Scope:               "private",
   146  		WorkflowID:          "604a3a1e-94e3-4066-a34a-aa56873ef236",
   147  		WorkflowName:        "workflow_echo",
   148  		WorkflowParams: map[string]any{
   149  			"msg": "world",
   150  		},
   151  		WorkflowInput: map[string]any{
   152  			"msg": "hello",
   153  		},
   154  		CreatedAt:          time.Date(2018, time.September, 12, 15, 48, 18, 0, time.UTC),
   155  		FirstExecutionTime: &firstExecution,
   156  		NextExecutionTime:  &firstExecution,
   157  	}
   158  	if !reflect.DeepEqual(expected, actual) {
   159  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   160  	}
   161  }
   162  
   163  func TestListCronTriggers(t *testing.T) {
   164  	th.SetupHTTP()
   165  	defer th.TeardownHTTP()
   166  	th.Mux.HandleFunc("/cron_triggers", func(w http.ResponseWriter, r *http.Request) {
   167  		th.TestMethod(t, r, "GET")
   168  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   169  		w.Header().Add("Content-Type", "application/json")
   170  		if err := r.ParseForm(); err != nil {
   171  			t.Errorf("Failed to parse request form %v", err)
   172  		}
   173  		marker := r.Form.Get("marker")
   174  		switch marker {
   175  		case "":
   176  			fmt.Fprintf(w, `{
   177  				"cron_triggers": [
   178  					{
   179  						"created_at": "2018-09-12 15:48:18",
   180  						"first_execution_time": "2018-09-12 17:48:00",
   181  						"id": "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
   182  						"name": "crontrigger",
   183  						"next_execution_time": "2018-09-12 17:48:00",
   184  						"pattern": "0 0 1 1 *",
   185  						"project_id": "778c0f25df0d492a9a868ee9e2fbb513",
   186  						"remaining_executions": 42,
   187  						"scope": "private",
   188  						"updated_at": null,
   189  						"workflow_id": "604a3a1e-94e3-4066-a34a-aa56873ef236",
   190  						"workflow_input": "{\"msg\": \"hello\"}",
   191  						"workflow_name": "workflow_echo",
   192  						"workflow_params": "{\"msg\": \"world\"}"
   193  					}
   194  				],
   195  				"next": "%s/cron_triggers?marker=0520ffd8-f7f1-4f2e-845b-55d953a1cf46"
   196  			}`, th.Server.URL)
   197  		case "0520ffd8-f7f1-4f2e-845b-55d953a1cf46":
   198  			fmt.Fprint(w, `{ "cron_triggers": [] }`)
   199  		default:
   200  			t.Fatalf("Unexpected marker: [%s]", marker)
   201  		}
   202  	})
   203  	pages := 0
   204  	// Get all cron triggers
   205  	err := crontriggers.List(fake.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   206  		pages++
   207  		actual, err := crontriggers.ExtractCronTriggers(page)
   208  		if err != nil {
   209  			return false, err
   210  		}
   211  
   212  		firstExecution := time.Date(2018, time.September, 12, 17, 48, 0, 0, time.UTC)
   213  
   214  		expected := []crontriggers.CronTrigger{
   215  			{
   216  				ID:                  "0520ffd8-f7f1-4f2e-845b-55d953a1cf46",
   217  				Name:                "crontrigger",
   218  				Pattern:             "0 0 1 1 *",
   219  				ProjectID:           "778c0f25df0d492a9a868ee9e2fbb513",
   220  				RemainingExecutions: 42,
   221  				Scope:               "private",
   222  				WorkflowID:          "604a3a1e-94e3-4066-a34a-aa56873ef236",
   223  				WorkflowName:        "workflow_echo",
   224  				WorkflowParams: map[string]any{
   225  					"msg": "world",
   226  				},
   227  				WorkflowInput: map[string]any{
   228  					"msg": "hello",
   229  				},
   230  				CreatedAt:          time.Date(2018, time.September, 12, 15, 48, 18, 0, time.UTC),
   231  				FirstExecutionTime: &firstExecution,
   232  				NextExecutionTime:  &firstExecution,
   233  			},
   234  		}
   235  
   236  		if !reflect.DeepEqual(expected, actual) {
   237  			t.Errorf("Expected %#v, but was %#v", expected, actual)
   238  		}
   239  		return true, nil
   240  	})
   241  	if err != nil {
   242  		t.Fatal(err)
   243  	}
   244  	if pages != 1 {
   245  		t.Errorf("Expected one page, got %d", pages)
   246  	}
   247  }
   248  
   249  func TestToExecutionListQuery(t *testing.T) {
   250  	for expected, opts := range map[string]*crontriggers.ListOpts{
   251  		newValue("workflow_input", `{"msg":"Hello"}`): {
   252  			WorkflowInput: map[string]any{
   253  				"msg": "Hello",
   254  			},
   255  		},
   256  		newValue("name", `neq:not_name`): {
   257  			Name: &crontriggers.ListFilter{
   258  				Filter: crontriggers.FilterNEQ,
   259  				Value:  "not_name",
   260  			},
   261  		},
   262  		newValue("workflow_name", `eq:workflow`): {
   263  			WorkflowName: &crontriggers.ListFilter{
   264  				Filter: crontriggers.FilterEQ,
   265  				Value:  "workflow",
   266  			},
   267  		},
   268  		newValue("created_at", `gt:2018-01-01 00:00:00`): {
   269  			CreatedAt: &crontriggers.ListDateFilter{
   270  				Filter: crontriggers.FilterGT,
   271  				Value:  time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC),
   272  			},
   273  		},
   274  	} {
   275  		actual, _ := opts.ToCronTriggerListQuery()
   276  		th.AssertEquals(t, expected, actual)
   277  	}
   278  }
   279  
   280  func newValue(param, value string) string {
   281  	v := url.Values{}
   282  	v.Add(param, value)
   283  	return "?" + v.Encode()
   284  }