Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mock for VolumeDriver interface #4056

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ecs-init/volumes/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package driver

import "github.com/aws/amazon-ecs-agent/ecs-init/volumes/types"

//go:generate mockgen.sh $GOPACKAGE_mock $GOFILE ./mock

// VolumeDriver contains the methods for volume drivers to implement
type VolumeDriver interface {
// Setup is used to add a volume to the driver's state.
// It should be called when the state of a volume is being loaded from storage.
Setup(volumeName string, volume *types.Volume)

// Create mounts the volume on the host.
Create(createRequest *CreateRequest) error

// Remove unmounts the volume from the host.
Remove(removeRequest *RemoveRequest) error
}

// CreateRequest holds fields necessary for creating a volume
type CreateRequest struct {
Name string
Path string
Options map[string]string
}

// RemoveRequest holds fields necessary for removing a volume
type RemoveRequest struct {
Name string
}
89 changes: 89 additions & 0 deletions ecs-init/volumes/driver/mock/driver_mocks.go

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

8 changes: 5 additions & 3 deletions ecs-init/volumes/ecs_volume_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"strings"
"sync"

"github.com/aws/amazon-ecs-agent/ecs-init/volumes/driver"
"github.com/aws/amazon-ecs-agent/ecs-init/volumes/types"
"github.com/cihub/seelog"
)

Expand All @@ -38,7 +40,7 @@ func NewECSVolumeDriver() *ECSVolumeDriver {
}

