| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- package opencost
- import "testing"
- func TestCloudCostPropertiesIntersection(t *testing.T) {
- testCases := map[string]struct {
- baseCCP *CloudCostProperties
- intCCP *CloudCostProperties
- expectedCCP *CloudCostProperties
- }{
- "When properties match between both CloudCostProperties": {
- baseCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- intCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- expectedCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- },
- "When one of the properties differ in the two CloudCostProperties": {
- baseCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- intCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service2",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- expectedCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- },
- "When two of the properties differ in the two CloudCostProperties": {
- baseCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- intCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID2",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service2",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- expectedCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- },
- "When all properties differ in the two CloudCostProperties": {
- baseCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- },
- },
- intCCP: &CloudCostProperties{
- Provider: "CustomProvider2",
- ProviderID: "ProviderID2",
- AccountID: "WorkGroupID2",
- AccountName: "AccountName2",
- InvoiceEntityID: "InvoiceEntityID2",
- InvoiceEntityName: "InvoiceEntityName2",
- RegionID: "RegionID2",
- AvailabilityZone: "AvailabilityZone2",
- Service: "Service2",
- Category: "Category2",
- Labels: map[string]string{
- "key2": "value2",
- },
- },
- expectedCCP: &CloudCostProperties{
- Provider: "",
- ProviderID: "",
- AccountID: "",
- AccountName: "",
- InvoiceEntityID: "",
- InvoiceEntityName: "",
- RegionID: "",
- AvailabilityZone: "",
- Service: "",
- Category: "",
- Labels: map[string]string{},
- },
- },
- "When labels differ": {
- baseCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value1",
- "key2": "value2",
- "key3": "value3",
- },
- },
- intCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key1": "value2",
- "key2": "value2",
- "key4": "value4",
- },
- },
- expectedCCP: &CloudCostProperties{
- Provider: "CustomProvider",
- ProviderID: "ProviderID1",
- AccountID: "WorkGroupID1",
- AccountName: "AccountName1",
- InvoiceEntityID: "InvoiceEntityID1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "Service1",
- Category: "Category1",
- Labels: map[string]string{
- "key2": "value2",
- },
- },
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- actualCCP := tc.baseCCP.Intersection(tc.intCCP)
- if !actualCCP.Equal(tc.expectedCCP) {
- t.Errorf("Case %s: properties dont match with expected CloudCostProperties: %v actual %v", name, tc.expectedCCP, actualCCP)
- }
- })
- }
- }
- func TestCloudCostProperties_hashKey(t *testing.T) {
- tests := map[string]struct {
- props *CloudCostProperties
- want string
- }{
- "enpty props": {
- props: &CloudCostProperties{},
- want: "cbf29ce484222325",
- },
- "All props no labels": {
- props: &CloudCostProperties{
- ProviderID: "providerid1",
- Provider: "provider1",
- AccountID: "workgroup1",
- AccountName: "AccountName1",
- InvoiceEntityID: "billing1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "service1",
- Category: "category1",
- Labels: map[string]string{},
- },
- want: "d07ffd0bd6d5eaf1",
- },
- "All props": {
- props: &CloudCostProperties{
- ProviderID: "providerid1",
- Provider: "provider1",
- AccountID: "workgroup1",
- AccountName: "AccountName1",
- InvoiceEntityID: "billing1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "service1",
- Category: "category1",
- Labels: map[string]string{
- "label1": "value1",
- "label2": "value2",
- },
- },
- want: "318cb6294bf9e2d5",
- },
- "All props swap labels": {
- props: &CloudCostProperties{
- ProviderID: "providerid1",
- Provider: "provider1",
- AccountID: "workgroup1",
- AccountName: "AccountName1",
- InvoiceEntityID: "billing1",
- InvoiceEntityName: "InvoiceEntityName1",
- RegionID: "RegionID1",
- AvailabilityZone: "AvailabilityZone1",
- Service: "service1",
- Category: "category1",
- Labels: map[string]string{
- "label2": "value2",
- "label1": "value1",
- },
- },
- want: "318cb6294bf9e2d5",
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- if got := tt.props.hashKey(); got != tt.want {
- t.Errorf("hashKey() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|