Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get-readiness-health-check command [v8] #3391

Open
wants to merge 2 commits into
base: v8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions actor/v7action/process_readiness_health_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package v7action

import (
"sort"

"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)

type ProcessReadinessHealthCheck struct {
ProcessType string
HealthCheckType constant.HealthCheckType
Endpoint string
InvocationTimeout int64
Interval int64
}

type ProcessReadinessHealthChecks []ProcessReadinessHealthCheck

func (phs ProcessReadinessHealthChecks) Sort() {
sort.Slice(phs, func(i int, j int) bool {
var iScore int
var jScore int

switch phs[i].ProcessType {
case constant.ProcessTypeWeb:
iScore = 0
default:
iScore = 1
}

switch phs[j].ProcessType {
case constant.ProcessTypeWeb:
jScore = 0
default:
jScore = 1
}

if iScore == 1 && jScore == 1 {
return phs[i].ProcessType < phs[j].ProcessType
}
return iScore < jScore
})
}

func (actor Actor) GetApplicationProcessReadinessHealthChecksByNameAndSpace(appName string, spaceGUID string) ([]ProcessReadinessHealthCheck, Warnings, error) {
app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
if err != nil {
return nil, allWarnings, err
}

ccv3Processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(app.GUID)
allWarnings = append(allWarnings, Warnings(warnings)...)
if err != nil {
return nil, allWarnings, err
}

var processReadinessHealthChecks ProcessReadinessHealthChecks
for _, ccv3Process := range ccv3Processes {
processReadinessHealthCheck := ProcessReadinessHealthCheck{
ProcessType: ccv3Process.Type,
HealthCheckType: ccv3Process.ReadinessHealthCheckType,
Endpoint: ccv3Process.ReadinessHealthCheckEndpoint,
InvocationTimeout: ccv3Process.ReadinessHealthCheckInvocationTimeout,
Interval: ccv3Process.ReadinessHealthCheckInterval,
}
processReadinessHealthChecks = append(processReadinessHealthChecks, processReadinessHealthCheck)
}

processReadinessHealthChecks.Sort()

return processReadinessHealthChecks, allWarnings, nil
}
176 changes: 176 additions & 0 deletions actor/v7action/process_readiness_health_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package v7action_test

import (
"errors"

"code.cloudfoundry.org/cli/actor/actionerror"
. "code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/resources"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Process Readiness Health Check Actions", func() {
var (
actor *Actor
fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
)

BeforeEach(func() {
fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
})

Describe("ProcessReadinessHealthChecks", func() {
var readinessHealthChecks ProcessReadinessHealthChecks

BeforeEach(func() {
readinessHealthChecks = ProcessReadinessHealthChecks{
{
ProcessType: "worker",
HealthCheckType: constant.Process,
},
{
ProcessType: "console",
HealthCheckType: constant.Process,
},
{
ProcessType: constant.ProcessTypeWeb,
HealthCheckType: constant.HTTP,
Endpoint: constant.ProcessHealthCheckEndpointDefault,
},
}
})

Describe("Sort", func() {
It("sorts readiness health checks with web first and then alphabetically sorted", func() {
readinessHealthChecks.Sort()
Expect(readinessHealthChecks[0].ProcessType).To(Equal(constant.ProcessTypeWeb))
Expect(readinessHealthChecks[1].ProcessType).To(Equal("console"))
Expect(readinessHealthChecks[2].ProcessType).To(Equal("worker"))
})
})
})

Describe("GetApplicationProcessReadinessHealthChecksByNameAndSpace", func() {
var (
warnings Warnings
executeErr error
processReadinessHealthChecks []ProcessReadinessHealthCheck
)

JustBeforeEach(func() {
processReadinessHealthChecks, warnings, executeErr = actor.GetApplicationProcessReadinessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
})

When("application does not exist", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetApplicationsReturns(
[]resources.Application{},
ccv3.Warnings{"some-warning"},
nil,
)
})

It("returns the error and warnings", func() {
Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: "some-app-name"}))
Expect(warnings).To(ConsistOf("some-warning"))
})
})

When("getting application returns an error", func() {
var expectedErr error

BeforeEach(func() {
expectedErr = errors.New("some-error")
fakeCloudControllerClient.GetApplicationsReturns(
[]resources.Application{},
ccv3.Warnings{"some-warning"},
expectedErr,
)
})

It("returns the error and warnings", func() {
Expect(executeErr).To(Equal(expectedErr))
Expect(warnings).To(ConsistOf("some-warning"))
})
})

When("application exists", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetApplicationsReturns(
[]resources.Application{
{
GUID: "some-app-guid",
},
},
ccv3.Warnings{"some-warning"},
nil,
)
})

