github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  	var name string
    56  
    57  	if len(names) > 0 {
    58  		name = names[0]
    59  	} else {
    60  		name = NewStackName()
    61  	}
    62  
    63  	requestBody := fmt.Sprintf(
    64  		`{"name":"%s","description":"CF CLI integration test stack, please delete"}`,
    65  		name,
    66  	)
    67  	session := CF("curl", "-v", "-X", "POST", "/v3/stacks", "-d", requestBody)
    68  
    69  	Eventually(session).Should(Say("201 Created"))
    70  	Eventually(session).Should(Exit(0))
    71  
    72  	return name
    73  }
    74  
    75  // CreateStackWithGUID creates a stack with a random name and returns its name and guid
    76  func CreateStackWithGUID() (string, string) {
    77  	type StackStruct struct {
    78  		GUID string `json:"guid"`
    79  	}
    80  	name := NewStackName()
    81  	requestBody := fmt.Sprintf(
    82  		`{"name":"%s","description":"CF CLI integration test stack, please delete"}`,
    83  		name,
    84  	)
    85  	session := CF("curl", "-X", "POST", "/v3/stacks", "-d", requestBody)
    86  
    87  	Eventually(session).Should(Exit(0))
    88  	thisStack := StackStruct{}
    89  	err := json.Unmarshal(session.Out.Contents(), &thisStack)
    90  	Expect(err).ToNot(HaveOccurred())
    91  	stackGUID := thisStack.GUID
    92  	Expect(len(stackGUID)).ToNot(Equal(0))
    93  	return name, stackGUID
    94  }
    95  
    96  // DeleteStack deletes a specific stack
    97  func DeleteStack(name string) {
    98  	session := CF("stack", "--guid", name)
    99  	Eventually(session).Should(Exit(0))
   100  	guid := strings.TrimSpace(string(session.Out.Contents()))
   101  
   102  	session = CF("curl", "-v", "-X", "DELETE", "/v3/stacks/"+guid)
   103  
   104  	Eventually(session).Should(Say("204 No Content"))
   105  	Eventually(session).Should(Exit(0))
   106  }
   107  
   108  // EnsureMinimumNumberOfStacks ensures there are at least <num> stacks in the foundation by creating new ones if there
   109  // are fewer than the specified number
   110  func EnsureMinimumNumberOfStacks(num int) []string {
   111  	var stacks []string
   112  	for stacks = FetchStacks(); len(stacks) < num; {
   113  		stacks = append(stacks, CreateStack())
   114  	}
   115  	return stacks
   116  }