Files
opentf/internal/command/cliconfig/svcauthconfig/helper_program_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

99 lines
3.0 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 svcauthconfig
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/opentofu/svchost"
"github.com/opentofu/svchost/svcauth"
)
func TestHelperProgramCredentialsSource(t *testing.T) {
// The helper script used in this test currently assumes a Unix-like
// environment where scripts are directly executable based on their #!
// line and where bash is available. This is an assumption we inherited
// from our predecessor which we'd like to address someday, but for now
// we'll just skip this test unless we're on Linux or macOS since those
// are the two OSes most commonly used for OpenTofu development where
// we can expect this to work. (Other unixes could potentially work
// but we don't want to maintain a huge list here.)
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("this test only works on Unix-like systems")
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
program := filepath.Join(wd, "testdata", "helperprog", "test-helper")
t.Logf("testing with helper at %s", program)
src := NewHelperProgramCredentialsStore(program)
t.Run("happy path", func(t *testing.T) {
creds, err := src.ForHost(t.Context(), svchost.Hostname("example.com"))
if err != nil {
t.Fatal(err)
}
if got, want := HostCredentialsBearerToken(t, creds), "example-token"; got != want {
t.Errorf("wrong token %q; want %q", got, want)
}
})
t.Run("no credentials", func(t *testing.T) {
creds, err := src.ForHost(t.Context(), svchost.Hostname("nothing.example.com"))
if err != nil {
t.Fatal(err)
}
if creds != nil {
t.Errorf("got credentials; want nil")
}
})
t.Run("unsupported credentials type", func(t *testing.T) {
creds, err := src.ForHost(t.Context(), svchost.Hostname("other-cred-type.example.com"))
if err != nil {
t.Fatal(err)
}
if creds != nil {
t.Errorf("got credentials; want nil")
}
})
t.Run("lookup error", func(t *testing.T) {
_, err := src.ForHost(t.Context(), svchost.Hostname("fail.example.com"))
if err == nil {
t.Error("completed successfully; want error")
}
})
t.Run("store happy path", func(t *testing.T) {
err := src.StoreForHost(t.Context(), svchost.Hostname("example.com"), svcauth.HostCredentialsToken("example-token"))
if err != nil {
t.Fatal(err)
}
})
t.Run("store error", func(t *testing.T) {
err := src.StoreForHost(t.Context(), svchost.Hostname("fail.example.com"), svcauth.HostCredentialsToken("example-token"))
if err == nil {
t.Error("completed successfully; want error")
}
})
t.Run("forget happy path", func(t *testing.T) {
err := src.ForgetForHost(t.Context(), svchost.Hostname("example.com"))
if err != nil {
t.Fatal(err)
}
})
t.Run("forget error", func(t *testing.T) {
err := src.ForgetForHost(t.Context(), svchost.Hostname("fail.example.com"))
if err == nil {
t.Error("completed successfully; want error")
}
})
}