func TestGetConfiguration(t *testing.T) { valid := getValidConfiguration(t) tests := []DeployerTestCase{ { "expect success for GetConfiguration", "", func(w http.ResponseWriter, r *http.Request) { // Get name from path, find in valid, and return its properties. rtype := path.Base(path.Dir(r.URL.Path)) rname := path.Base(r.URL.Path) for _, resource := range valid.Resources { if resource.Type == rtype && resource.Name == rname { util.LogHandlerExitWithYAML("resourcifier: get configuration", w, resource.Properties, http.StatusOK) return } } status := fmt.Sprintf("resource %s of type %s not found", rname, rtype) http.Error(w, status, http.StatusInternalServerError) }, }, { "expect error for GetConfiguration", "cannot get configuration", deployerErrorHandler, }, } for _, dtc := range tests { ts := httptest.NewServer(http.HandlerFunc(dtc.Handler)) defer ts.Close() deployer := NewDeployer(ts.URL) result, err := deployer.GetConfiguration(valid) if err != nil { message := err.Error() if !strings.Contains(message, dtc.Error) { t.Errorf("error in test case:%s:%s\n", dtc.Description, message) } } else { if dtc.Error != "" { t.Errorf("expected error:%s\ndid not occur in test case:%s\n", dtc.Error, dtc.Description) } if !reflect.DeepEqual(valid, result) { t.Errorf("error in test case:%s:\nwant:%s\nhave:%s\n", dtc.Description, util.ToYAMLOrError(valid), util.ToYAMLOrError(result)) } } } }
func putConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: update configuration" util.LogHandlerEntry(handler, r) defer r.Body.Close() c := getConfiguration(w, r, handler) if c != nil { if _, err := backend.Configure(c, configurator.ReplaceOperation); err != nil { e := errors.New("cannot replace configuration: " + err.Error() + "\n") util.LogAndReturnError(handler, http.StatusBadRequest, e, w) return } util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) return } util.LogHandlerExit(handler, http.StatusOK, "OK", w) }
func createConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: create configuration" util.LogHandlerEntry(handler, r) defer r.Body.Close() c := getConfiguration(w, r, handler) if c != nil { _, err := backend.Configure(c, configurator.CreateOperation) if err != nil { util.LogAndReturnError(handler, http.StatusBadRequest, err, w) return } util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) return } util.LogHandlerExit(handler, http.StatusOK, "OK", w) }