. "github.com/cloudfoundry/hm9000/desiredstatefetcher" "github.com/cloudfoundry/hm9000/testhelpers/app" . "github.com/cloudfoundry/hm9000/testhelpers/custommatchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Desired State Server Response JSON", func() { var ( a app.App response DesiredStateServerResponse ) BeforeEach(func() { a = app.NewApp() desired, _ := json.Marshal(a.DesiredState(0)) responseJson := fmt.Sprintf(` { "results":{"%s":%s}, "bulk_token":{"id":17} } `, a.AppGuid, string(desired)) var err error response, err = NewDesiredStateServerResponse([]byte(responseJson)) Ω(err).ShouldNot(HaveOccured()) }) It("can parse from JSON", func() { Ω(response.Results).Should(HaveLen(1)) Ω(response.Results[a.AppGuid]).Should(EqualDesiredState(a.DesiredState(0)))
app1 = app.NewApp() app2 = app.NewApp() app3 = app.NewApp() store = NewStore(conf, etcdAdapter) }) AfterEach(func() { etcdAdapter.Disconnect() }) Describe("Saving desired state ", func() { BeforeEach(func() { err := store.SaveDesiredState([]models.DesiredAppState{ app1.DesiredState(0), app2.DesiredState(0), }) Ω(err).ShouldNot(HaveOccured()) }) It("can stores the passed in desired state", func() { nodes, err := etcdAdapter.List("/desired") Ω(err).ShouldNot(HaveOccured()) Ω(nodes).Should(HaveLen(2)) Ω(nodes).Should(ContainElement(storeadapter.StoreNode{ Key: "/desired/" + app1.AppGuid + "-" + app1.AppVersion, Value: app1.DesiredState(0).ToJson(), TTL: conf.DesiredStateTTL - 1, })) Ω(nodes).Should(ContainElement(storeadapter.StoreNode{
Ω(query.Get("bulk_token")).Should(Equal("{}")) }) Context("when a response with desired state is received", func() { var ( a1 app.App a2 app.App ) BeforeEach(func() { a1 = app.NewApp() a2 = app.NewApp() response = DesiredStateServerResponse{ Results: map[string]models.DesiredAppState{ a1.AppGuid: a1.DesiredState(0), a2.AppGuid: a2.DesiredState(0), }, BulkToken: BulkToken{ Id: 5, }, } httpClient.LastRequest().Succeed(response.ToJson()) }) It("should store the desired states", func() { desired, _ := store.GetDesiredState() Ω(desired).Should(HaveLen(2)) Ω(desired).Should(ContainElement(EqualDesiredState(a1.DesiredState(0)))) Ω(desired).Should(ContainElement(EqualDesiredState(a2.DesiredState(0))))
} etcdStoreAdapter.Set([]storeadapter.StoreNode{node}) } Context("When /desired and /actual are missing", func() { It("Should not send any start or stop messages", func() { startMessages, stopMessages, err := analyzer.Analyze() Ω(err).ShouldNot(HaveOccured()) Ω(startMessages).Should(BeEmpty()) Ω(stopMessages).Should(BeEmpty()) }) }) Context("When /desired and /actual are empty", func() { BeforeEach(func() { desired := a1.DesiredState(42) actual := a2.GetInstance(0).Heartbeat(30) insertDesiredIntoStore(desired) insertActualIntoStore(actual) etcdStoreAdapter.Delete("/desired/" + desired.StoreKey()) etcdStoreAdapter.Delete("/actual/" + actual.StoreKey()) }) It("Should not send any start or stop messages", func() { startMessages, stopMessages, err := analyzer.Analyze() Ω(err).ShouldNot(HaveOccured()) Ω(startMessages).Should(BeEmpty()) Ω(stopMessages).Should(BeEmpty()) })
err = store.BumpActualFreshness(time.Unix(17, 0)) Ω(err).Should(Equal(errIn)) store.Reset() err = store.BumpDesiredFreshness(time.Unix(12, 0)) Ω(err).ShouldNot(HaveOccured()) err = store.BumpActualFreshness(time.Unix(12, 0)) Ω(err).ShouldNot(HaveOccured()) }) }) Describe("Setting, getting, and deleting desired state", func() { It("should set, get, and delete the desired state", func() { desired1 := app1.DesiredState(0) desired2 := app2.DesiredState(0) err := store.SaveDesiredState([]models.DesiredAppState{desired1, desired1, desired2}) Ω(err).ShouldNot(HaveOccured()) desired, err := store.GetDesiredState() Ω(err).ShouldNot(HaveOccured()) Ω(desired).Should(HaveLen(2)) Ω(desired).Should(ContainElement(EqualDesiredState(desired1))) Ω(desired).Should(ContainElement(EqualDesiredState(desired2))) desired2.NumberOfInstances = 17 desired3 := app.NewApp().DesiredState(2) err = store.SaveDesiredState([]models.DesiredAppState{desired2, desired3})
var ( fetcher *desiredstatefetcher.DesiredStateFetcher a1 app.App a2 app.App a3 app.App resultChan chan desiredstatefetcher.DesiredStateFetcherResult ) BeforeEach(func() { resultChan = make(chan desiredstatefetcher.DesiredStateFetcherResult, 1) a1 = app.NewApp() a2 = app.NewApp() a3 = app.NewApp() stateServer.SetDesiredState([]models.DesiredAppState{ a1.DesiredState(0), a2.DesiredState(0), a3.DesiredState(0), }) fetcher = desiredstatefetcher.New(conf, natsRunner.MessageBus, store.NewStore(conf, storeAdapter), httpclient.NewHttpClient(), &timeprovider.RealTimeProvider{}) fetcher.Fetch(resultChan) }) It("requests for the first set of data from the CC and stores the response", func() { var node storeadapter.StoreNode var err error Eventually(func() error { node, err = storeAdapter.Get("/desired/" + a1.AppGuid + "-" + a1.AppVersion) return err }, 1, 0.1).ShouldNot(HaveOccured())