Skip to content

Commit

Permalink
statemanager: update state file version and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yumex93 authored and linkar-ec2 committed Dec 7, 2018
1 parent 7d7bac0 commit e422544
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 7 deletions.
5 changes: 3 additions & 2 deletions agent/statemanager/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ const (
// 17)
// a) Add 'secrets' field to 'apicontainer.Container'
// b) Add 'ssmsecret' field to 'resources'
// 18) Add 'AvailabilityZone' field to the TaskResponse struct

// 18)
// a) Add 'AvailabilityZone' field to the TaskResponse struct
// b) Add 'asmsecret' field to 'resources'
ECSDataVersion = 18

// ecsDataFile specifies the filename in the ECS_DATADIR
Expand Down
43 changes: 41 additions & 2 deletions agent/statemanager/state_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func TestLoadsDataForPrivateRegistryTask(t *testing.T) {

// verify that the state manager correctly loads ssm secrets related fields in state file
func TestLoadsDataForSecretsTask(t *testing.T) {
cleanup, err := setupWindowsTest(filepath.Join(".", "testdata", "v16", "secrets", "ecs_agent_data.json"))
cleanup, err := setupWindowsTest(filepath.Join(".", "testdata", "v17", "secrets", "ecs_agent_data.json"))
require.Nil(t, err, "Failed to set up test")
defer cleanup()
cfg := &config.Config{DataDir: filepath.Join(".", "testdata", "v16", "secrets")}
cfg := &config.Config{DataDir: filepath.Join(".", "testdata", "v17", "secrets")}
taskEngine := engine.NewTaskEngine(&config.Config{}, nil, nil, nil, nil, dockerstate.NewTaskEngineState(), nil, nil)
var containerInstanceArn, cluster, savedInstanceID string
var sequenceNumber int64
Expand Down Expand Up @@ -287,3 +287,42 @@ func TestLoadsDataForAddingAvailabilityZoneInTask(t *testing.T) {
assert.Equal(t, 1, len(task.Containers))
assert.Equal(t, "us-west-2c", availabilityZone)
}

// verify that the state manager correctly loads asm secrets related fields in state file
func TestLoadsDataForASMSecretsTask(t *testing.T) {
cleanup, err := setupWindowsTest(filepath.Join(".", "testdata", "v18", "secrets", "ecs_agent_data.json"))
require.Nil(t, err, "Failed to set up test")
defer cleanup()
cfg := &config.Config{DataDir: filepath.Join(".", "testdata", "v18", "secrets")}
taskEngine := engine.NewTaskEngine(&config.Config{}, nil, nil, nil, nil, dockerstate.NewTaskEngineState(), nil, nil)
var containerInstanceArn, cluster, savedInstanceID string
var sequenceNumber int64
stateManager, err := statemanager.NewStateManager(cfg,
statemanager.AddSaveable("TaskEngine", taskEngine),
statemanager.AddSaveable("ContainerInstanceArn", &containerInstanceArn),
statemanager.AddSaveable("Cluster", &cluster),
statemanager.AddSaveable("EC2InstanceID", &savedInstanceID),
statemanager.AddSaveable("SeqNum", &sequenceNumber),
)
assert.NoError(t, err)
err = stateManager.Load()
assert.NoError(t, err)
assert.Equal(t, "state-file", cluster)
assert.EqualValues(t, 0, sequenceNumber)
tasks, err := taskEngine.ListTasks()
assert.NoError(t, err)
assert.Equal(t, 1, len(tasks))
task := tasks[0]
assert.Equal(t, "arn:aws:ecs:us-west-2:1234567890:task/33425c99-5db7-45fb-8244-bc94d00661e4", task.Arn)
assert.Equal(t, "secrets-state", task.Family)
assert.Equal(t, 1, len(task.Containers))
container := task.Containers[0]
assert.Equal(t, "container_1", container.Name)
assert.NotNil(t, container.Secrets)
secret := container.Secrets[1]
assert.Equal(t, "ENVIRONMENT_VARIABLE", secret.Type)
assert.Equal(t, "asm-secret", secret.Name)
assert.Equal(t, "us-west-2", secret.Region)
assert.Equal(t, "secret-value-from", secret.ValueFrom)
assert.Equal(t, "asm", secret.Provider)
}
219 changes: 219 additions & 0 deletions agent/statemanager/testdata/v17/secrets/ecs_agent_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"Data": {
"Cluster": "state-file",
"ContainerInstanceArn": "arn:aws:ecs:us-west-2:1234567890:container-instance/46efd519-df3f-4096-8f34-faebb1747752",
"EC2InstanceID": "i-0da29eb1a8a98768b",
"TaskEngine": {
"Tasks": [
{
"Arn": "arn:aws:ecs:us-west-2:1234567890:task/33425c99-5db7-45fb-8244-bc94d00661e4",
"Family": "secrets-state",
"Version": "1",
"Containers": [
{
"Name": "container_1",
"Image": "amazonlinux:1",
"ImageID": "sha256:7f929d2604c7e504a568eac9a2523c1b9e9b15e1fcee4076e1411a552913d08e",
"Command": [
"sleep",
"3600"
],
"Cpu": 0,
"Memory": 512,
"Links": null,
"volumesFrom": [],
"mountPoints": [],
"portMappings": [],
"Essential": true,
"EntryPoint": null,
"environment": {},
"overrides": {
"command": null
},
"dockerConfig": {
"config": "{}",
"hostConfig": "{\"CapAdd\":[],\"CapDrop\":[]}",
"version": "1.17"
},
"registryAuthentication": null,
"secrets": [
{
"name": "ssm-secret",
"valueFrom": "secret-value-from",
"provider": "ssm",
"containerPath": null,
"type": "ENVIRONMENT_VARIABLE",
"region": "us-west-2"
}
],
"LogsAuthStrategy": "",
"desiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"TransitionDependencySet": {
"1": {
"ContainerDependencies": null,
"ResourceDependencies": [
{
"Name": "cgroup",
"RequiredStatus": 1
},
{
"Name": "ssmsecret",
"RequiredStatus": 1
}
]
}
},
"RunDependencies": null,
"IsInternal": "NORMAL",
"ApplyingError": {
"error": "API error (500): Get https://registry-1.docker.io/v2/library/amazonlinux/manifests/1: toomanyrequests: too many failed login attempts for username or IP address\n",
"name": "CannotPullContainerError"
},
"SentStatus": "RUNNING",
"metadataFileUpdated": false,
"KnownExitCode": null,
"KnownPortBindings": null
}
],
"resources": {
"ssmsecret": [
{
"taskARN": "/ecs/33425c99-5db7-45fb-8244-bc94d00661e4",
"executionCredentialsID": "b1a6ede6-1a9f-4ab3-a02e-bd3e51b11244",
"createdAt": "0001-01-01T00:00:00Z",
"desiredStatus": "CREATED",
"knownStatus": "CREATED",
"secretResources": {
"us-west-2": [
{
"name": "ssm-secret",
"valueFrom": "secret-value-from",
"provider": "ssm",
"containerPath": null,
"type": "ENVIRONMENT_VARIABLE",
"region": "us-west-2"
}
]
}
}
],
"cgroup": [
{
"cgroupRoot": "/ecs/33425c99-5db7-45fb-8244-bc94d00661e4",
"cgroupMountPath": "/sys/fs/cgroup",
"createdAt": "0001-01-01T00:00:00Z",
"desiredStatus": "CREATED",
"knownStatus": "CREATED",
"resourceSpec": {
"cpu": {
"shares": 2
}
}
}
]
},
"volumes": [],
"DesiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"KnownTime": "2018-10-04T18:05:49.121835686Z",
"PullStartedAt": "2018-10-04T18:05:34.359798761Z",
"PullStoppedAt": "2018-10-04T18:05:48.445985904Z",
"ExecutionStoppedAt": "0001-01-01T00:00:00Z",
"SentStatus": "RUNNING",
"StartSequenceNumber": 2,
"StopSequenceNumber": 0,
"executionCredentialsID": "b1a6ede6-1a9f-4ab3-a02e-bd3e51b11244",
"ENI": null,
"MemoryCPULimitsEnabled": true,
"PlatformFields": {}
}
],
"IdToContainer": {
"8f5e6e3091f221c876103289ddabcbcdeb64acd7ac7e2d0cf4da2be2be9d8956": {
"DockerId": "8f5e6e3091f221c876103289ddabcbcdeb64acd7ac7e2d0cf4da2be2be9d8956",
"DockerName": "ecs-private-registry-state-1-container1-a68ef4b6e0fba38d3500",
"Container": {
"Name": "container_1",
"Image": "amazonlinux:1",
"ImageID": "sha256:7f929d2604c7e504a568eac9a2523c1b9e9b15e1fcee4076e1411a552913d08e",
"Command": [
"sleep",
"3600"
],
"Cpu": 0,
"Memory": 512,
"Links": null,
"volumesFrom": [],
"mountPoints": [],
"portMappings": [],
"Essential": true,
"EntryPoint": null,
"environment": {},
"overrides": {
"command": null
},
"dockerConfig": {
"config": "{}",
"hostConfig": "{\"CapAdd\":[],\"CapDrop\":[]}",
"version": "1.17"
},
"registryAuthentication": null,
"LogsAuthStrategy": "",
"desiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"TransitionDependencySet": {
"1": {
"ContainerDependencies": null,
"ResourceDependencies": [
{
"Name": "cgroup",
"RequiredStatus": 1
},
{
"Name": "ssmsecret",
"RequiredStatus": 1
},
{
"Name": "asmsecret",
"RequiredStatus": 1
}
]
}
},
"RunDependencies": null,
"IsInternal": "NORMAL",
"ApplyingError": {
"error": "API error (500): Get https://registry-1.docker.io/v2/library/amazonlinux/manifests/1: toomanyrequests: too many failed login attempts for username or IP address\n",
"name": "CannotPullContainerError"
},
"SentStatus": "RUNNING",
"metadataFileUpdated": false,
"KnownExitCode": null,
"KnownPortBindings": null
}
}
},
"IdToTask": {
"8f5e6e3091f221c876103289ddabcbcdeb64acd7ac7e2d0cf4da2be2be9d8956": "arn:aws:ecs:us-west-2:1234567890:task/33425c99-5db7-45fb-8244-bc94d00661e4"
},
"ImageStates": [
{
"Image": {
"ImageID": "sha256:7f929d2604c7e504a568eac9a2523c1b9e9b15e1fcee4076e1411a552913d08e",
"Names": [
"amazonlinux:1"
],
"Size": 165452304
},
"PulledAt": "2018-10-04T18:05:48.445644088Z",
"LastUsedAt": "2018-10-04T18:05:48.445645342Z",
"PullSucceeded": false
}
],
"ENIAttachments": null,
"IPToTask": {}
}
},
"Version": 17
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
{
"Name": "ssmsecret",
"RequiredStatus": 1
},
{
"Name": "asmsecret",
"RequiredStatus": 1
}
]
}
Expand All @@ -93,7 +97,7 @@
"desiredStatus": "CREATED",
"knownStatus": "CREATED",
"secretResources": {
"us-west-2": [
"secret-value-from_us-west-2": [
{
"name": "asm-secret",
"valueFrom": "secret-value-from",
Expand Down Expand Up @@ -244,5 +248,5 @@
"IPToTask": {}
}
},
"Version": 16
}
"Version": 18
}

0 comments on commit e422544

Please sign in to comment.