mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
Go 1.17 and earlier used a different syntax for build constraint comments, starting with "+build". Go 1.18 changed this to the modern "go:build" form as part of standardizing the structure of toolchain directive comments, and so for a while it was convention to include comments in both styles to allow building with both old and new Go compilers. However, Go 1.17 is no longer supported, and regardless of that we only expect OpenTofu to be built with the specific version we have selected in "go.mod" and ".go-version" anyway, so we no longer need the legacy form of these comments: the all supported Go toolchains now support the new form, which this commit retains. golangci-lint v2.6.0 includes a check for this legacy form, so removing this will also allow us to upgrade to a newer version of that linter aggregator in a future commit. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
294 lines
7.8 KiB
Go
294 lines
7.8 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
//go:build !windows && !darwin
|
|
|
|
package cliconfig
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func (tfs *testFileSystem) trim(name string) string {
|
|
return strings.TrimLeft(name, string(os.PathSeparator))
|
|
}
|
|
|
|
var commonEnvVars = []string{
|
|
"XDG_CONFIG_HOME",
|
|
"XDG_DATA_HOME",
|
|
"TF_CLI_CONFIG_FILE",
|
|
}
|
|
|
|
func TestConfigFileLocations(t *testing.T) {
|
|
home := os.Getenv("HOME")
|
|
xdgDir := filepath.Join(home, ".myconfig")
|
|
tests := []locationTest{
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: ".tofurc only",
|
|
files: []string{filepath.Join(home, ".tofurc")},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config0.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://config0.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: ".terraformrc only",
|
|
files: []string{filepath.Join(home, ".terraformrc")},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config0.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://config0.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: ".terraformrc and .tofurc",
|
|
files: []string{filepath.Join(home, ".terraformrc"), filepath.Join(home, ".tofurc")},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config1.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v1": "https://config1.example.com/",
|
|
},
|
|
},
|
|
"0and1.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v1": "https://0and1.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "xdg directory, but with .tofurc and .terraformrc present",
|
|
files: []string{filepath.Join(home, ".terraformrc"), filepath.Join(home, ".tofurc"), filepath.Join(xdgDir, "opentofu", "tofurc")},
|
|
directories: []string{xdgDir},
|
|
envVars: map[string]string{"XDG_CONFIG_HOME": xdgDir},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config1.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v1": "https://config1.example.com/",
|
|
},
|
|
},
|
|
"0and1.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v1": "https://0and1.example.com/",
|
|
},
|
|
},
|
|
"1and2.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v1": "https://1and2.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "xdg directory without .tofurc and .terraformrc present",
|
|
files: []string{filepath.Join(xdgDir, "opentofu", "tofurc")},
|
|
directories: []string{xdgDir},
|
|
envVars: map[string]string{"XDG_CONFIG_HOME": xdgDir},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config0.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://config0.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "ignore everything else when env override is present",
|
|
files: []string{filepath.Join(home, "mytofufile"), filepath.Join(home, ".terraformrc"), filepath.Join(home, ".tofurc")},
|
|
envVars: map[string]string{"TF_CLI_CONFIG_FILE": filepath.Join(home, "mytofufile")},
|
|
},
|
|
expected: map[string]*ConfigHost{
|
|
"config0.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://config0.example.com/",
|
|
},
|
|
},
|
|
"0and1.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://0and1.example.com/",
|
|
},
|
|
},
|
|
"0and2.example.com": {
|
|
Services: map[string]interface{}{
|
|
"modules.v0": "https://0and2.example.com/",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
tfs := testFileSystem{
|
|
fsys: fstest.MapFS{},
|
|
}
|
|
cl := ConfigLoader{
|
|
ConfigFileSystem: &tfs,
|
|
}
|
|
n := len(test.files)
|
|
for i, file := range test.files {
|
|
b, err := getFile(i, n)
|
|
if err != nil {
|
|
t.Fatalf("failed to generate file %s: %v", file, err)
|
|
}
|
|
tfs.fsys[strings.TrimLeft(file, string(os.PathSeparator))] = &fstest.MapFile{
|
|
Data: b,
|
|
Mode: 0o600,
|
|
}
|
|
}
|
|
for _, directory := range test.directories {
|
|
tfs.fsys[strings.TrimLeft(directory, string(os.PathSeparator))] = &fstest.MapFile{
|
|
Data: nil,
|
|
Mode: fs.ModeDir | 0o755,
|
|
}
|
|
}
|
|
for _, v := range commonEnvVars {
|
|
t.Setenv(v, "")
|
|
}
|
|
for k, v := range test.envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
actual, diags := cl.LoadConfig(t.Context())
|
|
if diags.HasErrors() {
|
|
t.Fatalf("no errors expected, got errors from diags")
|
|
}
|
|
if diff := cmp.Diff(actual.Hosts, test.expected); diff != "" {
|
|
t.Error("unexpected result\n" + diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigDirLocations(t *testing.T) {
|
|
home := os.Getenv("HOME")
|
|
xdgDir := filepath.Join(home, ".myconfig")
|
|
terraformD := filepath.Join(home, ".terraform.d")
|
|
tests := []directoryLocationTest{
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "default directory",
|
|
},
|
|
expected: []string{terraformD},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "xdg directory",
|
|
envVars: map[string]string{"XDG_CONFIG_HOME": xdgDir},
|
|
directories: []string{xdgDir},
|
|
},
|
|
expected: []string{filepath.Join(xdgDir, "opentofu")},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "xdg directory, but with extant .terraform.d directory",
|
|
envVars: map[string]string{"XDG_CONFIG_HOME": xdgDir},
|
|
directories: []string{xdgDir, terraformD},
|
|
},
|
|
expected: []string{terraformD},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
tfs := testFileSystem{
|
|
fsys: fstest.MapFS{},
|
|
}
|
|
cl := ConfigLoader{
|
|
ConfigFileSystem: &tfs,
|
|
}
|
|
for _, directory := range test.directories {
|
|
tfs.fsys[strings.TrimLeft(directory, string(os.PathSeparator))] = &fstest.MapFile{
|
|
Data: nil,
|
|
Mode: fs.ModeDir | 0o755,
|
|
}
|
|
}
|
|
for _, v := range commonEnvVars {
|
|
t.Setenv(v, "")
|
|
}
|
|
for k, v := range test.envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
actual, err := cl.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("no errors expected, got errors from diags")
|
|
}
|
|
if diff := cmp.Diff(actual, test.expected[0]); diff != "" {
|
|
t.Error("unexpected result\n" + diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestDataDirLocations(t *testing.T) {
|
|
home := os.Getenv("HOME")
|
|
xdgDir := filepath.Join(home, ".mydata")
|
|
terraformD := filepath.Join(home, ".terraform.d")
|
|
// Note that neither of these tests depend on the existence of the underlying directories.
|
|
tests := []directoryLocationTest{
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "default directory",
|
|
},
|
|
expected: []string{terraformD},
|
|
},
|
|
{
|
|
locationTestParameters: locationTestParameters{
|
|
name: "xdg directory",
|
|
envVars: map[string]string{"XDG_DATA_HOME": xdgDir},
|
|
},
|
|
expected: []string{terraformD, filepath.Join(xdgDir, "opentofu")},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
tfs := testFileSystem{
|
|
fsys: fstest.MapFS{},
|
|
}
|
|
cl := ConfigLoader{
|
|
ConfigFileSystem: &tfs,
|
|
}
|
|
for _, directory := range test.directories {
|
|
tfs.fsys[strings.TrimLeft(directory, string(os.PathSeparator))] = &fstest.MapFile{
|
|
Data: nil,
|
|
Mode: fs.ModeDir | 0o755,
|
|
}
|
|
}
|
|
for _, v := range commonEnvVars {
|
|
t.Setenv(v, "")
|
|
}
|
|
for k, v := range test.envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
actual, err := cl.DataDirs()
|
|
if err != nil {
|
|
t.Fatalf("no errors expected, got errors from diags")
|
|
}
|
|
if diff := cmp.Diff(actual, test.expected); diff != "" {
|
|
t.Error("unexpected result\n" + diff)
|
|
}
|
|
})
|
|
}
|
|
}
|