|
| 1 | +package middlewares |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws/awsutil" |
| 8 | + "github.com/r3labs/diff/v2" |
| 9 | + "github.com/snyk/driftctl/pkg/resource" |
| 10 | + "github.com/snyk/driftctl/pkg/resource/aws" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAwsApiGatewayBasePathMappingReconciler_Execute(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + resourcesFromState []*resource.Resource |
| 17 | + remoteResources []*resource.Resource |
| 18 | + expected []*resource.Resource |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "with managed resources", |
| 22 | + resourcesFromState: []*resource.Resource{ |
| 23 | + { |
| 24 | + Id: "mapping1", |
| 25 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 26 | + }, |
| 27 | + { |
| 28 | + Id: "mapping2", |
| 29 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 30 | + }, |
| 31 | + }, |
| 32 | + remoteResources: []*resource.Resource{ |
| 33 | + { |
| 34 | + Id: "mapping1", |
| 35 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 36 | + }, |
| 37 | + { |
| 38 | + Id: "mapping1", |
| 39 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 40 | + }, |
| 41 | + { |
| 42 | + Id: "mapping2", |
| 43 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 44 | + }, |
| 45 | + { |
| 46 | + Id: "mapping2", |
| 47 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 48 | + }, |
| 49 | + }, |
| 50 | + expected: []*resource.Resource{ |
| 51 | + { |
| 52 | + Id: "mapping1", |
| 53 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 54 | + }, |
| 55 | + { |
| 56 | + Id: "mapping2", |
| 57 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "with unmanaged resources", |
| 63 | + resourcesFromState: []*resource.Resource{}, |
| 64 | + remoteResources: []*resource.Resource{ |
| 65 | + { |
| 66 | + Id: "mapping1", |
| 67 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 68 | + }, |
| 69 | + { |
| 70 | + Id: "mapping1", |
| 71 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 72 | + }, |
| 73 | + { |
| 74 | + Id: "mapping2", |
| 75 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 76 | + }, |
| 77 | + { |
| 78 | + Id: "mapping2", |
| 79 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 80 | + }, |
| 81 | + }, |
| 82 | + expected: []*resource.Resource{ |
| 83 | + { |
| 84 | + Id: "mapping1", |
| 85 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 86 | + }, |
| 87 | + { |
| 88 | + Id: "mapping2", |
| 89 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "with deleted resources", |
| 95 | + resourcesFromState: []*resource.Resource{ |
| 96 | + { |
| 97 | + Id: "mapping1", |
| 98 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 99 | + }, |
| 100 | + { |
| 101 | + Id: "mapping2", |
| 102 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 103 | + }, |
| 104 | + }, |
| 105 | + remoteResources: []*resource.Resource{}, |
| 106 | + expected: []*resource.Resource{}, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "with a mix of managed, unmanaged and deleted resources", |
| 110 | + resourcesFromState: []*resource.Resource{ |
| 111 | + { |
| 112 | + Id: "mapping1", |
| 113 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 114 | + }, |
| 115 | + { |
| 116 | + Id: "mapping2", |
| 117 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 118 | + }, |
| 119 | + { |
| 120 | + Id: "mapping4", |
| 121 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 122 | + }, |
| 123 | + }, |
| 124 | + remoteResources: []*resource.Resource{ |
| 125 | + { |
| 126 | + Id: "mapping1", |
| 127 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 128 | + }, |
| 129 | + { |
| 130 | + Id: "mapping1", |
| 131 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 132 | + }, |
| 133 | + { |
| 134 | + Id: "mapping2", |
| 135 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 136 | + }, |
| 137 | + { |
| 138 | + Id: "mapping2", |
| 139 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 140 | + }, |
| 141 | + { |
| 142 | + Id: "mapping3", |
| 143 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 144 | + }, |
| 145 | + { |
| 146 | + Id: "mapping3", |
| 147 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 148 | + }, |
| 149 | + }, |
| 150 | + expected: []*resource.Resource{ |
| 151 | + { |
| 152 | + Id: "mapping1", |
| 153 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 154 | + }, |
| 155 | + { |
| 156 | + Id: "mapping2", |
| 157 | + Type: aws.AwsApiGatewayV2MappingResourceType, |
| 158 | + }, |
| 159 | + { |
| 160 | + Id: "mapping3", |
| 161 | + Type: aws.AwsApiGatewayBasePathMappingResourceType, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + m := NewAwsApiGatewayBasePathMappingReconciler() |
| 169 | + err := m.Execute(&tt.remoteResources, &tt.resourcesFromState) |
| 170 | + if err != nil { |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + changelog, err := diff.Diff(tt.expected, tt.remoteResources) |
| 174 | + if err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + if len(changelog) > 0 { |
| 178 | + for _, change := range changelog { |
| 179 | + t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To)) |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
0 commit comments