Skip to content

Incorrect order of destroying resources - Flaky/inconsistent behavior #23635

Closed
@cvbarros

Description

@cvbarros

Terraform Version

0.12.7

Terraform Configuration Files

resource "teamcity_project" "vcs_root_project" {
  name = "vcs_root_project"
}

resource "teamcity_vcs_root_git" "git_test" {
	name = "application"
	project_id = teamcity_project.vcs_root_project.id
	fetch_url = "https://github.com/cvbarros/terraform-provider-teamcity"
	default_branch = "refs/head/master"
	branches = [
    	"+:refs/(pull/*)/head",
    	"+:refs/heads/develop",
  	]
	username_style = "userid"
	submodule_checkout = "checkout"
	enable_branch_spec_tags = true
	modification_check_interval = 60
}

Debug Output

https://gist.github.com/cvbarros/cd508eaeb50c2a310fc2c1057ddc3832
Relevant extract:

2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalApply
2019/12/10 23:31:14 [DEBUG] teamcity_project.vcs_root_project: applying the planned Delete change
2019/12/10 23:31:14 [TRACE] GRPCProvider: ApplyResourceChange
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalRequireState
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalApplyPre
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalApplyProvisioners
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalApply
2019/12/10 23:31:14 [DEBUG] teamcity_vcs_root_git.git_test: applying the planned Delete change
2019/12/10 23:31:14 [TRACE] GRPCProvider: ApplyResourceChange
2019/12/10 23:31:14 [DEBUG]: resourceProjectDelete - Destroying project VcsRootProject
2019/12/10 23:31:14 [DEBUG]: resourceVcsRootGitDelete - Destroying vcs root VcsRootProject_Application
2019/12/10 23:31:14 [INFO]: resourceVcsRootGitDelete - Destroyed vcs root VcsRootProject_Application
2019/12/10 23:31:14 [DEBUG] teamcity_vcs_root_git.git_test: apply errored, but we're indicating that via the Error pointer rather than returning it: Error '404' when deleting vcsRoot: Responding with error, status code: 404 (Not Found).
Details: jetbrains.buildServer.server.rest.errors.NotFoundException: No VCS root found by internal or external id 'VcsRootProject_Application'.
Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalWriteState
2019/12/10 23:31:14 [TRACE] EvalWriteState: writing current state object for teamcity_vcs_root_git.git_test
2019/12/10 23:31:14 [TRACE] <root>: eval: *terraform.EvalApplyPost
2019/12/10 23:31:14 [ERROR] <root>: eval: *terraform.EvalApplyPost, err: Error '404' when deleting vcsRoot: Responding with error, status code: 404 (Not Found).
Details: jetbrains.buildServer.server.rest.errors.NotFoundException: No VCS root found by internal or external id 'VcsRootProject_Application'.
Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.
2019/12/10 23:31:14 [ERROR] <root>: eval: *terraform.EvalSequence, err: Error '404' when deleting vcsRoot: Responding with error, status code: 404 (Not Found).
Details: jetbrains.buildServer.server.rest.errors.NotFoundException: No VCS root found by internal or external id 'VcsRootProject_Application'.
Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.
2019/12/10 23:31:14 [ERROR] <root>: eval: *terraform.EvalOpFilter, err: Error '404' when deleting vcsRoot: Responding with error, status code: 404 (Not Found).
Details: jetbrains.buildServer.server.rest.errors.NotFoundException: No VCS root found by internal or external id 'VcsRootProject_Application'.
Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.
2019/12/10 23:31:14 [TRACE] [walkDestroy] Exiting eval tree: teamcity_vcs_root_git.git_test (destroy)
2019/12/10 23:31:14 [TRACE] vertex "teamcity_vcs_root_git.git_test (destroy)": visit complete
2019/12/10 23:31:14 [TRACE] dag/walk: upstream of "teamcity_vcs_root_git.git_test (clean up state)" errored, so skipping
2019/12/10 23:31:14 [INFO]: resourceProjectDelete - Destroyed project VcsRootProject

Crash Output

Expected Behavior

Resource teamcity_project.vcs_root_project is a dependency of resource teamcity_vcs_root_git.git_test.
Thus, the destroy should happen in the sequence teamcity_vcs_root_git.git_test -> teamcity_project.vcs_root_project

Actual Behavior

teamcity_project.vcs_root_project got deleted before the destroy for teamcity_vcs_root_git.git_test got called.
For this resource, when the project is deleted, the VCS Root also is removed. Then when Terraform calls destroy for teamcity_vcs_root_git.git_test, a 404 from upstream API occurs.

