func TestGetStateRunWithFullFormatOption(t *testing.T) { settings := &fakesettings.FakeSettingsService{} settings.AgentId = "my-agent-id" settings.Vm.Name = "vm-abc-def" specService, jobSupervisor, fakeVitals, action := buildGetStateAction(settings) jobSupervisor.StatusStatus = "running" specService.Spec = boshas.V1ApplySpec{ Deployment: "fake-deployment", } expectedVitals := boshvitals.Vitals{ Load: []string{"foo", "bar", "baz"}, } fakeVitals.GetVitals = expectedVitals expectedVm := map[string]interface{}{"name": "vm-abc-def"} state, err := action.Run("full") assert.NoError(t, err) boshassert.MatchesJsonString(t, state.AgentId, `"my-agent-id"`) boshassert.MatchesJsonString(t, state.JobState, `"running"`) boshassert.MatchesJsonString(t, state.Deployment, `"fake-deployment"`) assert.Equal(t, *state.Vitals, expectedVitals) boshassert.MatchesJsonMap(t, state.Vm, expectedVm) }
func init() { Describe("Testing with Ginkgo", func() { It("mount disk should be asynchronous", func() { settings := &fakesettings.FakeSettingsService{} _, action := buildMountDiskAction(settings) Expect(action.IsAsynchronous()).To(BeTrue()) }) It("is not persistent", func() { settings := &fakesettings.FakeSettingsService{} _, action := buildMountDiskAction(settings) Expect(action.IsPersistent()).To(BeFalse()) }) It("mount disk", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) result, err := mountDisk.Run("vol-123") Expect(err).NotTo(HaveOccurred()) boshassert.MatchesJsonString(GinkgoT(), result, "{}") Expect(settings.SettingsWereLoaded).To(BeTrue()) Expect(platform.MountPersistentDiskDevicePath).To(Equal("/dev/sdf")) Expect(platform.MountPersistentDiskMountPoint).To(Equal("/foo/store")) }) It("mount disk when store already mounted", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) platform.IsMountPointResult = true result, err := mountDisk.Run("vol-123") Expect(err).NotTo(HaveOccurred()) boshassert.MatchesJsonString(GinkgoT(), result, "{}") Expect(platform.IsMountPointPath).To(Equal("/foo/store")) Expect(platform.MountPersistentDiskDevicePath).To(Equal("/dev/sdf")) Expect(platform.MountPersistentDiskMountPoint).To(Equal("/foo/store_migration_target")) }) It("mount disk when device path not found", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} _, mountDisk := buildMountDiskAction(settings) _, err := mountDisk.Run("vol-456") Expect(err).To(HaveOccurred()) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("json with value", func() { resp := NewValueResponse("some value") boshassert.MatchesJsonString(GinkgoT(), resp, `{"value":"some value"}`) }) It("json with exception", func() { resp := NewExceptionResponse("oops!") boshassert.MatchesJsonString(GinkgoT(), resp, `{"exception":{"message":"oops!"}}`) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("mount disk should be asynchronous", func() { settings := &fakesettings.FakeSettingsService{} _, action := buildMountDiskAction(settings) assert.True(GinkgoT(), action.IsAsynchronous()) }) It("mount disk", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) result, err := mountDisk.Run("vol-123") assert.NoError(GinkgoT(), err) boshassert.MatchesJsonString(GinkgoT(), result, "{}") assert.True(GinkgoT(), settings.SettingsWereRefreshed) assert.Equal(GinkgoT(), platform.MountPersistentDiskDevicePath, "/dev/sdf") assert.Equal(GinkgoT(), platform.MountPersistentDiskMountPoint, "/foo/store") }) It("mount disk when store already mounted", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) platform.IsMountPointResult = true result, err := mountDisk.Run("vol-123") assert.NoError(GinkgoT(), err) boshassert.MatchesJsonString(GinkgoT(), result, "{}") assert.Equal(GinkgoT(), platform.IsMountPointPath, "/foo/store") assert.Equal(GinkgoT(), platform.MountPersistentDiskDevicePath, "/dev/sdf") assert.Equal(GinkgoT(), platform.MountPersistentDiskMountPoint, "/foo/store_migration_target") }) It("mount disk when device path not found", func() { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} _, mountDisk := buildMountDiskAction(settings) _, err := mountDisk.Run("vol-456") assert.Error(GinkgoT(), err) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("unmount disk should be asynchronous", func() { platform := fakeplatform.NewFakePlatform() action := buildUnmountDiskAction(platform) Expect(action.IsAsynchronous()).To(BeTrue()) }) It("is not persistent", func() { platform := fakeplatform.NewFakePlatform() action := buildUnmountDiskAction(platform) Expect(action.IsPersistent()).To(BeFalse()) }) It("unmount disk when the disk is mounted", func() { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = true unmountDisk := buildUnmountDiskAction(platform) result, err := unmountDisk.Run("vol-123") Expect(err).ToNot(HaveOccurred()) boshassert.MatchesJsonString(GinkgoT(), result, `{"message":"Unmounted partition of /dev/sdf"}`) Expect(platform.UnmountPersistentDiskDevicePath).To(Equal("/dev/sdf")) }) It("unmount disk when the disk is not mounted", func() { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = false mountDisk := buildUnmountDiskAction(platform) result, err := mountDisk.Run("vol-123") Expect(err).ToNot(HaveOccurred()) boshassert.MatchesJsonString(GinkgoT(), result, `{"message":"Partition of /dev/sdf is not mounted"}`) Expect(platform.UnmountPersistentDiskDevicePath).To(Equal("/dev/sdf")) }) It("unmount disk when device path not found", func() { platform := fakeplatform.NewFakePlatform() mountDisk := buildUnmountDiskAction(platform) _, err := mountDisk.Run("vol-456") Expect(err).To(HaveOccurred()) }) }) }
func assertRequestIsProcessedSynchronously(t *testing.T, req boshmbus.Request) { settings, handler, platform, taskService, actionFactory := getAgentDependencies() // when action is successful actionFactory.CreateAction = &fakeaction.TestAction{ RunValue: "some value", } agent := New(settings, handler, platform, taskService, actionFactory) err := agent.Run() assert.NoError(t, err) assert.True(t, handler.ReceivedRun) resp := handler.Func(req) assert.Equal(t, req.Method, actionFactory.CreateMethod) assert.Equal(t, req.GetPayload(), actionFactory.CreateAction.RunPayload) assert.Equal(t, boshmbus.NewValueResponse("some value"), resp) // when action returns an error actionFactory.CreateAction = &fakeaction.TestAction{ RunErr: errors.New("some error"), } agent = New(settings, handler, platform, taskService, actionFactory) agent.Run() resp = handler.Func(req) boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"some error"}}`) }
func init() { Describe("Testing with Ginkgo", func() { It("list disk should be synchronous", func() { settings := &fakesettings.FakeSettingsService{} platform := fakeplatform.NewFakePlatform() action := NewListDisk(settings, platform) assert.False(GinkgoT(), action.IsAsynchronous()) }) It("list disk run", func() { settings := &fakesettings.FakeSettingsService{ Disks: boshsettings.Disks{ Persistent: map[string]string{ "volume-1": "/dev/sda", "volume-2": "/dev/sdb", "volume-3": "/dev/sdc", }, }, } platform := fakeplatform.NewFakePlatform() platform.MountedDevicePaths = []string{"/dev/sdb", "/dev/sdc"} action := NewListDisk(settings, platform) value, err := action.Run() assert.NoError(GinkgoT(), err) boshassert.MatchesJsonString(GinkgoT(), value, `["volume-2","volume-3"]`) }) }) }
func testLogs(t assert.TestingT, logType string, filters []string, expectedFilters []string) { deps, action := buildLogsAction() deps.copier.FilteredCopyToTempTempDir = "/fake-temp-dir" deps.compressor.CompressFilesInDirTarballPath = "logs_test.go" deps.blobstore.CreateBlobId = "my-blob-id" logs, err := action.Run(logType, filters) assert.NoError(t, err) var expectedPath string switch logType { case "job": expectedPath = filepath.Join("/fake", "dir", "sys", "log") case "agent": expectedPath = filepath.Join("/fake", "dir", "bosh", "log") } assert.Equal(t, expectedPath, deps.copier.FilteredCopyToTempDir) assert.Equal(t, expectedFilters, deps.copier.FilteredCopyToTempFilters) assert.Equal(t, deps.copier.FilteredCopyToTempTempDir, deps.compressor.CompressFilesInDirDir) assert.Equal(t, deps.copier.CleanUpTempDir, deps.compressor.CompressFilesInDirDir) assert.Equal(t, deps.compressor.CompressFilesInDirTarballPath, deps.blobstore.CreateFileName) boshassert.MatchesJsonString(t, logs, `{"blobstore_id":"my-blob-id"}`) }
func init() { Describe("Testing with Ginkgo", func() { It("migrate disk should be asynchronous", func() { _, action := buildMigrateDiskAction() Expect(action.IsAsynchronous()).To(BeTrue()) }) It("is not persistent", func() { _, action := buildMigrateDiskAction() Expect(action.IsPersistent()).To(BeFalse()) }) It("migrate disk action run", func() { platform, action := buildMigrateDiskAction() value, err := action.Run() Expect(err).ToNot(HaveOccurred()) boshassert.MatchesJsonString(GinkgoT(), value, "{}") Expect(platform.MigratePersistentDiskFromMountPoint).To(Equal("/foo/store")) Expect(platform.MigratePersistentDiskToMountPoint).To(Equal("/foo/store_migration_target")) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("get", func() { fs, specPath, service := buildV1Service() fs.WriteToFile(specPath, `{"deployment":"fake-deployment-name"}`) spec, err := service.Get() assert.NoError(GinkgoT(), err) expectedSpec := V1ApplySpec{ Deployment: "fake-deployment-name", } assert.Equal(GinkgoT(), expectedSpec, spec) }) It("set", func() { fs, specPath, service := buildV1Service() spec := V1ApplySpec{ JobSpec: JobSpec{ Name: "fake-job", }, } err := service.Set(spec) assert.NoError(GinkgoT(), err) specPathStats := fs.GetFileTestStat(specPath) assert.NotNil(GinkgoT(), specPathStats) boshassert.MatchesJsonString(GinkgoT(), spec, specPathStats.Content) }) }) }
func TestMigrateDiskActionRun(t *testing.T) { platform, action := buildMigrateDiskAction() value, err := action.Run() assert.NoError(t, err) boshassert.MatchesJsonString(t, value, "{}") assert.Equal(t, platform.MigratePersistentDiskFromMountPoint, "/foo/store") assert.Equal(t, platform.MigratePersistentDiskToMountPoint, "/foo/store_migration_target") }
func init() { Describe("Testing with Ginkgo", func() { It("unmount disk should be asynchronous", func() { platform := fakeplatform.NewFakePlatform() action := buildUnmountDiskAction(platform) assert.True(GinkgoT(), action.IsAsynchronous()) }) It("unmount disk when the disk is mounted", func() { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = true unmountDisk := buildUnmountDiskAction(platform) result, err := unmountDisk.Run("vol-123") assert.NoError(GinkgoT(), err) boshassert.MatchesJsonString(GinkgoT(), result, `{"message":"Unmounted partition of /dev/sdf"}`) assert.Equal(GinkgoT(), platform.UnmountPersistentDiskDevicePath, "/dev/sdf") }) It("unmount disk when the disk is not mounted", func() { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = false mountDisk := buildUnmountDiskAction(platform) result, err := mountDisk.Run("vol-123") assert.NoError(GinkgoT(), err) boshassert.MatchesJsonString(GinkgoT(), result, `{"message":"Partition of /dev/sdf is not mounted"}`) assert.Equal(GinkgoT(), platform.UnmountPersistentDiskDevicePath, "/dev/sdf") }) It("unmount disk when device path not found", func() { platform := fakeplatform.NewFakePlatform() mountDisk := buildUnmountDiskAction(platform) _, err := mountDisk.Run("vol-456") assert.Error(GinkgoT(), err) }) }) }
func TestUnmountDiskWhenTheDiskIsNotMounted(t *testing.T) { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = false mountDisk := buildUnmountDiskAction(platform) result, err := mountDisk.Run("vol-123") assert.NoError(t, err) boshassert.MatchesJsonString(t, result, `{"message":"Partition of /dev/sdf is not mounted"}`) assert.Equal(t, platform.UnmountPersistentDiskDevicePath, "/dev/sdf") }
func TestUnmountDiskWhenTheDiskIsMounted(t *testing.T) { platform := fakeplatform.NewFakePlatform() platform.UnmountPersistentDiskDidUnmount = true unmountDisk := buildUnmountDiskAction(platform) payload := `{"arguments":["vol-123"]}` result, err := unmountDisk.Run([]byte(payload)) assert.NoError(t, err) boshassert.MatchesJsonString(t, result, `{"message":"Unmounted partition of /dev/sdf"}`) assert.Equal(t, platform.UnmountPersistentDiskDevicePath, "/dev/sdf") }
func TestDispatchRespondsWithExceptionWhenTheMethodIsUnknown(t *testing.T) { logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies() req := boshhandler.NewRequest("reply to me", "gibberish", []byte{}) actionFactory.CreateErr = true dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner) resp := dispatcher.Dispatch(req) boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"unknown message gibberish"}}`) assert.Equal(t, actionFactory.CreateMethod, "gibberish") }
func TestRunRespondsWithExceptionWhenTheMethodIsUnknown(t *testing.T) { req := boshmbus.NewRequest("reply to me", "gibberish", []byte{}) settings, handler, platform, taskService, actionFactory := getAgentDependencies() agent := New(settings, handler, platform, taskService, actionFactory) err := agent.Run() assert.NoError(t, err) assert.True(t, handler.ReceivedRun) resp := handler.Func(req) boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"unknown message gibberish"}}`) }
func TestGetTaskRunReturnsARunningTask(t *testing.T) { taskService, action := buildGetTaskAction() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateRunning, }, } taskValue, err := action.Run("57") assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `{"agent_task_id":"found-57-id","state":"running"}`) }
func TestApplyReturnsApplied(t *testing.T) { _, _, action := buildApplyAction() applySpec := boshas.V1ApplySpec{ JobSpec: boshas.JobSpec{ Name: "router", }, } value, err := action.Run(applySpec) assert.NoError(t, err) boshassert.MatchesJsonString(t, value, `"applied"`) }
func TestMountDisk(t *testing.T) { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) result, err := mountDisk.Run("vol-123") assert.NoError(t, err) boshassert.MatchesJsonString(t, result, "{}") assert.True(t, settings.SettingsWereRefreshed) assert.Equal(t, platform.MountPersistentDiskDevicePath, "/dev/sdf") assert.Equal(t, platform.MountPersistentDiskMountPoint, "/foo/store") }
func TestGetTaskRunReturnsASuccessfulTask(t *testing.T) { taskService, action := buildGetTaskAction() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateDone, Value: "some-task-value", }, } taskValue, err := action.Run("57") assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `"some-task-value"`) }
func TestDispatchHandlesSynchronousActionWhenErr(t *testing.T) { logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies() // when action returns an error actionFactory.CreateAction = &fakeaction.TestAction{} actionRunner.RunErr = errors.New("some error") dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner) req := boshhandler.NewRequest("reply to me!", "some action", []byte("some payload")) resp := dispatcher.Dispatch(req) expectedJson := fmt.Sprintf("{\"exception\":{\"message\":\"Action Failed %s: some error\"}}", req.Method) boshassert.MatchesJsonString(t, resp, expectedJson) assert.Equal(t, actionFactory.CreateMethod, "some action") }
func TestDrainReturnsIntegerValueOfDrainscriptStdoutAfterTrimmingWhitespace(t *testing.T) { cmdRunner, fs, _, specService, action := buildDrain() currentSpec := boshas.V1ApplySpec{} currentSpec.JobSpec.Template = "foo" specService.Spec = currentSpec drainScriptPath := filepath.Join("/fake-dir/jobs", currentSpec.JobSpec.Template, "bin", "drain") fs.WriteToFile(drainScriptPath, "") cmdRunner.AddCmdResult(drainScriptPath+" job_check_status hash_unchanged", fakesys.FakeCmdResult{Stdout: "-56\n"}) value, err := action.Run(drainTypeStatus) assert.NoError(t, err) boshassert.MatchesJsonString(t, value, "-56") }
func TestSet(t *testing.T) { fs, specPath, service := buildV1Service() spec := V1ApplySpec{ JobSpec: JobSpec{ Name: "fake-job", }, } err := service.Set(spec) assert.NoError(t, err) specPathStats := fs.GetFileTestStat(specPath) assert.NotNil(t, specPathStats) boshassert.MatchesJsonString(t, spec, specPathStats.Content) }
func TestMountDiskWhenStoreAlreadyMounted(t *testing.T) { settings := &fakesettings.FakeSettingsService{} settings.Disks.Persistent = map[string]string{"vol-123": "/dev/sdf"} platform, mountDisk := buildMountDiskAction(settings) platform.IsMountPointResult = true result, err := mountDisk.Run("vol-123") assert.NoError(t, err) boshassert.MatchesJsonString(t, result, "{}") assert.Equal(t, platform.IsMountPointPath, "/foo/store") assert.Equal(t, platform.MountPersistentDiskDevicePath, "/dev/sdf") assert.Equal(t, platform.MountPersistentDiskMountPoint, "/foo/store_migration_target") }
func TestGetTaskRunReturnsAFailedTask(t *testing.T) { taskService, action := buildGetTaskAction() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateFailed, Error: errors.New("Oops we failed..."), }, } taskValue, err := action.Run("57") assert.Error(t, err) assert.Equal(t, "Oops we failed...", err.Error()) boshassert.MatchesJsonString(t, taskValue, `null`) }
func TestExternalValidateWritesConfigFile(t *testing.T) { fs, runner, uuidGen, configPath := getExternalBlobstoreDependencies() options := map[string]string{"fake-key": "fake-value"} blobstore := newExternalBlobstore("fake-provider", options, fs, runner, uuidGen, configPath) runner.CommandExistsValue = true assert.NoError(t, blobstore.Validate()) s3CliConfig, err := fs.ReadFile(configPath) assert.NoError(t, err) expectedJson := map[string]string{"fake-key": "fake-value"} boshassert.MatchesJsonString(t, expectedJson, s3CliConfig) }
func TestGetTaskRunReturnsAFailedTask(t *testing.T) { taskService, action := buildGetTaskAction() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateFailed, Error: "Oops we failed...", }, } taskValue, err := action.Run([]byte(`{"arguments":["57"]}`)) assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `{"agent_task_id":"found-57-id","state":"failed","exception":"Oops we failed..."}`) }
func TestGetTaskRunReturnsASuccessfulTask(t *testing.T) { taskService, action := buildGetTaskAction() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateDone, Value: "some-task-value", }, } taskValue, err := action.Run([]byte(`{"arguments":["57"]}`)) assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `{"agent_task_id":"found-57-id","state":"done","value":"some-task-value"}`) }
func TestGetTaskRunReturns(t *testing.T) { settings, fs, platform, taskService := getFakeFactoryDependencies() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateFailed, }, } factory := NewFactory(settings, fs, platform, taskService) getTask := factory.Create("get_task") taskValue, err := getTask.Run([]byte(`{"arguments":["57"]}`)) assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `{"agent_task_id":"found-57-id","state":"failed"}`) }
func TestGetTaskRunReturnsASuccessfulTask(t *testing.T) { settings, platform, blobstore, taskService := getFakeFactoryDependencies() taskService.Tasks = map[string]boshtask.Task{ "57": boshtask.Task{ Id: "found-57-id", State: boshtask.TaskStateDone, Value: "some-task-value", }, } factory := NewFactory(settings, platform, blobstore, taskService) getTask := factory.Create("get_task") taskValue, err := getTask.Run([]byte(`{"arguments":["57"]}`)) assert.NoError(t, err) boshassert.MatchesJsonString(t, taskValue, `{"agent_task_id":"found-57-id","state":"done","value":"some-task-value"}`) }