|
| 1 | +package middlewares |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws/awsutil" |
| 8 | + "github.com/cloudskiff/driftctl/pkg/resource" |
| 9 | + "github.com/r3labs/diff/v2" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTagsAllManager_Execute(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + remoteResources *[]resource.Resource |
| 16 | + resourcesFromState *[]resource.Resource |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "With multiple resources that are tags_all compatible", |
| 21 | + remoteResources: &[]resource.Resource{ |
| 22 | + &resource.AbstractResource{ |
| 23 | + Id: "dummy-instance", |
| 24 | + Type: "aws_instance", |
| 25 | + Attrs: &resource.Attributes{ |
| 26 | + "tags": map[string]interface{}{ |
| 27 | + "Name": "toto", |
| 28 | + "Terraform": "true", |
| 29 | + }, |
| 30 | + "tags_all": map[string]interface{}{ |
| 31 | + "Name": "toto", |
| 32 | + "Terraform": "true", |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + &resource.AbstractResource{ |
| 37 | + Id: "dummy-ebs-volume", |
| 38 | + Type: "aws_ebs_volume", |
| 39 | + Attrs: &resource.Attributes{ |
| 40 | + "tags": map[string]interface{}{ |
| 41 | + "Name": "tata", |
| 42 | + "Terraform": "true", |
| 43 | + }, |
| 44 | + "tags_all": map[string]interface{}{ |
| 45 | + "Name": "tata", |
| 46 | + "Terraform": "true", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + resourcesFromState: &[]resource.Resource{ |
| 52 | + &resource.AbstractResource{ |
| 53 | + Id: "dummy-instance", |
| 54 | + Type: "aws_instance", |
| 55 | + Attrs: &resource.Attributes{ |
| 56 | + "tags": map[string]interface{}{ |
| 57 | + "Name": "toto", |
| 58 | + }, |
| 59 | + "tags_all": map[string]interface{}{ |
| 60 | + "Name": "toto", |
| 61 | + "Terraform": "true", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + &resource.AbstractResource{ |
| 66 | + Id: "dummy-ebs-volume", |
| 67 | + Type: "aws_ebs_volume", |
| 68 | + Attrs: &resource.Attributes{ |
| 69 | + "tags": map[string]interface{}{ |
| 70 | + "Name": "tata", |
| 71 | + }, |
| 72 | + "tags_all": map[string]interface{}{ |
| 73 | + "Name": "tata", |
| 74 | + "Terraform": "true", |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + for _, tt := range tests { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + a := NewTagsAllManager() |
| 84 | + if err := a.Execute(tt.remoteResources, tt.resourcesFromState); (err != nil) != tt.wantErr { |
| 85 | + t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr) |
| 86 | + } |
| 87 | + changelog, err := diff.Diff(tt.resourcesFromState, tt.remoteResources) |
| 88 | + if err != nil { |
| 89 | + t.Error(err) |
| 90 | + } |
| 91 | + if len(changelog) > 0 { |
| 92 | + for _, change := range changelog { |
| 93 | + t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To)) |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments