mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
NO_JIRA: Add function to get container status before making api call (#16116)
This commit is contained in:
parent
1fbfb5905b
commit
08a19e532d
@ -75,3 +75,10 @@ func ServiceLogContains(t *testing.T, service libservice.Service, target string)
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return strings.Contains(logs, target)
|
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))
|
||||||
|
}
|
||||||
|
@ -73,17 +73,22 @@ func (g ConnectContainer) Start() error {
|
|||||||
if g.container == nil {
|
if g.container == nil {
|
||||||
return fmt.Errorf("container has not been initialized")
|
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 {
|
func (g ConnectContainer) Terminate() error {
|
||||||
return cluster.TerminateContainer(c.ctx, c.container, true)
|
return cluster.TerminateContainer(g.ctx, g.container, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g ConnectContainer) GetAdminAddr() (string, int) {
|
func (g ConnectContainer) GetAdminAddr() (string, int) {
|
||||||
return "localhost", g.adminPort
|
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
|
// NewConnectService returns a container that runs envoy sidecar, launched by
|
||||||
// "consul connect envoy", for service name (serviceName) on the specified
|
// "consul connect envoy", for service name (serviceName) on the specified
|
||||||
// node. The container exposes port serviceBindPort and envoy admin port
|
// node. The container exposes port serviceBindPort and envoy admin port
|
||||||
|
@ -54,7 +54,7 @@ func (g exampleContainer) Restart() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g exampleContainer) GetLogs() (string, error) {
|
func (g exampleContainer) GetLogs() (string, error) {
|
||||||
rc, err := g.container.Logs(context.Background())
|
rc, err := g.container.Logs(g.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("could not get logs for example service %s: %w", g.GetServiceName(), err)
|
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)
|
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) {
|
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)
|
namePrefix := fmt.Sprintf("%s-service-example-%s", node.GetDatacenter(), name)
|
||||||
containerName := utils.RandName(namePrefix)
|
containerName := utils.RandName(namePrefix)
|
||||||
|
@ -80,22 +80,27 @@ func (g gatewayContainer) GetAdminAddr() (string, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g gatewayContainer) Restart() error {
|
func (g gatewayContainer) Restart() error {
|
||||||
_, err := g.container.State(context.Background())
|
_, err := g.container.State(g.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error get gateway state %s", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("error stop gateway %s", err)
|
return fmt.Errorf("error stop gateway %s", err)
|
||||||
}
|
}
|
||||||
err = g.container.Start(context.Background())
|
err = g.container.Start(g.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error start gateway %s", err)
|
return fmt.Errorf("error start gateway %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
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) {
|
func NewGatewayService(ctx context.Context, name string, kind string, node libcluster.Agent) (Service, error) {
|
||||||
nodeConfig := node.GetConfig()
|
nodeConfig := node.GetConfig()
|
||||||
if nodeConfig.ScratchDir == "" {
|
if nodeConfig.ScratchDir == "" {
|
||||||
|
@ -15,4 +15,5 @@ type Service interface {
|
|||||||
Start() (err error)
|
Start() (err error)
|
||||||
Terminate() error
|
Terminate() error
|
||||||
Restart() error
|
Restart() error
|
||||||
|
GetStatus() (string, error)
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ func TestBasicConnectService(t *testing.T) {
|
|||||||
libassert.AssertUpstreamEndpointStatus(t, adminPort, "static-server.default", "HEALTHY", 1)
|
libassert.AssertUpstreamEndpointStatus(t, adminPort, "static-server.default", "HEALTHY", 1)
|
||||||
libassert.GetEnvoyListenerTCPFilters(t, adminPort)
|
libassert.GetEnvoyListenerTCPFilters(t, adminPort)
|
||||||
|
|
||||||
|
libassert.AssertContainerState(t, clientService, "running")
|
||||||
libassert.HTTPServiceEchoes(t, "localhost", port, "")
|
libassert.HTTPServiceEchoes(t, "localhost", port, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user