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