terraform: test case for #10982 (passes)
This commit is contained in:
parent
320840f4d5
commit
c36b6c42ba
|
@ -285,14 +285,17 @@ func tokenizeResourceAddress(s string) (map[string]string, error) {
|
||||||
// "1" (optional, omission implies: "0")
|
// "1" (optional, omission implies: "0")
|
||||||
`(?:\[(?P<index>\d+)\])?` +
|
`(?:\[(?P<index>\d+)\])?` +
|
||||||
`\z`)
|
`\z`)
|
||||||
|
|
||||||
groupNames := re.SubexpNames()
|
groupNames := re.SubexpNames()
|
||||||
rawMatches := re.FindAllStringSubmatch(s, -1)
|
rawMatches := re.FindAllStringSubmatch(s, -1)
|
||||||
if len(rawMatches) != 1 {
|
if len(rawMatches) != 1 {
|
||||||
return nil, fmt.Errorf("Problem parsing address: %q", s)
|
return nil, fmt.Errorf("Problem parsing address: %q", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
matches := make(map[string]string)
|
matches := make(map[string]string)
|
||||||
for i, m := range rawMatches[0] {
|
for i, m := range rawMatches[0] {
|
||||||
matches[groupNames[i]] = m
|
matches[groupNames[i]] = m
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches, nil
|
return matches, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Input string
|
Input string
|
||||||
Expected *ResourceAddress
|
Expected *ResourceAddress
|
||||||
Output string
|
Output string
|
||||||
|
Err bool
|
||||||
}{
|
}{
|
||||||
"implicit primary managed instance, no specific index": {
|
"implicit primary managed instance, no specific index": {
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -116,6 +117,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"implicit primary data instance, no specific index": {
|
"implicit primary data instance, no specific index": {
|
||||||
"data.aws_instance.foo",
|
"data.aws_instance.foo",
|
||||||
|
@ -127,6 +129,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"implicit primary, explicit index": {
|
"implicit primary, explicit index": {
|
||||||
"aws_instance.foo[2]",
|
"aws_instance.foo[2]",
|
||||||
|
@ -138,6 +141,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: 2,
|
Index: 2,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"implicit primary, explicit index over ten": {
|
"implicit primary, explicit index over ten": {
|
||||||
"aws_instance.foo[12]",
|
"aws_instance.foo[12]",
|
||||||
|
@ -149,6 +153,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: 12,
|
Index: 12,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"explicit primary, explicit index": {
|
"explicit primary, explicit index": {
|
||||||
"aws_instance.foo.primary[2]",
|
"aws_instance.foo.primary[2]",
|
||||||
|
@ -161,6 +166,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: 2,
|
Index: 2,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"tainted": {
|
"tainted": {
|
||||||
"aws_instance.foo.tainted",
|
"aws_instance.foo.tainted",
|
||||||
|
@ -173,6 +179,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"deposed": {
|
"deposed": {
|
||||||
"aws_instance.foo.deposed",
|
"aws_instance.foo.deposed",
|
||||||
|
@ -185,6 +192,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"with a hyphen": {
|
"with a hyphen": {
|
||||||
"aws_instance.foo-bar",
|
"aws_instance.foo-bar",
|
||||||
|
@ -196,6 +204,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"managed in a module": {
|
"managed in a module": {
|
||||||
"module.child.aws_instance.foo",
|
"module.child.aws_instance.foo",
|
||||||
|
@ -208,6 +217,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"data in a module": {
|
"data in a module": {
|
||||||
"module.child.data.aws_instance.foo",
|
"module.child.data.aws_instance.foo",
|
||||||
|
@ -220,6 +230,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"nested modules": {
|
"nested modules": {
|
||||||
"module.a.module.b.module.forever.aws_instance.foo",
|
"module.a.module.b.module.forever.aws_instance.foo",
|
||||||
|
@ -232,6 +243,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"just a module": {
|
"just a module": {
|
||||||
"module.a",
|
"module.a",
|
||||||
|
@ -243,6 +255,7 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"just a nested module": {
|
"just a nested module": {
|
||||||
"module.a.module.b",
|
"module.a.module.b",
|
||||||
|
@ -254,26 +267,38 @@ func TestParseResourceAddress(t *testing.T) {
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
"module missing resource type": {
|
||||||
|
"module.name.foo",
|
||||||
|
nil,
|
||||||
|
"",
|
||||||
|
true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for tn, tc := range cases {
|
for tn, tc := range cases {
|
||||||
out, err := ParseResourceAddress(tc.Input)
|
t.Run(tn, func(t *testing.T) {
|
||||||
if err != nil {
|
out, err := ParseResourceAddress(tc.Input)
|
||||||
t.Fatalf("%s: unexpected err: %#v", tn, err)
|
if (err != nil) != tc.Err {
|
||||||
}
|
t.Fatalf("%s: unexpected err: %#v", tn, err)
|
||||||
|
}
|
||||||
|
if tc.Err {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(out, tc.Expected) {
|
if !reflect.DeepEqual(out, tc.Expected) {
|
||||||
t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
|
t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := tc.Input
|
expected := tc.Input
|
||||||
if tc.Output != "" {
|
if tc.Output != "" {
|
||||||
expected = tc.Output
|
expected = tc.Output
|
||||||
}
|
}
|
||||||
if out.String() != expected {
|
if out.String() != expected {
|
||||||
t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, expected, out)
|
t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, expected, out)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue