github.com/deis/workflow-e2e@v2.12.2-0.20180227201524-4105be7001fe+incompatible/tests/cmd/keys/commands.go (about)

     1  package keys
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"path"
     8  	"time"
     9  
    10  	"github.com/deis/workflow-e2e/tests/cmd"
    11  	"github.com/deis/workflow-e2e/tests/model"
    12  	"github.com/deis/workflow-e2e/tests/settings"
    13  
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  	. "github.com/onsi/gomega/gexec"
    17  )
    18  
    19  // The functions in this file implement SUCCESS CASES for commonly used `deis keys` subcommands.
    20  // This allows each of these to be re-used easily in multiple contexts.
    21  
    22  // Add executes `deis keys:add` as the specified user to add a new key to that user's account.
    23  func Add(user model.User) (string, string) {
    24  	keyName, keyPath := createKey()
    25  	sess, err := cmd.Start("deis keys:add %s.pub", &user, keyPath)
    26  	Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("Uploading %s.pub to deis... done", keyName))
    27  	Expect(err).NotTo(HaveOccurred())
    28  	Eventually(sess).Should(Exit(0))
    29  	time.Sleep(5 * time.Second) // Wait for the key to propagate before continuing
    30  	return keyName, keyPath
    31  }
    32  
    33  // Remove executes `deis keys:remove` as the specified user to remove the specified key from that
    34  // user's account.
    35  func Remove(user model.User, keyName string) {
    36  	sess, err := cmd.Start("deis keys:remove %s", &user, keyName)
    37  	Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("Removing %s SSH Key... done", keyName))
    38  	Eventually(sess).Should(Exit(0))
    39  	Expect(err).NotTo(HaveOccurred())
    40  }
    41  
    42  func createKey() (string, string) {
    43  	keyName := fmt.Sprintf("deiskey-%v", rand.Intn(1000))
    44  	sshHome := path.Join(settings.TestHome, ".ssh")
    45  	os.MkdirAll(sshHome, 0777)
    46  	keyPath := path.Join(sshHome, keyName)
    47  	if _, err := os.Stat(keyPath); os.IsNotExist(err) {
    48  		_, err := cmd.Execute("ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''", keyName, keyPath)
    49  		Expect(err).NotTo(HaveOccurred())
    50  	}
    51  	os.Chmod(keyPath, 0600)
    52  	return keyName, keyPath
    53  }