pulumi/tests/remote_test.go

157 lines
5.1 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 tests
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
)
const remoteTestRepo = "https://github.com/pulumi/test-repo.git"
func TestInvalidRemoteFlags(t *testing.T) {
t.Parallel()
commands := []string{"preview", "up", "refresh", "destroy"}
tests := map[string]struct {
args []string
err string
}{
Use new API for deployments (#15684) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Updates Remote Automation API to use the new stable URLs for the Deployments API. Adds support for `inheritSettings` to allow inheriting deployment settings pre-existing on the stack. I've tested this manually by editing the automation-api-examples for remote deployments, but not sure of a great way to add automated tests since automation api doesn't yet have support for setting deployment settings. EDIT: I considered just setting up a static stack for this but abandoned it because of concerns around parallel runs. We can add automated tests for this once we support creating deployment settings with automation api (coming soon). Fixes #12739 Fixes #15518 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-04-16 23:23:56 +00:00
"no url and no inherit-settings": {
err: "error: the url arg must be specified if not passing --remote-inherit-settings",
},
"no branch or commit": {
args: []string{remoteTestRepo},
err: "error: either `--remote-git-branch` or `--remote-git-commit` is required",
},
"both branch and commit": {
args: []string{remoteTestRepo, "--remote-git-branch", "branch", "--remote-git-commit", "commit"},
err: "error: `--remote-git-branch` and `--remote-git-commit` cannot both be specified",
},
"both ssh private key and path": {
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
args: []string{
remoteTestRepo, "--remote-git-branch", "branch", "--remote-git-auth-ssh-private-key", "key",
"--remote-git-auth-ssh-private-key-path", "path",
},
err: "error: `--remote-git-auth-ssh-private-key` and `--remote-git-auth-ssh-private-key-path` " +
"cannot both be specified",
},
"ssh private key path doesn't exist": {
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
args: []string{
remoteTestRepo, "--remote-git-branch", "branch", "--remote-git-auth-ssh-private-key-path",
"doesntexist",
},
err: "error: reading SSH private key path",
},
"invalid env": {
args: []string{remoteTestRepo, "--remote-git-branch", "branch", "--remote-env", "invalid"},
err: `expected value of the form "NAME=value": missing "=" in "invalid"`,
},
"empty env name": {
args: []string{remoteTestRepo, "--remote-git-branch", "branch", "--remote-env", "=value"},
err: `error: expected non-empty environment name in "=value"`,
},
"invalid secret env": {
args: []string{remoteTestRepo, "--remote-git-branch", "branch", "--remote-env-secret", "blah"},
err: `expected value of the form "NAME=value": missing "=" in "blah"`,
},
"empty secret env name": {
args: []string{remoteTestRepo, "--remote-git-branch", "branch", "--remote-env-secret", "=value"},
err: `error: expected non-empty environment name in "=value"`,
},
}
for _, command := range commands {
command := command
for name, tc := range tests {
tc := tc
t.Run(command+"_"+name, func(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer deleteIfNotFailed(e)
// Remote flags currently require PULUMI_EXPERIMENTAL.
e.Env = append(e.Env, "PULUMI_EXPERIMENTAL=true")
args := []string{command, "--remote"}
_, err := e.RunCommandExpectError("pulumi", append(args, tc.args...)...)
assert.NotEmpty(t, tc.err)
assert.Contains(t, err, tc.err)
})
}
}
}
func TestRemoteLifecycle(t *testing.T) {
// This test requires the service with access to Pulumi Deployments.
// Set PULUMI_ACCESS_TOKEN to an access token with access to Pulumi Deployments,
// set PULUMI_TEST_OWNER to the organization to use for the fully qualified stack,
// and set PULUMI_TEST_DEPLOYMENTS_API to any value to enable the test.
if os.Getenv("PULUMI_ACCESS_TOKEN") == "" {
t.Skipf("Skipping: PULUMI_ACCESS_TOKEN is not set")
}
if os.Getenv("PULUMI_TEST_OWNER") == "" {
t.Skipf("Skipping: PULUMI_TEST_OWNER is not set")
}
if os.Getenv("PULUMI_TEST_DEPLOYMENTS_API") == "" {
t.Skipf("Skipping: PULUMI_TEST_DEPLOYMENTS_API is not set")
}
t.Parallel()
e := ptesting.NewEnvironment(t)
defer deleteIfNotFailed(e)
// Remote flags currently require PULUMI_EXPERIMENTAL.
e.Env = append(e.Env, "PULUMI_EXPERIMENTAL=true")
randomSuffix := func() string {
b := make([]byte, 4)
_, err := rand.Read(b)
assert.NoError(t, err)
return hex.EncodeToString(b)
}
owner := os.Getenv("PULUMI_TEST_OWNER")
proj := "go_remote_proj"
stack := strings.ToLower("p-t-remotelifecycle-" + randomSuffix())
fullyQualifiedStack := fmt.Sprintf("%s/%s/%s", owner, proj, stack)
e.RunCommand("pulumi", "stack", "init", "--no-select", "--stack", fullyQualifiedStack)
args := func(command string) []string {
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
return []string{
command, remoteTestRepo, "--stack", fullyQualifiedStack,
"--remote", "--remote-git-branch", "refs/heads/master", "--remote-git-repo-dir", "goproj",
}
}
e.RunCommand("pulumi", args("preview")...)
e.RunCommand("pulumi", args("up")...)
e.RunCommand("pulumi", args("refresh")...)
e.RunCommand("pulumi", args("destroy")...)
e.RunCommand("pulumi", "stack", "rm", "--stack", fullyQualifiedStack, "--yes")
}