github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/stack.go (about) 1 package helpers 2 3 import ( 4 "encoding/json" 5 6 "fmt" 7 "strings" 8 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gbytes" 11 . "github.com/onsi/gomega/gexec" 12 ) 13 14 type ccStacks struct { 15 Resources []struct { 16 Entity struct { 17 Name string `json:"name"` 18 } `json:"entity"` 19 } `json:"resources"` 20 } 21 22 // FetchStacks returns all the stack names present in the foundation. 23 func FetchStacks() []string { 24 session := CF("curl", "/v2/stacks") 25 26 Eventually(session).Should(Exit(0)) 27 28 var rawStacks ccStacks 29 err := json.Unmarshal(session.Out.Contents(), &rawStacks) 30 Expect(err).ToNot(HaveOccurred()) 31 32 var stacks []string 33 for _, stack := range rawStacks.Resources { 34 stacks = append(stacks, stack.Entity.Name) 35 } 36 37 return stacks 38 } 39 40 // PreferredStack returns the cflinuxfs3 stack name if it present, otherwise cflinuxfs2 is returned. 41 func PreferredStack() string { 42 stacks := FetchStacks() 43 44 for _, name := range stacks { 45 if name == "cflinuxfs3" { 46 return name 47 } 48 } 49 50 return "cflinuxfs2" 51 } 52 53 // CreateStack creates a new stack with the user provided name. If a name is not provided, a random name is used 54 func CreateStack(names ...string) string { 55 name := NewStackName() 56 if len(names) > 0 { 57 name = names[0] 58 } 59 60 requestBody := fmt.Sprintf( 61 `{"name":"%s","description":"CF CLI integration test stack, please delete"}`, 62 name, 63 ) 64 session := CF("curl", "-v", "-X", "POST", "/v2/stacks", "-d", requestBody) 65 66 Eventually(session).Should(Say("201 Created")) 67 Eventually(session).Should(Exit(0)) 68 69 return name 70 } 71 72 // DeleteStack deletes a specific stack 73 func DeleteStack(name string) { 74 session := CF("stack", "--guid", name) 75 Eventually(session).Should(Exit(0)) 76 guid := strings.TrimSpace(string(session.Out.Contents())) 77 78 session = CF("curl", "-v", "-X", "DELETE", "/v2/stacks/"+guid) 79 80 Eventually(session).Should(Say("204 No Content")) 81 Eventually(session).Should(Exit(0)) 82 } 83 84 // EnsureMinimumNumberOfStacks ensures there are at least 2 stacks in the foundation by creating new ones if there 85 // are fewer than 2 86 func EnsureMinimumNumberOfStacks(num int) []string { 87 var stacks []string 88 for stacks = FetchStacks(); len(stacks) < 2; { 89 stacks = append(stacks, CreateStack()) 90 } 91 return stacks 92 }