|
| 1 | +// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package acctest |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "reflect" |
| 12 | + "testing" |
| 13 | + |
| 14 | + oci_identity "github.com/oracle/oci-go-sdk/v60/identity" |
| 15 | +) |
| 16 | + |
| 17 | +func setUp(t *testing.T) { |
| 18 | + |
| 19 | + SweeperResourceCompartmentIdMap = map[string]map[string][]string{"compartment-1_ocid": { |
| 20 | + "vcn": []string{"vcn-1_ocid"}, |
| 21 | + }, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func tearDown(t *testing.T) { |
| 26 | + SweeperResourceCompartmentIdMap = map[string]map[string][]string{} |
| 27 | +} |
| 28 | + |
| 29 | +func TestUnitAddResourceIdToSweeperResourceIdMap(t *testing.T) { |
| 30 | + |
| 31 | + testsweeperResourceCompartmentIdMapWithCompID := map[string]map[string][]string{ |
| 32 | + "compartment-1_ocid": { |
| 33 | + "vcn": []string{"vcn-1_ocid", "vcn-2_ocid"}, |
| 34 | + }, |
| 35 | + } |
| 36 | + testsweeperResourceCompartmentIdMapWithNonExistentCompID := map[string]map[string][]string{ |
| 37 | + "nonexistent_compID": { |
| 38 | + "vcn": []string{"vcn-1_ocid"}, |
| 39 | + }, |
| 40 | + } |
| 41 | + testsweeperResourceCompartmentIdMapWithNonExistentResourceType := map[string]map[string][]string{ |
| 42 | + "compartment-1_ocid": { |
| 43 | + "vcn": []string{"vcn-1_ocid"}, |
| 44 | + "nonexistent_resource_type": []string{"vcn-1_ocid"}, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + type args struct { |
| 49 | + compartmentId string |
| 50 | + resourceType string |
| 51 | + resourceId string |
| 52 | + } |
| 53 | + |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + args args |
| 57 | + want map[string]map[string][]string |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "Test with nonexistent value for compartmentId", |
| 61 | + args: args{ |
| 62 | + compartmentId: "nonexistent_compID", |
| 63 | + resourceType: "vcn", |
| 64 | + resourceId: "vcn-1_ocid", |
| 65 | + }, |
| 66 | + want: testsweeperResourceCompartmentIdMapWithNonExistentCompID, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Test for existing compartmentId but nonexistent resourceType", |
| 70 | + args: args{ |
| 71 | + compartmentId: "compartment-1_ocid", |
| 72 | + resourceType: "nonexistent_resource_type", |
| 73 | + resourceId: "vcn-1_ocid", |
| 74 | + }, |
| 75 | + want: testsweeperResourceCompartmentIdMapWithNonExistentResourceType, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Test for existing compartmentId and resourceType", |
| 79 | + args: args{ |
| 80 | + compartmentId: "compartment-1_ocid", |
| 81 | + resourceType: "vcn", |
| 82 | + resourceId: "vcn-2_ocid", |
| 83 | + }, |
| 84 | + want: testsweeperResourceCompartmentIdMapWithCompID, |
| 85 | + }, |
| 86 | + } |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + setUp(t) |
| 90 | + defer tearDown(t) |
| 91 | + AddResourceIdToSweeperResourceIdMap(tt.args.compartmentId, tt.args.resourceType, tt.args.resourceId) |
| 92 | + if !reflect.DeepEqual(SweeperResourceCompartmentIdMap, tt.want) { |
| 93 | + t.Errorf("AddResourceIdToSweeperResourceIdMap() = %v, want %v", SweeperResourceCompartmentIdMap, tt.want) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +func TestUnitInSweeperExcludeList(t *testing.T) { |
| 102 | + type args struct { |
| 103 | + sweeperName string |
| 104 | + } |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + args args |
| 108 | + want bool |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "sweeperName present in sweep_exclude_list", |
| 112 | + args: args{ |
| 113 | + sweeperName: "EmailSuppression", |
| 114 | + }, |
| 115 | + want: true, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "sweeperName not present in sweep_exclude_list", |
| 119 | + args: args{ |
| 120 | + sweeperName: "AbsentSweeper", |
| 121 | + }, |
| 122 | + want: false, |
| 123 | + }, |
| 124 | + } |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + os.Setenv("sweep_exclude_list", "EmailSuppression,EmailSender,BudgetBudget,AiAnomalyDetectionModel,AiAnomalyDetectionDataAsset,AiAnomalyDetectionProject") |
| 128 | + defer os.Unsetenv("sweep_exclude_list") |
| 129 | + if got := InSweeperExcludeList(tt.args.sweeperName); got != tt.want { |
| 130 | + t.Errorf("InSweeperExcludeList() = %v, want %v", got, tt.want) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +func TestUnitGetAvalabilityDomains(t *testing.T) { |
| 138 | + type args struct { |
| 139 | + compartmentId string |
| 140 | + } |
| 141 | + |
| 142 | + identityClientListAvailabilityDomains = func(client *oci_identity.IdentityClient, adRequest oci_identity.ListAvailabilityDomainsRequest) (oci_identity.ListAvailabilityDomainsResponse, error) { |
| 143 | + Id := "1" |
| 144 | + Name := "Domain1" |
| 145 | + CompartmentId := "compID" |
| 146 | + opcRequestId := "test_opc" |
| 147 | + if *adRequest.CompartmentId == "compID" { |
| 148 | + return oci_identity.ListAvailabilityDomainsResponse{ |
| 149 | + OpcRequestId: &opcRequestId, |
| 150 | + RawResponse: &http.Response{}, |
| 151 | + Items: []oci_identity.AvailabilityDomain{{ |
| 152 | + CompartmentId: &CompartmentId, |
| 153 | + Id: &Id, |
| 154 | + Name: &Name, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, nil |
| 158 | + } |
| 159 | + return oci_identity.ListAvailabilityDomainsResponse{}, errors.New("invalid compId") |
| 160 | + } |
| 161 | + |
| 162 | + tests := []struct { |
| 163 | + name string |
| 164 | + args args |
| 165 | + want map[string]string |
| 166 | + wantErr bool |
| 167 | + }{ |
| 168 | + { |
| 169 | + name: "Test with valid compartmentID", |
| 170 | + args: args{ |
| 171 | + compartmentId: "compID", |
| 172 | + }, |
| 173 | + |
| 174 | + want: map[string]string{"1": "Domain1"}, |
| 175 | + wantErr: false, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "Test with invalid compartmentID", |
| 179 | + args: args{ |
| 180 | + compartmentId: "invalid_compID", |
| 181 | + }, |
| 182 | + want: map[string]string{}, |
| 183 | + wantErr: true, |
| 184 | + }, |
| 185 | + } |
| 186 | + for _, tt := range tests { |
| 187 | + t.Run(tt.name, func(t *testing.T) { |
| 188 | + got, err := GetAvalabilityDomains(tt.args.compartmentId) |
| 189 | + fmt.Println(err) |
| 190 | + if (err != nil) != tt.wantErr { |
| 191 | + t.Errorf("GetAvalabilityDomains() error = %v, wantErr %v", err, tt.wantErr) |
| 192 | + return |
| 193 | + } |
| 194 | + if !reflect.DeepEqual(got, tt.want) { |
| 195 | + t.Errorf("GetAvalabilityDomains() = %v, want %v", got, tt.want) |
| 196 | + } |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func TestUnitGetResourceIdsToSweep(t *testing.T) { |
| 202 | + type args struct { |
| 203 | + compartmentId string |
| 204 | + resourceName string |
| 205 | + } |
| 206 | + |
| 207 | + tests := []struct { |
| 208 | + name string |
| 209 | + args args |
| 210 | + want []string |
| 211 | + }{ |
| 212 | + { |
| 213 | + name: "Test for nonexisting compartmentId", |
| 214 | + args: args{ |
| 215 | + compartmentId: "non_existing_compID", |
| 216 | + resourceName: "vcn", |
| 217 | + }, |
| 218 | + want: nil, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "Test for existing compartmentId but nonexisting resourceName", |
| 222 | + args: args{ |
| 223 | + compartmentId: "compartment-1_ocid", |
| 224 | + resourceName: "non_existing_resource_name", |
| 225 | + }, |
| 226 | + want: nil, |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "Test with existing compartmentId and resourceName", |
| 230 | + args: args{ |
| 231 | + compartmentId: "compartment-1_ocid", |
| 232 | + resourceName: "vcn", |
| 233 | + }, |
| 234 | + want: []string{"vcn-1_ocid"}, |
| 235 | + }, |
| 236 | + } |
| 237 | + for _, tt := range tests { |
| 238 | + t.Run(tt.name, func(t *testing.T) { |
| 239 | + setUp(t) |
| 240 | + defer tearDown(t) |
| 241 | + if got := GetResourceIdsToSweep(tt.args.compartmentId, tt.args.resourceName); !reflect.DeepEqual(got, tt.want) { |
| 242 | + t.Errorf("GetResourceIdsToSweep() = %v, want %v", got, tt.want) |
| 243 | + } |
| 244 | + }) |
| 245 | + } |
| 246 | +} |
0 commit comments