-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(aws): move part of unit tests to integration (#4884)
* test(aws): move part of unit tests to integration * fix typo * fix test --------- Co-authored-by: knqyf263 <[email protected]>
- Loading branch information
Showing
4 changed files
with
126 additions
and
55 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//go:build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
awscommands "github.com/aquasecurity/trivy/pkg/cloud/aws/commands" | ||
"github.com/aquasecurity/trivy/pkg/flag" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
testcontainers "github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/localstack" | ||
) | ||
|
||
func TestAwsCommandRun(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
options flag.Options | ||
envs map[string]string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "fail without region", | ||
options: flag.Options{ | ||
RegoOptions: flag.RegoOptions{SkipPolicyUpdate: true}, | ||
}, | ||
envs: map[string]string{ | ||
"AWS_ACCESS_KEY_ID": "test", | ||
"AWS_SECRET_ACCESS_KEY": "test", | ||
}, | ||
wantErr: "Invalid Configuration: Missing Region", | ||
}, | ||
{ | ||
name: "fail without creds", | ||
options: flag.Options{ | ||
RegoOptions: flag.RegoOptions{SkipPolicyUpdate: true}, | ||
AWSOptions: flag.AWSOptions{ | ||
Region: "us-east-1", | ||
}, | ||
}, | ||
wantErr: "failed to retrieve credentials", | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
localstackC, addr := setupLocalStack(t, ctx) | ||
defer localstackC.Terminate(ctx) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
tt.options.AWSOptions.Endpoint = addr | ||
tt.options.GlobalOptions.Timeout = time.Minute | ||
|
||
t.Setenv("AWS_PROFILE", "non-existent-profile") | ||
for k, v := range tt.envs { | ||
t.Setenv(k, v) | ||
} | ||
|
||
err := awscommands.Run(context.Background(), tt.options) | ||
|
||
if tt.wantErr != "" { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErr, tt.name) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func setupLocalStack(t *testing.T, ctx context.Context) (*localstack.LocalStackContainer, string) { | ||
t.Helper() | ||
|
||
container, err := localstack.RunContainer(ctx, testcontainers.CustomizeRequest( | ||
testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: "localstack/localstack:2.2.0", | ||
}, | ||
}, | ||
)) | ||
require.NoError(t, err) | ||
|
||
p, err := container.MappedPort(ctx, "4566/tcp") | ||
require.NoError(t, err) | ||
|
||
provider, err := testcontainers.NewDockerProvider() | ||
require.NoError(t, err) | ||
defer provider.Close() | ||
|
||
host, err := provider.DaemonHost(ctx) | ||
require.NoError(t, err) | ||
|
||
return container, fmt.Sprintf("http://%s:%d", host, p.Int()) | ||
|
||
} |
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