github.com/engineyard/workflow-cli@v2.21.6+incompatible/parser/healthchecks_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/arschles/assert"
     9  	"github.com/teamhephy/controller-sdk-go/api"
    10  	"github.com/teamhephy/workflow-cli/pkg/testutil"
    11  )
    12  
    13  // Create fake implementations of each method that return the argument
    14  // we expect to have called the function (as an error to satisfy the interface).
    15  
    16  func (d FakeDeisCmd) HealthchecksList(string, string) error {
    17  	return errors.New("healthchecks:list")
    18  }
    19  
    20  func (d FakeDeisCmd) HealthchecksSet(string, string, string, *api.Healthcheck) error {
    21  	return errors.New("healthchecks:set")
    22  }
    23  
    24  func (d FakeDeisCmd) HealthchecksUnset(string, string, []string) error {
    25  	return errors.New("healthchecks:unset")
    26  }
    27  
    28  func TestHealthchecks(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	cf, server, err := testutil.NewTestServerAndClient()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	defer server.Close()
    36  	var b bytes.Buffer
    37  	cmdr := FakeDeisCmd{WOut: &b, ConfigFile: cf}
    38  
    39  	// cases defines the arguments and expected return of the call.
    40  	// if expected is "", it defaults to args[0].
    41  	cases := []struct {
    42  		args     []string
    43  		expected string
    44  	}{
    45  		{
    46  			args:     []string{"healthchecks:list"},
    47  			expected: "",
    48  		},
    49  		{
    50  			args:     []string{"healthchecks:set", "liveness", "httpGet", "80"},
    51  			expected: "",
    52  		},
    53  		{
    54  			args:     []string{"healthchecks:set", "liveness", "httpGet", "80", "--headers=test-header:test-value"},
    55  			expected: "",
    56  		},
    57  		{
    58  			args:     []string{"healthchecks:set", "liveness", "exec", "ls"},
    59  			expected: "",
    60  		},
    61  		{
    62  			args:     []string{"healthchecks:set", "liveness", "tcpSocket", "80"},
    63  			expected: "",
    64  		},
    65  		{
    66  			args:     []string{"healthchecks:unset", "liveness"},
    67  			expected: "",
    68  		},
    69  		{
    70  			args:     []string{"healthchecks"},
    71  			expected: "healthchecks:list",
    72  		},
    73  		{
    74  			args:     []string{"healthchecks:set", "alien", "httpGet", "80"},
    75  			expected: "probe type alien is invalid. Must be one of [liveness readiness]",
    76  		},
    77  		{
    78  			args:     []string{"healthchecks:unset", "alien", "httpGet", "80"},
    79  			expected: "probe type alien is invalid. Must be one of [liveness readiness]",
    80  		},
    81  	}
    82  
    83  	// For each case, check that calling the route with the arguments
    84  	// returns the expected error, which is args[0] if not provided.
    85  	for _, c := range cases {
    86  		var expected string
    87  		if c.expected == "" {
    88  			expected = c.args[0]
    89  		} else {
    90  			expected = c.expected
    91  		}
    92  		err = Healthchecks(c.args, cmdr)
    93  		assert.Err(t, errors.New(expected), err)
    94  	}
    95  }