pulumi/pkg/testing/integration/command.go
Matt Ellis d3240fdc64 Require pulumi login before commands that need a backend
This change does three major things:

1. Removes the ability to be logged into multiple clouds at the same
time. Previously, we supported being logged into multiple clouds at
the same time and the CLI would fan out requests and join responses
when needed. In general, this was only useful for Pulumi employees
that wanted run against multiple copies of the service (say production
and staging) but overall was very confusing (for example in the old
world a stack with the same identity could appear twice (since it was
in two backends) which the CLI didn't handle very well).

2. Stops treating the "local" backend as a special thing, from the
point of view of the CLI. Previouly we'd always connect to the local
backend and merge that data with whatever was in clouds we were
connected to. We had gestures like `--local` in `pulumi stack init`
that meant "use the local mode". Instead, to use the local mode now
you run `pulumi login --cloud-url local://` and then you are logged in
the local backend. Since you can only ever be logged into a single
backend, we can remove the `--local` and `--remote` flags from `pulumi
stack init`, it just now requires you to be logged in and creates a
stack in whatever back end you were logged into. When logging into the
local backend, you are not prompted for an access key.

3. Prompt for login in places where you have to log in, if you are not
already logged in.
2018-04-05 10:19:41 -07:00

122 lines
3.2 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package integration
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
// RunCommand executes the specified command and additional arguments, wrapping any output in the
// specialized test output streams that list the location the test is running in.
func RunCommand(t *testing.T, name string, args []string, wd string, opts *ProgramTestOptions) error {
path := args[0]
command := strings.Join(args, " ")
fprintf(opts.Stdout, "**** Invoke '%v' in '%v'\n", command, wd)
// Spawn a goroutine to print out "still running..." messages.
finished := false
go func() {
for !finished {
time.Sleep(30 * time.Second)
if !finished {
fprintf(opts.Stderr, "Still running command '%s' (%s)...\n", command, wd)
}
}
}()
env := os.Environ()
if opts.Env != nil {
env = append(env, opts.Env...)
}
env = append(env, "PULUMI_RETAIN_CHECKPOINTS=true")
env = append(env, "PULUMI_CONFIG_PASSPHRASE=correct horse battery staple")
cmd := exec.Cmd{
Path: path,
Dir: wd,
Args: args,
Env: env,
}
startTime := time.Now()
var runout []byte
var runerr error
if opts.Verbose || os.Getenv("PULUMI_VERBOSE_TEST") != "" {
cmd.Stdout = opts.Stdout
cmd.Stderr = opts.Stderr
runerr = cmd.Run()
} else {
runout, runerr = cmd.CombinedOutput()
}
endTime := time.Now()
if opts.ReportStats != nil {
// Note: This data is archived and used by external analytics tools. Take care if changing the schema or format
// of this data.
opts.ReportStats.ReportCommand(TestCommandStats{
StartTime: startTime.Format("2006/01/02 15:04:05"),
EndTime: endTime.Format("2006/01/02 15:04:05"),
ElapsedSeconds: float64((endTime.Sub(startTime)).Nanoseconds()) / 1000000000,
StepName: name,
CommandLine: command,
StackName: string(opts.GetStackName()),
TestID: wd,
TestName: filepath.Base(opts.Dir),
IsError: runerr != nil,
CloudURL: opts.CloudURL,
CloudPPC: opts.PPCName,
})
}
finished = true
if runerr != nil {
fprintf(opts.Stderr, "Invoke '%v' failed: %s\n", command, cmdutil.DetailedError(runerr))
if !opts.Verbose {
// We've seen long fprintf's fail on Travis, so avoid panicing.
if _, err := fmt.Fprintf(opts.Stderr, "%s\n", string(runout)); err != nil {
fprintf(opts.Stderr, "\n\nOutput truncated: %v\n", err)
}
}
}
// If we collected any program output, write it to a log file -- success or failure.
if len(runout) > 0 {
if logFile, err := writeCommandOutput(name, wd, runout); err != nil {
fprintf(opts.Stderr, "Failed to write output: %v\n", err)
} else {
fprintf(opts.Stderr, "Wrote output to %s\n", logFile)
}
}
return runerr
}
func withOptionalYarnFlags(args []string) []string {
flags := os.Getenv("YARNFLAGS")
if flags != "" {
return append(args, flags)
}
return args
}
// addFlagIfNonNil will take a set of command-line flags, and add a new one if the provided flag value is not empty.
func addFlagIfNonNil(args []string, flag, flagValue string) []string {
if flagValue != "" {
args = append(args, flag, flagValue)
}
return args
}