pulumi/pkg/backend/httpstate/token_source_test.go

156 lines
4.0 KiB
Go
Raw Normal View History

// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpstate
import (
"context"
"fmt"
"runtime"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTokenSource(t *testing.T) {
// TODO[pulumi/pulumi#16500] fix flaky test and unskip
t.Skip("Skipping flaky test: TODO[pulumi/pulumi#16500] to unskip")
if runtime.GOOS == "windows" {
t.Skip("Flaky on Windows CI workers due to the use of timer+Sleep")
}
t.Parallel()
ctx := context.Background()
dur := 20 * time.Millisecond
backend := &testTokenBackend{tokens: map[string]time.Time{}}
tok0, tok0Expires := backend.NewToken(dur)
ts, err := newTokenSource(ctx, tok0, tok0Expires, dur, backend.Refresh)
assert.NoError(t, err)
defer ts.Close()
for i := 0; i < 64; i++ {
2023-03-01 22:23:20 +00:00
tok, err := ts.GetToken(ctx)
assert.NoError(t, err)
assert.NoError(t, backend.VerifyToken(tok))
t.Logf("STEP: %d, TOKEN: %s", i, tok)
// tok0 initially
if i == 0 {
assert.Equal(t, tok0, tok)
}
// definitely a fresh token by step 32
// allow some leeway due to time.Sleep concurrency
if i > 32 {
assert.NotEqual(t, tok0, tok)
}
time.Sleep(dur / 16)
}
}
func TestTokenSourceWithQuicklyExpiringInitialToken(t *testing.T) {
// TODO[pulumi/pulumi#16500] fix flaky test and unskip
t.Skip("Skipping flaky test: TODO[pulumi/pulumi#16500] to unskip")
if runtime.GOOS == "windows" {
t.Skip("Flaky on Windows CI workers due to the use of timer+Sleep")
}
t.Parallel()
ctx := context.Background()
dur := 40 * time.Millisecond
backend := &testTokenBackend{tokens: map[string]time.Time{}}
tok0, tok0Expires := backend.NewToken(dur / 10)
ts, err := newTokenSource(ctx, tok0, tok0Expires, dur, backend.Refresh)
assert.NoError(t, err)
defer ts.Close()
for i := 0; i < 4; i++ {
2023-03-01 22:23:20 +00:00
tok, err := ts.GetToken(ctx)
assert.NoError(t, err)
assert.NoError(t, backend.VerifyToken(tok))
t.Logf("STEP: %d, TOKEN: %s", i, tok)
time.Sleep(dur / 16)
}
}
type testTokenBackend struct {
mu sync.Mutex
counter int
tokens map[string]time.Time
}
func (ts *testTokenBackend) NewToken(duration time.Duration) (string, time.Time) {
ts.mu.Lock()
defer ts.mu.Unlock()
return ts.newTokenInner(duration)
}
func (ts *testTokenBackend) Refresh(
ctx context.Context,
duration time.Duration,
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
currentToken string,
) (string, time.Time, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
if err := ts.verifyTokenInner(currentToken); err != nil {
return "", time.Time{}, err
}
tok, expires := ts.newTokenInner(duration)
return tok, expires, nil
}
func (ts *testTokenBackend) TokenName(refreshCount int) string {
ts.mu.Lock()
defer ts.mu.Unlock()
return ts.tokenNameInner(refreshCount)
}
func (ts *testTokenBackend) VerifyToken(token string) error {
ts.mu.Lock()
defer ts.mu.Unlock()
return ts.verifyTokenInner(token)
}
func (ts *testTokenBackend) newTokenInner(duration time.Duration) (string, time.Time) {
now := time.Now()
ts.counter++
tok := ts.tokenNameInner(ts.counter)
expires := now.Add(duration)
ts.tokens[tok] = now.Add(duration)
return tok, expires
}
func (ts *testTokenBackend) tokenNameInner(refreshCount int) string {
return fmt.Sprintf("token-%d", ts.counter)
}
func (ts *testTokenBackend) verifyTokenInner(token string) error {
now := time.Now()
expires, gotCurrentToken := ts.tokens[token]
if !gotCurrentToken {
return fmt.Errorf("Unknown token: %v", token)
}
if now.After(expires) {
return fmt.Errorf("Expired token %v (%v past expiration)",
token, now.Sub(expires))
}
return nil
}