func SetUp(cpiRequestType string) (*ghttp.Server, *strings.Reader, config.Cpi, bosh.CpiRequest) { var err error server := ghttp.NewServer() jsonReader := strings.NewReader(fmt.Sprintf(`{"api_url":"%s", "agent":{"blobstore": {"provider":"local","some": "options"}, "mbus":"localhost"}, "max_reserve_node_attempts":1}`, server.URL())) request := bosh.CpiRequest{Method: cpiRequestType} cpiConfig, err := config.New(jsonReader, request) Expect(err).ToNot(HaveOccurred()) return server, jsonReader, cpiConfig, request }
) 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 = ghttp.NewServer() serverURL, err := url.Parse(server.URL()) Expect(err).ToNot(HaveOccurred()) jsonReader = strings.NewReader(fmt.Sprintf(`{"apiserver":"%s", "agent":{"blobstore": {"provider":"local","some": "options"}, "mbus":"localhost"}, "max_create_vm_attempts":1}`, serverURL.Host)) request = bosh.CpiRequest{Method: bosh.SET_VM_METADATA} cpiConfig, err = config.New(jsonReader, request) Expect(err).ToNot(HaveOccurred()) }) 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",
func main() { responseLogBuffer = new(bytes.Buffer) multiWriter := io.MultiWriter(os.Stderr, responseLogBuffer) logLevel := os.Getenv("RACKHD_CPI_LOG_LEVEL") log.SetOutput(multiWriter) switch logLevel { case "DEBUG": log.SetLevel(log.DebugLevel) case "INFO": log.SetLevel(log.InfoLevel) case "ERROR": log.SetLevel(log.ErrorLevel) case "FATAL": log.SetLevel(log.FatalLevel) default: log.SetLevel(log.DebugLevel) } configPath := flag.String("configPath", "", "Path to configuration file") flag.Parse() file, err := os.Open(*configPath) defer file.Close() if err != nil { log.Error(fmt.Sprintf("unable to open configuration file %s", err)) exitWithDefaultError(err) } reqBytes, err := ioutil.ReadAll(os.Stdin) if err != nil { exitWithDefaultError(err) } req := bosh.CpiRequest{} err = json.Unmarshal(reqBytes, &req) if err != nil { exitWithDefaultError(err) } cpiConfig, err := config.New(file, req) if err != nil { exitWithDefaultError(err) } implemented, err := cpi.ImplementsMethod(req.Method) if err != nil { exitWithDefaultError(err) } if !implemented { exitWithNotImplementedError(fmt.Errorf("Method: %s is not implemented", req.Method)) } switch req.Method { case bosh.CREATE_STEMCELL: cid, err := cpi.CreateStemcell(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running CreateStemcell: %s", err)) } exitWithResult(cid) case bosh.CREATE_VM: vmcid, err := cpi.CreateVM(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running CreateVM: %s", err)) } exitWithResult(vmcid) case bosh.DELETE_STEMCELL: err = cpi.DeleteStemcell(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running DeleteStemcell: %s", err)) } exitWithResult("") case bosh.DELETE_VM: err = cpi.DeleteVM(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running DeleteVM: %s", err)) } exitWithResult("") case bosh.SET_VM_METADATA: err := cpi.SetVMMetadata(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running SetVMMetadata: %s", err)) } exitWithResult("") case bosh.HAS_VM: hasVM, err := cpi.HasVM(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running HasVM: %s", err)) } exitWithResult(hasVM) case bosh.CREATE_DISK: diskCID, err := cpi.CreateDisk(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running CreateDisk: %s", err)) } exitWithResult(diskCID) case bosh.DELETE_DISK: err := cpi.DeleteDisk(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running DeleteDisk: %s", err)) } exitWithResult("") case bosh.ATTACH_DISK: err := cpi.AttachDisk(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running AttachDisk: %s", err)) } exitWithResult("") case bosh.DETACH_DISK: err := cpi.DetachDisk(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running DetachDisk: %s", err)) } exitWithResult("") case bosh.HAS_DISK: diskExists, err := cpi.HasDisk(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running HasDisk: %s", err)) } exitWithResult(diskExists) case bosh.GET_DISKS: diskCIDs, err := cpi.GetDisks(cpiConfig, req.Arguments) if err != nil { exitWithDefaultError(fmt.Errorf("Error running GetDisks: %s", err)) } exitWithResult(diskCIDs) default: exitWithDefaultError(fmt.Errorf("Unexpected command: %s dispatched...aborting", req.Method)) } }
. "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/rackhd/rackhd-cpi/bosh" "github.com/rackhd/rackhd-cpi/config" ) var _ = Describe("Creating a config", func() { var request bosh.CpiRequest BeforeEach(func() { request = bosh.CpiRequest{} }) It("checks that the API server URI is set", func() { jsonReader := strings.NewReader(`{}`) _, err := config.New(jsonReader, request) Expect(err).To(MatchError("ApiServer IP is not set")) }) It("checks that the agent configuration has an mbus setting", func() { jsonReader := strings.NewReader(`{"apiserver":"localhost:8080", "agent":{"blobstore": {"some": "options"}}}`) _, err := config.New(jsonReader, request) Expect(err).To(MatchError(`Agent config invalid {map[some:options] []}`)) }) It("checks that the agent configuration includes a blobstore section", func() { jsonReader := strings.NewReader(`{"apiserver":"localhost:8080", "agent":{"mbus":"localhost"}}`) _, err := config.New(jsonReader, request) Expect(err).To(MatchError(`Agent config invalid {map[] localhost []}`)) })
. "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" ) var _ = Describe("Nodes", func() { var server *ghttp.Server var jsonReader *strings.Reader var cpiConfig config.Cpi BeforeEach(func() { server = ghttp.NewServer() serverURL, err := url.Parse(server.URL()) Expect(err).ToNot(HaveOccurred()) jsonReader = strings.NewReader(fmt.Sprintf(`{"apiserver":"%s", "agent":{"blobstore": {"provider":"local","some": "options"}, "mbus":"localhost"}, "max_create_vm_attempts":1}`, serverURL.Host)) cpiConfig, err = config.New(jsonReader, bosh.CpiRequest{}) Expect(err).ToNot(HaveOccurred()) }) AfterEach(func() { server.Close() }) Describe("Getting nodes", func() { It("return expected nodes' fields", func() { expectedNodes := helpers.LoadNodes("../spec_assets/dummy_two_node_response.json") expectedNodesData, err := json.Marshal(expectedNodes) Expect(err).ToNot(HaveOccurred()) server.AppendHandlers( ghttp.CombineHandlers( ghttp.VerifyRequest("GET", "/api/common/nodes"),