mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
Refactors our use of `slogtest` to instantiate a "standard logger" across most of our tests. This standard logger incorporates https://github.com/coder/slog/pull/217 to also ignore database query canceled errors by default, which are a source of low-severity flakes. Any test that has set non-default `slogtest.Options` is left alone. In particular, `coderdtest` defaults to ignoring all errors. We might consider revisiting that decision now that we have better tools to target the really common flaky Error logs on shutdown.
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
//go:build !race
|
|
|
|
// This test is excluded from the race detector because the underlying
|
|
// hc-install library makes massive allocations and can take 1-2 minutes
|
|
// to complete.
|
|
package terraform_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/go-version"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/provisioner/terraform"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestInstall(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
ctx := context.Background()
|
|
dir := t.TempDir()
|
|
log := testutil.Logger(t)
|
|
|
|
// Install spins off 8 installs with Version and waits for them all
|
|
// to complete. The locking mechanism within Install should
|
|
// prevent multiple binaries from being installed, so the function
|
|
// should perform like a single install.
|
|
install := func(version *version.Version) string {
|
|
var wg sync.WaitGroup
|
|
paths := make(chan string, 8)
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
p, err := terraform.Install(ctx, log, dir, version)
|
|
assert.NoError(t, err)
|
|
paths <- p
|
|
}()
|
|
}
|
|
go func() {
|
|
wg.Wait()
|
|
close(paths)
|
|
}()
|
|
var firstPath string
|
|
for p := range paths {
|
|
if firstPath == "" {
|
|
firstPath = p
|
|
} else {
|
|
require.Equal(t, firstPath, p, "installs returned different paths")
|
|
}
|
|
}
|
|
return firstPath
|
|
}
|
|
|
|
version1 := terraform.TerraformVersion
|
|
binPath := install(version1)
|
|
|
|
checkBinModTime := func() time.Time {
|
|
binInfo, err := os.Stat(binPath)
|
|
require.NoError(t, err)
|
|
require.Greater(t, binInfo.Size(), int64(0))
|
|
return binInfo.ModTime()
|
|
}
|
|
|
|
modTime1 := checkBinModTime()
|
|
|
|
// Since we're using the same version the install should be idempotent.
|
|
install(terraform.TerraformVersion)
|
|
modTime2 := checkBinModTime()
|
|
require.Equal(t, modTime1, modTime2)
|
|
|
|
// Ensure a new install happens when version changes
|
|
version2 := version.Must(version.NewVersion("1.2.0"))
|
|
|
|
// Sanity-check
|
|
require.NotEqual(t, version2.String(), version1.String())
|
|
|
|
install(version2)
|
|
|
|
modTime3 := checkBinModTime()
|
|
require.Greater(t, modTime3, modTime2)
|
|
}
|