Steps to Reproduce

The issue happens when running a "Import" acceptance test for the provider, both locally and on CI. First, it was detected on CI
Relevant test case:

func TestAccVcsRootGit_Import(t *testing.T) {
	resName := "teamcity_vcs_root_git.git_test"
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckVcsRootGitDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccVcsRootGitBasic,
			},
			{
				ResourceName:      resName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

const testAccVcsRootGitBasic = `
resource "teamcity_project" "vcs_root_project" {
  name = "vcs_root_project"
}

resource "teamcity_vcs_root_git" "git_test" {
	name = "application"
	project_id = teamcity_project.vcs_root_project.id
	fetch_url = "https://github.com/cvbarros/terraform-provider-teamcity"
	default_branch = "refs/head/master"
	branches = [
    	"+:refs/(pull/*)/head",
    	"+:refs/heads/develop",
  	]
	username_style = "userid"
	submodule_checkout = "checkout"
	enable_branch_spec_tags = true
	modification_check_interval = 60
}
`

Full Source

Additional Context

There seems to be some sort of race condition happening, as these failures are intermittent. Below is a gist containing traces, where the same test was ran with no changes (environment, code) and succeeds:
https://gist.github.com/cvbarros/97bd088ae8084c89d340fde2e9db54ea
Relevant extract:

2019/12/10 23:29:46 [DEBUG] teamcity_project.vcs_root_project: applying the planned Delete change
2019/12/10 23:29:46 [TRACE] GRPCProvider: ApplyResourceChange
2019/12/10 23:29:46 [DEBUG]: resourceProjectDelete - Destroying project VcsRootProject
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalRequireState
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalApplyPre
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalApplyProvisioners
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalIf
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalApply
2019/12/10 23:29:46 [DEBUG] teamcity_vcs_root_git.git_test: applying the planned Delete change
2019/12/10 23:29:46 [TRACE] GRPCProvider: ApplyResourceChange
2019/12/10 23:29:46 [DEBUG]: resourceVcsRootGitDelete - Destroying vcs root VcsRootProject_Application
2019/12/10 23:29:46 [INFO]: resourceVcsRootGitDelete - Destroyed vcs root VcsRootProject_Application
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalWriteState
2019/12/10 23:29:46 [TRACE] EvalWriteState: removing state object for teamcity_vcs_root_git.git_test
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalApplyPost
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalUpdateStateHook
2019/12/10 23:29:46 [TRACE] [walkDestroy] Exiting eval tree: teamcity_vcs_root_git.git_test (destroy)
2019/12/10 23:29:46 [TRACE] vertex "teamcity_vcs_root_git.git_test (destroy)": visit complete
2019/12/10 23:29:46 [TRACE] dag/walk: visiting "teamcity_vcs_root_git.git_test (clean up state)"
2019/12/10 23:29:46 [TRACE] vertex "teamcity_vcs_root_git.git_test (clean up state)": starting visit (*terraform.NodeDestroyResource)
2019/12/10 23:29:46 [TRACE] vertex "teamcity_vcs_root_git.git_test (clean up state)": evaluating
2019/12/10 23:29:46 [TRACE] [walkDestroy] Entering eval tree: teamcity_vcs_root_git.git_test (clean up state)
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalForgetResourceState
2019/12/10 23:29:46 [TRACE] EvalForgetResourceState: Pruned husk of teamcity_vcs_root_git.git_test from state
2019/12/10 23:29:46 [TRACE] [walkDestroy] Exiting eval tree: teamcity_vcs_root_git.git_test (clean up state)
2019/12/10 23:29:46 [TRACE] vertex "teamcity_vcs_root_git.git_test (clean up state)": visit complete
2019/12/10 23:29:46 [INFO]: resourceProjectDelete - Destroyed project VcsRootProject
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalWriteState
2019/12/10 23:29:46 [TRACE] EvalWriteState: removing state object for teamcity_project.vcs_root_project
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalApplyPost
2019/12/10 23:29:46 [TRACE] <root>: eval: *terraform.EvalUpdateStateHook
2019/12/10 23:29:46 [TRACE] [walkDestroy] Exiting eval tree: teamcity_project.vcs_root_project (destroy)
2019/12/10 23:29:46 [TRACE] vertex "teamcity_project.vcs_root_project (destroy)": visit complete
2019/12/10 23:29:46 [TRACE] dag/walk: visiting "teamcity_project.vcs_root_project (clean up state)"

References

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions