How to run acceptance tests on GitHub Actions and local envrionment?

Hi, I’m trying to develop my custom provider.
But I have some troubles with CI on GitHub Actions and running tests on the local environment.

I have read some documents below:

I forked template repository and started developing my first custom provider.
And then I got errors like this:

=== RUN   TestAccDatabaseResource
    database_resource_test.go:14: Step 1/3 error: Error running pre-apply refresh: exit status 1
        
        Error: Inconsistent dependency lock file
        
        The following dependency selections recorded in the lock file are
        inconsistent with the current configuration:
          - provider registry.terraform.io/okkez/mysql: required by this configuration but no version is selected
        
        To make the initial dependency selections that will initialize the dependency
        lock file, run:
          terraform init
--- FAIL: TestAccDatabaseResource (0.25s)

How to solve these issues?

Current status:
I can use all resources and data sources in my custom provider.
I can run acceptance tests on the local environment with $HOME/.terraformrc file.

provider_installation {
  dev_overrides {
      "okkez/mysql" = "$GOPATH"
  }
}
$ TF_ACC=1 go test -v ./internal/provider/
=== RUN   TestAccDatabaseResource
--- PASS: TestAccDatabaseResource (5.22s)
=== RUN   TestAccDefaultRoleResource
    default_roles_resource_test.go:15: user: test-user-0187@%, role1: test-role-0119@%, role2: test-role-0963@%
--- PASS: TestAccDefaultRoleResource (5.11s)
=== RUN   TestAccGlobalVariableResource
--- PASS: TestAccGlobalVariableResource (4.25s)
=== RUN   TestAccGrantPrivilegeResource
    grant_privilege_resource_test.go:14: database: test-database-0439 user: test-user-0478@%
--- PASS: TestAccGrantPrivilegeResource (4.04s)
=== RUN   TestAccGrantRoleResource
    grant_role_resource_test.go:14: user: test-user-0191, role1: test-role1-0646, role2: test-role2-0007
--- PASS: TestAccGrantRoleResource (4.43s)
=== RUN   TestAccRoleResource
--- PASS: TestAccRoleResource (3.53s)
=== RUN   TestAccTablesDataSource
--- PASS: TestAccTablesDataSource (1.28s)
=== RUN   TestAccUserResource
--- PASS: TestAccUserResource (4.83s)
PASS
ok      github.com/okkez/terraform-provider-mysql/internal/provider     (cached)

I cannot run acceptance tests without $HOME/.terraformrc file

=== RUN   TestAccDatabaseResource
2023-05-08T18:05:54.047+0900 [WARN]  sdk.helper_resource: Error running Terraform CLI command: test_step_number=1
  error=
  | exit status 1
  |
  | Error: Inconsistent dependency lock file
  |
  | The following dependency selections recorded in the lock file are
  | inconsistent with the current configuration:
  |   - provider registry.terraform.io/okkez/mysql: required by this configuration but no version is selected
  |
  | To make the initial dependency selections that will initialize the dependency
  | lock file, run:
  |   terraform init
   test_name=TestAccDatabaseResource test_terraform_path=/home/okkez/.local/share/rtx/installs/terraform/1.4.2/bin/terraform test_working_directory=/tmp/plugintest1512033159
2023-05-08T18:05:54.047+0900 [ERROR] sdk.helper_resource: Unexpected error: test_name=TestAccDatabaseResource test_terraform_path=/home/okkez/.local/share/rtx/installs/terraform/1.4.2/bin/terraform test_working_directory=/tmp/plugintest1512033159 test_step_number=1
  error=
  | Error running pre-apply refresh: exit status 1
  |
  | Error: Inconsistent dependency lock file
  |
  | The following dependency selections recorded in the lock file are
  | inconsistent with the current configuration:
  |   - provider registry.terraform.io/okkez/mysql: required by this configuration but no version is selected
  |
  | To make the initial dependency selections that will initialize the dependency
  | lock file, run:
  |   terraform init

    database_resource_test.go:14: Step 1/3 error: Error running pre-apply refresh: exit status 1

        Error: Inconsistent dependency lock file

        The following dependency selections recorded in the lock file are
        inconsistent with the current configuration:
          - provider registry.terraform.io/okkez/mysql: required by this configuration but no version is selected

        To make the initial dependency selections that will initialize the dependency
        lock file, run:
          terraform init
--- FAIL: TestAccDatabaseResource (0.13s)

Thanks,

1 Like

Hi @okkez, welcome to the HashiCorp discuss forums :wave: !

Thanks for providing your provider code as reference! I believe what’s causing your error is the inclusion of the provider config in your acceptance tests, Config input:

It looks like your provider code is already setup to accept the environment variables here, so you should be able to remove the provider config (from buildConfig) from your resource acceptance tests and let the framework manage your provider via ProtoV6ProviderFactories.

Here’s a semi-related issue that has some more explanation around the testing framework and how features like dev_overrides interact with it: `dev_overrides` in `.terraformrc` appears to break acceptance tests (`make testacc`/TF_ACC=1) · Issue #1171 · hashicorp/terraform-plugin-sdk · GitHub

Let me know if this helps!

Thank you for quick response.

I read `dev_overrides` in `.terraformrc` appears to break acceptance tests (`make testacc`/TF_ACC=1) · Issue #1171 · hashicorp/terraform-plugin-sdk · GitHub and check my code carefully.
And I found the fix as below:

diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go
index 4fcb41c..74eb7c2 100644
--- a/internal/provider/provider_test.go
+++ b/internal/provider/provider_test.go
@@ -19,7 +19,7 @@ import (
 // CLI command executed to create a provider server to which the CLI can
 // reattach.
 var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
-       "test": providerserver.NewProtocol6WithError(New("test")()),
+       "mysql": providerserver.NewProtocol6WithError(New("test")()),
 }

 func testAccPreCheck(t *testing.T) {

Thanks.

2 Likes