pulumi/pkg/testing/integration/pulumi.go
joeduffy 8b5874dab5 General prep work for refresh
This change includes a bunch of refactorings I made in prep for
doing refresh (first, the command, see pulumi/pulumi#1081):

* The primary change is to change the way the engine's core update
  functionality works with respect to deploy.Source.  This is the
  way we can plug in new sources of resource information during
  planning (and, soon, diffing).  The way I intend to model refresh
  is by having a new kind of source, deploy.RefreshSource, which
  will let us do virtually everything about an update/diff the same
  way with refreshes, which avoid otherwise duplicative effort.

  This includes changing the planOptions (nee deployOptions) to
  take a new SourceFunc callback, which is responsible for creating
  a source specific to the kind of plan being requested.

  Preview, Update, and Destroy now are primarily differentiated by
  the kind of deploy.Source that they return, rather than sprinkling
  things like `if Destroying` throughout.  This tidies up some logic
  and, more importantly, gives us precisely the refresh hook we need.

* Originally, we used the deploy.NullSource for Destroy operations.
  This simply returns nothing, which is how Destroy works.  For some
  reason, we were no longer doing this, and instead had some
  `if Destroying` cases sprinkled throughout the deploy.EvalSource.
  I think this is a vestige of some old way we did configuration, at
  least judging by a comment, which is apparently no longer relevant.

* Move diff and diff-printing logic within the engine into its own
  pkg/engine/diff.go file, to prepare for upcoming work.

* I keep noticing benign diffs anytime I regenerate protobufs.  I
  suspect this is because we're also on different versions.  I changed
  generate.sh to also dump the version into grpc_version.txt.  At
  least we can understand where the diffs are coming from, decide
  whether to take them (i.e., a newer version), and ensure that as
  a team we are monotonically increasing, and not going backwards.

* I also tidied up some tiny things I noticed while in there, like
  comments, incorrect types, lint suppressions, and so on.
2018-03-28 07:45:23 -07:00

101 lines
3.4 KiB
Go

package integration
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/pulumi/pulumi/pkg/testing"
"github.com/pulumi/pulumi/pkg/workspace"
"github.com/stretchr/testify/assert"
)
// CreateBasicPulumiRepo will initialize the environment with a basic Pulumi repository and
// project file definition. Returns the repo owner and name used.
func CreateBasicPulumiRepo(e *testing.Environment) (string, string) {
e.RunCommand("git", "init")
// We need to specify a well-known owner so that commands don't fail for reasons like
// the organization isn't in the private beta. The repository name is randomly generated
// so that testcases can assume no stacks, etc.
const owner = "moolumi"
name := fmt.Sprintf("integration-test-%x", time.Now().Unix())
e.RunCommand("pulumi", "init", "--owner", owner, "--name", name)
contents := "name: pulumi-test\ndescription: a test\nruntime: nodejs\n"
filePath := fmt.Sprintf("%s.yaml", workspace.ProjectFile)
filePath = path.Join(e.CWD, filePath)
err := ioutil.WriteFile(filePath, []byte(contents), os.ModePerm)
assert.NoError(e, err, "writing %s file", filePath)
return owner, name
}
// GetRepository returns the contents of the workspace's repository settings file. Assumes the
// bookkeeping dir (.pulumi) is in the CWD. Any IO errors fails the test.
func GetRepository(e *testing.Environment) workspace.Repository {
relativePathToRepoFile := fmt.Sprintf("%s/%s", workspace.BookkeepingDir, workspace.RepoFile)
if !e.PathExists(relativePathToRepoFile) {
e.Fatalf("did not find .pulumi/settings.json")
}
path := path.Join(e.CWD, relativePathToRepoFile)
contents, err := ioutil.ReadFile(path)
if err != nil {
e.Fatalf("error reading %s: %v", workspace.RepoFile, err)
}
var repo workspace.Repository
err = json.Unmarshal(contents, &repo)
if err != nil {
e.Fatalf("error unmarshalling JSON: %v", err)
}
return repo
}
// GetStacks returns the list of stacks and current stack by scraping `pulumi stack ls`.
// Assumes .pulumi is in the current working directory. Fails the test on IO errors.
func GetStacks(e *testing.Environment) ([]string, *string) {
out, _ := e.RunCommand("pulumi", "stack", "ls")
outLines := strings.Split(out, "\n")
if len(outLines) == 0 {
e.Fatalf("command didn't output as expected")
}
// Confirm header row matches.
// TODO(pulumi/pulumi/issues/496): Provide structured output for pulumi commands. e.g., so we can avoid this
// err-prone scraping with just deserializings a JSON object.
assert.True(e, strings.HasPrefix(outLines[0], "NAME"))
var stackNames []string
var currentStack *string
stackSummaries := outLines[1:]
for _, summary := range stackSummaries {
if summary == "" {
break
}
firstSpace := strings.Index(summary, " ")
if firstSpace != -1 {
stackName := strings.TrimSpace(summary[:firstSpace])
if strings.HasSuffix(stackName, "*") {
currentStack = &stackName
stackName = strings.TrimSuffix(stackName, "*")
}
stackNames = append(stackNames, stackName)
}
}
return stackNames, currentStack
}
// TestAccountAccessToken is a Pulumi access token of a well-known test account. Most importantly, we can
// rely on this test account existing in all Pulumi environments. GitHub user "lumi-test-1". See
// pulumi-service/cmd/service/model/test.go for more information.
// nolint: gas, intentionally using a test token
const TestAccountAccessToken = "gGo1WLk2Jh_NZlqo2FFvjPtBvo73IW3xsJvBRmDOPW4="