package cli import ( "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" ) func TestHandlePing(t *testing.T) { tests := []struct { name string runOutput []byte runErr error expectedArgs []string expectedBody []AbraAppService expectedCode int }{ { name: "success", runOutput: []byte(` {"app":{ "chaos":"false", "created":"3 days ago", "image":"image:4.1.0-22", "ports":"1880/tcp", "service":"app", "state":"running", "status":"Up 3 days (healthy)", "version":"1.0.0+4.1.0-22"} } `), expectedArgs: []string{"app", "ps", "myapp", "-m"}, expectedBody: []AbraAppService{{ Service: "app", Chaos: false, Created: "3 days ago", Image: "image:4.1.0-22", Ports: "1880/tcp", State: "running", Status: "Up 3 days (healthy)", Version: "1.0.0+4.1.0-22", }}, expectedCode: http.StatusOK, }, { name: "success sorted", runOutput: []byte(` {"app":{ "chaos":"false", "created":"3 days ago", "image":"image:4.1.0-22", "ports":"1880/tcp", "service":"app", "state":"running", "status":"Up 3 days (healthy)", "version":"1.0.0+4.1.0-22" }, "base": { "chaos":"false", "created":"3 days ago", "image":"image:6.1.0", "ports":"1880/tcp", "service":"base", "state":"running", "status":"Up 3 days (healthy)", "version":"1.0.0+6.1.0" } }`), expectedArgs: []string{"app", "ps", "myapp", "-m"}, expectedBody: []AbraAppService{{ Service: "app", Chaos: false, Created: "3 days ago", Image: "image:4.1.0-22", Ports: "1880/tcp", State: "running", Status: "Up 3 days (healthy)", Version: "1.0.0+4.1.0-22", }, { Service: "base", Chaos: false, Created: "3 days ago", Image: "image:6.1.0", Ports: "1880/tcp", State: "running", Status: "Up 3 days (healthy)", Version: "1.0.0+6.1.0", }}, expectedCode: http.StatusOK, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockCmd := new(MockCommandRunner) args := []interface{}{"abra"} for _, arg := range tt.expectedArgs { args = append(args, arg) } mockCmd. On("Run", args...). Return(tt.runOutput, tt.runErr) h := &abraHandler{cmd: mockCmd} req := httptest.NewRequest(http.MethodPost, "/", nil) rec := httptest.NewRecorder() h.handleGetAppServices(rec, req, "myapp") assert.Equal(t, tt.expectedCode, rec.Code) mockCmd.AssertExpectations(t) }) } }