github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/command/arguments/add_test.go (about) 1 package arguments 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/hashicorp/terraform/internal/addrs" 8 "github.com/hashicorp/terraform/internal/tfdiags" 9 ) 10 11 func TestParseAdd(t *testing.T) { 12 tests := map[string]struct { 13 args []string 14 want *Add 15 wantError string 16 }{ 17 "defaults": { 18 []string{"test_foo.bar"}, 19 &Add{ 20 Addr: mustResourceInstanceAddr("test_foo.bar"), 21 State: &State{Lock: true}, 22 ViewType: ViewHuman, 23 }, 24 ``, 25 }, 26 "some flags": { 27 []string{"-optional=true", "test_foo.bar"}, 28 &Add{ 29 Addr: mustResourceInstanceAddr("test_foo.bar"), 30 State: &State{Lock: true}, 31 Optional: true, 32 ViewType: ViewHuman, 33 }, 34 ``, 35 }, 36 "-from-state": { 37 []string{"-from-state", "module.foo.test_foo.baz"}, 38 &Add{ 39 Addr: mustResourceInstanceAddr("module.foo.test_foo.baz"), 40 State: &State{Lock: true}, 41 ViewType: ViewHuman, 42 FromState: true, 43 }, 44 ``, 45 }, 46 "-provider": { 47 []string{"-provider=provider[\"example.com/happycorp/test\"]", "test_foo.bar"}, 48 &Add{ 49 Addr: mustResourceInstanceAddr("test_foo.bar"), 50 State: &State{Lock: true}, 51 ViewType: ViewHuman, 52 Provider: &addrs.AbsProviderConfig{ 53 Provider: addrs.NewProvider("example.com", "happycorp", "test"), 54 }, 55 }, 56 ``, 57 }, 58 "state options from extended flag set": { 59 []string{"-state=local.tfstate", "test_foo.bar"}, 60 &Add{ 61 Addr: mustResourceInstanceAddr("test_foo.bar"), 62 State: &State{Lock: true, StatePath: "local.tfstate"}, 63 ViewType: ViewHuman, 64 }, 65 ``, 66 }, 67 68 // Error cases 69 "missing required argument": { 70 nil, 71 &Add{ 72 ViewType: ViewHuman, 73 State: &State{Lock: true}, 74 }, 75 `Too few command line arguments`, 76 }, 77 "too many arguments": { 78 []string{"-from-state", "resource_foo.bar", "module.foo.resource_foo.baz"}, 79 &Add{ 80 ViewType: ViewHuman, 81 State: &State{Lock: true}, 82 FromState: true, 83 }, 84 `Too many command line arguments`, 85 }, 86 "invalid target address": { 87 []string{"definitely-not_a-VALID-resource"}, 88 &Add{ 89 ViewType: ViewHuman, 90 State: &State{Lock: true}, 91 }, 92 `Error parsing resource address: definitely-not_a-VALID-resource`, 93 }, 94 "invalid provider flag": { 95 []string{"-provider=/this/isn't/quite/correct", "resource_foo.bar"}, 96 &Add{ 97 Addr: mustResourceInstanceAddr("resource_foo.bar"), 98 ViewType: ViewHuman, 99 State: &State{Lock: true}, 100 }, 101 `Invalid provider string: /this/isn't/quite/correct`, 102 }, 103 "incompatible options": { 104 []string{"-from-state", "-provider=provider[\"example.com/happycorp/test\"]", "test_compute.bar"}, 105 &Add{ViewType: ViewHuman, 106 Addr: mustResourceInstanceAddr("test_compute.bar"), 107 State: &State{Lock: true}, 108 FromState: true, 109 }, 110 `Incompatible command-line options`, 111 }, 112 } 113 114 for name, test := range tests { 115 t.Run(name, func(t *testing.T) { 116 got, diags := ParseAdd(test.args) 117 if test.wantError != "" { 118 if len(diags) != 1 { 119 t.Fatalf("got %d diagnostics; want exactly 1\n", len(diags)) 120 } 121 if diags[0].Severity() != tfdiags.Error { 122 t.Fatalf("got a warning; want an error\n%s", diags.ErrWithWarnings()) 123 } 124 if desc := diags[0].Description(); desc.Summary != test.wantError { 125 t.Fatalf("wrong error\ngot: %s\nwant: %s", desc.Summary, test.wantError) 126 } 127 } else { 128 if len(diags) != 0 { 129 t.Fatalf("got %d diagnostics; want none\n%s", len(diags), diags.Err().Error()) 130 } 131 } 132 133 if diff := cmp.Diff(test.want, got); diff != "" { 134 t.Errorf("unexpected result\n%s", diff) 135 } 136 }) 137 } 138 } 139 140 func mustResourceInstanceAddr(s string) addrs.AbsResourceInstance { 141 addr, diags := addrs.ParseAbsResourceInstanceStr(s) 142 if diags.HasErrors() { 143 panic(diags.Err()) 144 } 145 return addr 146 }