// Setup creates the mount helper
func (e *ECSVolumeDriver) Setup(name string, v *Volume) {
func (e *ECSVolumeDriver) Setup(name string, v *types.Volume) {
e.lock.Lock()
defer e.lock.Unlock()

Expand All @@ -52,7 +54,7 @@ func (e *ECSVolumeDriver) Setup(name string, v *Volume) {
}

// Create implements ECSVolumeDriver's Create volume method
func (e *ECSVolumeDriver) Create(r *CreateRequest) error {
func (e *ECSVolumeDriver) Create(r *driver.CreateRequest) error {
e.lock.Lock()
defer e.lock.Unlock()

Expand Down Expand Up @@ -93,7 +95,7 @@ func setOptions(options map[string]string) *MountHelper {
}

// Remove implements ECSVolumeDriver's Remove volume method
func (e *ECSVolumeDriver) Remove(req *RemoveRequest) error {
func (e *ECSVolumeDriver) Remove(req *driver.RemoveRequest) error {
e.lock.Lock()
defer e.lock.Unlock()
mnt, ok := e.volumeMounts[req.Name]
Expand Down
19 changes: 10 additions & 9 deletions ecs-init/volumes/ecs_volume_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
"errors"
"testing"

"github.com/aws/amazon-ecs-agent/ecs-init/volumes/driver"
"github.com/stretchr/testify/assert"
)

func TestVolumeDriverCreateHappyPath(t *testing.T) {
e := NewECSVolumeDriver()
req := CreateRequest{
req := driver.CreateRequest{
Name: "vol",
Path: VolumeMountPathPrefix + "vol",
Options: map[string]string{
Expand All @@ -43,7 +44,7 @@ func TestVolumeDriverCreateHappyPath(t *testing.T) {

func TestVolumeDriverCreateFailure(t *testing.T) {
e := NewECSVolumeDriver()
req := CreateRequest{
req := driver.CreateRequest{
Name: "vol",
Path: VolumeMountPathPrefix + "vol",
Options: map[string]string{
Expand All @@ -65,7 +66,7 @@ func TestVolumeDriverCreateFailure(t *testing.T) {
func TestCreateVolumeExists(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := CreateRequest{
req := driver.CreateRequest{
Name: "vol",
Path: VolumeMountPathPrefix + "vol",
Options: map[string]string{
Expand All @@ -80,7 +81,7 @@ func TestCreateVolumeExists(t *testing.T) {

func TestCreateVolumeMissingOption(t *testing.T) {
e := NewECSVolumeDriver()
req := CreateRequest{
req := driver.CreateRequest{
Name: "vol",
Path: VolumeMountPathPrefix + "vol",
Options: map[string]string{
Expand All @@ -95,7 +96,7 @@ func TestCreateVolumeMissingOption(t *testing.T) {
func TestRemoveVolumeHappyPath(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := RemoveRequest{
req := driver.RemoveRequest{
Name: "vol",
}
lookPath = func(string) (string, error) {
Expand All @@ -115,7 +116,7 @@ func TestRemoveVolumeHappyPath(t *testing.T) {
func TestRemoveVolumeUnmounted(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := RemoveRequest{
req := driver.RemoveRequest{
Name: "vol",
}
lookPath = func(string) (string, error) {
Expand All @@ -135,7 +136,7 @@ func TestRemoveVolumeUnmounted(t *testing.T) {
func TestRemoveUnmountFailure(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := RemoveRequest{
req := driver.RemoveRequest{
Name: "vol",
}
lookPath = func(string) (string, error) {
Expand All @@ -155,7 +156,7 @@ func TestRemoveUnmountFailure(t *testing.T) {
func TestRemoveUnmountNotFound(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := RemoveRequest{
req := driver.RemoveRequest{
Name: "vol",
}
lookPath = func(string) (string, error) {
Expand All @@ -171,7 +172,7 @@ func TestRemoveUnmountNotFound(t *testing.T) {
func TestRemoveVolumeNotPresent(t *testing.T) {
e := NewECSVolumeDriver()
e.volumeMounts["vol"] = &MountHelper{}
req := RemoveRequest{
req := driver.RemoveRequest{
Name: "vol1",
}
assert.Error(t, e.Remove(&req), "expected error when volume to remove is not found")
Expand Down
47 changes: 11 additions & 36 deletions ecs-init/volumes/ecs_volume_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"sync"
"time"

"github.com/aws/amazon-ecs-agent/ecs-init/volumes/driver"
"github.com/aws/amazon-ecs-agent/ecs-init/volumes/types"
"github.com/cihub/seelog"
"github.com/docker/go-plugins-helpers/volume"
)
Expand All @@ -33,51 +35,24 @@ const (

// AmazonECSVolumePlugin holds list of volume drivers and volumes information
type AmazonECSVolumePlugin struct {
volumeDrivers map[string]VolumeDriver
volumes map[string]*Volume
volumeDrivers map[string]driver.VolumeDriver
volumes map[string]*types.Volume
state *StateManager
lock sync.RWMutex
}

// NewAmazonECSVolumePlugin initiates the volume drivers
func NewAmazonECSVolumePlugin() *AmazonECSVolumePlugin {
plugin := &AmazonECSVolumePlugin{
volumeDrivers: map[string]VolumeDriver{
volumeDrivers: map[string]driver.VolumeDriver{
"efs": NewECSVolumeDriver(),
},
volumes: make(map[string]*Volume),
volumes: make(map[string]*types.Volume),
state: NewStateManager(),
}
return plugin
}

// VolumeDriver contains the methods for volume drivers to implement
type VolumeDriver interface {
Setup(string, *Volume)
Create(*CreateRequest) error
Remove(*RemoveRequest) error
}

// Volume holds full details about a volume
type Volume struct {
Type string
Path string
Options map[string]string
CreatedAt string
}

// CreateRequest holds fields necessary for creating a volume
type CreateRequest struct {
Name string
Path string
Options map[string]string
}

// RemoveRequest holds fields necessary for removing a volume
type RemoveRequest struct {
Name string
}

// LoadState loads past state information of the plugin
func (a *AmazonECSVolumePlugin) LoadState() error {
a.lock.Lock()
Expand All @@ -101,7 +76,7 @@ func (a *AmazonECSVolumePlugin) LoadState() error {
seelog.Errorf("Could not load state: %v", err)
return fmt.Errorf("could not load plugin state: %v", err)
}
volume := &Volume{
volume := &types.Volume{
Type: vol.Type,
Path: vol.Path,
Options: vol.Options,
Expand All @@ -114,7 +89,7 @@ func (a *AmazonECSVolumePlugin) LoadState() error {
return nil
}

func (a *AmazonECSVolumePlugin) getVolumeDriver(driverType string) (VolumeDriver, error) {
func (a *AmazonECSVolumePlugin) getVolumeDriver(driverType string) (driver.VolumeDriver, error) {
if driverType == "" {
return a.volumeDrivers[defaultDriverType], nil
}
Expand Down Expand Up @@ -165,7 +140,7 @@ func (a *AmazonECSVolumePlugin) Create(r *volume.CreateRequest) error {
}
}

req := &CreateRequest{
req := &driver.CreateRequest{
Name: r.Name,
Path: target,
Options: r.Options,
Expand All @@ -180,7 +155,7 @@ func (a *AmazonECSVolumePlugin) Create(r *volume.CreateRequest) error {
return err
}
seelog.Infof("Volume %s created successfully", r.Name)
vol := &Volume{
vol := &types.Volume{
Type: driverType,
Path: target,
Options: r.Options,
Expand Down Expand Up @@ -270,7 +245,7 @@ func (a *AmazonECSVolumePlugin) Remove(r *volume.RemoveRequest) error {
return fmt.Errorf("no corresponding volume driver found for type %s", vol.Type)
}

req := &RemoveRequest{
req := &driver.RemoveRequest{
Name: r.Name,
}
err = volDriver.Remove(req)
Expand Down
Loading