github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/healthchecks_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  func TestHealthchecksList(t *testing.T) {
    15  	t.Parallel()
    16  	cf, server, err := testutil.NewTestServerAndClient()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer server.Close()
    21  	var b bytes.Buffer
    22  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
    23  
    24  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, _ *http.Request) {
    25  		testutil.SetHeaders(w)
    26  		fmt.Fprintf(w, `{
    27    "uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
    28    "app": "foo",
    29    "owner": "bar",
    30    "values": {},
    31    "memory": {},
    32    "cpu": {},
    33    "tags": {},
    34    "registry": {},
    35    "healthcheck": {
    36      "web": {
    37        "livenessProbe": {
    38          "initialDelaySeconds": 50,
    39          "timeoutSeconds": 50,
    40          "periodSeconds": 10,
    41          "failureThreshold": 3,
    42          "httpGet": {
    43            "port": 80,
    44            "path": "/"
    45          },
    46          "successThreshold": 1
    47        }
    48      }
    49    },
    50    "created": "2016-09-12T22:20:14Z",
    51    "updated": "2016-09-12T22:20:14Z"
    52  }`)
    53  	})
    54  
    55  	err = cmdr.HealthchecksList("foo", "web")
    56  	assert.NoError(t, err)
    57  
    58  	assert.Equal(t, b.String(), `App:             foo                                                                                                            
    59  UUID:            c039a380-6068-4511-b35a-535a73b86ef5                                                                           
    60  Owner:           bar                                                                                                            
    61  Created:         2016-09-12T22:20:14Z                                                                                           
    62  Updated:         2016-09-12T22:20:14Z                                                                                           
    63  Healthchecks:    
    64                   livenessProbe web http-get headers=[] path=/ port=80 delay=50s timeout=50s period=10s #success=1 #failure=3    
    65  `, "output")
    66  }
    67  
    68  func TestHealthchecksListNoHealthCheck(t *testing.T) {
    69  	t.Parallel()
    70  	cf, server, err := testutil.NewTestServerAndClient()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	defer server.Close()
    75  	var b bytes.Buffer
    76  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
    77  
    78  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, _ *http.Request) {
    79  		testutil.SetHeaders(w)
    80  		fmt.Fprintf(w, `{
    81    "uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
    82    "app": "foo",
    83    "owner": "bar",
    84    "values": {},
    85    "memory": {},
    86    "cpu": {},
    87    "tags": {},
    88    "registry": {},
    89    "healthcheck": {},
    90    "created": "2016-09-12T22:20:14Z",
    91    "updated": "2016-09-12T22:20:14Z"
    92  }`)
    93  	})
    94  
    95  	err = cmdr.HealthchecksList("foo", "")
    96  	assert.NoError(t, err)
    97  
    98  	assert.Equal(t, b.String(), `No health checks configured.
    99  `, "output")
   100  }
   101  
   102  func TestHealthchecksListAllHealthChecks(t *testing.T) {
   103  	t.Parallel()
   104  	cf, server, err := testutil.NewTestServerAndClient()
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer server.Close()
   109  	var b bytes.Buffer
   110  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   111  
   112  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, _ *http.Request) {
   113  		testutil.SetHeaders(w)
   114  		fmt.Fprintf(w, `{
   115    "uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
   116    "app": "foo",
   117    "owner": "bar",
   118    "values": {},
   119    "memory": {},
   120    "cpu": {},
   121    "tags": {},
   122    "registry": {},
   123    "healthcheck": {
   124      "web": {
   125        "livenessProbe": {
   126          "initialDelaySeconds": 50,
   127          "timeoutSeconds": 50,
   128          "periodSeconds": 10,
   129          "failureThreshold": 3,
   130          "httpGet": {
   131            "port": 80,
   132            "path": "/"
   133          },
   134          "successThreshold": 1
   135        }
   136      },
   137  		"task": {
   138        "livenessProbe": {
   139          "initialDelaySeconds": 50,
   140          "timeoutSeconds": 50,
   141          "periodSeconds": 10,
   142          "failureThreshold": 3,
   143          "httpGet": {
   144            "port": 8000,
   145            "path": "/"
   146          },
   147          "successThreshold": 1
   148        }
   149      }
   150    },
   151    "created": "2016-09-12T22:20:14Z",
   152    "updated": "2016-09-12T22:20:14Z"
   153  }`)
   154  	})
   155  
   156  	err = cmdr.HealthchecksList("foo", "")
   157  	assert.NoError(t, err)
   158  
   159  	assert.Equal(t, b.String(), `App:             foo                                                                                                               
   160  UUID:            c039a380-6068-4511-b35a-535a73b86ef5                                                                              
   161  Owner:           bar                                                                                                               
   162  Created:         2016-09-12T22:20:14Z                                                                                              
   163  Updated:         2016-09-12T22:20:14Z                                                                                              
   164  Healthchecks:    
   165                   livenessProbe task http-get headers=[] path=/ port=8000 delay=50s timeout=50s period=10s #success=1 #failure=3    
   166                   livenessProbe web http-get headers=[] path=/ port=80 delay=50s timeout=50s period=10s #success=1 #failure=3       
   167  `, "output")
   168  }
   169  
   170  func TestHealthchecksSet(t *testing.T) {
   171  	t.Parallel()
   172  	cf, server, err := testutil.NewTestServerAndClient()
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  	defer server.Close()
   177  	var b bytes.Buffer
   178  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   179  
   180  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, _ *http.Request) {
   181  		testutil.SetHeaders(w)
   182  		fmt.Fprintf(w, `{
   183    "uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
   184    "app": "foo",
   185    "owner": "bar",
   186    "values": {},
   187    "memory": {},
   188    "cpu": {},
   189    "tags": {},
   190    "registry": {},
   191    "healthcheck": {
   192      "web": {
   193        "livenessProbe": {
   194          "initialDelaySeconds": 50,
   195          "timeoutSeconds": 50,
   196          "periodSeconds": 10,
   197          "failureThreshold": 3,
   198          "httpGet": {
   199            "port": 80,
   200            "path": "/"
   201          },
   202          "successThreshold": 1
   203        }
   204      }
   205    },
   206    "created": "2016-09-12T22:20:14Z",
   207    "updated": "2016-09-12T22:20:14Z"
   208  }`)
   209  	})
   210  
   211  	err = cmdr.HealthchecksSet("foo", "livenessProbe", "web", &api.Healthcheck{})
   212  	assert.NoError(t, err)
   213  	assert.Equal(t, testutil.StripProgress(b.String()), `Applying livenessProbe healthcheck... done
   214  
   215  App:             foo                                                                                                            
   216  UUID:            c039a380-6068-4511-b35a-535a73b86ef5                                                                           
   217  Owner:           bar                                                                                                            
   218  Created:         2016-09-12T22:20:14Z                                                                                           
   219  Updated:         2016-09-12T22:20:14Z                                                                                           
   220  Healthchecks:    
   221                   livenessProbe web http-get headers=[] path=/ port=80 delay=50s timeout=50s period=10s #success=1 #failure=3    
   222  `, "output")
   223  }
   224  
   225  func TestHealthchecksUnset(t *testing.T) {
   226  	t.Parallel()
   227  	cf, server, err := testutil.NewTestServerAndClient()
   228  	if err != nil {
   229  		t.Fatal(err)
   230  	}
   231  	defer server.Close()
   232  	var b bytes.Buffer
   233  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
   234  
   235  	server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, _ *http.Request) {
   236  		testutil.SetHeaders(w)
   237  		fmt.Fprintf(w, `{
   238    "uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
   239    "app": "foo",
   240    "owner": "bar",
   241    "values": {},
   242    "memory": {},
   243    "cpu": {},
   244    "tags": {},
   245    "registry": {},
   246    "healthcheck": {},
   247    "created": "2016-09-12T22:20:14Z",
   248    "updated": "2016-09-12T22:20:14Z"
   249  }`)
   250  	})
   251  
   252  	err = cmdr.HealthchecksUnset("foo", "web", []string{"livenessProbe"})
   253  	assert.NoError(t, err)
   254  	assert.Equal(t, testutil.StripProgress(b.String()), `Removing healthchecks... done
   255  
   256  No health checks configured.
   257  `, "output")
   258  }