Skip to content

Commit

Permalink
adding FIS register handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mye956 committed Aug 7, 2024
1 parent 5c2d1bf commit 25e6395
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 0 deletions.
67 changes: 67 additions & 0 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/aws/amazon-ecs-agent/ecs-agent/metrics"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds"
fault "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/fault/v1/handlers"
faulttype "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/fault/v1/types"
tp "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/taskprotection/v1/handlers"
tmdsv1 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v1"
tmdsv2 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v2"
Expand Down Expand Up @@ -90,6 +92,9 @@ func taskServerSetup(
agentAPIV1HandlersSetup(muxRouter, state, credentialsManager, cluster, tmdsAgentState,
taskProtectionClientFactory, metricsFactory)

// TODO: Future PR to pass in TMDS server router once all of the handlers have been implemented.
registerFaultHandlers(nil, tmdsAgentState, metricsFactory)

return tmds.NewServer(auditLogger,
tmds.WithHandler(muxRouter),
tmds.WithListenAddress(tmds.AddressIPv4()),
Expand Down Expand Up @@ -184,6 +189,68 @@ func agentAPIV1HandlersSetup(
Methods("GET")
}

// registerFISHandlers adds handlers for fault endpoints
// TODO: Will need to be called in taskServerSetup once all of the handlers have been implemented
func registerFaultHandlers(
muxRouter *mux.Router,
agentState *v4.TMDSAgentState,
metricsFactory metrics.EntryFactory,
) {
handler := fault.FaultHandler{
AgentState: agentState,
MetricsFactory: metricsFactory,
}

if muxRouter == nil {
seelog.Warn("Error, unable to add fault handlers to TMDS")
return
}

// Setting up handler endpoints for network blackhole port fault injections
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.BlackHolePortFaultType),
handler.StartNetworkBlackholePort(),
).Methods("PUT")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.BlackHolePortFaultType),
handler.StopBlackHolePort(),
).Methods("DELETE")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.BlackHolePortFaultType),
handler.CheckBlackHolePortStatus(),
).Methods("GET")

// Setting up handler endpoints for network latency fault injections
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.LatencyFaultType),
handler.StartLatency(),
).Methods("PUT")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.LatencyFaultType),
handler.StopLatency(),
).Methods("DELETE")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.LatencyFaultType),
handler.CheckLatencyStatus(),
).Methods("GET")

// Setting up handler endpoints for network packet loss fault injections
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.PacketLossFaultType),
handler.StartPacketLoss(),
).Methods("PUT")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.PacketLossFaultType),
handler.StopPacketLoss(),
).Methods("DELETE")
muxRouter.HandleFunc(
fault.FaultNetworkFaultPath(faulttype.PacketLossFaultType),
handler.CheckPacketLossStatus(),
).Methods("GET")

seelog.Debug("Successfully set up FIS TMDS handlers")
}

// ServeTaskHTTPEndpoint serves task/container metadata, task/container stats, IAM Role Credentials, and Agent APIs
// for tasks being managed by the agent.
func ServeTaskHTTPEndpoint(
Expand Down
116 changes: 116 additions & 0 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/config"
mock_dockerstate "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate/mocks"
v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"
agentV4 "github.com/aws/amazon-ecs-agent/agent/handlers/v4"
mock_stats "github.com/aws/amazon-ecs-agent/agent/stats/mock"
apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status"
mock_ecs "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/mocks"
Expand All @@ -42,15 +43,18 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks"
mock_audit "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/mocks"
mock_metrics "github.com/aws/amazon-ecs-agent/ecs-agent/metrics/mocks"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/aws/amazon-ecs-agent/ecs-agent/stats"
faulttype "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/fault/v1/types"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
tp "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/taskprotection/v1/handlers"
tptypes "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/taskprotection/v1/types"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
tmdsv1 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v1"
v2 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v2"
v4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state"
"github.com/gorilla/mux"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -107,6 +111,7 @@ const (
privateDNSName = "ip-172-31-47-69.us-west-2.compute.internal"
subnetGatewayIpv4Address = "172.31.32.1/20"
taskCredentialsID = "taskCredentialsId"
endpointId = "endpointId"
)

var (
Expand Down Expand Up @@ -3561,3 +3566,114 @@ func TestUpdateTaskProtection(t *testing.T) {
},
}))
}

func TestRegisterHandler(t *testing.T) {
tcs := []struct {
name string
expectedStatusCode int
fault string
method string
}{
{
name: "PUT blackholeport",
expectedStatusCode: http.StatusOK,
fault: faulttype.BlackHolePortFaultType,
method: "PUT",
},
{
name: "DELETE blackholeport",
expectedStatusCode: http.StatusOK,
fault: faulttype.BlackHolePortFaultType,
method: "DELETE",
},
{
name: "GET blackholeport",
expectedStatusCode: http.StatusOK,
fault: faulttype.BlackHolePortFaultType,
method: "GET",
},
{
name: "PUT latency",
expectedStatusCode: http.StatusOK,
fault: faulttype.LatencyFaultType,
method: "PUT",
},
{
name: "DELETE latency",
expectedStatusCode: http.StatusOK,
fault: faulttype.LatencyFaultType,
method: "DELETE",
},
{
name: "GET latency",
expectedStatusCode: http.StatusOK,
fault: faulttype.BlackHolePortFaultType,
method: "GET",
},
{
name: "PUT packet loss",
expectedStatusCode: http.StatusOK,
fault: faulttype.PacketLossFaultType,
method: "PUT",
},
{
name: "DELETE packet loss",
expectedStatusCode: http.StatusOK,
fault: faulttype.PacketLossFaultType,
method: "DELETE",
},
{
name: "GET packet loss",
expectedStatusCode: http.StatusOK,
fault: faulttype.PacketLossFaultType,
method: "GET",
},
{
name: "PUT unknown",
expectedStatusCode: http.StatusNotFound,
fault: "unknown",
method: "PUT",
},
{
name: "DELETE unknown",
expectedStatusCode: http.StatusNotFound,
fault: "unknown",
method: "DELETE",
},
{
name: "GET unknown",
expectedStatusCode: http.StatusNotFound,
fault: "unknown",
method: "GET",
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// Mocks
ctrl := gomock.NewController(t)
defer ctrl.Finish()

state := mock_dockerstate.NewMockTaskEngineState(ctrl)
statsEngine := mock_stats.NewMockEngine(ctrl)
ecsClient := mock_ecs.NewMockECSClient(ctrl)

agentState := agentV4.NewTMDSAgentState(state, statsEngine, ecsClient, clusterName, availabilityzone, vpcID, containerInstanceArn)
metricsFactory := mock_metrics.NewMockEntryFactory(ctrl)

router := mux.NewRouter()

registerFaultHandlers(router, agentState, metricsFactory)
var requestBody io.Reader
req, err := http.NewRequest(tc.method, fmt.Sprintf("/api/%s/fault/v1/%s", endpointId, tc.fault),
requestBody)
require.NoError(t, err)

// Send the request and record the response
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)

assert.Equal(t, recorder.Code, tc.expectedStatusCode)
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 25e6395

Please sign in to comment.