-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config Generation: Add tests for listers (#1698)
Add a one-line helper that can be added to acceptance tests to check that the resource's lister works The concept is pretty simple: After running a test config, we can check that the resource we just applied is returned by the lister
- Loading branch information
1 parent
1e9621b
commit 66c9b05
Showing
20 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package testutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/grafana/terraform-provider-grafana/v3/internal/common" | ||
"github.com/grafana/terraform-provider-grafana/v3/internal/resources/cloud" | ||
"github.com/grafana/terraform-provider-grafana/v3/internal/resources/grafana" | ||
"github.com/grafana/terraform-provider-grafana/v3/pkg/provider" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// CheckLister is a resource.TestCheckFunc that checks that the resource's lister | ||
// function returns the given ID. | ||
// This is meant to be used at least once in every resource's tests to ensure that | ||
// the resource's lister function is working correctly. | ||
func CheckLister(terraformResource string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// Get the resource from the state | ||
rs, ok := s.RootModule().Resources[terraformResource] | ||
if !ok { | ||
return fmt.Errorf("resource not found: %s", terraformResource) | ||
} | ||
id := rs.Primary.ID | ||
|
||
// Find the resource info | ||
var resource *common.Resource | ||
for _, r := range provider.Resources() { | ||
if r.Name == rs.Type { | ||
resource = r | ||
break | ||
} | ||
} | ||
if resource == nil { | ||
return fmt.Errorf("resource type %s not found", rs.Type) | ||
} | ||
|
||
// Get the resource's lister function | ||
lister := resource.ListIDsFunc | ||
if lister == nil { | ||
return fmt.Errorf("resource %s does not have a lister function", terraformResource) | ||
} | ||
|
||
// Get the list of IDs from the lister function | ||
ctx := context.Background() | ||
var listerData any = grafana.NewListerData(false) | ||
if resource.Category == common.CategoryCloud { | ||
listerData = cloud.NewListerData(os.Getenv("GRAFANA_CLOUD_ORG")) | ||
} | ||
ids, err := lister(ctx, Provider.Meta().(*common.Client), listerData) | ||
if err != nil { | ||
return fmt.Errorf("error listing %s: %w", terraformResource, err) | ||
} | ||
|
||
// Check that the ID is in the list | ||
for _, i := range ids { | ||
if i == id { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("resource %s with ID %s not found in list: %v", terraformResource, id, ids) | ||
} | ||
} |