terraform-provider-docker/internal/provider/test_helpers.go
Jonatan Reiners 09528d1c0e
chore: update Go version to 1.22 for consistency across workflows, jo… (#613)
* chore: update Go version to 1.22 for consistency across workflows, jobs, and modules.

* build: Update GitHub Actions setup-go and golangci-lint versions.

* refactor: Replace ioutil.ReadFile and ioutil.ReadAll with os.ReadFile and io.ReadAll.

* go fmt

* 🔄 Update actions/checkout and actions/setup-go to v4 and v5, respectively.

*  Refactor: Add b64 function to convert file to base64 in testacc_setup.sh.

*  Update Go version requirement in CONTRIBUTING.md.

*  Fix typo and improve error message format.

*  enhance: Improve error message in TestAccDockerContainer_uploadAsBase64.

*  Fix: Update file permissions for test2.txt in docker container tests.
2024-05-08 14:59:49 +02:00

69 lines
1.8 KiB
Go

package provider
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// resourceType the type of the resource
type resourceType int
const (
RESOURCE resourceType = iota // a resource
DATA_SOURCE // a data-source
)
// String converts the the resourceType into
// the name of the directory the test configuartions
// are int
func (r resourceType) String() string {
return [...]string{
"resources",
"data-sources",
}[r]
}
const (
TEST_CONFIG_BASE_DIR = "testdata"
)
// loadTestConfiguration loads the configuration for the test for the type of the
// resource, the resource itself, like 'docker_container' and the name of the test,
// like 'testAccDockerContainerPrivateImage'
//
// As a convention the test configurations are in
// 'testdata/<resourceType>/<resourceName>/<testName>.tf', e.g.
// 'testdata/resources/docker_container/testAccDockerContainerPrivateImage.tf'
func loadTestConfiguration(t *testing.T, resourceType resourceType, resourceName, testName string) string {
wd, err := os.Getwd()
if err != nil {
t.Errorf("failed to get current working directory: %v", err)
}
testConfig := strings.ReplaceAll(filepath.Join(wd, "..", "..", TEST_CONFIG_BASE_DIR, resourceType.String(), resourceName, fmt.Sprintf("%s.tf", testName)), "\\", "\\\\")
testConfigContent, err := os.ReadFile(testConfig)
if err != nil {
t.Errorf("failed to read test configuration at '%s': %v", testConfig, err)
}
return string(testConfigContent)
}
// mapEquals returns true if the expectedValue is found under the given key in
// the map. Otherwise returns false, as well when the map ist nil
func mapEquals(key, expectedValue string, m map[string]string) bool {
if m == nil {
return false
}
extractedValue, ok := m[key]
if ok && extractedValue == expectedValue {
return true
}
return false
}