github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/jobs/stop_start_test.go (about)

     1  package jobs
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/daticahealth/cli/commands/services"
     9  	"github.com/daticahealth/cli/test"
    10  )
    11  
    12  var jobsStopTests = []struct {
    13  	jobID     string
    14  	svcName   string
    15  	pod       string
    16  	expectErr bool
    17  }{
    18  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", test.SvcLabel, test.Pod, false},
    19  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", test.SvcLabelAlt, test.Pod, true},
    20  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", "database-1", test.Pod, true},
    21  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", "invalid-svc-name", test.Pod, true},
    22  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", "database-2", "csb01", true},
    23  	{"00000000-0000-0000-0000-aaaaaaaaaaaa", "database-2", test.Pod, true},
    24  }
    25  
    26  func TestJobsStop(t *testing.T) {
    27  	mux, server, baseURL := test.Setup()
    28  	defer test.Teardown(server)
    29  	settings := test.GetSettings(baseURL.String())
    30  
    31  	// The Stop/Start commands do a lookup of a service by its name; the mocked results here help translate the service name to an ID -- which is used to call
    32  	//	the jobs endpoint.
    33  	mux.HandleFunc("/environments/"+test.EnvID+"/services",
    34  		func(w http.ResponseWriter, r *http.Request) {
    35  			test.AssertEquals(t, r.Method, "GET")
    36  			fmt.Fprint(w, fmt.Sprintf(`[{"id":"%s","label":"%s","name":"code","redeployable":false},{"id":"%s","label":"%s","name":"code","redeployable":true},{"id":"%s","label":"database-1","name":"postgresql","redeployable":true},{"id":"%s","label":"database-2","name":"postgresql","redeployable":true}]`, test.SvcIDAlt, test.SvcLabelAlt, test.SvcID, test.SvcLabel, "3", "4"))
    37  		},
    38  	)
    39  	mux.HandleFunc("/environments/"+test.EnvID+"/services/"+test.SvcID+"/jobs",
    40  		func(w http.ResponseWriter, r *http.Request) {
    41  			test.AssertEquals(t, r.Method, "GET")
    42  			fmt.Fprint(w, `[{"id":"1"}]`)
    43  		},
    44  	)
    45  	mux.HandleFunc("/environments/"+test.EnvID+"/services/"+test.SvcID+"/jobs/00000000-0000-0000-0000-aaaaaaaaaaaa/stop",
    46  		func(w http.ResponseWriter, r *http.Request) {
    47  			test.AssertEquals(t, r.Method, "POST")
    48  		},
    49  	)
    50  	mux.HandleFunc("/environments/"+test.EnvID+"/services/"+test.SvcID+"/jobs/00000000-0000-0000-0000-aaaaaaaaaaaa/start",
    51  		func(w http.ResponseWriter, r *http.Request) {
    52  			test.AssertEquals(t, r.Method, "POST")
    53  		},
    54  	)
    55  	for _, data := range jobsStopTests {
    56  		t.Logf("Data: %+v", data)
    57  
    58  		// test
    59  		err := CmdStop(data.jobID, data.svcName, New(settings), services.New(settings), false, &test.FakePrompts{})
    60  
    61  		// assert
    62  		if err != nil != data.expectErr {
    63  			t.Errorf("Unexpected error: %s", err)
    64  			continue
    65  		}
    66  
    67  		err = CmdStart(data.jobID, data.svcName, New(settings), services.New(settings))
    68  		t.Logf("Data: %+v", data)
    69  		t.Logf("Error: %s", err)
    70  		// assert
    71  		if err != nil != data.expectErr {
    72  			t.Errorf("Unexpected error: %s", err)
    73  			continue
    74  		}
    75  	}
    76  }