|
| 1 | +package workspace |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/hcl/v2" |
| 7 | + "github.com/turbot/pipe-fittings/v2/modconfig" |
| 8 | + pfworkspace "github.com/turbot/pipe-fittings/v2/workspace" |
| 9 | + "github.com/turbot/powerpipe/internal/resources" |
| 10 | +) |
| 11 | + |
| 12 | +func TestResourceFilterFromTagArgs(t *testing.T) { |
| 13 | + mod := modconfig.NewMod("tag_filter_mod", ".", hcl.Range{}) |
| 14 | + controls := map[string]*resources.Control{ |
| 15 | + "deprecated_true": makeTaggedControl(t, mod, "deprecated_true", map[string]string{"deprecated": "true"}), |
| 16 | + "deprecated_false": makeTaggedControl(t, mod, "deprecated_false", map[string]string{"deprecated": "false"}), |
| 17 | + "no_tag": makeTaggedControl(t, mod, "no_tag", map[string]string{}), |
| 18 | + "other_tag_only": makeTaggedControl(t, mod, "other_tag_only", map[string]string{"env": "qa"}), |
| 19 | + } |
| 20 | + modResources := resources.NewModResources(mod).(*resources.PowerpipeModResources) |
| 21 | + for _, c := range controls { |
| 22 | + _ = modResources.AddResource(c) |
| 23 | + } |
| 24 | + mod.Resources = modResources |
| 25 | + |
| 26 | + w := &PowerpipeWorkspace{ |
| 27 | + Workspace: pfworkspace.Workspace{ |
| 28 | + Mod: mod, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + tagArgs []string |
| 35 | + wantNames map[string]struct{} |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "equals match", |
| 39 | + tagArgs: []string{"deprecated=true"}, |
| 40 | + wantNames: map[string]struct{}{ |
| 41 | + "tag_filter_mod.control.deprecated_true": {}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "not equals includes missing", |
| 46 | + tagArgs: []string{"deprecated!=true"}, |
| 47 | + wantNames: map[string]struct{}{ |
| 48 | + "tag_filter_mod.control.deprecated_false": {}, |
| 49 | + "tag_filter_mod.control.no_tag": {}, |
| 50 | + "tag_filter_mod.control.other_tag_only": {}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "not equals multiple values includes missing", |
| 55 | + tagArgs: []string{"deprecated!=true", "deprecated!=false"}, |
| 56 | + wantNames: map[string]struct{}{ |
| 57 | + "tag_filter_mod.control.no_tag": {}, |
| 58 | + "tag_filter_mod.control.other_tag_only": {}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "mix equals and not equals honors both", |
| 63 | + tagArgs: []string{"deprecated!=true", "env=qa"}, |
| 64 | + wantNames: map[string]struct{}{ |
| 65 | + "tag_filter_mod.control.other_tag_only": {}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + filter := ResourceFilterFromTagArgs(tt.tagArgs) |
| 73 | + got, err := pfworkspace.FilterWorkspaceResourcesOfType[*resources.Control](&w.Workspace, filter) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("FilterWorkspaceResourcesOfType error: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if len(got) != len(tt.wantNames) { |
| 79 | + t.Fatalf("expected %d results, got %d", len(tt.wantNames), len(got)) |
| 80 | + } |
| 81 | + for name := range tt.wantNames { |
| 82 | + if _, ok := got[name]; !ok { |
| 83 | + t.Fatalf("expected %s but not present", name) |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func makeTaggedControl(t *testing.T, mod *modconfig.Mod, name string, tags map[string]string) *resources.Control { |
| 91 | + t.Helper() |
| 92 | + |
| 93 | + title := "title" |
| 94 | + description := "desc" |
| 95 | + sql := "select 1" |
| 96 | + control := resources.NewControl(&hcl.Block{Type: "control"}, mod, name).(*resources.Control) |
| 97 | + control.Title = &title |
| 98 | + control.Description = &description |
| 99 | + control.SQL = &sql |
| 100 | + control.Tags = tags |
| 101 | + |
| 102 | + return control |
| 103 | +} |
0 commit comments