Skip to content

Commit f903046

Browse files
committed
Check if ECS data directory exists within agent container
1 parent 6516538 commit f903046

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

agent/data/client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package data
1515

1616
import (
17+
"fmt"
18+
"os"
1719
"path/filepath"
1820
"sync"
1921

@@ -39,6 +41,7 @@ const (
3941
resAttachmentsBucketName = "resattachments"
4042
metadataBucketName = "metadata"
4143
emptyAgentVersionMsg = "No version info available in boltDB. Either this is a fresh instance, or we were using state file to persist data. Transformer not applicable."
44+
notDirectoryErrorMsg = "path %s is not a valid directory"
4245
)
4346

4447
var (
@@ -53,6 +56,7 @@ var (
5356
resAttachmentsBucketName,
5457
metadataBucketName,
5558
}
59+
dirExists = checkDirectoryExists
5660
)
5761

5862
// Client specifies the data management interface to persist and manage various kinds of data in the agent.
@@ -133,7 +137,15 @@ func NewWithSetup(dataDir string) (Client, error) {
133137
// setup initiates the boltdb client and makes sure the buckets we use and transformer are created, and
134138
// registers transformation functions to transformer.
135139
func setup(dataDir string) (*client, error) {
140+
// Check if the directory is valid
141+
err := dirExists(dataDir)
142+
if err != nil {
143+
return nil, err
144+
}
136145
db, err := bolt.Open(filepath.Join(dataDir, dbName), dbMode, nil)
146+
if err != nil {
147+
return nil, err
148+
}
137149
err = db.Update(func(tx *bolt.Tx) error {
138150
for _, b := range buckets {
139151
_, err = tx.CreateBucketIfNotExists([]byte(b))
@@ -167,3 +179,15 @@ func setup(dataDir string) (*client, error) {
167179
func (c *client) Close() error {
168180
return c.DB.Close()
169181
}
182+
183+
// Checks if the data directory exists within the agent container
184+
func checkDirectoryExists(path string) error {
185+
fileInfo, err := os.Stat(path)
186+
if err != nil {
187+
return err
188+
}
189+
if !fileInfo.IsDir() {
190+
return fmt.Errorf(notDirectoryErrorMsg, path)
191+
}
192+
return nil
193+
}

agent/data/client_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717
package data
1818

1919
import (
20+
"fmt"
2021
"path/filepath"
2122
"testing"
2223

2324
generaldata "github.com/aws/amazon-ecs-agent/ecs-agent/data"
2425
"github.com/aws/amazon-ecs-agent/ecs-agent/modeltransformer"
2526

27+
"github.com/stretchr/testify/assert"
2628
"github.com/stretchr/testify/require"
2729
bolt "go.etcd.io/bbolt"
2830
)
2931

32+
const (
33+
statPathNotFoundErr = "stat %s: no such file or directory"
34+
)
35+
3036
func newTestClient(t *testing.T) Client {
3137
testDir := t.TempDir()
3238

@@ -56,3 +62,43 @@ func newTestClient(t *testing.T) Client {
5662
})
5763
return testClient
5864
}
65+
66+
func TestSetupInvalidDirectoryErrors(t *testing.T) {
67+
originalDirExists := dirExists
68+
defer func() {
69+
dirExists = originalDirExists
70+
}()
71+
tcs := []struct {
72+
name string
73+
dirPath string
74+
mockDirExists func(string) error
75+
expectedError error
76+
}{
77+
{
78+
name: "path not found",
79+
dirPath: "/path/",
80+
mockDirExists: func(path string) error {
81+
return fmt.Errorf(statPathNotFoundErr, path)
82+
},
83+
expectedError: fmt.Errorf(statPathNotFoundErr, "/path/"),
84+
},
85+
{
86+
name: "path is not a directory",
87+
dirPath: "/data/blah.txt",
88+
mockDirExists: func(path string) error {
89+
return fmt.Errorf(notDirectoryErrorMsg, path)
90+
},
91+
expectedError: fmt.Errorf(notDirectoryErrorMsg, "/data/blah.txt"),
92+
},
93+
}
94+
95+
for _, tc := range tcs {
96+
t.Run(tc.name, func(t *testing.T) {
97+
dirExists = tc.mockDirExists
98+
99+
_, err := setup(tc.dirPath)
100+
101+
assert.Equal(t, tc.expectedError, err)
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)