github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/quota_test.go (about) 1 package gcp 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 8 "github.com/openshift/installer/pkg/types/gcp" 9 ) 10 11 func TestMergeAllUsage(t *testing.T) { 12 var testCases = []struct { 13 name string 14 into []gcp.QuotaUsage 15 update []gcp.QuotaUsage 16 expected []gcp.QuotaUsage 17 }{ 18 { 19 name: "no previous data, one new", 20 into: nil, 21 update: []gcp.QuotaUsage{{Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}}, 22 expected: []gcp.QuotaUsage{{Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}}, 23 }, 24 { 25 name: "no previous data, many new", 26 into: nil, 27 update: []gcp.QuotaUsage{ 28 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 29 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 1}, 30 }, 31 expected: []gcp.QuotaUsage{ 32 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 33 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 1}, 34 }, 35 }, 36 { 37 name: "merge adds new entry", 38 into: []gcp.QuotaUsage{ 39 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 40 }, 41 update: []gcp.QuotaUsage{ 42 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 1}, 43 }, 44 expected: []gcp.QuotaUsage{ 45 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 46 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 1}, 47 }, 48 }, 49 { 50 name: "merge updates current entry", 51 into: []gcp.QuotaUsage{ 52 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 53 }, 54 update: []gcp.QuotaUsage{ 55 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 56 }, 57 expected: []gcp.QuotaUsage{ 58 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 2}, 59 }, 60 }, 61 { 62 name: "merge updates current entry only with full match", 63 into: []gcp.QuotaUsage{ 64 {Metric: &gcp.Metric{Service: "s", Limit: "l", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, 65 }, 66 update: []gcp.QuotaUsage{ 67 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, // no dimensions 68 {Metric: &gcp.Metric{Service: "s", Limit: "L", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, // different limit 69 {Metric: &gcp.Metric{Service: "S", Limit: "l", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, // different service 70 {Metric: &gcp.Metric{Service: "s", Limit: "l", Dimensions: map[string]string{"a": "B"}}, Amount: 1}, // different dimensions 71 {Metric: &gcp.Metric{Service: "s", Limit: "l", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, // match 72 }, 73 expected: []gcp.QuotaUsage{ 74 {Metric: &gcp.Metric{Service: "s", Limit: "l", Dimensions: map[string]string{"a": "b"}}, Amount: 2}, 75 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 76 {Metric: &gcp.Metric{Service: "s", Limit: "L", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, 77 {Metric: &gcp.Metric{Service: "S", Limit: "l", Dimensions: map[string]string{"a": "b"}}, Amount: 1}, 78 {Metric: &gcp.Metric{Service: "s", Limit: "l", Dimensions: map[string]string{"a": "B"}}, Amount: 1}, 79 }, 80 }, 81 } 82 for _, testCase := range testCases { 83 t.Run(testCase.name, func(t *testing.T) { 84 if diff := cmp.Diff(mergeAllUsage(testCase.into, testCase.update), testCase.expected, cmp.AllowUnexported(gcp.QuotaUsage{}, gcp.Metric{})); diff != "" { 85 t.Errorf("%s: got incorrect usage after merge: %v", testCase.name, diff) 86 } 87 }) 88 } 89 } 90 91 func TestQuotaIntegration(t *testing.T) { 92 uninstaller := &ClusterUninstaller{ 93 pendingItemTracker: newPendingItemTracker(), 94 } 95 uninstaller.insertPendingItems("fake", []cloudResource{{ 96 key: "first", 97 quota: nil, 98 }, { 99 key: "second", 100 quota: []gcp.QuotaUsage{ 101 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 1}, 102 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 1}, 103 }, 104 }, { 105 key: "third", 106 quota: []gcp.QuotaUsage{ 107 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 123}, 108 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 111}, 109 }, 110 }}) 111 for _, item := range uninstaller.getPendingItems("fake") { 112 uninstaller.deletePendingItems("fake", []cloudResource{item}) 113 } 114 if diff := cmp.Diff(uninstaller.pendingItemTracker.removedQuota, []gcp.QuotaUsage{ 115 {Metric: &gcp.Metric{Service: "s", Limit: "l"}, Amount: 124}, 116 {Metric: &gcp.Metric{Service: "S", Limit: "L"}, Amount: 112}, 117 }, cmp.AllowUnexported(gcp.QuotaUsage{}, gcp.Metric{})); diff != "" { 118 t.Errorf("didn't get correct removed quota: %v", diff) 119 } 120 }