Skip to content
Open
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
35 changes: 4 additions & 31 deletions cmd/multi_service_coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/LeanerCloud/CUDly/pkg/common"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

// ==================== Tests for coverage improvement ====================
Expand Down Expand Up @@ -583,16 +584,7 @@ func TestProcessService_GetRegionsError(t *testing.T) {
toolCfg.Regions = []string{"us-east-1"}

// Setup mock to return empty recommendations
params := common.RecommendationParams{
Service: common.ServiceRDS,
Region: "us-east-1",
PaymentOption: "all-upfront",
Term: "3yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockClient.On("GetRecommendations", ctx, params).Return([]common.Recommendation{}, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return([]common.Recommendation{}, nil)

recs, results := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceRDS, true, toolCfg, engineVersionData{})

Expand All @@ -619,16 +611,7 @@ func TestProcessService_GetRecommendationsError(t *testing.T) {
accountCache := NewAccountAliasCache(awsCfg)

// Setup mock to return error
params := common.RecommendationParams{
Service: common.ServiceEC2,
Region: "us-east-1",
PaymentOption: "partial-upfront",
Term: "1yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockClient.On("GetRecommendations", ctx, params).Return([]common.Recommendation(nil), errors.New("API error"))
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return([]common.Recommendation(nil), errors.New("API error"))

recs, results := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceEC2, true, toolCfg, engineVersionData{})

Expand All @@ -655,22 +638,12 @@ func TestProcessService_AllRecommendationsFilteredOut(t *testing.T) {
mockClient := &MockRecommendationsClient{}
accountCache := NewAccountAliasCache(awsCfg)

params := common.RecommendationParams{
Service: common.ServiceRDS,
Region: "us-east-1",
PaymentOption: "no-upfront",
Term: "1yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}

// Return recommendations that don't match the filter
mockRecs := []common.Recommendation{
{ResourceType: "db.t3.small", Count: 5, Region: "us-east-1", EstimatedSavings: 100},
{ResourceType: "db.t3.medium", Count: 3, Region: "us-east-1", EstimatedSavings: 200},
}
mockClient.On("GetRecommendations", ctx, params).Return(mockRecs, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(mockRecs, nil)

recs, results := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceRDS, true, toolCfg, engineVersionData{})

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi_service_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func fetchRecommendationsForRegion(
ExcludeSPTypes: cfg.ExcludeSPTypes,
}

recs, err := recClient.GetRecommendations(ctx, params)
recs, err := recClient.GetRecommendations(ctx, &params)
if err != nil {
AppLogger.Printf(" ❌ Failed to fetch recommendations: %v\n", err)
return nil
Expand Down
64 changes: 8 additions & 56 deletions cmd/multi_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,8 @@ func TestProcessServiceWithMocks(t *testing.T) {
mockClient := &MockRecommendationsClient{}

// Setup expectations
termStr := "1yr"
if toolCfg.TermYears == 3 {
termStr = "3yr"
}
for _, region := range tt.testRegions {
params := common.RecommendationParams{
Service: tt.service,
Region: region,
PaymentOption: toolCfg.PaymentOption,
Term: termStr,
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockClient.On("GetRecommendations", ctx, params).Return(tt.mockRecs, nil)
for range tt.testRegions {
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(tt.mockRecs, nil)
}

// Set regions in toolCfg for this test
Expand Down Expand Up @@ -337,19 +324,10 @@ func TestProcessService_SavingsPlansAccountLevel(t *testing.T) {
// Savings Plans should only query us-east-1 once (account-level). Use the
// per-plan-type Compute slug now that the legacy umbrella has been
// retired from createServiceClient dispatch.
params := common.RecommendationParams{
Service: common.ServiceSavingsPlansCompute,
Region: "us-east-1",
PaymentOption: "all-upfront",
Term: "3yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockRecs := []common.Recommendation{
{Service: common.ServiceSavingsPlansCompute, ResourceType: "ComputeSP", Count: 1, Region: "us-east-1", EstimatedSavings: 1000},
}
mockClient.On("GetRecommendations", ctx, params).Return(mockRecs, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(mockRecs, nil)

accountCache := NewAccountAliasCache(awsCfg)
recs, results := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceSavingsPlansCompute, true, toolCfg, engineVersionData{})
Expand Down Expand Up @@ -383,20 +361,11 @@ func TestProcessService_WithInstanceLimit(t *testing.T) {

mockClient := &MockRecommendationsClient{}

params := common.RecommendationParams{
Service: common.ServiceRDS,
Region: "us-east-1",
PaymentOption: "partial-upfront",
Term: "1yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockRecs := []common.Recommendation{
{ResourceType: "db.t3.micro", Count: 10, Region: "us-east-1", EstimatedSavings: 100},
{ResourceType: "db.t3.small", Count: 10, Region: "us-east-1", EstimatedSavings: 200},
}
mockClient.On("GetRecommendations", ctx, params).Return(mockRecs, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(mockRecs, nil)

accountCache := NewAccountAliasCache(awsCfg)
recs, _ := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceRDS, true, toolCfg, engineVersionData{})
Expand Down Expand Up @@ -425,20 +394,11 @@ func TestProcessService_WithOverrideCount(t *testing.T) {

mockClient := &MockRecommendationsClient{}

params := common.RecommendationParams{
Service: common.ServiceElastiCache,
Region: "us-east-1",
PaymentOption: "no-upfront",
Term: "1yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockRecs := []common.Recommendation{
{ResourceType: "cache.t3.micro", Count: 10, Region: "us-east-1", EstimatedSavings: 100},
{ResourceType: "cache.t3.small", Count: 5, Region: "us-east-1", EstimatedSavings: 200},
}
mockClient.On("GetRecommendations", ctx, params).Return(mockRecs, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(mockRecs, nil)

accountCache := NewAccountAliasCache(awsCfg)
recs, _ := processService(ctx, awsCfg, mockClient, accountCache, common.ServiceElastiCache, true, toolCfg, engineVersionData{})
Expand All @@ -465,21 +425,13 @@ func TestProcessService_MultipleRegions(t *testing.T) {

mockClient := &MockRecommendationsClient{}

// Setup mock for each region
// Setup mock for each region call in order; Once() ensures testify cycles
// through the returns so each region's call gets region-appropriate results.
for _, region := range toolCfg.Regions {
params := common.RecommendationParams{
Service: common.ServiceRDS,
Region: region,
PaymentOption: "all-upfront",
Term: "3yr",
LookbackPeriod: "7d",
IncludeSPTypes: toolCfg.IncludeSPTypes,
ExcludeSPTypes: toolCfg.ExcludeSPTypes,
}
mockRecs := []common.Recommendation{
{ResourceType: "db.t3.small", Count: 2, Region: region, EstimatedSavings: 100},
}
mockClient.On("GetRecommendations", ctx, params).Return(mockRecs, nil)
mockClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(mockRecs, nil).Once()
}

accountCache := NewAccountAliasCache(awsCfg)
Expand Down
4 changes: 2 additions & 2 deletions cmd/multi_service_test_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MockRecommendationsClient struct {
mock.Mock
}

func (m *MockRecommendationsClient) GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error) {
func (m *MockRecommendationsClient) GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error) {
args := m.Called(ctx, params)
if args.Get(0) == nil {
return nil, args.Error(1)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (m *MockServiceClient) GetRegion() string {
return args.String(0)
}

func (m *MockServiceClient) GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error) {
func (m *MockServiceClient) GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error) {
args := m.Called(ctx, params)
if args.Get(0) == nil {
return nil, args.Error(1)
Expand Down
2 changes: 1 addition & 1 deletion internal/purchase/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (m *MockServiceClient) PurchaseCommitment(ctx context.Context, rec common.R
return args.Get(0).(common.PurchaseResult), args.Error(1)
}

func (m *MockServiceClient) GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error) {
func (m *MockServiceClient) GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error) {
args := m.Called(ctx, params)
if args.Get(0) == nil {
return nil, args.Error(1)
Expand Down
2 changes: 1 addition & 1 deletion internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ func (s *Scheduler) fetchAndConvert(ctx context.Context, prov provider.Provider,
LookbackPeriod: fmt.Sprintf("%dd", lookbackDays),
}
var recErr error
recs, recErr = recClient.GetRecommendations(ctx, params)
recs, recErr = recClient.GetRecommendations(ctx, &params)
if recErr != nil {
logging.Warnf("fetchAndConvert: %s GetRecommendations fallback failed: %v", providerName, recErr)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ type MockRecommendationsClient struct {
mock.Mock
}

func (m *MockRecommendationsClient) GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error) {
func (m *MockRecommendationsClient) GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error) {
args := m.Called(ctx, params)
if args.Get(0) == nil {
return nil, args.Error(1)
Expand Down Expand Up @@ -1589,7 +1589,7 @@ func TestScheduler_CollectAWSRecommendations_FallbackToFiltered(t *testing.T) {
mockFactory.On("CreateAndValidateProvider", mock.Anything, "aws", mock.Anything).Return(mockProvider, nil)
mockProvider.On("GetRecommendationsClient", ctx).Return(mockRecClient, nil)
mockRecClient.On("GetAllRecommendations", ctx).Return([]common.Recommendation{}, nil) // Empty
mockRecClient.On("GetRecommendations", ctx, mock.AnythingOfType("common.RecommendationParams")).Return(filteredRecommendations, nil)
mockRecClient.On("GetRecommendations", ctx, mock.AnythingOfType("*common.RecommendationParams")).Return(filteredRecommendations, nil)

scheduler := &Scheduler{
config: mockStore,
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ServiceClient interface {
GetRegion() string

// Recommendations
GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error)
GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error)

// Commitments (RI/SP/CUD/etc)
GetExistingCommitments(ctx context.Context) ([]common.Commitment, error)
Expand All @@ -56,7 +56,7 @@ type ServiceClient interface {
// RecommendationsClient provides centralized recommendations across all services
type RecommendationsClient interface {
// Get recommendations with filtering
GetRecommendations(ctx context.Context, params common.RecommendationParams) ([]common.Recommendation, error)
GetRecommendations(ctx context.Context, params *common.RecommendationParams) ([]common.Recommendation, error)

// Get recommendations for a specific service
GetRecommendationsForService(ctx context.Context, service common.ServiceType) ([]common.Recommendation, error)
Expand Down
Loading
Loading