draft tests
This commit is contained in:
119
models/resources/tests/compute_test.go
Normal file
119
models/resources/tests/compute_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestComputeResource_GetType(t *testing.T) {
|
||||
r := &resources.ComputeResource{}
|
||||
assert.Equal(t, tools.COMPUTE_RESOURCE.String(), r.GetType())
|
||||
}
|
||||
|
||||
func TestComputeResource_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
cr := &resources.ComputeResource{}
|
||||
accessor := cr.GetAccessor(req)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
|
||||
func TestComputeResource_ConvertToPricedResource(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
cr := &resources.ComputeResource{}
|
||||
cr.UUID = "comp123"
|
||||
cr.AbstractInstanciatedResource.UUID = cr.UUID
|
||||
result := cr.ConvertToPricedResource(tools.COMPUTE_RESOURCE, req)
|
||||
assert.NotNil(t, result)
|
||||
assert.IsType(t, &resources.PricedComputeResource{}, result)
|
||||
}
|
||||
|
||||
func TestComputeResourcePricingProfile_GetPrice_CPUs(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
profile := resources.ComputeResourcePricingProfile{
|
||||
CPUsPrices: map[string]float64{"Xeon": 2.0},
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[pricing.TimePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{Price: 1.0},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
price, err := profile.GetPrice(2, 3600, start, end, "cpus", "Xeon")
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, price, float64(0))
|
||||
}
|
||||
|
||||
func TestComputeResourcePricingProfile_GetPrice_InvalidParams(t *testing.T) {
|
||||
profile := resources.ComputeResourcePricingProfile{}
|
||||
_, err := profile.GetPrice(1, 3600, time.Now(), time.Now())
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "params must be set", err.Error())
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_GetPrice(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
profile := &resources.ComputeResourcePricingProfile{
|
||||
CPUsPrices: map[string]float64{"Xeon": 1.0},
|
||||
GPUsPrices: map[string]float64{"Tesla": 2.0},
|
||||
RAMPrice: 0.5,
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[pricing.TimePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{Price: 1.0},
|
||||
},
|
||||
},
|
||||
}
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "comp456",
|
||||
PricingProfiles: []pricing.PricingProfileITF{profile},
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
ExplicitBookingDurationS: 3600,
|
||||
},
|
||||
CPUsLocated: map[string]float64{"Xeon": 2},
|
||||
GPUsLocated: map[string]float64{"Tesla": 1},
|
||||
RAMLocated: 4,
|
||||
}
|
||||
|
||||
price, err := r.GetPrice()
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, price, float64(0))
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_GetPrice_MissingProfile(t *testing.T) {
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "comp789",
|
||||
},
|
||||
}
|
||||
_, err := r.GetPrice()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_FillWithDefaultProcessingUsage(t *testing.T) {
|
||||
usage := &resources.ProcessingUsage{
|
||||
CPUs: map[string]*models.CPU{"t": {Model: "Xeon", Cores: 4}},
|
||||
GPUs: map[string]*models.GPU{"t1": {Model: "Tesla"}},
|
||||
RAM: &models.RAM{SizeGb: 16},
|
||||
}
|
||||
r := &resources.PricedComputeResource{
|
||||
CPUsLocated: make(map[string]float64),
|
||||
GPUsLocated: make(map[string]float64),
|
||||
RAMLocated: 0,
|
||||
}
|
||||
r.FillWithDefaultProcessingUsage(usage)
|
||||
assert.Equal(t, float64(4), r.CPUsLocated["Xeon"])
|
||||
assert.Equal(t, float64(1), r.GPUsLocated["Tesla"])
|
||||
assert.Equal(t, float64(16), r.RAMLocated)
|
||||
}
|
||||
125
models/resources/tests/data_test.go
Normal file
125
models/resources/tests/data_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDataResource_GetType(t *testing.T) {
|
||||
d := &resources.DataResource{}
|
||||
assert.Equal(t, tools.DATA_RESOURCE.String(), d.GetType())
|
||||
}
|
||||
|
||||
func TestDataResource_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
acc := (&resources.DataResource{}).GetAccessor(req)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestDataResource_ConvertToPricedResource(t *testing.T) {
|
||||
d := &resources.DataResource{}
|
||||
d.UUID = "123"
|
||||
res := d.ConvertToPricedResource(tools.DATA_RESOURCE, &tools.APIRequest{})
|
||||
assert.IsType(t, &resources.PricedDataResource{}, res)
|
||||
|
||||
nilRes := d.ConvertToPricedResource(tools.PROCESSING_RESOURCE, &tools.APIRequest{})
|
||||
assert.Nil(t, nilRes)
|
||||
}
|
||||
|
||||
func TestDataInstance_StoreDraftDefault(t *testing.T) {
|
||||
di := &resources.DataInstance{
|
||||
Source: "test-src",
|
||||
ResourceInstance: resources.ResourceInstance[*resources.DataResourcePartnership]{
|
||||
Env: []models.Param{},
|
||||
},
|
||||
}
|
||||
di.StoreDraftDefault()
|
||||
assert.Len(t, di.ResourceInstance.Env, 1)
|
||||
assert.Equal(t, "source", di.ResourceInstance.Env[0].Attr)
|
||||
assert.Equal(t, "test-src", di.ResourceInstance.Env[0].Value)
|
||||
|
||||
// Call again, should not duplicate
|
||||
di.StoreDraftDefault()
|
||||
assert.Len(t, di.ResourceInstance.Env, 1)
|
||||
}
|
||||
|
||||
func TestDataResourcePricingStrategy_GetQuantity(t *testing.T) {
|
||||
tests := []struct {
|
||||
strategy resources.DataResourcePricingStrategy
|
||||
input float64
|
||||
expected float64
|
||||
}{
|
||||
{resources.PER_DOWNLOAD, 1, 1},
|
||||
{resources.PER_TB_DOWNLOADED, 1, 1000},
|
||||
{resources.PER_GB_DOWNLOADED, 2.5, 2.5},
|
||||
{resources.PER_MB_DOWNLOADED, 1, 0.001},
|
||||
{resources.PER_KB_DOWNLOADED, 1, 0.000001},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
q, err := tt.strategy.GetQuantity(tt.input)
|
||||
require.NoError(t, err)
|
||||
assert.InDelta(t, tt.expected, q, 1e-9)
|
||||
}
|
||||
|
||||
_, err := resources.DataResourcePricingStrategy(999).GetQuantity(1)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDataResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
profile := &resources.DataResourcePricingProfile{}
|
||||
profile.Pricing.BuyingStrategy = pricing.PAY_PER_USE
|
||||
assert.False(t, profile.IsPurchased())
|
||||
|
||||
profile.Pricing.BuyingStrategy = pricing.SUBSCRIPTION
|
||||
assert.True(t, profile.IsPurchased())
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetPrice(t *testing.T) {
|
||||
now := time.Now()
|
||||
later := now.Add(1 * time.Hour)
|
||||
mockPrice := 42.0
|
||||
|
||||
pricingProfile := &resources.DataResourcePricingProfile{AccessPricingProfile: pricing.AccessPricingProfile[resources.DataResourcePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[resources.DataResourcePricingStrategy]{Price: 42.0}},
|
||||
}
|
||||
pricingProfile.Pricing.OverrideStrategy = resources.PER_GB_DOWNLOADED
|
||||
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &later,
|
||||
PricingProfiles: []pricing.PricingProfileITF{
|
||||
pricingProfile,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
price, err := r.GetPrice()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, mockPrice, price)
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetPrice_NoProfiles(t *testing.T) {
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "test-resource",
|
||||
},
|
||||
}
|
||||
_, err := r.GetPrice()
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetType(t *testing.T) {
|
||||
r := &resources.PricedDataResource{}
|
||||
assert.Equal(t, tools.DATA_RESOURCE, r.GetType())
|
||||
}
|
||||
142
models/resources/tests/priced_resource_test.go
Normal file
142
models/resources/tests/priced_resource_test.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
// ---- Mock PricingProfile ----
|
||||
|
||||
type MockPricingProfile struct {
|
||||
pricing.PricingProfileITF
|
||||
Purchased bool
|
||||
ReturnErr bool
|
||||
ReturnCost float64
|
||||
}
|
||||
|
||||
func (m *MockPricingProfile) IsPurchased() bool {
|
||||
return m.Purchased
|
||||
}
|
||||
|
||||
func (m *MockPricingProfile) GetPrice(amount float64, explicitDuration float64, start time.Time, end time.Time, _ ...string) (float64, error) {
|
||||
if m.ReturnErr {
|
||||
return 0, errors.New("mock error")
|
||||
}
|
||||
return m.ReturnCost, nil
|
||||
}
|
||||
|
||||
// ---- Tests ----
|
||||
|
||||
func TestGetIDAndCreatorAndType(t *testing.T) {
|
||||
r := resources.PricedResource{
|
||||
ResourceID: "res-123",
|
||||
CreatorID: "user-abc",
|
||||
ResourceType: tools.DATA_RESOURCE,
|
||||
}
|
||||
assert.Equal(t, "res-123", r.GetID())
|
||||
assert.Equal(t, "user-abc", r.GetCreatorID())
|
||||
assert.Equal(t, tools.DATA_RESOURCE, r.GetType())
|
||||
}
|
||||
|
||||
func TestIsPurchased(t *testing.T) {
|
||||
t.Run("nil selected pricing returns false", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
assert.False(t, r.IsPurchased())
|
||||
})
|
||||
|
||||
t.Run("returns true if pricing profile is purchased", func(t *testing.T) {
|
||||
mock := &MockPricingProfile{Purchased: true}
|
||||
r := &resources.PricedResource{SelectedPricing: mock}
|
||||
assert.True(t, r.IsPurchased())
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAndSetLocationStartEnd(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
|
||||
now := time.Now()
|
||||
r.SetLocationStart(now)
|
||||
r.SetLocationEnd(now.Add(2 * time.Hour))
|
||||
|
||||
assert.Equal(t, now, *r.GetLocationStart())
|
||||
assert.Equal(t, now.Add(2*time.Hour), *r.GetLocationEnd())
|
||||
}
|
||||
|
||||
func TestGetExplicitDurationInS(t *testing.T) {
|
||||
t.Run("uses explicit duration if set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{ExplicitBookingDurationS: 3600}
|
||||
assert.Equal(t, 3600.0, r.GetExplicitDurationInS())
|
||||
})
|
||||
|
||||
t.Run("computes duration from start and end", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
r := &resources.PricedResource{UsageStart: &start, UsageEnd: &end}
|
||||
assert.InDelta(t, 7200.0, r.GetExplicitDurationInS(), 0.1)
|
||||
})
|
||||
|
||||
t.Run("defaults to 1 hour when times not set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
assert.InDelta(t, 3600.0, r.GetExplicitDurationInS(), 0.1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPrice(t *testing.T) {
|
||||
t.Run("returns error if no pricing profile", func(t *testing.T) {
|
||||
r := &resources.PricedResource{ResourceID: "no-profile"}
|
||||
price, err := r.GetPrice()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
assert.Equal(t, 0.0, price)
|
||||
})
|
||||
|
||||
t.Run("uses first profile if selected is nil", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(30 * time.Minute)
|
||||
mock := &MockPricingProfile{ReturnCost: 42.0}
|
||||
r := &resources.PricedResource{
|
||||
PricingProfiles: []pricing.PricingProfileITF{mock},
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
}
|
||||
price, err := r.GetPrice()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 42.0, price)
|
||||
})
|
||||
|
||||
t.Run("returns error if profile GetPrice fails", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnErr: true}
|
||||
r := &resources.PricedResource{
|
||||
SelectedPricing: mock,
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
}
|
||||
price, err := r.GetPrice()
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, 0.0, price)
|
||||
})
|
||||
|
||||
t.Run("uses SelectedPricing if set", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnCost: 10.0}
|
||||
r := &resources.PricedResource{
|
||||
SelectedPricing: mock,
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
}
|
||||
price, err := r.GetPrice()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 10.0, price)
|
||||
})
|
||||
}
|
||||
115
models/resources/tests/processing_test.go
Normal file
115
models/resources/tests/processing_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
. "cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProcessingResource_GetType(t *testing.T) {
|
||||
r := &ProcessingResource{}
|
||||
assert.Equal(t, tools.PROCESSING_RESOURCE.String(), r.GetType())
|
||||
}
|
||||
|
||||
func TestPricedProcessingResource_GetType(t *testing.T) {
|
||||
r := &PricedProcessingResource{}
|
||||
assert.Equal(t, tools.PROCESSING_RESOURCE, r.GetType())
|
||||
}
|
||||
|
||||
func TestPricedProcessingResource_GetExplicitDurationInS(t *testing.T) {
|
||||
now := time.Now()
|
||||
after := now.Add(2 * time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input PricedProcessingResource
|
||||
expected float64
|
||||
}{
|
||||
{
|
||||
name: "Service without explicit duration",
|
||||
input: PricedProcessingResource{
|
||||
IsService: true,
|
||||
},
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "Nil start time, non-service",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
UsageStart: nil,
|
||||
},
|
||||
},
|
||||
expected: float64((1 * time.Hour).Seconds()),
|
||||
},
|
||||
{
|
||||
name: "Duration computed from start and end",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &after,
|
||||
},
|
||||
},
|
||||
expected: float64((2 * time.Hour).Seconds()),
|
||||
},
|
||||
{
|
||||
name: "Explicit duration takes precedence",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
ExplicitBookingDurationS: 1337,
|
||||
},
|
||||
},
|
||||
expected: 1337,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.expected, test.input.GetExplicitDurationInS())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessingResource_GetAccessor(t *testing.T) {
|
||||
request := &tools.APIRequest{}
|
||||
r := &ProcessingResource{}
|
||||
acc := r.GetAccessor(request)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestProcessingResourcePricingProfile_GetPrice(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
mockPricing := pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{
|
||||
Price: 100.0,
|
||||
},
|
||||
}
|
||||
profile := &ProcessingResourcePricingProfile{mockPricing}
|
||||
price, err := profile.GetPrice(0, 0, start, end)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100.0, price)
|
||||
}
|
||||
|
||||
func TestProcessingResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
nonPurchased := &ProcessingResourcePricingProfile{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{
|
||||
BuyingStrategy: pricing.PAY_PER_USE,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.False(t, nonPurchased.IsPurchased())
|
||||
|
||||
purchased := &ProcessingResourcePricingProfile{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{
|
||||
BuyingStrategy: pricing.UNLIMITED,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.True(t, purchased.IsPurchased())
|
||||
}
|
||||
106
models/resources/tests/resource_test.go
Normal file
106
models/resources/tests/resource_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type MockInstance struct {
|
||||
ID string
|
||||
Name string
|
||||
resources.ResourceInstance[*MockPartner]
|
||||
}
|
||||
|
||||
func (m *MockInstance) GetID() string { return m.ID }
|
||||
func (m *MockInstance) GetName() string { return m.Name }
|
||||
func (m *MockInstance) ClearEnv() {}
|
||||
func (m *MockInstance) ClearPeerGroups() {}
|
||||
func (m *MockInstance) GetPricingsProfiles(string, []string) []pricing.PricingProfileITF { return nil }
|
||||
func (m *MockInstance) GetPeerGroups() ([]resources.ResourcePartnerITF, []map[string][]string) {
|
||||
return nil, []map[string][]string{
|
||||
{"peer1": {"group1"}},
|
||||
}
|
||||
}
|
||||
|
||||
type MockPartner struct {
|
||||
groups map[string][]string
|
||||
}
|
||||
|
||||
func (m *MockPartner) GetPeerGroups() map[string][]string {
|
||||
return m.groups
|
||||
}
|
||||
func (m *MockPartner) ClearPeerGroups() {}
|
||||
func (m *MockPartner) GetPricingsProfiles(string, []string) []pricing.PricingProfileITF {
|
||||
return nil
|
||||
}
|
||||
|
||||
type MockDBObject struct {
|
||||
utils.AbstractObject
|
||||
isDraft bool
|
||||
}
|
||||
|
||||
func (m *MockDBObject) IsDrafted() bool {
|
||||
return m.isDraft
|
||||
}
|
||||
|
||||
func TestGetSelectedInstance_WithValidIndex(t *testing.T) {
|
||||
index := 1
|
||||
inst1 := &MockInstance{ID: "1"}
|
||||
inst2 := &MockInstance{ID: "2"}
|
||||
resource := &resources.AbstractInstanciatedResource[*MockInstance]{
|
||||
AbstractResource: resources.AbstractResource{SelectedInstanceIndex: &index},
|
||||
Instances: []*MockInstance{inst1, inst2},
|
||||
}
|
||||
result := resource.GetSelectedInstance()
|
||||
assert.Equal(t, inst2, result)
|
||||
}
|
||||
|
||||
func TestGetSelectedInstance_NoIndex(t *testing.T) {
|
||||
inst := &MockInstance{ID: "1"}
|
||||
resource := &resources.AbstractInstanciatedResource[*MockInstance]{
|
||||
Instances: []*MockInstance{inst},
|
||||
}
|
||||
result := resource.GetSelectedInstance()
|
||||
assert.Equal(t, inst, result)
|
||||
}
|
||||
|
||||
func TestCanUpdate_WhenOnlyStateDiffers(t *testing.T) {
|
||||
resource := &resources.AbstractResource{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
||||
set := &MockDBObject{isDraft: true}
|
||||
canUpdate, updated := resource.CanUpdate(set)
|
||||
assert.True(t, canUpdate)
|
||||
assert.Equal(t, set, updated)
|
||||
}
|
||||
|
||||
func TestVerifyAuthAction_WithMatchingGroup(t *testing.T) {
|
||||
inst := &MockInstance{
|
||||
ResourceInstance: resources.ResourceInstance[*MockPartner]{
|
||||
Partnerships: []*MockPartner{
|
||||
{groups: map[string][]string{"peer1": {"group1"}}},
|
||||
},
|
||||
},
|
||||
}
|
||||
req := &tools.APIRequest{PeerID: "peer1", Groups: []string{"group1"}}
|
||||
result := resources.VerifyAuthAction([]*MockInstance{inst}, req)
|
||||
assert.Len(t, result, 1)
|
||||
}
|
||||
|
||||
type FakeResource struct {
|
||||
resources.AbstractInstanciatedResource[*MockInstance]
|
||||
}
|
||||
|
||||
func (f *FakeResource) Trim() {}
|
||||
func (f *FakeResource) SetAllowedInstances(*tools.APIRequest) {}
|
||||
func (f *FakeResource) VerifyAuth(*tools.APIRequest) bool { return true }
|
||||
|
||||
func TestNewAccessor_ReturnsValid(t *testing.T) {
|
||||
acc := resources.NewAccessor[*FakeResource](tools.COMPUTE_RESOURCE, &tools.APIRequest{}, func() utils.DBObject {
|
||||
return &FakeResource{}
|
||||
})
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
149
models/resources/tests/storage_test.go
Normal file
149
models/resources/tests/storage_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
)
|
||||
|
||||
func TestStorageResource_GetType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
assert.Equal(t, tools.STORAGE_RESOURCE.String(), res.GetType())
|
||||
}
|
||||
|
||||
func TestStorageResource_GetAccessor(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
req := &tools.APIRequest{}
|
||||
accessor := res.GetAccessor(req)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
|
||||
func TestStorageResource_ConvertToPricedResource_ValidType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
res.AbstractInstanciatedResource.CreatorID = "creator"
|
||||
res.AbstractInstanciatedResource.UUID = "res-id"
|
||||
priced := res.ConvertToPricedResource(tools.STORAGE_RESOURCE, &tools.APIRequest{})
|
||||
assert.NotNil(t, priced)
|
||||
assert.IsType(t, &resources.PricedStorageResource{}, priced)
|
||||
}
|
||||
|
||||
func TestStorageResource_ConvertToPricedResource_InvalidType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
priced := res.ConvertToPricedResource(tools.COMPUTE_RESOURCE, &tools.APIRequest{})
|
||||
assert.Nil(t, priced)
|
||||
}
|
||||
|
||||
func TestStorageResourceInstance_ClearEnv(t *testing.T) {
|
||||
inst := &resources.StorageResourceInstance{
|
||||
Credentials: &resources.Credentials{Login: "test"},
|
||||
ResourceInstance: resources.ResourceInstance[*resources.StorageResourcePartnership]{
|
||||
Env: []models.Param{{Attr: "A"}},
|
||||
Inputs: []models.Param{{Attr: "B"}},
|
||||
Outputs: []models.Param{{Attr: "C"}},
|
||||
},
|
||||
}
|
||||
|
||||
inst.ClearEnv()
|
||||
assert.Nil(t, inst.Credentials)
|
||||
assert.Empty(t, inst.Env)
|
||||
assert.Empty(t, inst.Inputs)
|
||||
assert.Empty(t, inst.Outputs)
|
||||
}
|
||||
|
||||
func TestStorageResourceInstance_StoreDraftDefault(t *testing.T) {
|
||||
inst := &resources.StorageResourceInstance{
|
||||
Source: "my-source",
|
||||
ResourceInstance: resources.ResourceInstance[*resources.StorageResourcePartnership]{
|
||||
Env: []models.Param{},
|
||||
},
|
||||
}
|
||||
|
||||
inst.StoreDraftDefault()
|
||||
assert.Len(t, inst.Env, 1)
|
||||
assert.Equal(t, "source", inst.Env[0].Attr)
|
||||
assert.Equal(t, "my-source", inst.Env[0].Value)
|
||||
assert.True(t, inst.Env[0].Readonly)
|
||||
}
|
||||
|
||||
func TestStorageResourcePricingStrategy_GetQuantity(t *testing.T) {
|
||||
tests := []struct {
|
||||
strategy resources.StorageResourcePricingStrategy
|
||||
dataGB float64
|
||||
expect float64
|
||||
}{
|
||||
{resources.PER_DATA_STORED, 1.2, 1.2},
|
||||
{resources.PER_TB_STORED, 1.2, 1200},
|
||||
{resources.PER_GB_STORED, 2.5, 2.5},
|
||||
{resources.PER_MB_STORED, 1.0, 1000},
|
||||
{resources.PER_KB_STORED, 0.1, 100000},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
q, err := tt.strategy.GetQuantity(tt.dataGB)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expect, q)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageResourcePricingStrategy_GetQuantity_Invalid(t *testing.T) {
|
||||
invalid := resources.StorageResourcePricingStrategy(99)
|
||||
q, err := invalid.GetQuantity(1.0)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 0.0, q)
|
||||
}
|
||||
|
||||
func TestPricedStorageResource_GetPrice_NoProfiles(t *testing.T) {
|
||||
res := &resources.PricedStorageResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "res-id",
|
||||
},
|
||||
}
|
||||
_, err := res.GetPrice()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPricedStorageResource_GetPrice_WithPricing(t *testing.T) {
|
||||
now := time.Now()
|
||||
end := now.Add(2 * time.Hour)
|
||||
profile := &resources.StorageResourcePricingProfile{
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[resources.StorageResourcePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[resources.StorageResourcePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[resources.StorageResourcePricingStrategy]{
|
||||
BuyingStrategy: pricing.PAY_PER_USE,
|
||||
Price: 42.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
res := &resources.PricedStorageResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &end,
|
||||
PricingProfiles: []pricing.PricingProfileITF{profile},
|
||||
},
|
||||
UsageStorageGB: 1.0,
|
||||
}
|
||||
price, err := res.GetPrice()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42.0, price)
|
||||
}
|
||||
|
||||
func TestStorageResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
p := &resources.StorageResourcePricingProfile{
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[resources.StorageResourcePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[resources.StorageResourcePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[resources.StorageResourcePricingStrategy]{BuyingStrategy: pricing.PAY_PER_USE},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.False(t, p.IsPurchased())
|
||||
|
||||
p.Pricing.BuyingStrategy = pricing.UNLIMITED
|
||||
assert.True(t, p.IsPurchased())
|
||||
}
|
||||
62
models/resources/tests/workflow_test.go
Normal file
62
models/resources/tests/workflow_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
)
|
||||
|
||||
func TestWorkflowResource_GetType(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
assert.Equal(t, tools.WORKFLOW_RESOURCE.String(), w.GetType())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_ConvertToPricedResource(t *testing.T) {
|
||||
w := &resources.WorkflowResource{
|
||||
AbstractResource: resources.AbstractResource{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
Name: "Test Workflow",
|
||||
UUID: "workflow-uuid",
|
||||
CreatorID: "creator-id",
|
||||
},
|
||||
Logo: "logo.png",
|
||||
},
|
||||
}
|
||||
|
||||
req := &tools.APIRequest{
|
||||
PeerID: "peer-1",
|
||||
Groups: []string{"group1"},
|
||||
}
|
||||
|
||||
pr := w.ConvertToPricedResource(tools.WORKFLOW_RESOURCE, req)
|
||||
assert.Equal(t, "creator-id", pr.GetCreatorID())
|
||||
assert.Equal(t, tools.WORKFLOW_RESOURCE, pr.GetType())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_ClearEnv(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
assert.Equal(t, w, w.ClearEnv())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_Trim(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
w.Trim()
|
||||
// nothing to assert; just test that it doesn't panic
|
||||
}
|
||||
|
||||
func TestWorkflowResource_SetAllowedInstances(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
w.SetAllowedInstances(&tools.APIRequest{})
|
||||
// no-op; just confirm no crash
|
||||
}
|
||||
|
||||
func TestWorkflowResource_GetAccessor(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
request := &tools.APIRequest{}
|
||||
accessor := w.GetAccessor(request)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
Reference in New Issue
Block a user