github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/helpers/stack.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"fmt"
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gbytes"
     9  	. "github.com/onsi/gomega/gexec"
    10  	"strings"
    11  )
    12  
    13  type ccStacks struct {
    14  	Resources []struct {
    15  		Entity struct {
    16  			Name string `json:"name"`
    17  		} `json:"entity"`
    18  	} `json:"resources"`
    19  }
    20  
    21  func FetchStacks() []string {
    22  	session := CF("curl", "/v2/stacks")
    23  
    24  	Eventually(session).Should(Exit(0))
    25  
    26  	var rawStacks ccStacks
    27  	err := json.Unmarshal(session.Out.Contents(), &rawStacks)
    28  	Expect(err).ToNot(HaveOccurred())
    29  
    30  	var stacks []string
    31  	for _, stack := range rawStacks.Resources {
    32  		stacks = append(stacks, stack.Entity.Name)
    33  	}
    34  
    35  	return stacks
    36  }
    37  
    38  func CreateStack(names ...string) string {
    39  	name := NewStackName()
    40  	if len(names) > 0 {
    41  		name = names[0]
    42  	}
    43  
    44  	requestBody := fmt.Sprintf(
    45  		`{"name":"%s","description":"CF CLI integration test stack, please delete"}`,
    46  		name,
    47  	)
    48  	session := CF("curl", "-v", "-X", "POST", "/v2/stacks", "-d", requestBody)
    49  
    50  	Eventually(session).Should(Say("201 Created"))
    51  	Eventually(session).Should(Exit(0))
    52  
    53  	return name
    54  }
    55  
    56  func DeleteStack(name string) {
    57  	session := CF("stack", "--guid", name)
    58  	Eventually(session).Should(Exit(0))
    59  	guid := strings.TrimSpace(string(session.Out.Contents()))
    60  
    61  	session = CF("curl", "-v", "-X", "DELETE", "/v2/stacks/"+guid)
    62  
    63  	Eventually(session).Should(Say("204 No Content"))
    64  	Eventually(session).Should(Exit(0))
    65  }
    66  
    67  func EnsureMinimumNumberOfStacks(num int) []string {
    68  	var stacks []string
    69  	for stacks = FetchStacks(); len(stacks) < 2; {
    70  		stacks = append(stacks, CreateStack())
    71  	}
    72  	return stacks
    73  }