| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- package aws
- import (
- "fmt"
- "testing"
- "github.com/opencost/opencost/core/pkg/log"
- "github.com/opencost/opencost/core/pkg/util/json"
- "github.com/opencost/opencost/pkg/cloud"
- )
- func TestS3Configuration_Validate(t *testing.T) {
- testCases := map[string]struct {
- config S3Configuration
- expected error
- }{
- "valid config access key": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- expected: nil,
- },
- "valid config service account": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- expected: nil,
- },
- "access key invalid": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- },
- },
- expected: fmt.Errorf("S3Configuration: AccessKey: missing Secret"),
- },
- "missing Authorizer": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: nil,
- },
- expected: fmt.Errorf("S3Configuration: missing Authorizer"),
- },
- "missing bucket": {
- config: S3Configuration{
- Bucket: "",
- Region: "region",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- expected: fmt.Errorf("S3Configuration: missing bucket"),
- },
- "missing region": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- expected: fmt.Errorf("S3Configuration: missing region"),
- },
- "missing account": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "",
- Authorizer: &ServiceAccount{},
- },
- expected: fmt.Errorf("S3Configuration: missing account"),
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual := testCase.config.Validate()
- actualString := "nil"
- if actual != nil {
- actualString = actual.Error()
- }
- expectedString := "nil"
- if testCase.expected != nil {
- expectedString = testCase.expected.Error()
- }
- if actualString != expectedString {
- t.Errorf("errors do not match: Actual: '%s', Expected: '%s", actualString, expectedString)
- }
- })
- }
- }
- func TestS3Configuration_Equals(t *testing.T) {
- testCases := map[string]struct {
- left S3Configuration
- right cloud.Config
- expected bool
- }{
- "matching config": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- expected: true,
- },
- "different Authorizer": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- expected: false,
- },
- "missing both Authorizer": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: nil,
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: nil,
- },
- expected: true,
- },
- "missing left Authorizer": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: nil,
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- expected: false,
- },
- "missing right Authorizer": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: nil,
- },
- expected: false,
- },
- "different bucket": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket2",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- expected: false,
- },
- "different region": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region2",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- expected: false,
- },
- "different account": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account2",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- expected: false,
- },
- "different config": {
- left: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- right: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- expected: false,
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual := testCase.left.Equals(testCase.right)
- if actual != testCase.expected {
- t.Errorf("incorrect result: Actual: '%t', Expected: '%t", actual, testCase.expected)
- }
- })
- }
- }
- func TestS3Configuration_JSON(t *testing.T) {
- testCases := map[string]struct {
- config S3Configuration
- }{
- "Empty Config": {
- config: S3Configuration{},
- },
- "AccessKey": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- },
- "ServiceAccount": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &ServiceAccount{},
- },
- },
- "AssumeRole with AccessKey": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AssumeRole{
- Authorizer: &AccessKey{
- ID: "id",
- Secret: "secret",
- },
- RoleARN: "12345",
- },
- },
- },
- "AssumeRole with ServiceAccount": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AssumeRole{
- Authorizer: &ServiceAccount{},
- RoleARN: "12345",
- },
- },
- },
- "RoleArnNil": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AssumeRole{
- Authorizer: nil,
- RoleARN: "12345",
- },
- },
- },
- "AssumeRole with AssumeRole with ServiceAccount": {
- config: S3Configuration{
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &AssumeRole{
- Authorizer: &AssumeRole{
- RoleARN: "12345",
- Authorizer: &ServiceAccount{},
- },
- RoleARN: "12345",
- },
- },
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- // test JSON Marshalling
- configJSON, err := json.Marshal(testCase.config)
- if err != nil {
- t.Errorf("failed to marshal configuration: %s", err.Error())
- }
- log.Info(string(configJSON))
- unmarshalledConfig := &S3Configuration{}
- err = json.Unmarshal(configJSON, unmarshalledConfig)
- if err != nil {
- t.Errorf("failed to unmarshal configuration: %s", err.Error())
- }
- if !testCase.config.Equals(unmarshalledConfig) {
- t.Error("config does not equal unmarshalled config")
- }
- })
- }
- }
|