github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/testing/integration/pulumi.go (about)

     1  // Copyright 2016-2018, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package integration
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"strings"
    23  
    24  	"github.com/pulumi/pulumi/sdk/v3/go/common/testing"
    25  	"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  // CreateBasicPulumiRepo will initialize the environment with a basic Pulumi repository and
    30  // project file definition. Returns the repo owner and name used.
    31  func CreateBasicPulumiRepo(e *testing.Environment) {
    32  	e.RunCommand("git", "init")
    33  
    34  	contents := "name: pulumi-test\ndescription: a test\nruntime: nodejs\n"
    35  	filePath := fmt.Sprintf("%s.yaml", workspace.ProjectFile)
    36  	filePath = path.Join(e.CWD, filePath)
    37  	err := ioutil.WriteFile(filePath, []byte(contents), os.ModePerm)
    38  	assert.NoError(e, err, "writing %s file", filePath)
    39  }
    40  
    41  // CreatePulumiRepo will initialize the environment with a basic Pulumi repository and
    42  // project file definition based on the project file content.
    43  // Returns the repo owner and name used.
    44  func CreatePulumiRepo(e *testing.Environment, projectFileContent string) {
    45  	e.RunCommand("git", "init")
    46  	filePath := path.Join(e.CWD, fmt.Sprintf("%s.yaml", workspace.ProjectFile))
    47  	err := ioutil.WriteFile(filePath, []byte(projectFileContent), os.ModePerm)
    48  	assert.NoError(e, err, "writing %s file", filePath)
    49  }
    50  
    51  // GetStacks returns the list of stacks and current stack by scraping `pulumi stack ls`.
    52  // Assumes .pulumi is in the current working directory. Fails the test on IO errors.
    53  func GetStacks(e *testing.Environment) ([]string, *string) {
    54  	out, err := e.RunCommand("pulumi", "stack", "ls")
    55  
    56  	outLines := strings.Split(out, "\n")
    57  	if len(outLines) == 0 {
    58  		e.Fatalf("command didn't output as expected")
    59  	}
    60  
    61  	// Confirm header row matches.
    62  	// TODO(pulumi/pulumi/issues/496): Provide structured output for pulumi commands. e.g., so we can avoid this
    63  	// err-prone scraping with just deserializings a JSON object.
    64  	assert.True(e, strings.HasPrefix(outLines[0], "NAME"), "First line was: %q\n--\n%q\n--\n%q\n", outLines[0], out, err)
    65  
    66  	var stackNames []string
    67  	var currentStack *string
    68  	stackSummaries := outLines[1:]
    69  	for _, summary := range stackSummaries {
    70  		if summary == "" {
    71  			break
    72  		}
    73  		firstSpace := strings.Index(summary, " ")
    74  		if firstSpace != -1 {
    75  			stackName := strings.TrimSpace(summary[:firstSpace])
    76  			if strings.HasSuffix(stackName, "*") {
    77  				currentStack = &stackName
    78  				stackName = strings.TrimSuffix(stackName, "*")
    79  			}
    80  			stackNames = append(stackNames, stackName)
    81  		}
    82  	}
    83  
    84  	return stackNames, currentStack
    85  }