"github.com/rackhd/rackhd-cpi/rackhdapi"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
	"github.com/rackhd/rackhd-cpi/helpers"
)

var _ = Describe("DeleteDisk", func() {
	var server *ghttp.Server
	var jsonReader *strings.Reader
	var cpiConfig config.Cpi
	var request bosh.CpiRequest

	BeforeEach(func() {
		server, jsonReader, cpiConfig, request = helpers.SetUp(bosh.DELETE_DISK)

	})

	AfterEach(func() {
		server.Close()
	})

	Context("when given a disk cid for an existing, unattached disk", func() {
		var extInput bosh.MethodArguments
		var expectedDeleteDiskBodyBytes []byte

		BeforeEach(func() {
			expectedNodes := helpers.LoadNodes("../spec_assets/dummy_disks_response.json")
			expectedNodesData, err := json.Marshal(expectedNodes)
			Expect(err).ToNot(HaveOccurred())
	. "github.com/rackhd/rackhd-cpi/cpi"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
	"github.com/rackhd/rackhd-cpi/helpers"
)

var _ = Describe("AttachDisk", func() {
	var server *ghttp.Server
	var jsonReader *strings.Reader
	var cpiConfig config.Cpi
	var request bosh.CpiRequest

	BeforeEach(func() {
		server, jsonReader, cpiConfig, request = helpers.SetUp(bosh.ATTACH_DISK)
	})

	AfterEach(func() {
		server.Close()
	})

	Context("given a disk CID that exists", func() {
		It("returns true", func() {
			jsonInput := []byte(`[
					"valid_disk_cid_1"
				]`)
			var extInput bosh.MethodArguments
			err := json.Unmarshal(jsonInput, &extInput)
			Expect(err).ToNot(HaveOccurred())
	. "github.com/rackhd/rackhd-cpi/cpi"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
	"github.com/rackhd/rackhd-cpi/helpers"
)

var _ = Describe("GetDisks", func() {
	var server *ghttp.Server
	var jsonReader *strings.Reader
	var cpiConfig config.Cpi
	var request bosh.CpiRequest

	BeforeEach(func() {
		server, jsonReader, cpiConfig, request = helpers.SetUp(bosh.GET_DISKS)
	})

	AfterEach(func() {
		server.Close()
	})

	Context("given a vm CID that exists", func() {
		Context("the vm has persistent disk", func() {
			It("returns disk CID", func() {
				jsonInput := []byte(`[
						"valid_vm_cid_1"
					]`)
				var extInput bosh.MethodArguments
				err := json.Unmarshal(jsonInput, &extInput)
				Expect(err).ToNot(HaveOccurred())
	"github.com/rackhd/rackhd-cpi/helpers"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
)

var _ = Describe("Cpi/HasVm", func() {
	Context("Has VM", func() {
		var server *ghttp.Server
		var jsonReader *strings.Reader
		var cpiConfig config.Cpi
		var request bosh.CpiRequest

		BeforeEach(func() {
			server, jsonReader, cpiConfig, request = helpers.SetUp(bosh.HAS_VM)
		})

		AfterEach(func() {
			server.Close()
		})

		It("Find a vm that exist", func() {
			cid := "vm-1234"

			var metadataInput bosh.MethodArguments
			metadataInput = append(metadataInput, cid)
			expectedNodes := helpers.LoadNodes("../spec_assets/dummy_two_node_response.json")
			expectedNodesData, err := json.Marshal(expectedNodes)
			Expect(err).ToNot(HaveOccurred())
			Expect(w.Tasks).To(HaveLen(4))
			Expect(w.Name).To(ContainSubstring(uID))

			Expect(w.Tasks[0].TaskName).To(Equal("Task.Obm.Node.PxeBoot"))
			Expect(w.Tasks[1].TaskName).To(Equal("Task.Obm.Node.Reboot"))
			Expect(w.Tasks[2].TaskName).To(Equal("Task.Linux.Bootstrap.Ubuntu"))
			Expect(w.Tasks[3].TaskName).To(Equal(r.Name))
		})
	})

	Describe("buildReserveNodeWorkflowOptions", func() {
		var server *ghttp.Server
		var cpiConfig config.Cpi

		BeforeEach(func() {
			server, _, cpiConfig, _ = helpers.SetUp("")
		})

		Context("when the node uses IPMI", func() {
			It("sets the OBM settings to IPMI", func() {
				expectedNode := helpers.LoadNode("../spec_assets/dummy_one_node_with_ipmi_response.json")
				expectedNodeData, err := json.Marshal(expectedNode)
				Expect(err).ToNot(HaveOccurred())

				nodeID := "5665a65a0561790005b77b85"
				server.AppendHandlers(
					ghttp.CombineHandlers(
						ghttp.VerifyRequest("GET", fmt.Sprintf("/api/common/nodes/%s", nodeID)),
						ghttp.RespondWith(http.StatusOK, expectedNodeData),
					),
				)
	"github.com/onsi/gomega/ghttp"
	"github.com/rackhd/rackhd-cpi/bosh"
	"github.com/rackhd/rackhd-cpi/config"
	"github.com/rackhd/rackhd-cpi/cpi"
	"github.com/rackhd/rackhd-cpi/helpers"
)

var _ = Describe("Setting VM Metadata", func() {
	Context("When called with metadata", func() {
		var server *ghttp.Server
		var jsonReader *strings.Reader
		var cpiConfig config.Cpi
		var request bosh.CpiRequest

		BeforeEach(func() {
			server, jsonReader, cpiConfig, request = helpers.SetUp(bosh.SET_VM_METADATA)
		})

		AfterEach(func() {
			server.Close()
		})

		It("Sends a request to set metadata to the RackHD API", func() {

			id := "55e79ea54e66816f6152fff9"
			cid := "vm-5678"
			metadata := map[string]interface{}{
				"stuff":  "definitely",
				"thing1": 3563456,
				"thing2": "bloop",
			}
	"github.com/rackhd/rackhd-cpi/config"
	"github.com/rackhd/rackhd-cpi/helpers"
	"github.com/rackhd/rackhd-cpi/rackhdapi"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
)

var _ = Describe("Workflows", func() {
	var server *ghttp.Server
	var jsonReader *strings.Reader
	var cpiConfig config.Cpi

	BeforeEach(func() {
		server, jsonReader, cpiConfig, _ = helpers.SetUp("")
	})

	AfterEach(func() {
		server.Close()
	})

	Describe("GetActiveWorkflows", func() {
		Context("there is a running workflow", func() {
			It("returns a node's active workflow", func() {
				rawWorkflow := helpers.LoadJSON("../spec_assets/dummy_workflow_response.json")
				httpResponse := []byte(fmt.Sprintf("%s", string(rawWorkflow)))
				var expectedResponse rackhdapi.WorkflowResponse
				err := json.Unmarshal(httpResponse, &expectedResponse)
				Expect(err).ToNot(HaveOccurred())