Skip to content

Commit b6f8033

Browse files
committed
Add plan import tests
1 parent 0252e5a commit b6f8033

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

internal/terraform/context_plan_import_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,3 +1890,107 @@ output "foo" {
18901890
t.Errorf("should have reported the cycle to contain the target resource, but got %s", got)
18911891
}
18921892
}
1893+
1894+
func TestContext2Plan_importIdentityModule(t *testing.T) {
1895+
p := testProvider("aws")
1896+
m := testModule(t, "import-identity-module")
1897+
1898+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
1899+
ResourceTypes: map[string]*configschema.Block{
1900+
"aws_lb": {
1901+
Attributes: map[string]*configschema.Attribute{
1902+
"id": {
1903+
Type: cty.String,
1904+
Computed: true,
1905+
},
1906+
},
1907+
},
1908+
},
1909+
IdentityTypes: map[string]*configschema.Object{
1910+
"aws_lb": {
1911+
Attributes: map[string]*configschema.Attribute{
1912+
"name": {
1913+
Type: cty.String,
1914+
Required: true,
1915+
},
1916+
},
1917+
Nesting: configschema.NestingSingle,
1918+
},
1919+
},
1920+
})
1921+
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
1922+
ImportedResources: []providers.ImportedResource{
1923+
{
1924+
TypeName: "aws_lb",
1925+
State: cty.ObjectVal(map[string]cty.Value{
1926+
"id": cty.StringVal("foo"),
1927+
}),
1928+
},
1929+
},
1930+
}
1931+
ctx := testContext2(t, &ContextOpts{
1932+
Providers: map[addrs.Provider]providers.Factory{
1933+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
1934+
},
1935+
})
1936+
1937+
diags := ctx.Validate(m, &ValidateOpts{})
1938+
if diags.HasErrors() {
1939+
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
1940+
}
1941+
1942+
_, diags = ctx.Plan(m, states.NewState(), DefaultPlanOpts)
1943+
if diags.HasErrors() {
1944+
t.Fatalf("unexpected errors: %s", diags.Err())
1945+
}
1946+
}
1947+
1948+
func TestContext2Plan_importIdentityMissingRequired(t *testing.T) {
1949+
p := testProvider("aws")
1950+
m := testModule(t, "import-identity-module")
1951+
1952+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
1953+
ResourceTypes: map[string]*configschema.Block{
1954+
"aws_lb": {
1955+
Attributes: map[string]*configschema.Attribute{
1956+
"id": {
1957+
Type: cty.String,
1958+
Computed: true,
1959+
},
1960+
},
1961+
},
1962+
},
1963+
IdentityTypes: map[string]*configschema.Object{
1964+
"aws_lb": {
1965+
Attributes: map[string]*configschema.Attribute{
1966+
"name": {
1967+
Type: cty.String,
1968+
Required: true,
1969+
},
1970+
"id": {
1971+
Type: cty.String,
1972+
Required: true,
1973+
},
1974+
},
1975+
Nesting: configschema.NestingSingle,
1976+
},
1977+
},
1978+
})
1979+
1980+
ctx := testContext2(t, &ContextOpts{
1981+
Providers: map[addrs.Provider]providers.Factory{
1982+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
1983+
},
1984+
})
1985+
1986+
diags := ctx.Validate(m, &ValidateOpts{})
1987+
1988+
if len(diags) != 1 {
1989+
t.Fatalf("expected one diag, got %d: %s", len(diags), diags.ErrWithWarnings())
1990+
}
1991+
1992+
got := diags.Err().Error()
1993+
if !strings.Contains(got, "Missing required field:") {
1994+
t.Errorf("should have reported a missing required field, but got %s", got)
1995+
}
1996+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "lb_id" {
2+
value = 1
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module "child" {
2+
source = "./child"
3+
}
4+
5+
import {
6+
to = aws_lb.foo
7+
identity = {
8+
name = "bar"
9+
}
10+
}
11+
12+
resource "aws_lb" "foo" {}

0 commit comments

Comments
 (0)