NO_JIRA: Add function to get container status before making api call (#16116)

This commit is contained in:
Anita Akaeze 2023-02-01 10:48:54 -05:00 committed by GitHub
parent 3fe148891b
commit ccae7fd123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 7 deletions

View File

@ -75,3 +75,10 @@ func ServiceLogContains(t *testing.T, service libservice.Service, target string)
require.NoError(t, err)
return strings.Contains(logs, target)
}
// AssertContainerState validates service container status
func AssertContainerState(t *testing.T, service libservice.Service, state string) {
containerStatus, err := service.GetStatus()
require.NoError(t, err)
require.Equal(t, containerStatus, state, fmt.Sprintf("Expected: %s. Got %s", containerStatus, state))
}

View File

@ -73,17 +73,22 @@ func (g ConnectContainer) Start() error {
if g.container == nil {
return fmt.Errorf("container has not been initialized")
}
return g.container.Start(context.Background())
return g.container.Start(g.ctx)
}
func (c ConnectContainer) Terminate() error {
return cluster.TerminateContainer(c.ctx, c.container, true)
func (g ConnectContainer) Terminate() error {
return cluster.TerminateContainer(g.ctx, g.container, true)
}
func (g ConnectContainer) GetAdminAddr() (string, int) {
return "localhost", g.adminPort
}
func (g ConnectContainer) GetStatus() (string, error) {
state, err := g.container.State(g.ctx)
return state.Status, err
}
// NewConnectService returns a container that runs envoy sidecar, launched by
// "consul connect envoy", for service name (serviceName) on the specified
// node. The container exposes port serviceBindPort and envoy admin port

View File

@ -54,7 +54,7 @@ func (g exampleContainer) Restart() error {
}
func (g exampleContainer) GetLogs() (string, error) {
rc, err := g.container.Logs(context.Background())
rc, err := g.container.Logs(g.ctx)
if err != nil {
return "", fmt.Errorf("could not get logs for example service %s: %w", g.GetServiceName(), err)
}
@ -90,6 +90,11 @@ func (c exampleContainer) Terminate() error {
return cluster.TerminateContainer(c.ctx, c.container, true)
}
func (c exampleContainer) GetStatus() (string, error) {
state, err := c.container.State(c.ctx)
return state.Status, err
}
func NewExampleService(ctx context.Context, name string, httpPort int, grpcPort int, node libcluster.Agent) (Service, error) {
namePrefix := fmt.Sprintf("%s-service-example-%s", node.GetDatacenter(), name)
containerName := utils.RandName(namePrefix)

View File

@ -80,22 +80,27 @@ func (g gatewayContainer) GetAdminAddr() (string, int) {
}
func (g gatewayContainer) Restart() error {
_, err := g.container.State(context.Background())
_, err := g.container.State(g.ctx)
if err != nil {
return fmt.Errorf("error get gateway state %s", err)
}
err = g.container.Stop(context.Background(), nil)
err = g.container.Stop(g.ctx, nil)
if err != nil {
return fmt.Errorf("error stop gateway %s", err)
}
err = g.container.Start(context.Background())
err = g.container.Start(g.ctx)
if err != nil {
return fmt.Errorf("error start gateway %s", err)
}
return nil
}
func (g gatewayContainer) GetStatus() (string, error) {
state, err := g.container.State(g.ctx)
return state.Status, err
}
func NewGatewayService(ctx context.Context, name string, kind string, node libcluster.Agent) (Service, error) {
nodeConfig := node.GetConfig()
if nodeConfig.ScratchDir == "" {

View File

@ -15,4 +15,5 @@ type Service interface {
Start() (err error)
Terminate() error
Restart() error
GetStatus() (string, error)
}

View File

@ -31,6 +31,7 @@ func TestBasicConnectService(t *testing.T) {
libassert.AssertUpstreamEndpointStatus(t, adminPort, "static-server.default", "HEALTHY", 1)
libassert.GetEnvoyListenerTCPFilters(t, adminPort)
libassert.AssertContainerState(t, clientService, "running")
libassert.HTTPServiceEchoes(t, "localhost", port, "")
}