Context("when combo manifest path does not exist", func() { BeforeEach(func() { err := fakeFs.RemoveAll(comboManifestPath) Expect(err).ToNot(HaveOccurred()) }) It("returns an error", func() { _, err := parser.Parse(comboManifestPath) Expect(err).To(HaveOccurred()) }) }) Context("when parser fails to read the combo manifest file", func() { BeforeEach(func() { fakeFs.ReadFileError = errors.New("fake-read-file-error") }) It("returns an error", func() { _, err := parser.Parse(comboManifestPath) Expect(err).To(HaveOccurred()) }) }) BeforeEach(func() { contents := ` --- name: fake-deployment-name update: update_watch_time: 2000-7000 resource_pools:
}) It("returns error if file contains invalid json", func() { err := fs.WriteFileString("/config.json", "-") Expect(err).ToNot(HaveOccurred()) _, err = NewConfigFromPath("/config.json", fs) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Unmarshalling config")) }) It("returns error if file cannot be read", func() { err := fs.WriteFileString("/config.json", "{}") Expect(err).ToNot(HaveOccurred()) fs.ReadFileError = errors.New("fake-read-err") _, err = NewConfigFromPath("/config.json", fs) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-read-err")) }) }) var _ = Describe("Config", func() { var ( config Config ) Describe("Validate", func() { BeforeEach(func() { config = validConfig
func rootDesc() { var ( repo ReleaseRepo deploymentStateService DeploymentStateService fs *fakesys.FakeFileSystem fakeUUIDGenerator *fakeuuid.FakeGenerator ) BeforeEach(func() { logger := boshlog.NewLogger(boshlog.LevelNone) fs = fakesys.NewFakeFileSystem() fakeUUIDGenerator = &fakeuuid.FakeGenerator{} fakeUUIDGenerator.GeneratedUUID = "fake-uuid" deploymentStateService = NewFileSystemDeploymentStateService(fs, fakeUUIDGenerator, logger, "/fake/path") deploymentStateService.Load() repo = NewReleaseRepo(deploymentStateService, fakeUUIDGenerator) }) Describe("List", func() { Context("when a current release exists", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "fake-guid-a", Name: "fake-name-a", Version: "fake-version-a"}, ReleaseRecord{ID: "fake-guid-b", Name: "fake-name-b", Version: "fake-version-b"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) It("returns existing release", func() { records, err := repo.List() Expect(err).ToNot(HaveOccurred()) Expect(records).To(Equal([]ReleaseRecord{ { ID: "fake-guid-a", Name: "fake-name-a", Version: "fake-version-a", }, { ID: "fake-guid-b", Name: "fake-name-b", Version: "fake-version-b", }, })) }) }) Context("when there are no releases recorded", func() { It("returns not found", func() { records, err := repo.List() Expect(err).ToNot(HaveOccurred()) Expect(records).To(HaveLen(0)) }) }) Context("when the config service fails to load", func() { BeforeEach(func() { fs.ReadFileError = errors.New("kaboom") }) It("returns an error", func() { _, err := repo.List() Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Loading existing config")) }) }) }) Describe("Update", func() { Context("when there are no existing releases", func() { It("saves the provided releases to the config file", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).ToNot(HaveOccurred()) conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) Expect(conf.Releases).To(ConsistOf( ReleaseRecord{ID: "fake-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "fake-uuid", Name: "name2", Version: "2"}, )) }) }) Context("when the existing releases exactly match the provided releases", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "old-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "old-uuid", Name: "name2", Version: "2"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) }) Context("when existing versions differ from the provided release versions", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "old-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "old-uuid", Name: "name2", Version: "3"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) It("saves the provided releases to the config file", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).ToNot(HaveOccurred()) conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) Expect(conf.Releases).To(ConsistOf( ReleaseRecord{ID: "fake-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "fake-uuid", Name: "name2", Version: "2"}, )) }) }) Context("when existing names differ from the provided release names", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "old-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "old-uuid", Name: "other-name", Version: "2"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) It("saves the provided releases to the config file", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).ToNot(HaveOccurred()) conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) Expect(conf.Releases).To(ConsistOf( ReleaseRecord{ID: "fake-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "fake-uuid", Name: "name2", Version: "2"}, )) }) }) Context("when a release is removed", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "old-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "old-uuid", Name: "name2", Version: "2"}, ReleaseRecord{ID: "old-uuid", Name: "name3", Version: "3"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) It("saves the provided releases to the config file", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).ToNot(HaveOccurred()) conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) Expect(conf.Releases).To(ConsistOf( ReleaseRecord{ID: "fake-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "fake-uuid", Name: "name2", Version: "2"}, )) }) }) Context("when a release is added", func() { BeforeEach(func() { conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) conf.Releases = []ReleaseRecord{ ReleaseRecord{ID: "old-uuid", Name: "name1", Version: "1"}, } err = deploymentStateService.Save(conf) Expect(err).ToNot(HaveOccurred()) }) It("saves the provided releases to the config file", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).ToNot(HaveOccurred()) conf, err := deploymentStateService.Load() Expect(err).ToNot(HaveOccurred()) Expect(conf.Releases).To(ConsistOf( ReleaseRecord{ID: "fake-uuid", Name: "name1", Version: "1"}, ReleaseRecord{ID: "fake-uuid", Name: "name2", Version: "2"}, )) }) }) Context("when the config service fails to save", func() { BeforeEach(func() { fs.WriteFileError = errors.New("kaboom") }) It("returns an error", func() { err := repo.Update([]release.Release{ fakerelease.New("name1", "1"), fakerelease.New("name2", "2"), }) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("kaboom")) }) }) }) }
_, _ = action.Run("fake-blobstore-id", "fake-fingerprint", 2) tag, message, _ := logger.ErrorArgsForCall(0) Expect(tag).To(Equal("Sync DNS action")) Expect(message).To(Equal("Failed to remove dns blob file at path 'fake-blobstore-file-path'")) }) }) }) }) }) Context("when local DNS state could not be read", func() { BeforeEach(func() { err := fakeFileSystem.WriteFileString(stateFilePath, `{"version": 2}`) Expect(err).ToNot(HaveOccurred()) fakeFileSystem.ReadFileError = errors.New("fake-read-error") fakeFileSystem.RegisterReadFileError(stateFilePath, fakeFileSystem.ReadFileError) }) It("returns error", func() { _, err := action.Run("fake-blobstore-id", "fake-fingerprint", 2) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("reading local DNS state")) }) }) Context("when there is no local DNS state", func() { It("runs successfully and creates a new state file", func() { Expect(fakeFileSystem.FileExists(stateFilePath)).To(BeFalse()) response, err := action.Run("fake-blobstore-id", "fake-fingerprint", 1)
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.Info{ TaskID: "fake-task-id", Method: "fake-method", Payload: []byte("fake-payload"), } manager := boshtask.NewManagerProvider().NewManager(logger, fs, "/dir/path") err := manager.AddInfo(taskInfo) Expect(err).ToNot(HaveOccurred()) // Check expected file location with another manager otherManager := boshtask.NewManager(logger, fs, "/dir/path/tasks.json") taskInfos, err := otherManager.GetInfos() Expect(err).ToNot(HaveOccurred()) Expect(taskInfos).To(Equal([]boshtask.Info{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("GetInfos", func() { It("can load multiple tasks", func() { err := manager.AddInfo(boshtask.Info{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddInfo(boshtask.Info{ 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.GetInfos() Expect(err).ToNot(HaveOccurred()) Expect(taskInfos).To(ConsistOf([]boshtask.Info{ boshtask.Info{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }, boshtask.Info{ 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.GetInfos() 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.AddInfo(boshtask.Info{ 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.GetInfos() Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-read-error")) }) }) Describe("AddInfo", func() { It("can add multiple tasks", func() { err := manager.AddInfo(boshtask.Info{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddInfo(boshtask.Info{ 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.Info err = json.Unmarshal(content, &decodedMap) Expect(err).ToNot(HaveOccurred()) Expect(decodedMap).To(Equal(map[string]boshtask.Info{ "fake-task-id-1": boshtask.Info{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }, "fake-task-id-2": boshtask.Info{ 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.WriteFileError = errors.New("fake-write-error") err := manager.AddInfo(boshtask.Info{ TaskID: "fake-task-id", Method: "fake-method", Payload: []byte("fake-payload"), }) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) Describe("RemoveInfo", func() { BeforeEach(func() { err := manager.AddInfo(boshtask.Info{ TaskID: "fake-task-id-1", Method: "fake-method-1", Payload: []byte("fake-payload-1"), }) Expect(err).ToNot(HaveOccurred()) err = manager.AddInfo(boshtask.Info{ 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.RemoveInfo("fake-task-id-1") Expect(err).ToNot(HaveOccurred()) content, err := fs.ReadFile("/dir/path") Expect(err).ToNot(HaveOccurred()) var decodedMap map[string]boshtask.Info err = json.Unmarshal(content, &decodedMap) Expect(err).ToNot(HaveOccurred()) Expect(decodedMap).To(Equal(map[string]boshtask.Info{ "fake-task-id-2": boshtask.Info{ 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.RemoveInfo("fake-unknown-task-id") Expect(err).ToNot(HaveOccurred()) }) It("returns an error when failing to remove task", func() { fs.WriteFileError = errors.New("fake-write-error") err := manager.RemoveInfo("fake-task-id") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-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.WriteFileError = errors.New("fake-write-error") err := service.Set(newSpec) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-write-error")) }) }) Describe("PopulateDHCPNetworks", func() { var settings boshsettings.Settings var unresolvedSpec V1ApplySpec var staticSpec NetworkSpec var dhcpSpec NetworkSpec var manualSetting boshsettings.Network var dynamicSetting boshsettings.Network BeforeEach(func() { settings = boshsettings.Settings{ Networks: boshsettings.Networks{}, } manualSetting = boshsettings.Network{ Type: "manual", IP: "fake-manual-ip", Netmask: "fake-manual-netmask", Gateway: "fake-manual-gateway", Mac: "fake-manual-mac", } dynamicSetting = boshsettings.Network{ Type: "dynamic", IP: "fake-dynamic-ip", Netmask: "fake-dynamic-netmask", Gateway: "fake-dynamic-gateway", } unresolvedSpec = V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{}, } staticSpec = NetworkSpec{ Fields: map[string]interface{}{ "ip": "fake-net1-ip", "netmask": "fake-net1-netmask", "gateway": "fake-net1-gateway", "mac": "fake-net1-mac", }, } dhcpSpec = NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": "fake-net2-ip", "netmask": "fake-net2-netmask", "gateway": "fake-net2-gateway", }, } }) Context("when associated network is in settings", func() { Context("when there are no networks configured with DHCP", func() { BeforeEach(func() { settings.Networks["fake-net"] = manualSetting unresolvedSpec.NetworkSpecs["fake-net"] = staticSpec }) It("returns spec without modifying any networks", func() { spec, err := service.PopulateDHCPNetworks(unresolvedSpec, settings) Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(unresolvedSpec)) }) }) Context("when there is network with name 'local' and ip 127.0.0.1", func() { BeforeEach(func() { unresolvedSpec.NetworkSpecs["local"] = NetworkSpec{ Fields: map[string]interface{}{"ip": "127.0.0.1"}, } }) It("returns spec without modifying any networks", func() { spec, err := service.PopulateDHCPNetworks(unresolvedSpec, settings) Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(unresolvedSpec)) }) }) Context("when there are networks configured with DHCP", func() { BeforeEach(func() { settings.Networks["static-net1"] = manualSetting settings.Networks["dhcp-net2"] = dynamicSetting unresolvedSpec.NetworkSpecs["static-net1"] = staticSpec unresolvedSpec.NetworkSpecs["dhcp-net2"] = dhcpSpec }) It("returns spec with networks modified via DHCP and keeps everything else the same", func() { spec, err := service.PopulateDHCPNetworks(unresolvedSpec, settings) Expect(err).ToNot(HaveOccurred()) Expect(spec).To(Equal(V1ApplySpec{ Deployment: "fake-deployment", NetworkSpecs: map[string]NetworkSpec{ "static-net1": staticSpec, "dhcp-net2": NetworkSpec{ Fields: map[string]interface{}{ "type": NetworkSpecTypeDynamic, "ip": dynamicSetting.IP, "netmask": dynamicSetting.Netmask, "gateway": dynamicSetting.Gateway, }, }, }, })) }) }) }) Context("when associated network cannot be found in settings", func() { BeforeEach(func() { settings.Networks["net-present-in-settings"] = manualSetting unresolvedSpec.NetworkSpecs["net-present-in-settings"] = staticSpec unresolvedSpec.NetworkSpecs["net-not-present-in-settings"] = dhcpSpec }) It("returns error", func() { spec, err := service.PopulateDHCPNetworks(unresolvedSpec, settings) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Network 'net-not-present-in-settings' is not found in settings")) Expect(spec).To(Equal(V1ApplySpec{})) }) }) }) }) }
Context("when saving to index fails", func() { It("returns error", func() { fakeFS.WriteFileError = errors.New("Could not save") record := CompiledPackageRecord{ BlobID: "fake-blob-id", BlobSHA1: "fake-sha1", } pkg := birelpkg.Package{ Name: "fake-package-name", } err := compiledPackageRepo.Save(pkg, record) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Saving compiled package")) }) }) Context("when reading from index fails", func() { It("returns error", func() { err := compiledPackageRepo.Save(pkg, record) fakeFS.ReadFileError = errors.New("fake-error") _, _, err = compiledPackageRepo.Find(pkg) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Finding compiled package")) }) }) }) })