github.com/deiscc/workflow-e2e@v0.0.0-20181208071258-117299af888f/tests/tags_test.go (about) 1 package tests 2 3 import ( 4 "regexp" 5 "strings" 6 7 deis "github.com/deiscc/controller-sdk-go" 8 "github.com/deiscc/workflow-e2e/tests/cmd" 9 "github.com/deiscc/workflow-e2e/tests/cmd/apps" 10 "github.com/deiscc/workflow-e2e/tests/cmd/auth" 11 "github.com/deiscc/workflow-e2e/tests/cmd/builds" 12 "github.com/deiscc/workflow-e2e/tests/model" 13 "github.com/deiscc/workflow-e2e/tests/settings" 14 "github.com/deiscc/workflow-e2e/tests/util" 15 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/ginkgo/extensions/table" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 . "github.com/onsi/gomega/gexec" 21 ) 22 23 var _ = Describe("deis tags", func() { 24 25 Context("with an existing user", func() { 26 27 var user model.User 28 29 BeforeEach(func() { 30 user = auth.RegisterAndLogin() 31 }) 32 33 AfterEach(func() { 34 auth.Cancel(user) 35 }) 36 37 Context("who owns an existing app that has already been deployed", func() { 38 39 var app model.App 40 41 BeforeEach(func() { 42 app = apps.Create(user, "--no-remote") 43 builds.Create(user, app) 44 }) 45 46 AfterEach(func() { 47 apps.Destroy(user, app) 48 }) 49 50 Specify("that user can list that app's tags", func() { 51 sess, err := cmd.Start("deis tags:list --app=%s", &user, app.Name) 52 Eventually(sess).Should(Say("=== %s Tags", app.Name)) 53 Expect(err).NotTo(HaveOccurred()) 54 Eventually(sess).Should(Exit(0)) 55 }) 56 57 Specify("that user cannot set an invalid tag", func() { 58 sess, err := cmd.Start("deis tags:set --app=%s munkafolyamat=yeah", &user, app.Name) 59 Eventually(sess, settings.MaxEventuallyTimeout).ShouldNot(Say("=== %s Tags", app.Name)) 60 Eventually(sess).ShouldNot(Say(`munkafolyamat\s+yeah`, app.Name)) 61 Eventually(sess.Err).Should(Say(util.PrependError(deis.ErrTagNotFound))) 62 Expect(err).NotTo(HaveOccurred()) 63 Eventually(sess).Should(Exit(1)) 64 }) 65 66 Specify("that user cannot unset an invalid tag", func() { 67 sess, err := cmd.Start("deis tags:unset --app=%s munkafolyamat", &user, app.Name) 68 Eventually(sess).ShouldNot(Say(`munkafolyamat\s+yeah`, app.Name)) 69 Expect(err).NotTo(HaveOccurred()) 70 Eventually(sess).Should(Exit(1)) 71 }) 72 73 Specify("that user can set a valid tag", func() { 74 // Find a valid tag to set 75 // Use original $HOME dir or else kubectl can't find its config 76 sess, err := cmd.Start("HOME=%s kubectl get nodes -o jsonpath={.items[*].metadata..labels}", nil, settings.ActualHome) 77 Expect(err).NotTo(HaveOccurred()) 78 Eventually(sess).Should(Exit(0)) 79 80 // grep output like "map[kubernetes.io/hostname:192.168.64.2 node:worker1]" 81 re := regexp.MustCompile(`([\w\.\-]{0,253}/?[-_\.\w]{1,63}:[-_\.\w]{1,63})`) 82 pairs := re.FindAllString(string(sess.Out.Contents()), -1) 83 // Use the first key:value pair found 84 label := strings.Split(pairs[0], ":") 85 86 sess, err = cmd.Start("deis tags:set --app=%s %s=%s", &user, app.Name, label[0], label[1]) 87 Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("=== %s Tags", app.Name)) 88 Eventually(sess).Should(Say(`%s\s+%s`, label[0], label[1])) 89 Expect(err).NotTo(HaveOccurred()) 90 Eventually(sess).Should(Exit(0)) 91 92 sess, err = cmd.Start("deis tags:list --app=%s", &user, app.Name) 93 Eventually(sess).Should(Say("=== %s Tags", app.Name)) 94 Eventually(sess).Should(Say(`%s\s+%s`, label[0], label[1])) 95 Expect(err).NotTo(HaveOccurred()) 96 Eventually(sess).Should(Exit(0)) 97 }) 98 99 Context("and a tag has already been added to the app", func() { 100 101 var label []string 102 103 BeforeEach(func() { 104 // Find a valid tag to set 105 // Use original $HOME dir or else kubectl can't find its config 106 sess, err := cmd.Start("HOME=%s kubectl get nodes -o jsonpath={.items[*].metadata..labels}", nil, settings.ActualHome) 107 Expect(err).NotTo(HaveOccurred()) 108 Eventually(sess).Should(Exit(0)) 109 110 // grep output like "map[kubernetes.io/hostname:192.168.64.2 node:worker1]" 111 re := regexp.MustCompile(`([\w\.\-]{0,253}/?[-_\.\w]{1,63}:[-_\.\w]{1,63})`) 112 pairs := re.FindAllString(string(sess.Out.Contents()), -1) 113 // Use the first key:value pair found 114 label = strings.Split(pairs[0], ":") 115 116 sess, err = cmd.Start("deis tags:set --app=%s %s=%s", &user, app.Name, label[0], label[1]) 117 Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("=== %s Tags", app.Name)) 118 Eventually(sess).Should(Say(`%s\s+%s`, label[0], label[1])) 119 Expect(err).NotTo(HaveOccurred()) 120 Eventually(sess).Should(Exit(0)) 121 }) 122 123 Specify("that user can unset that tag from that app", func() { 124 sess, err := cmd.Start("deis tags:unset --app=%s %s", &user, app.Name, label[0]) 125 Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("=== %s Tags", app.Name)) 126 Eventually(sess).ShouldNot(Say(`%s\s+%s`, label[0], label[1])) 127 Expect(err).NotTo(HaveOccurred()) 128 Eventually(sess).Should(Exit(0)) 129 130 sess, err = cmd.Start("deis tags:list --app=%s", &user, app.Name) 131 Eventually(sess).Should(Say("=== %s Tags", app.Name)) 132 Eventually(sess).ShouldNot(Say(`%s\s+%s`, label[0], label[1])) 133 Eventually(sess).ShouldNot(Say(`munkafolyamat\s+yeah`, app.Name)) 134 Expect(err).NotTo(HaveOccurred()) 135 Eventually(sess).Should(Exit(0)) 136 }) 137 138 }) 139 140 }) 141 142 }) 143 144 DescribeTable("any user can get command-line help for tags", func(command string, expected string) { 145 sess, err := cmd.Start(command, nil) 146 Eventually(sess).Should(Say(expected)) 147 Expect(err).NotTo(HaveOccurred()) 148 Eventually(sess).Should(Exit(0)) 149 // TODO: test that help output was more than five lines long 150 }, 151 Entry("helps on \"help tags\"", 152 "deis help tags", "Valid commands for tags:"), 153 Entry("helps on \"tags -h\"", 154 "deis tags -h", "Valid commands for tags:"), 155 Entry("helps on \"tags --help\"", 156 "deis tags --help", "Valid commands for tags:"), 157 Entry("helps on \"help tags:list\"", 158 "deis help tags:list", "Lists tags for an application."), 159 Entry("helps on \"tags:list -h\"", 160 "deis tags:list -h", "Lists tags for an application."), 161 Entry("helps on \"tags:list --help\"", 162 "deis tags:list --help", "Lists tags for an application."), 163 Entry("helps on \"help tags:set\"", 164 "deis help tags:set", "Sets tags for an application."), 165 Entry("helps on \"tags:set -h\"", 166 "deis tags:set -h", "Sets tags for an application."), 167 Entry("helps on \"tags:set --help\"", 168 "deis tags:set --help", "Sets tags for an application."), 169 Entry("helps on \"help tags:unset\"", 170 "deis help tags:unset", "Unsets tags for an application."), 171 Entry("helps on \"tags:unset -h\"", 172 "deis tags:unset -h", "Unsets tags for an application."), 173 Entry("helps on \"tags:unset --help\"", 174 "deis tags:unset --help", "Unsets tags for an application."), 175 ) 176 177 })