Skip to content

Commit

Permalink
Add support for Agent connection to mpi regardless of NGINX Instance …
Browse files Browse the repository at this point in the history
…status (#916)

* chore: allow commander service to connect irrespective of nginx availability

* chore: unit test

* chore: integration test

* chore: refactored

* chore: remove extra code

* chore: updated build target

* chore: updating variables

* chore: separated nginx less test from official agent testing
  • Loading branch information
RRashmit authored Nov 8, 2024
1 parent 2a9548f commit 399bf5c
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 90 deletions.
4 changes: 1 addition & 3 deletions internal/command/command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ func (cs *CommandService) CreateConnection(
resource *mpi.Resource,
) (*mpi.CreateConnectionResponse, error) {
correlationID := logger.GetCorrelationID(ctx)

// Only send a resource update message if instances other than the agent instance are found
if len(resource.GetInstances()) <= 1 {
return nil, errors.New("waiting for data plane instances to be found before sending create connection request")
slog.InfoContext(ctx, "No Data Plane Instance found")
}

request := &mpi.CreateConnectionRequest{
Expand Down
17 changes: 17 additions & 0 deletions internal/command/command_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ func TestCommandService_UpdateDataPlaneStatusSubscribeError(t *testing.T) {
}
}

func TestCommandService_CreateConnection(t *testing.T) {
ctx := context.Background()
commandServiceClient := &v1fakes.FakeCommandServiceClient{}

commandService := NewCommandService(
ctx,
commandServiceClient,
types.AgentConfig(),
make(chan *mpi.ManagementPlaneRequest),
)

// connection created when no nginx instance found
resource := protos.GetHostResource()
_, err := commandService.CreateConnection(ctx, resource)
require.NoError(t, err)
}

func TestCommandService_UpdateDataPlaneHealth(t *testing.T) {
ctx := context.Background()
commandServiceClient := &v1fakes.FakeCommandServiceClient{}
Expand Down
34 changes: 34 additions & 0 deletions test/docker/nginxless-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -euxo pipefail

handle_term()
{
echo "received TERM signal"
echo "stopping nginx-agent ..."
kill -TERM "${agent_pid}" 2>/dev/null
}

trap 'handle_term' TERM

cat /etc/nginx-agent/nginx-agent.conf;

# start nginx-agent, pass args
echo "starting nginx-agent ..."
nginx-agent "$@" &

agent_pid=$!

if [ $? != 0 ]; then
echo "couldn't start the agent, please check the log file"
exit 1
fi

wait_term()
{
wait ${agent_pid}
}

wait_term

echo "nginx-agent process has stopped, exiting."
69 changes: 69 additions & 0 deletions test/helpers/test_containers_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,75 @@ func StartAgentlessContainer(
return container
}

func StartNginxLessContainer(
ctx context.Context,
tb testing.TB,
containerNetwork *testcontainers.DockerNetwork,
parameters *Parameters,
) testcontainers.Container {
tb.Helper()

packageName := Env(tb, "PACKAGE_NAME")
packageRepo := Env(tb, "PACKAGES_REPO")
baseImage := Env(tb, "BASE_IMAGE")
buildTarget := Env(tb, "BUILD_TARGET")
osRelease := Env(tb, "OS_RELEASE")
osVersion := Env(tb, "OS_VERSION")
dockerfilePath := Env(tb, "DOCKERFILE_PATH")
tag := Env(tb, "TAG")
imagePath := Env(tb, "IMAGE_PATH")
containerRegistry := Env(tb, "CONTAINER_NGINX_IMAGE_REGISTRY")

req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: "../../",
Dockerfile: dockerfilePath,
KeepImage: false,
PrintBuildLog: true,
BuildArgs: map[string]*string{
"PACKAGE_NAME": ToPtr(packageName),
"PACKAGES_REPO": ToPtr(packageRepo),
"BASE_IMAGE": ToPtr(baseImage),
"OS_RELEASE": ToPtr(osRelease),
"OS_VERSION": ToPtr(osVersion),
"ENTRY_POINT": ToPtr("./test/docker/nginxless-entrypoint.sh"),
"CONTAINER_NGINX_IMAGE_REGISTRY": ToPtr(containerRegistry),
"IMAGE_PATH": ToPtr(imagePath),
"TAG": ToPtr(tag),
},
BuildOptionsModifier: func(buildOptions *types.ImageBuildOptions) {
buildOptions.Target = buildTarget
},
},
ExposedPorts: []string{"9094/tcp"},
WaitingFor: wait.ForLog(parameters.LogMessage),
Networks: []string{
containerNetwork.Name,
},
NetworkAliases: map[string][]string{
containerNetwork.Name: {
"agent",
},
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: parameters.NginxAgentConfigPath,
ContainerFilePath: "/etc/nginx-agent/nginx-agent.conf",
FileMode: configFilePermissions,
},
},
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})

require.NoError(tb, err)

return container
}

func StartMockManagementPlaneGrpcContainer(
ctx context.Context,
tb testing.TB,
Expand Down
Loading

0 comments on commit 399bf5c

Please sign in to comment.