forked from hobbyfarm/gargantua
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8db7f04
commit 097d607
Showing
11 changed files
with
224 additions
and
74 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,157 @@ | ||
package costservice | ||
|
||
import ( | ||
"github.com/hobbyfarm/gargantua/v3/pkg/labels" | ||
"github.com/hobbyfarm/gargantua/v3/pkg/util" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_newCostGroup(t *testing.T) { | ||
creationUnixTimestamp := int64(100) | ||
creation := time.Unix(creationUnixTimestamp, 0) | ||
|
||
tests := []struct { | ||
name string | ||
input *unstructured.Unstructured | ||
want *costGroup | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "ok", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostGroup: "my-cost-group", | ||
labels.CostBasePrice: "10.01", | ||
labels.CostTimeUnit: util.TimeUnitSeconds, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &costGroup{ | ||
Id: "vm-test", | ||
Kind: "VirtualMachine", | ||
CostGroup: "my-cost-group", | ||
BasePrice: 10.01, | ||
TimeUnit: util.TimeUnitSeconds, | ||
CreationTimestamp: creationUnixTimestamp, | ||
}, | ||
}, | ||
{ | ||
name: "no cost group", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostBasePrice: "10.01", | ||
labels.CostTimeUnit: util.TimeUnitSeconds, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return assert.ErrorContains(t, err, labels.CostGroup) | ||
}, | ||
}, | ||
{ | ||
name: "no base price", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostGroup: "my-cost-group", | ||
labels.CostTimeUnit: util.TimeUnitSeconds, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return assert.ErrorContains(t, err, labels.CostBasePrice) | ||
}, | ||
}, | ||
{ | ||
name: "invalid base price", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostGroup: "my-cost-group", | ||
labels.CostBasePrice: "invalid", | ||
labels.CostTimeUnit: util.TimeUnitSeconds, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return assert.ErrorContains(t, err, labels.CostBasePrice) | ||
}, | ||
}, | ||
|
||
{ | ||
name: "no time unit", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostGroup: "my-cost-group", | ||
labels.CostBasePrice: "10.01", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return assert.ErrorContains(t, err, labels.CostTimeUnit) | ||
}, | ||
}, | ||
{ | ||
name: "invalid base price", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "VirtualMachine", | ||
"metadata": map[string]interface{}{ | ||
"name": "vm-test", | ||
"creationTimestamp": creation.Format(time.RFC3339), | ||
"labels": map[string]interface{}{ | ||
labels.CostGroup: "my-cost-group", | ||
labels.CostBasePrice: "10.01", | ||
labels.CostTimeUnit: "invalid", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return assert.ErrorContains(t, err, labels.CostTimeUnit) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := newCostGroup(tt.input) | ||
if tt.wantErr != nil { | ||
tt.wantErr(t, err) | ||
} else { | ||
assert.NoErrorf(t, err, "newCostGroup() error = %v", err) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
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.