Files
opentf/internal/command/providers_schema_test.go
Martin Atkins a800d250e5 command: "go fix" on various files we've changed recently anyway
We don't typically just broadly run automatic rewriting tools like "go fix"
across our codebase because that tends to cause annoying and unnecessary
merge conflicts when we're backporting to earlier release branches.

But all of the files in this commit were changed in some non-trivial way
already during the OpenTofu v1.11 development period anyway, and so the
likelyhood we'd be able to successfully backport from them is reduced and
therefore this seems like a good opportunity to do some focused
modernization using "go fix".

My rules for what to include or not are admittedly quite "vibes-based", but
the general idea was:

 - Focusing on files under the "command" directory only, because that's
   already been an area of intentional refactoring during this development
   period.
 - If the existing diff in a file is already significantly larger than
   the changes the fixer proposed to make, or if the fixer is proposing
   to change a line that was already changed in this development period.
 - More willing to include "_test.go" files than non-test files, even if
   they hadn't changed as much already, just because backports from test
   files for bug fixes tend to be entirely new test cases more than they
   are modifications to existing test cases, and so the risk of conflicts
   is lower there.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2026-03-17 15:25:30 -07:00

190 lines
5.1 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package command
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/cli"
"github.com/opentofu/opentofu/internal/command/workdir"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tofu"
"github.com/zclconf/go-cty/cty"
)
func TestProvidersSchema_error(t *testing.T) {
ui := new(cli.MockUi)
c := &ProvidersSchemaCommand{
Meta: Meta{
WorkingDir: workdir.NewDir("."),
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
}
if code := c.Run(nil); code != 1 {
fmt.Println(ui.OutputWriter.String())
t.Fatalf("expected error: \n%s", ui.OutputWriter.String())
}
}
func TestProvidersSchema_output(t *testing.T) {
fixtureDir := "testdata/providers-schema"
testDirs, err := os.ReadDir(fixtureDir)
if err != nil {
t.Fatal(err)
}
for _, entry := range testDirs {
if !entry.IsDir() {
continue
}
t.Run(entry.Name(), func(t *testing.T) {
td := t.TempDir()
inputDir := filepath.Join(fixtureDir, entry.Name())
testCopyDir(t, inputDir, td)
t.Chdir(td)
providerSource, close := newMockProviderSource(t, map[string][]string{
"test": {"1.2.3"},
})
defer close()
p := providersSchemaFixtureProvider()
view, done := testView(t)
m := Meta{
WorkingDir: workdir.NewDir("."),
testingOverrides: metaOverridesForProvider(p),
View: view,
ProviderSource: providerSource,
}
// `terraform init`
ic := &InitCommand{
Meta: m,
}
code := ic.Run([]string{})
output := done(t)
if code != 0 {
t.Fatalf("init failed\n%s", output.Stderr())
}
// `tofu provider schemas` command
// TODO meta-refactor-views: we need the ui here because the provider schema command is not yet migrated to views
// Once the command is migrated, remove this part and use the testView
ui := new(cli.MockUi)
m.Ui = ui
m.View = nil
pc := &ProvidersSchemaCommand{
Meta: m,
}
code = pc.Run([]string{"-json"})
if code != 0 {
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
}
var got, want providerSchemas
gotString := ui.OutputWriter.String()
if err := json.Unmarshal([]byte(gotString), &got); err != nil {
t.Fatal(err)
}
wantFile, err := os.Open("output.json")
if err != nil {
t.Fatalf("err: %s", err)
}
defer wantFile.Close()
byteValue, err := io.ReadAll(wantFile)
if err != nil {
t.Fatalf("err: %s", err)
}
if err := json.Unmarshal([]byte(byteValue), &want); err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, want) {
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
}
})
}
}
type providerSchemas struct {
FormatVersion string `json:"format_version"`
Schemas map[string]providerSchema `json:"provider_schemas"`
}
type providerSchema struct {
Provider any `json:"provider,omitempty"`
ResourceSchemas map[string]any `json:"resource_schemas,omitempty"`
DataSourceSchemas map[string]any `json:"data_source_schemas,omitempty"`
Functions map[string]any `json:"functions,omitempty"`
}
// testProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/providers-schema.
func providersSchemaFixtureProvider() *tofu.MockProvider {
p := testProvider()
p.GetProviderSchemaResponse = providersSchemaFixtureSchema()
return p
}
// providersSchemaFixtureSchema returns a schema suitable for processing the
// configuration in testdata/providers-schema.ß
func providersSchemaFixtureSchema() *providers.GetProviderSchemaResponse {
return &providers.GetProviderSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Optional: true},
},
},
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
"volumes": {
NestedType: &configschema.Object{
Nesting: configschema.NestingList,
Attributes: map[string]*configschema.Attribute{
"size": {Type: cty.String, Required: true},
"mount_point": {Type: cty.String, Required: true},
},
},
Optional: true,
},
},
},
},
},
Functions: map[string]providers.FunctionSpec{
"test_func": {
Description: "a basic string function",
Return: cty.String,
Summary: "test",
Parameters: []providers.FunctionParameterSpec{{
Name: "input",
Type: cty.Number,
}},
VariadicParameter: &providers.FunctionParameterSpec{
Name: "variadic_input",
Type: cty.List(cty.Bool),
},
},
},
}
}