Context("when reading configuration from config path succeeds", func() { Context("when writing job configuration succeeds", func() { It("returns no error because monit can track added job in jobs directory", func() { err := monit.AddJob("router", 0, "/some/config/path") Expect(err).ToNot(HaveOccurred()) writtenConfig, err := fs.ReadFileString( dirProvider.MonitJobsDir() + "/0000_router.monitrc") Expect(err).ToNot(HaveOccurred()) Expect(writtenConfig).To(Equal("fake-config")) }) }) Context("when writing job configuration fails", func() { It("returns error", func() { fs.WriteToFileError = errors.New("fake-write-error") err := monit.AddJob("router", 0, "/some/config/path") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) }) Context("when reading configuration from config path fails", func() { It("returns error", func() { fs.ReadFileError = errors.New("fake-read-error") err := monit.AddJob("router", 0, "/some/config/path") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-read-error"))
func init() { Describe("concreteV1Service", func() { var ( fs *fakesys.FakeFileSystem specPath = "/spec.json" service V1Service ) BeforeEach(func() { fs = fakesys.NewFakeFileSystem() service = NewConcreteV1Service(fs, specPath) }) Describe("Get", func() { Context("when filesystem has a spec file", func() { BeforeEach(func() { fs.WriteFileString(specPath, `{"deployment":"fake-deployment-name"}`) }) It("reads spec from filesystem", func() { spec, err := service.Get() Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(V1ApplySpec{Deployment: "fake-deployment-name"})) }) It("returns error if reading spec from filesystem errs", func() { fs.ReadFileError = errors.New("fake-read-error") spec, err := service.Get() Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-read-error")) Expect(spec).To(Equal(V1ApplySpec{})) }) }) Context("when filesystem does not have a spec file", func() { It("reads spec from filesystem", func() { spec, err := service.Get() Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(V1ApplySpec{})) }) }) }) Describe("Set", func() { newSpec := V1ApplySpec{Deployment: "fake-deployment-name"} It("writes spec to filesystem", func() { err := service.Set(newSpec) Expect(err).ToNot(HaveOccurred()) specPathStats := fs.GetFileTestStat(specPath) Expect(specPathStats).ToNot(BeNil()) boshassert.MatchesJSONBytes(GinkgoT(), newSpec, specPathStats.Content) }) It("returns error if writing spec to filesystem errs", func() { fs.WriteToFileError = errors.New("fake-write-error") err := service.Set(newSpec) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) Describe("PopulateDynamicNetworks", func() { Context("when there are no dynamic networks", func() { unresolvedSpec := V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{ "fake-net": NetworkSpec{ Fields: map[string]interface{}{"ip": "fake-net-ip"}, }, }, } It("returns spec without modifying any networks", func() { spec, err := service.PopulateDynamicNetworks(unresolvedSpec, boshsettings.Settings{}) Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{ "fake-net": NetworkSpec{ Fields: map[string]interface{}{"ip": "fake-net-ip"}, }, }, })) }) }) Context("when there are dynamic networks", func() { unresolvedSpec := V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{ "fake-net1": NetworkSpec{ Fields: map[string]interface{}{ "ip": "fake-net1-ip", "netmask": "fake-net1-netmask", "gateway": "fake-net1-gateway", }, }, "fake-net2": NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": "fake-net2-ip", "netmask": "fake-net2-netmask", "gateway": "fake-net2-gateway", }, }, "fake-net3": NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": "fake-net3-ip", "netmask": "fake-net3-netmask", "gateway": "fake-net3-gateway", }, }, }, } Context("when associated network is in settings", func() { settings := boshsettings.Settings{ Networks: boshsettings.Networks{ "fake-net1": boshsettings.Network{ IP: "fake-resolved1-ip", Netmask: "fake-resolved1-netmask", Gateway: "fake-resolved1-gateway", }, "fake-net2": boshsettings.Network{ IP: "fake-resolved2-ip", Netmask: "fake-resolved2-netmask", Gateway: "fake-resolved2-gateway", }, "fake-net3": boshsettings.Network{ IP: "fake-resolved3-ip", Netmask: "fake-resolved3-netmask", Gateway: "fake-resolved3-gateway", }, }, } It("returns spec with modified dynamic networks and keeping everything else the same", func() { spec, err := service.PopulateDynamicNetworks(unresolvedSpec, settings) Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{ "fake-net1": NetworkSpec{ Fields: map[string]interface{}{ // ip info not replaced "ip": "fake-net1-ip", "netmask": "fake-net1-netmask", "gateway": "fake-net1-gateway", }, }, "fake-net2": NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": "fake-resolved2-ip", "netmask": "fake-resolved2-netmask", "gateway": "fake-resolved2-gateway", }, }, "fake-net3": NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": "fake-resolved3-ip", "netmask": "fake-resolved3-netmask", "gateway": "fake-resolved3-gateway", }, }, }, })) }) }) Context("when associated network cannot be found in settings", func() { It("returns error", func() { spec, err := service.PopulateDynamicNetworks(unresolvedSpec, boshsettings.Settings{}) Expect(err).To(Equal(errors.New("Network fake-net2 is not found in settings"))) Expect(spec).To(Equal(V1ApplySpec{})) }) }) }) }) }) }
func init() { Describe("concreteManagerProvider", func() { Describe("NewManager", func() { It("returns manager with tasks.json as its tasks path", func() { logger := boshlog.NewLogger(boshlog.LevelNone) fs := fakesys.NewFakeFileSystem() taskInfo := boshtask.TaskInfo{ TaskID: "fake-task-id", Method: "fake-method", Payload: []byte("fake-payload"), } manager := boshtask.NewManagerProvider().NewManager(logger, fs, "/dir/path") err := manager.AddTaskInfo(taskInfo) Expect(err).ToNot(HaveOccurred()) // Check expected file location with another manager otherManager := boshtask.NewManager(logger, fs, "/dir/path/tasks.json") taskInfos, err := otherManager.GetTaskInfos() Expect(err).ToNot(HaveOccurred()) Expect(taskInfos).To(Equal([]boshtask.TaskInfo{taskInfo})) }) }) }) Describe("concreteManager", func() { var ( logger boshlog.Logger fs *fakesys.FakeFileSystem manager boshtask.Manager ) BeforeEach(func() { logger = boshlog.NewLogger(boshlog.LevelNone) fs = fakesys.NewFakeFileSystem() manager = boshtask.NewManager(logger, fs, "/dir/path") }) Describe("GetTaskInfos", func() { It("can load multiple tasks", func() { err := manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }) Expect(err).ToNot(HaveOccurred()) // Make sure we are not getting cached copy of taskInfos reloadedManager := boshtask.NewManager(logger, fs, "/dir/path") taskInfos, err := reloadedManager.GetTaskInfos() Expect(err).ToNot(HaveOccurred()) Expect(taskInfos).To(Equal([]boshtask.TaskInfo{ boshtask.TaskInfo{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }, boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }, })) }) It("succeeds when there is no tasks (file is not present)", func() { taskInfos, err := manager.GetTaskInfos() Expect(err).ToNot(HaveOccurred()) Expect(len(taskInfos)).To(Equal(0)) }) It("returns an error when failing to load tasks from the file that exists", func() { err := manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }) Expect(err).ToNot(HaveOccurred()) fs.ReadFileError = errors.New("fake-read-error") _, err = manager.GetTaskInfos() Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-read-error")) }) }) Describe("AddTaskInfo", func() { It("can add multiple tasks", func() { err := manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }) Expect(err).ToNot(HaveOccurred()) content, err := fs.ReadFile("/dir/path") Expect(err).ToNot(HaveOccurred()) var decodedMap map[string]boshtask.TaskInfo err = json.Unmarshal(content, &decodedMap) Expect(err).ToNot(HaveOccurred()) Expect(decodedMap).To(Equal(map[string]boshtask.TaskInfo{ "fake-task-id-1": boshtask.TaskInfo{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }, "fake-task-id-2": boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }, })) }) It("returns an error when failing to save task", func() { fs.WriteToFileError = errors.New("fake-write-error") err := manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id", Method: "fake-method", Payload: []byte("fake-payload"), }) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) Describe("RemoveTaskInfo", func() { BeforeEach(func() { err := manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddTaskInfo(boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }) Expect(err).ToNot(HaveOccurred()) }) It("removes the task", func() { err := manager.RemoveTaskInfo("fake-task-id-1") Expect(err).ToNot(HaveOccurred()) content, err := fs.ReadFile("/dir/path") Expect(err).ToNot(HaveOccurred()) var decodedMap map[string]boshtask.TaskInfo err = json.Unmarshal(content, &decodedMap) Expect(err).ToNot(HaveOccurred()) Expect(decodedMap).To(Equal(map[string]boshtask.TaskInfo{ "fake-task-id-2": boshtask.TaskInfo{ TaskID: "fake-task-id-2", Method: "fake-method-2", Payload: []byte("fake-payload-2"), }, })) }) It("does not return error when removing task that does not exist", func() { err := manager.RemoveTaskInfo("fake-unknown-task-id") Expect(err).ToNot(HaveOccurred()) }) It("returns an error when failing to remove task", func() { fs.WriteToFileError = errors.New("fake-write-error") err := manager.RemoveTaskInfo("fake-task-id") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) }) }