Exemple #1
0
func testSshSetupWithGivenPassword(t assert.TestingT, expectedPwd string) {
	settings := &fakesettings.FakeSettingsService{}
	settings.DefaultIP = "ww.xx.yy.zz"

	platform, action := buildSshAction(settings)

	expectedUser := "some-user"
	expectedKey := "some public key content"

	params := SshParams{
		User:      expectedUser,
		PublicKey: expectedKey,
		Password:  expectedPwd,
	}

	response, err := action.Run("setup", params)
	assert.NoError(t, err)

	assert.Equal(t, expectedUser, platform.CreateUserUsername)
	assert.Equal(t, expectedPwd, platform.CreateUserPassword)
	assert.Equal(t, "/foo/bosh_ssh", platform.CreateUserBasePath)
	assert.Equal(t, []string{boshsettings.VCAPUsername, boshsettings.AdminGroup}, platform.AddUserToGroupsGroups[expectedUser])
	assert.Equal(t, expectedKey, platform.SetupSshPublicKeys[expectedUser])

	expectedJSON := map[string]interface{}{
		"command": "setup",
		"status":  "success",
		"ip":      "ww.xx.yy.zz",
	}

	boshassert.MatchesJSONMap(t, response, expectedJSON)
}
Exemple #2
0
func init() {
	Describe("Testing with Ginkgo", func() {
		It("ssh should be synchronous", func() {
			settings := &fakesettings.FakeSettingsService{}
			_, action := buildSshAction(settings)
			Expect(action.IsAsynchronous()).To(BeFalse())
		})

		It("is not persistent", func() {
			settings := &fakesettings.FakeSettingsService{}
			_, action := buildSshAction(settings)
			Expect(action.IsPersistent()).To(BeFalse())
		})

		It("ssh setup without default ip", func() {

			settings := &fakesettings.FakeSettingsService{}
			_, action := buildSshAction(settings)

			params := SshParams{
				User:      "some-user",
				Password:  "some-pwd",
				PublicKey: "some-key",
			}
			_, err := action.Run("setup", params)
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("No default ip"))
		})
		It("ssh setup with username and password", func() {

			testSshSetupWithGivenPassword(GinkgoT(), "some-password")
		})
		It("ssh setup without password", func() {

			testSshSetupWithGivenPassword(GinkgoT(), "")
		})
		It("ssh run cleanup deletes ephemeral user", func() {

			settings := &fakesettings.FakeSettingsService{}
			platform, action := buildSshAction(settings)

			params := SshParams{UserRegex: "^foobar.*"}
			response, err := action.Run("cleanup", params)
			Expect(err).ToNot(HaveOccurred())
			Expect("^foobar.*").To(Equal(platform.DeleteEphemeralUsersMatchingRegex))

			boshassert.MatchesJSONMap(GinkgoT(), response, map[string]interface{}{
				"command": "cleanup",
				"status":  "success",
			})
		})
	})
}
Exemple #3
0
func init() {
	Describe("GetState", func() {
		It("get state should be synchronous", func() {
			settings := &fakesettings.FakeSettingsService{}
			_, _, _, action := buildGetStateAction(settings)
			Expect(action.IsAsynchronous()).To(BeFalse())
		})

		It("is not persistent", func() {
			settings := &fakesettings.FakeSettingsService{}
			_, _, _, action := buildGetStateAction(settings)
			Expect(action.IsPersistent()).To(BeFalse())
		})

		Describe("Run", func() {
			It("returns state", func() {
				settings := &fakesettings.FakeSettingsService{}
				settings.AgentID = "my-agent-id"
				settings.VM.Name = "vm-abc-def"

				specService, jobSupervisor, _, action := buildGetStateAction(settings)
				jobSupervisor.StatusStatus = "running"

				specService.Spec = boshas.V1ApplySpec{
					Deployment: "fake-deployment",
				}

				expectedSpec := GetStateV1ApplySpec{
					AgentID:      "my-agent-id",
					JobState:     "running",
					BoshProtocol: "1",
					VM:           boshsettings.VM{Name: "vm-abc-def"},
					Ntp: boshntp.NTPInfo{
						Offset:    "0.34958",
						Timestamp: "12 Oct 17:37:58",
					},
				}
				expectedSpec.Deployment = "fake-deployment"

				state, err := action.Run()
				Expect(err).ToNot(HaveOccurred())

				Expect(state.AgentID).To(Equal(expectedSpec.AgentID))
				Expect(state.JobState).To(Equal(expectedSpec.JobState))
				Expect(state.Deployment).To(Equal(expectedSpec.Deployment))
				boshassert.LacksJSONKey(GinkgoT(), state, "vitals")

				Expect(state).To(Equal(expectedSpec))
			})

			It("returns state in full format", func() {
				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")
				Expect(err).ToNot(HaveOccurred())

				boshassert.MatchesJSONString(GinkgoT(), state.AgentID, `"my-agent-id"`)
				boshassert.MatchesJSONString(GinkgoT(), state.JobState, `"running"`)
				boshassert.MatchesJSONString(GinkgoT(), state.Deployment, `"fake-deployment"`)
				Expect(*state.Vitals).To(Equal(expectedVitals))
				boshassert.MatchesJSONMap(GinkgoT(), state.VM, expectedVM)
			})

			Context("when current cannot be retrieved", func() {

				It("without current spec", func() {
					settings := &fakesettings.FakeSettingsService{}
					specService, _, _, action := buildGetStateAction(settings)

					specService.GetErr = errors.New("fake-spec-get-error")

					_, err := action.Run()
					Expect(err).To(HaveOccurred())
					Expect(err.Error()).To(ContainSubstring("fake-spec-get-error"))
				})
			})

			Context("when vitals cannot be retrieved", func() {
				It("returns error", func() {
					settings := &fakesettings.FakeSettingsService{}
					_, _, fakeVitals, action := buildGetStateAction(settings)

					fakeVitals.GetErr = errors.New("fake-vitals-get-error")

					_, err := action.Run("full")
					Expect(err).To(HaveOccurred())
					Expect(err.Error()).To(ContainSubstring("fake-vitals-get-error"))
				})
			})

		})

	})
}
func init() {
	Describe("Testing with Ginkgo", func() {
		It("compile package should be asynchronous", func() {
			_, action := buildCompilePackageAction()
			Expect(action.IsAsynchronous()).To(BeTrue())
		})

		It("is not persistent", func() {
			_, action := buildCompilePackageAction()
			Expect(action.IsPersistent()).To(BeFalse())
		})

		It("compile package compiles the package abd returns blob id", func() {

			compiler, action := buildCompilePackageAction()
			compiler.CompileBlobID = "my-blob-id"
			compiler.CompileSha1 = "some sha1"

			blobID, sha1, name, version, deps := getCompileActionArguments()

			expectedPkg := boshcomp.Package{
				BlobstoreID: blobID,
				Sha1:        sha1,
				Name:        name,
				Version:     version,
			}
			expectedJSON := map[string]interface{}{
				"result": map[string]string{
					"blobstore_id": "my-blob-id",
					"sha1":         "some sha1",
				},
			}
			expectedDeps := []boshmodels.Package{
				{
					Name:    "first_dep",
					Version: "first_dep_version",
					Source: boshmodels.Source{
						Sha1:        "first_dep_sha1",
						BlobstoreID: "first_dep_blobstore_id",
					},
				},
				{
					Name:    "sec_dep",
					Version: "sec_dep_version",
					Source: boshmodels.Source{
						Sha1:        "sec_dep_sha1",
						BlobstoreID: "sec_dep_blobstore_id",
					},
				},
			}

			val, err := action.Run(blobID, sha1, name, version, deps)

			Expect(err).ToNot(HaveOccurred())
			Expect(expectedPkg).To(Equal(compiler.CompilePkg))

			Expect(expectedDeps).To(Equal(compiler.CompileDeps))

			boshassert.MatchesJSONMap(GinkgoT(), val, expectedJSON)
		})
		It("compile package errs when compile fails", func() {

			compiler, action := buildCompilePackageAction()
			compiler.CompileErr = errors.New("fake-compile-error")

			blobID, sha1, name, version, deps := getCompileActionArguments()

			_, err := action.Run(blobID, sha1, name, version, deps)
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("fake-compile-error"))
		})
	})
}
Exemple #5
0
					}

					expectedVitals := boshvitals.Vitals{
						Load: []string{"foo", "bar", "baz"},
					}
					vitalsService.GetVitals = expectedVitals
					expectedVM := map[string]interface{}{"name": "vm-abc-def"}

					state, err := action.Run("full")
					Expect(err).ToNot(HaveOccurred())

					boshassert.MatchesJSONString(GinkgoT(), state.AgentID, `"my-agent-id"`)
					boshassert.MatchesJSONString(GinkgoT(), state.JobState, `"running"`)
					boshassert.MatchesJSONString(GinkgoT(), state.Deployment, `"fake-deployment"`)
					Expect(*state.Vitals).To(Equal(expectedVitals))
					boshassert.MatchesJSONMap(GinkgoT(), state.VM, expectedVM)
				})

				Describe("non-populated field formatting", func() {
					It("returns network as empty hash if not set", func() {
						specService.Spec = boshas.V1ApplySpec{NetworkSpecs: nil}
						state, err := action.Run("full")
						Expect(err).ToNot(HaveOccurred())
						boshassert.MatchesJSONString(GinkgoT(), state.NetworkSpecs, `{}`)

						// Non-empty NetworkSpecs
						specService.Spec = boshas.V1ApplySpec{
							NetworkSpecs: map[string]boshas.NetworkSpec{
								"fake-net-name": boshas.NetworkSpec{
									Fields: map[string]interface{}{"ip": "fake-ip"},
								},
				Password:  "some-pwd",
				PublicKey: "some-key",
			}

			_, err := action.Run("setup", params)
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("No default ip"))
		})

		It("ssh setup with username and password", func() {
			testSSHSetupWithGivenPassword("some-password")
		})

		It("ssh setup without password", func() {
			testSSHSetupWithGivenPassword("")
		})

		It("ssh run cleanup deletes ephemeral user", func() {
			response, err := action.Run("cleanup", SSHParams{UserRegex: "^foobar.*"})
			Expect(err).ToNot(HaveOccurred())
			Expect(platform.DeleteEphemeralUsersMatchingRegex).To(Equal("^foobar.*"))

			// Make sure empty ip field is not included in the response
			boshassert.MatchesJSONMap(GinkgoT(), response, map[string]interface{}{
				"command": "cleanup",
				"status":  "success",
			})
		})
	})
})
func init() {
	Describe("Testing with Ginkgo", func() {
		It("vitals construction", func() {
			_, service := buildVitalsService()
			vitals, err := service.Get()

			expectedVitals := map[string]interface{}{
				"cpu": map[string]string{
					"sys":  "10.0",
					"user": "56.0",
					"wait": "1.0",
				},
				"disk": map[string]interface{}{
					"system": map[string]string{
						"percent":       "50",
						"inode_percent": "10",
					},
					"ephemeral": map[string]string{
						"percent":       "75",
						"inode_percent": "20",
					},
					"persistent": map[string]string{
						"percent":       "100",
						"inode_percent": "75",
					},
				},
				"load": []string{"0.20", "4.55", "1.12"},
				"mem": map[string]string{
					"kb":      "700",
					"percent": "70",
				},
				"swap": map[string]string{
					"kb":      "600",
					"percent": "60",
				},
			}

			Expect(err).ToNot(HaveOccurred())

			boshassert.MatchesJSONMap(GinkgoT(), vitals, expectedVitals)
		})
		It("getting vitals when missing disks", func() {

			statsCollector, service := buildVitalsService()
			statsCollector.DiskStats = map[string]boshstats.DiskStats{
				"/": boshstats.DiskStats{
					DiskUsage:  boshstats.Usage{Used: 100, Total: 200},
					InodeUsage: boshstats.Usage{Used: 50, Total: 500},
				},
			}

			vitals, err := service.Get()
			Expect(err).ToNot(HaveOccurred())

			boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "ephemeral")
			boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "persistent")
		})
		It("get getting vitals on system disk error", func() {

			statsCollector, service := buildVitalsService()
			statsCollector.DiskStats = map[string]boshstats.DiskStats{}

			_, err := service.Get()
			Expect(err).To(HaveOccurred())
		})
	})
}