Files
opentf/internal/command/logout_test.go
Martin Atkins d2bef1fd47 Adopt OpenTofu's own "svchost" module
Previously we were using a third-party library, but that doesn't have any
support for passing context.Context through its API and so isn't suitable
for our goals of adding OpenTelemetry tracing for all outgoing network
requests.

We now have our own fork that is updated to use context.Context. It also
has a slightly reduced scope no longer including various details that
are tightly-coupled to our cliconfig mechanism and so better placed in the
main OpenTofu codebase so we can evolve it in future without making
lockstep library releases.

The "registry-address" library also uses svchost and uses some of its types
in its public API, so this also incorporates v2 of that library that is
updated to use our own svchost module.

Unfortunately this commit is a mix of mechanical updates to the new
libraries and some new code dealing with the functionality that is removed
in our fork of svchost. The new code is primarily in the "svcauthconfig"
package, which is similar in purpose "ociauthconfig" but for OpenTofu's
own auth mechanism instead of the OCI Distribution protocol's auth
mechanism.

This includes some additional plumbing of context.Context where it was
possible to do so without broad changes to files that would not otherwise
have been included in this commit, but there are a few leftover spots that
are context.TODO() which we'll address separately in later commits.

This removes the temporary workaround from d079da6e9e, since we are now
able to plumb the OpenTelemetry span tree all the way to the service
discovery requests.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-06-12 09:37:59 -07:00

92 lines
2.4 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 (
"path/filepath"
"strings"
"testing"
"github.com/mitchellh/cli"
"github.com/opentofu/svchost"
"github.com/opentofu/svchost/disco"
"github.com/opentofu/svchost/svcauth"
"github.com/opentofu/opentofu/internal/command/cliconfig"
"github.com/opentofu/opentofu/internal/command/cliconfig/svcauthconfig"
)
func TestLogout(t *testing.T) {
workDir := t.TempDir()
ui := cli.NewMockUi()
credsSrc := cliconfig.EmptyCredentialsSourceForTests(filepath.Join(workDir, "credentials.tfrc.json"))
c := &LogoutCommand{
Meta: Meta{
Ui: ui,
Services: disco.New(
disco.WithCredentials(credsSrc),
),
},
}
t.Run("with no hostname", func(t *testing.T) {
status := c.Run([]string{})
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}
if !strings.Contains(ui.ErrorWriter.String(), "The logout command expects exactly one argument") {
t.Errorf("unexpected error message: %s", ui.ErrorWriter.String())
}
})
testCases := []struct {
// Hostname to associate a pre-stored token
hostname string
// Command-line arguments
args []string
// true iff the token at hostname should be removed by the command
shouldRemove bool
}{
// Can remove token for a hostname
{"tfe.example.com", []string{"tfe.example.com"}, true},
// Logout does not remove tokens for other hostnames
{"tfe.example.com", []string{"other-tfe.acme.com"}, false},
}
for _, tc := range testCases {
host := svchost.Hostname(tc.hostname)
token := svcauth.HostCredentialsToken("some-token")
err := credsSrc.StoreForHost(t.Context(), host, token)
if err != nil {
t.Fatalf("unexpected error storing credentials: %s", err)
}
status := c.Run(tc.args)
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}
creds, err := credsSrc.ForHost(t.Context(), host)
if err != nil {
t.Errorf("failed to retrieve credentials: %s", err)
}
if tc.shouldRemove {
if creds != nil {
t.Errorf("wrong token %q; should have no token", svcauthconfig.HostCredentialsBearerToken(t, creds))
}
} else {
if got, want := svcauthconfig.HostCredentialsBearerToken(t, creds), "some-token"; got != want {
t.Errorf("wrong token %q; want %q", got, want)
}
}
}
}