github.com/anuvu/nomad@v0.8.7-atom1/e2e/consul/canary_tags_test.go (about) 1 package consul_test 2 3 import ( 4 "flag" 5 "testing" 6 "time" 7 8 consulapi "github.com/hashicorp/consul/api" 9 "github.com/hashicorp/nomad/api" 10 "github.com/hashicorp/nomad/helper" 11 "github.com/hashicorp/nomad/helper/uuid" 12 "github.com/hashicorp/nomad/jobspec" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var integration = flag.Bool("integration", false, "run integration tests") 18 19 func TestConsul(t *testing.T) { 20 if !*integration { 21 t.Skip("skipping test in non-integration mode.") 22 } 23 RegisterFailHandler(Fail) 24 RunSpecs(t, "Consul Canary Tags Test") 25 } 26 27 var _ = Describe("Consul Canary Tags Test", func() { 28 29 var ( 30 agent *consulapi.Agent 31 allocations *api.Allocations 32 deployments *api.Deployments 33 jobs *api.Jobs 34 system *api.System 35 job *api.Job 36 specFile string 37 ) 38 39 BeforeSuite(func() { 40 consulConf := consulapi.DefaultConfig() 41 consulClient, err := consulapi.NewClient(consulConf) 42 Expect(err).ShouldNot(HaveOccurred()) 43 agent = consulClient.Agent() 44 45 conf := api.DefaultConfig() 46 client, err := api.NewClient(conf) 47 Expect(err).ShouldNot(HaveOccurred()) 48 allocations = client.Allocations() 49 deployments = client.Deployments() 50 jobs = client.Jobs() 51 system = client.System() 52 }) 53 54 JustBeforeEach(func() { 55 var err error 56 job, err = jobspec.ParseFile(specFile) 57 Expect(err).ShouldNot(HaveOccurred()) 58 job.ID = helper.StringToPtr(*job.ID + uuid.Generate()[22:]) 59 resp, _, err := jobs.Register(job, nil) 60 Expect(err).ShouldNot(HaveOccurred()) 61 Expect(resp.EvalID).ShouldNot(BeEmpty()) 62 }) 63 64 AfterEach(func() { 65 jobs.Deregister(*job.ID, true, nil) 66 system.GarbageCollect() 67 }) 68 69 Describe("Consul Canary Tags Test", func() { 70 Context("Canary Tags", func() { 71 BeforeEach(func() { 72 specFile = "input/canary_tags.hcl" 73 }) 74 75 It("Should set and unset canary tags", func() { 76 77 // Eventually be running and healthy 78 Eventually(func() []string { 79 deploys, _, err := jobs.Deployments(*job.ID, nil) 80 Expect(err).ShouldNot(HaveOccurred()) 81 healthyDeploys := make([]string, 0, len(deploys)) 82 for _, d := range deploys { 83 if d.Status == "successful" { 84 healthyDeploys = append(healthyDeploys, d.ID) 85 } 86 } 87 return healthyDeploys 88 }, 5*time.Second, 20*time.Millisecond).Should(HaveLen(1)) 89 90 // Start a deployment 91 job.Meta = map[string]string{"version": "2"} 92 resp, _, err := jobs.Register(job, nil) 93 Expect(err).ShouldNot(HaveOccurred()) 94 Expect(resp.EvalID).ShouldNot(BeEmpty()) 95 96 // Eventually have a canary 97 var deploys []*api.Deployment 98 Eventually(func() []*api.Deployment { 99 deploys, _, err = jobs.Deployments(*job.ID, nil) 100 Expect(err).ShouldNot(HaveOccurred()) 101 return deploys 102 }, 2*time.Second, 20*time.Millisecond).Should(HaveLen(2)) 103 104 var deploy *api.Deployment 105 Eventually(func() []string { 106 deploy, _, err = deployments.Info(deploys[0].ID, nil) 107 Expect(err).ShouldNot(HaveOccurred()) 108 return deploy.TaskGroups["consul_canary_test"].PlacedCanaries 109 }, 2*time.Second, 20*time.Millisecond).Should(HaveLen(1)) 110 111 Eventually(func() bool { 112 allocID := deploy.TaskGroups["consul_canary_test"].PlacedCanaries[0] 113 alloc, _, err := allocations.Info(allocID, nil) 114 Expect(err).ShouldNot(HaveOccurred()) 115 return alloc.DeploymentStatus != nil && alloc.DeploymentStatus.Healthy != nil && *alloc.DeploymentStatus.Healthy 116 }, 3*time.Second, 20*time.Millisecond).Should(BeTrue()) 117 118 // Check Consul for canary tags 119 Eventually(func() []string { 120 services, err := agent.Services() 121 Expect(err).ShouldNot(HaveOccurred()) 122 for _, v := range services { 123 if v.Service == "canarytest" { 124 return v.Tags 125 } 126 } 127 return nil 128 }, 2*time.Second, 20*time.Millisecond).Should( 129 Equal([]string{"foo", "canary"})) 130 131 // Manually promote 132 { 133 resp, _, err := deployments.PromoteAll(deploys[0].ID, nil) 134 Expect(err).ShouldNot(HaveOccurred()) 135 Expect(resp.EvalID).ShouldNot(BeEmpty()) 136 } 137 138 // Eventually canary is removed 139 Eventually(func() bool { 140 allocID := deploy.TaskGroups["consul_canary_test"].PlacedCanaries[0] 141 alloc, _, err := allocations.Info(allocID, nil) 142 Expect(err).ShouldNot(HaveOccurred()) 143 return alloc.DeploymentStatus.Canary 144 }, 2*time.Second, 20*time.Millisecond).Should(BeFalse()) 145 146 // Check Consul canary tags were removed 147 Eventually(func() []string { 148 services, err := agent.Services() 149 Expect(err).ShouldNot(HaveOccurred()) 150 for _, v := range services { 151 if v.Service == "canarytest" { 152 return v.Tags 153 } 154 } 155 return nil 156 }, 2*time.Second, 20*time.Millisecond).Should( 157 Equal([]string{"foo", "bar"})) 158 }) 159 }) 160 }) 161 })