When("getting application processes returns an error", func() {
var expectedErr error

BeforeEach(func() {
expectedErr = errors.New("some-error")
fakeCloudControllerClient.GetApplicationProcessesReturns(
[]resources.Process{},
ccv3.Warnings{"some-process-warning"},
expectedErr,
)
})

It("returns the error and warnings", func() {
Expect(executeErr).To(Equal(expectedErr))
Expect(warnings).To(ConsistOf("some-warning", "some-process-warning"))
})
})

When("application has processes", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetApplicationProcessesReturns(
[]resources.Process{
{
GUID: "process-guid-1",
Type: "process-type-1",
ReadinessHealthCheckType: "readiness-health-check-type-1",
ReadinessHealthCheckEndpoint: "readiness-health-check-endpoint-1",
ReadinessHealthCheckInvocationTimeout: 42,
},
{
GUID: "process-guid-2",
Type: "process-type-2",
ReadinessHealthCheckType: "readiness-health-check-type-2",
ReadinessHealthCheckInvocationTimeout: 0,
},
},
ccv3.Warnings{"some-process-warning"},
nil,
)
})

It("returns health checks", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("some-warning", "some-process-warning"))
Expect(processReadinessHealthChecks).To(Equal([]ProcessReadinessHealthCheck{
{
ProcessType: "process-type-1",
HealthCheckType: "readiness-health-check-type-1",
Endpoint: "readiness-health-check-endpoint-1",
InvocationTimeout: 42,
},
{
ProcessType: "process-type-2",
HealthCheckType: "readiness-health-check-type-2",
InvocationTimeout: 0,
},
}))
})
})
})
})
})
72 changes: 48 additions & 24 deletions api/cloudcontroller/ccv3/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ var _ = Describe("Process", func() {
"endpoint": "/health",
"invocation_timeout": 42
}
},
"readiness_health_check": {
"type": "http",
"data": {
"interval": 9,
"endpoint": "/foo",
"invocation_timeout": 2
}
}
}`
server.AppendHandlers(
Expand All @@ -70,18 +78,22 @@ var _ = Describe("Process", func() {
Expect(err).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("this is a warning"))
Expect(process).To(MatchAllFields(Fields{
"GUID": Equal("process-1-guid"),
"Type": Equal("some-type"),
"AppGUID": Equal("some-app-guid"),
"Command": Equal(types.FilteredString{IsSet: true, Value: "start-command-1"}),
"Instances": Equal(types.NullInt{Value: 22, IsSet: true}),
"MemoryInMB": Equal(types.NullUint64{Value: 32, IsSet: true}),
"DiskInMB": Equal(types.NullUint64{Value: 1024, IsSet: true}),
"LogRateLimitInBPS": Equal(types.NullInt{Value: 512, IsSet: true}),
"HealthCheckType": Equal(constant.HTTP),
"HealthCheckEndpoint": Equal("/health"),
"HealthCheckInvocationTimeout": BeEquivalentTo(42),
"HealthCheckTimeout": BeEquivalentTo(90),
"GUID": Equal("process-1-guid"),
"Type": Equal("some-type"),
"AppGUID": Equal("some-app-guid"),
"Command": Equal(types.FilteredString{IsSet: true, Value: "start-command-1"}),
"Instances": Equal(types.NullInt{Value: 22, IsSet: true}),
"MemoryInMB": Equal(types.NullUint64{Value: 32, IsSet: true}),
"DiskInMB": Equal(types.NullUint64{Value: 1024, IsSet: true}),
"LogRateLimitInBPS": Equal(types.NullInt{Value: 512, IsSet: true}),
"HealthCheckType": Equal(constant.HTTP),
"HealthCheckEndpoint": Equal("/health"),
"HealthCheckInvocationTimeout": BeEquivalentTo(42),
"HealthCheckTimeout": BeEquivalentTo(90),
"ReadinessHealthCheckType": Equal(constant.HTTP),
"ReadinessHealthCheckEndpoint": Equal("/foo"),
"ReadinessHealthCheckInvocationTimeout": BeEquivalentTo(2),
"ReadinessHealthCheckInterval": BeEquivalentTo(9),
}))
})
})
Expand Down Expand Up @@ -317,6 +329,14 @@ var _ = Describe("Process", func() {
"endpoint": "/health",
"invocation_timeout": 42
}
},
"readiness_health_check": {
"type": "http",
"data": {
"interval": 9,
"endpoint": "/foo",
"invocation_timeout": 2
}
}
}`
server.AppendHandlers(
Expand All @@ -331,18 +351,22 @@ var _ = Describe("Process", func() {
Expect(err).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("this is a warning"))
Expect(process).To(MatchAllFields(Fields{
"GUID": Equal("process-1-guid"),
"Type": Equal("some-type"),
"AppGUID": Equal("some-app-guid"),
"Command": Equal(types.FilteredString{IsSet: true, Value: "start-command-1"}),
"Instances": Equal(types.NullInt{Value: 22, IsSet: true}),
"MemoryInMB": Equal(types.NullUint64{Value: 32, IsSet: true}),
"DiskInMB": Equal(types.NullUint64{Value: 1024, IsSet: true}),
"LogRateLimitInBPS": Equal(types.NullInt{Value: 64, IsSet: true}),
"HealthCheckType": Equal(constant.HTTP),
"HealthCheckEndpoint": Equal("/health"),
"HealthCheckInvocationTimeout": BeEquivalentTo(42),
"HealthCheckTimeout": BeEquivalentTo(90),
"GUID": Equal("process-1-guid"),
"Type": Equal("some-type"),
"AppGUID": Equal("some-app-guid"),
"Command": Equal(types.FilteredString{IsSet: true, Value: "start-command-1"}),
"Instances": Equal(types.NullInt{Value: 22, IsSet: true}),
"MemoryInMB": Equal(types.NullUint64{Value: 32, IsSet: true}),
"DiskInMB": Equal(types.NullUint64{Value: 1024, IsSet: true}),
"LogRateLimitInBPS": Equal(types.NullInt{Value: 64, IsSet: true}),
"HealthCheckType": Equal(constant.HTTP),
"HealthCheckEndpoint": Equal("/health"),
"HealthCheckInvocationTimeout": BeEquivalentTo(42),
"HealthCheckTimeout": BeEquivalentTo(90),
"ReadinessHealthCheckType": Equal(constant.HTTP),
"ReadinessHealthCheckEndpoint": Equal("/foo"),
"ReadinessHealthCheckInvocationTimeout": BeEquivalentTo(2),
"ReadinessHealthCheckInterval": BeEquivalentTo(9),
}))
})
})
Expand Down
1 change: 1 addition & 0 deletions command/common/command_list_v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type commandList struct {
FeatureFlag v7.FeatureFlagCommand `command:"feature-flag" description:"Retrieve an individual feature flag with status"`
FeatureFlags v7.FeatureFlagsCommand `command:"feature-flags" description:"Retrieve list of feature flags with status"`
GetHealthCheck v7.GetHealthCheckCommand `command:"get-health-check" description:"Show the type of health check performed on an app"`
GetReadinessHealthCheck v7.GetReadinessHealthCheckCommand `command:"get-readiness-health-check" description:"Show the type of readiness health check performed on an app"`
Help HelpCommand `command:"help" alias:"h" description:"Show help"`
InstallPlugin InstallPluginCommand `command:"install-plugin" description:"Install CLI plugin"`
IsolationSegments v7.IsolationSegmentsCommand `command:"isolation-segments" description:"List all isolation segments"`
Expand Down
3 changes: 2 additions & 1 deletion command/common/internal/help_all_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var HelpCategoryList = []HelpCategory{
{"env", "set-env", "unset-env"},
{"stacks", "stack"},
{"copy-source", "create-app-manifest"},
{"get-health-check", "set-health-check", "enable-ssh", "disable-ssh", "ssh-enabled", "ssh"},
{"get-health-check", "set-health-check", "get-readiness-health-check"},
{"enable-ssh", "disable-ssh", "ssh-enabled", "ssh"},
},
},
{
Expand Down
1 change: 1 addition & 0 deletions command/v7/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type Actor interface {
GetApplicationLabels(appName string, spaceGUID string) (map[string]types.NullString, v7action.Warnings, error)
GetApplicationPackages(appName string, spaceGUID string) ([]resources.Package, v7action.Warnings, error)
GetApplicationProcessHealthChecksByNameAndSpace(appName string, spaceGUID string) ([]v7action.ProcessHealthCheck, v7action.Warnings, error)
GetApplicationProcessReadinessHealthChecksByNameAndSpace(appName string, spaceGUID string) ([]v7action.ProcessReadinessHealthCheck, v7action.Warnings, error)
GetApplicationRevisionsDeployed(appGUID string) ([]resources.Revision, v7action.Warnings, error)
GetApplicationRoutes(appGUID string) ([]resources.Route, v7action.Warnings, error)
GetApplicationTasks(appName string, sortOrder v7action.SortOrder) ([]resources.Task, v7action.Warnings, error)
Expand Down
Loading
Loading