github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/timeouts_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/drycc/controller-sdk-go/api"
    10  	"github.com/drycc/workflow-cli/pkg/testutil"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type parseTimeoutCase struct {
    15  	Input         string
    16  	Key           string
    17  	Value         string
    18  	ExpectedError bool
    19  	ExpectedMsg   string
    20  }
    21  
    22  func TestParseTimeout(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	var errorHint = ` doesn't fit format type=#
    26  Examples: web=30 worker=300`
    27  
    28  	cases := []parseTimeoutCase{
    29  		{"web=20", "web", "20", false, ""},
    30  		{"=1", "", "", true, "=1" + errorHint},
    31  		{"web=", "", "", true, "web=" + errorHint},
    32  		{"1=", "", "", true, "1=" + errorHint},
    33  		{"web=ABCD", "", "", true, "web=ABCD" + errorHint},
    34  	}
    35  
    36  	for _, check := range cases {
    37  		key, value, err := parseTimeout(check.Input)
    38  		if check.ExpectedError {
    39  			assert.Equal(t, err.Error(), check.ExpectedMsg, "error")
    40  		} else {
    41  			assert.NoError(t, err)
    42  			assert.Equal(t, key, check.Key, "key")
    43  			assert.Equal(t, value, check.Value, "value")
    44  		}
    45  	}
    46  }
    47  
    48  type parseTimeoutsCase struct {
    49  	Input         []string
    50  	ExpectedMap   map[string]interface{}
    51  	ExpectedError bool
    52  	ExpectedMsg   string
    53  }
    54  
    55  func TestTimeoutTags(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	cases := []parseTimeoutsCase{
    59  		{[]string{"web=10", "worker=20"}, map[string]interface{}{"web": "10", "worker": "20"}, false, ""},
    60  		{[]string{"foo=", "web=10"}, nil, true, `foo= doesn't fit format type=#
    61  Examples: web=30 worker=300`},
    62  	}
    63  
    64  	for _, check := range cases {
    65  		actual, err := parseTimeouts(check.Input)
    66  		if check.ExpectedError {
    67  			assert.Equal(t, err.Error(), check.ExpectedMsg, "error")
    68  		} else {
    69  			assert.NoError(t, err)
    70  			assert.Equal(t, actual, check.ExpectedMap, "map")
    71  		}
    72  	}
    73  }
    74  
    75  func TestTimeoutsList(t *testing.T) {
    76  	t.Parallel()
    77  	cf, server, err := testutil.NewTestServerAndClient()
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	defer server.Close()
    82  
    83  	server.Mux.HandleFunc("/v2/apps/enterprise/config/", func(w http.ResponseWriter, _ *http.Request) {
    84  		testutil.SetHeaders(w)
    85  		fmt.Fprintf(w, `{
    86        "owner": "jkirk",
    87        "app": "enterprise",
    88        "values": {},
    89        "memory": {},
    90        "cpu": {},
    91        "tags": {},
    92        "registry": {},
    93        "created": "2014-01-01T00:00:00UTC",
    94        "updated": "2014-01-01T00:00:00UTC",
    95        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
    96        "termination_grace_period": {
    97          "web" : 10,
    98          "worker" : 20
    99        }
   100      }`)
   101  	})
   102  
   103  	var b bytes.Buffer
   104  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   105  
   106  	err = cmdr.TimeoutsList("enterprise")
   107  	assert.NoError(t, err)
   108  	assert.Equal(t, b.String(), `UUID                                    OWNER    PTYPE     TIMEOUT 
   109  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jkirk    web       10         
   110  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jkirk    worker    20         
   111  `, "output")
   112  
   113  	server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, _ *http.Request) {
   114  		testutil.SetHeaders(w)
   115  		fmt.Fprintf(w, `{
   116        "owner": "bedison",
   117        "app": "franklin",
   118        "values": {},
   119        "memory": {},
   120        "cpu": {},
   121        "tags": {},
   122        "registry": {},
   123        "created": "2014-01-01T00:00:00UTC",
   124        "updated": "2014-01-01T00:00:00UTC",
   125        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   126        }`)
   127  	})
   128  	b.Reset()
   129  
   130  	err = cmdr.TimeoutsList("franklin")
   131  	assert.NoError(t, err)
   132  	assert.Equal(t, b.String(), `Default (30 sec) or controlled by drycc controller.
   133  `, "output")
   134  }
   135  
   136  func TestTimeoutsSet(t *testing.T) {
   137  	t.Parallel()
   138  	cf, server, err := testutil.NewTestServerAndClient()
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	defer server.Close()
   143  
   144  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
   145  		testutil.SetHeaders(w)
   146  		if r.Method == "POST" {
   147  			testutil.AssertBody(t, api.Config{
   148  				Timeout: map[string]interface{}{
   149  					"web": "10",
   150  				},
   151  			}, r)
   152  		}
   153  
   154  		fmt.Fprintf(w, `{
   155        "owner": "jkirk",
   156        "app": "foo",
   157        "values": {},
   158        "memory": {},
   159        "cpu": {},
   160        "termination_grace_period": {
   161          "web": "10"
   162        },
   163        "tags": {},
   164        "registry": {},
   165        "created": "2014-01-01T00:00:00UTC",
   166        "updated": "2014-01-01T00:00:00UTC",
   167        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   168      }`)
   169  	})
   170  
   171  	var b bytes.Buffer
   172  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   173  
   174  	err = cmdr.TimeoutsSet("foo", []string{"web=10"})
   175  	assert.NoError(t, err)
   176  
   177  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
   178  
   179  UUID                                    OWNER    PTYPE    TIMEOUT 
   180  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jkirk    web      10         
   181  `, "output")
   182  
   183  	server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, r *http.Request) {
   184  		testutil.SetHeaders(w)
   185  		if r.Method == "POST" {
   186  			testutil.AssertBody(t, api.Config{
   187  				Timeout: map[string]interface{}{
   188  					"web": "10",
   189  				},
   190  			}, r)
   191  		}
   192  
   193  		fmt.Fprintf(w, `{
   194        "owner": "bedison",
   195        "app": "franklin",
   196        "values": {},
   197        "memory": {},
   198        "cpu": {},
   199        "termination_grace_period": {
   200          "web": "10"
   201        },
   202        "tags": {},
   203        "registry": {},
   204        "created": "2014-01-01T00:00:00UTC",
   205        "updated": "2014-01-01T00:00:00UTC",
   206        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   207      }`)
   208  	})
   209  	b.Reset()
   210  
   211  	err = cmdr.TimeoutsSet("franklin", []string{"web=10"})
   212  	assert.NoError(t, err)
   213  
   214  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
   215  
   216  UUID                                    OWNER      PTYPE    TIMEOUT 
   217  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    bedison    web      10         
   218  `, "output")
   219  
   220  	// with requests/timeout parameter
   221  	server.Mux.HandleFunc("/v2/apps/jim/config/", func(w http.ResponseWriter, r *http.Request) {
   222  		testutil.SetHeaders(w)
   223  		if r.Method == "POST" {
   224  			testutil.AssertBody(t, api.Config{
   225  				Timeout: map[string]interface{}{
   226  					"web":    "10",
   227  					"worker": "100",
   228  					"db":     "300",
   229  				},
   230  			}, r)
   231  		}
   232  
   233  		fmt.Fprintf(w, `{
   234        "owner": "foo",
   235        "app": "jim",
   236        "values": {},
   237        "memory": {},
   238        "cpu": {},
   239        "termination_grace_period": {
   240          "web": "10",
   241          "worker": "100",
   242          "db": "300"
   243        },
   244        "tags": {},
   245        "registry": {},
   246        "created": "2014-01-01T00:00:00UTC",
   247        "updated": "2014-01-01T00:00:00UTC",
   248        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   249      }`)
   250  	})
   251  	b.Reset()
   252  
   253  	err = cmdr.TimeoutsSet("jim", []string{"web=10", "worker=100", "db=300"})
   254  	assert.NoError(t, err)
   255  
   256  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
   257  
   258  UUID                                    OWNER    PTYPE     TIMEOUT 
   259  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    foo      db        300        
   260  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    foo      web       10         
   261  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    foo      worker    100        
   262  `, "output")
   263  
   264  }
   265  
   266  func TestTimeoutsUnset(t *testing.T) {
   267  	t.Parallel()
   268  	cf, server, err := testutil.NewTestServerAndClient()
   269  	if err != nil {
   270  		t.Fatal(err)
   271  	}
   272  	defer server.Close()
   273  
   274  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
   275  		testutil.SetHeaders(w)
   276  		if r.Method == "POST" {
   277  			testutil.AssertBody(t, api.Config{
   278  				Timeout: map[string]interface{}{
   279  					"web": nil,
   280  				},
   281  			}, r)
   282  		}
   283  
   284  		fmt.Fprintf(w, `{
   285        "owner": "jkirk",
   286        "app": "foo",
   287        "values": {},
   288        "memory": {},
   289        "cpu": {},
   290        "termination_grace_period": {
   291          "web": 10
   292        },
   293        "tags": {},
   294        "registry": {},
   295        "created": "2014-01-01T00:00:00UTC",
   296        "updated": "2014-01-01T00:00:00UTC",
   297        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   298      }`)
   299  	})
   300  
   301  	var b bytes.Buffer
   302  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   303  
   304  	err = cmdr.TimeoutsUnset("foo", []string{"web"})
   305  	assert.NoError(t, err)
   306  
   307  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
   308  
   309  UUID                                    OWNER    PTYPE    TIMEOUT 
   310  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jkirk    web      10         
   311  `, "output")
   312  
   313  	server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, r *http.Request) {
   314  		testutil.SetHeaders(w)
   315  		if r.Method == "POST" {
   316  			testutil.AssertBody(t, api.Config{
   317  				Timeout: map[string]interface{}{
   318  					"web": nil,
   319  				},
   320  			}, r)
   321  		}
   322  
   323  		fmt.Fprintf(w, `{
   324        "owner": "bedison",
   325        "app": "franklin",
   326        "values": {},
   327        "memory": {},
   328        "cpu": {},
   329        "termination_grace_period": {
   330          "web": 10
   331        },
   332        "tags": {},
   333        "registry": {},
   334        "created": "2014-01-01T00:00:00UTC",
   335        "updated": "2014-01-01T00:00:00UTC",
   336        "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   337      }`)
   338  	})
   339  	b.Reset()
   340  
   341  	err = cmdr.TimeoutsUnset("franklin", []string{"web"})
   342  	assert.NoError(t, err)
   343  
   344  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
   345  
   346  UUID                                    OWNER      PTYPE    TIMEOUT 
   347  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    bedison    web      10         
   348  `, "output")
   349  }