-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor sagemaker resource type (#519)
- Loading branch information
1 parent
8ce2b4c
commit ff1a157
Showing
5 changed files
with
119 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,121 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/gruntwork-io/cloud-nuke/telemetry" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/sagemaker" | ||
"github.com/aws/aws-sdk-go/service/sagemaker/sagemakeriface" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"github.com/gruntwork-io/cloud-nuke/logging" | ||
"github.com/gruntwork-io/cloud-nuke/util" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/gruntwork-io/cloud-nuke/telemetry" | ||
"github.com/stretchr/testify/require" | ||
"regexp" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// There's a built-in function WaitUntilDBInstanceAvailable but | ||
// the times that it was tested, it wasn't returning anything so we'll leave with the | ||
// custom one. | ||
|
||
func waitUntilNotebookInstanceCreated(svc *sagemaker.SageMaker, name *string) error { | ||
input := &sagemaker.DescribeNotebookInstanceInput{ | ||
NotebookInstanceName: name, | ||
} | ||
|
||
for i := 0; i < 600; i++ { | ||
instance, err := svc.DescribeNotebookInstance(input) | ||
status := instance.NotebookInstanceStatus | ||
|
||
if awsgo.StringValue(status) != "Pending" { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
logging.Logger.Debug("Waiting for SageMaker Notebook Instance to be created") | ||
} | ||
type mockedSageMakerNotebookInstance struct { | ||
sagemakeriface.SageMakerAPI | ||
ListNotebookInstancesOutput sagemaker.ListNotebookInstancesOutput | ||
StopNotebookInstanceOutput sagemaker.StopNotebookInstanceOutput | ||
DeleteNotebookInstanceOutput sagemaker.DeleteNotebookInstanceOutput | ||
} | ||
|
||
return SageMakerNotebookInstanceDeleteError{name: *name} | ||
func (m mockedSageMakerNotebookInstance) ListNotebookInstances(input *sagemaker.ListNotebookInstancesInput) (*sagemaker.ListNotebookInstancesOutput, error) { | ||
return &m.ListNotebookInstancesOutput, nil | ||
} | ||
|
||
func createTestNotebookInstance(t *testing.T, session *session.Session, name string, roleArn string) { | ||
svc := sagemaker.New(session) | ||
func (m mockedSageMakerNotebookInstance) StopNotebookInstance(input *sagemaker.StopNotebookInstanceInput) (*sagemaker.StopNotebookInstanceOutput, error) { | ||
return &m.StopNotebookInstanceOutput, nil | ||
} | ||
|
||
params := &sagemaker.CreateNotebookInstanceInput{ | ||
InstanceType: awsgo.String("ml.t2.medium"), | ||
NotebookInstanceName: awsgo.String(name), | ||
RoleArn: awsgo.String(roleArn), | ||
} | ||
func (m mockedSageMakerNotebookInstance) WaitUntilNotebookInstanceStopped(*sagemaker.DescribeNotebookInstanceInput) error { | ||
return nil | ||
} | ||
|
||
_, err := svc.CreateNotebookInstance(params) | ||
require.NoError(t, err) | ||
func (m mockedSageMakerNotebookInstance) WaitUntilNotebookInstanceDeleted(*sagemaker.DescribeNotebookInstanceInput) error { | ||
return nil | ||
} | ||
|
||
waitUntilNotebookInstanceCreated(svc, &name) | ||
func (m mockedSageMakerNotebookInstance) DeleteNotebookInstance(input *sagemaker.DeleteNotebookInstanceInput) (*sagemaker.DeleteNotebookInstanceOutput, error) { | ||
return &m.DeleteNotebookInstanceOutput, nil | ||
} | ||
|
||
func TestNukeNotebookInstance(t *testing.T) { | ||
func TestSageMakerNotebookInstances_GetAll(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
t.Parallel() | ||
|
||
region, err := getRandomRegion() | ||
|
||
require.NoError(t, errors.WithStackTrace(err)) | ||
|
||
session, err := session.NewSessionWithOptions( | ||
session.Options{ | ||
SharedConfigState: session.SharedConfigEnable, | ||
Config: awsgo.Config{ | ||
Region: awsgo.String(region), | ||
now := time.Now() | ||
testName1 := "test1" | ||
testName2 := "test2" | ||
smni := SageMakerNotebookInstances{ | ||
Client: mockedSageMakerNotebookInstance{ | ||
ListNotebookInstancesOutput: sagemaker.ListNotebookInstancesOutput{ | ||
NotebookInstances: []*sagemaker.NotebookInstanceSummary{ | ||
{ | ||
NotebookInstanceName: awsgo.String(testName1), | ||
CreationTime: awsgo.Time(now), | ||
}, | ||
{ | ||
NotebookInstanceName: awsgo.String(testName2), | ||
CreationTime: awsgo.Time(now.Add(1)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
notebookName := "cloud-nuke-test-" + util.UniqueID() | ||
excludeAfter := time.Now().Add(1 * time.Hour) | ||
|
||
role := createNotebookRole(t, session, notebookName+"-role") | ||
defer deleteNotebookRole(session, role) | ||
|
||
createTestNotebookInstance(t, session, notebookName, *role.Arn) | ||
|
||
defer func() { | ||
nukeAllNotebookInstances(session, []*string{¬ebookName}) | ||
} | ||
|
||
notebookNames, _ := getAllNotebookInstances(session, excludeAfter, config.Config{}) | ||
tests := map[string]struct { | ||
configObj config.ResourceType | ||
expected []string | ||
}{ | ||
"emptyFilter": { | ||
configObj: config.ResourceType{}, | ||
expected: []string{testName1, testName2}, | ||
}, | ||
"nameExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
NamesRegExp: []config.Expression{{ | ||
RE: *regexp.MustCompile(testName1), | ||
}}}, | ||
}, | ||
expected: []string{testName2}, | ||
}, | ||
"timeAfterExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
TimeAfter: aws.Time(now.Add(-1 * time.Hour)), | ||
}}, | ||
expected: []string{}, | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
names, err := smni.getAll(config.Config{ | ||
SageMakerNotebook: tc.configObj, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, awsgo.StringValueSlice(names)) | ||
}) | ||
} | ||
|
||
assert.NotContains(t, awsgo.StringValueSlice(notebookNames), strings.ToLower(notebookName)) | ||
}() | ||
} | ||
|
||
instances, err := getAllNotebookInstances(session, excludeAfter, config.Config{}) | ||
func TestSageMakerNotebookInstances_NukeAll(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
t.Parallel() | ||
|
||
if err != nil { | ||
assert.Failf(t, "Unable to fetch list of SageMaker Notebook Instances", errors.WithStackTrace(err).Error()) | ||
smni := SageMakerNotebookInstances{ | ||
Client: mockedSageMakerNotebookInstance{ | ||
StopNotebookInstanceOutput: sagemaker.StopNotebookInstanceOutput{}, | ||
DeleteNotebookInstanceOutput: sagemaker.DeleteNotebookInstanceOutput{}, | ||
}, | ||
} | ||
|
||
assert.Contains(t, awsgo.StringValueSlice(instances), notebookName) | ||
|
||
err := smni.nukeAll([]*string{aws.String("test")}) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.