Esempio n. 1
0
func TestExpandTemplate(t *testing.T) {
	//	roundTripResponse := &ExpandedConfiguration{}
	//	if err := yaml.Unmarshal([]byte(finalExpanded), roundTripResponse); err != nil {
	//		panic(err)
	//	}

	tests := []ExpanderTestCase{
		{
			"expect success for ExpandConfiguration",
			"",
			expanderSuccessHandler,
			getValidExpandedConfiguration(),
		},
		{
			"expect error for ExpandConfiguration",
			"simulated failure",
			expanderErrorHandler,
			nil,
		},
	}

	for _, etc := range tests {
		ts := httptest.NewServer(http.HandlerFunc(etc.Handler))
		defer ts.Close()

		expander := NewExpander("8081", ts.URL, getTestRepoProvider(t))
		resource := &common.Resource{
			Name: "test_invocation",
			Type: TestResourceType,
		}

		conf := &common.Configuration{
			Resources: []*common.Resource{
				resource,
			},
		}

		actualResponse, err := expander.ExpandConfiguration(conf)
		if err != nil {
			message := err.Error()
			if etc.Error == "" {
				t.Errorf("unexpected error in test case %s: %s", etc.Description, err)
			}
			if !strings.Contains(message, etc.Error) {
				t.Errorf("error in test case:%s:%s\n", etc.Description, message)
			}
		} else {
			if etc.Error != "" {
				t.Errorf("expected error:%s\ndid not occur in test case:%s\n",
					etc.Error, etc.Description)
			}

			expectedResponse := etc.ValidResponse
			if !reflect.DeepEqual(expectedResponse, actualResponse) {
				t.Errorf("error in test case:%s:\nwant:%s\nhave:%s\n",
					etc.Description, util.ToYAMLOrError(expectedResponse), util.ToYAMLOrError(actualResponse))
			}
		}
	}
}
Esempio n. 2
0
func TestLoadContent(t *testing.T) {
	c, err := LoadDir(testdir)
	if err != nil {
		t.Errorf("Failed to load chart: %s", err)
	}

	content, err := c.LoadContent()
	if err != nil {
		t.Errorf("Failed to load chart content: %s", err)
	}

	want := c.Chartfile()
	have := content.Chartfile
	if !reflect.DeepEqual(want, have) {
		t.Errorf("Unexpected chart file\nwant:\n%s\nhave:\n%s\n",
			util.ToYAMLOrError(want), util.ToYAMLOrError(have))
	}

	for _, member := range content.Members {
		have := member.Content
		wantMember, err := c.LoadMember(member.Path)
		if err != nil {
			t.Errorf("Failed to load chart member: %s", err)
		}

		t.Logf("%s:\n%s\n\n", member.Path, member.Content)
		want := wantMember.Content
		if !reflect.DeepEqual(want, have) {
			t.Errorf("Unexpected chart member %s\nwant:\n%v\nhave:\n%v\n", member.Path, want, have)
		}
	}
}
Esempio n. 3
0
func deployerSuccessHandler(w http.ResponseWriter, r *http.Request) {
	valid := &common.Configuration{}
	err := yaml.Unmarshal(validConfigurationTestCaseData, valid)
	if err != nil {
		status := fmt.Sprintf("cannot unmarshal test case data:%s", err)
		http.Error(w, status, http.StatusInternalServerError)
		return
	}

	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		status := fmt.Sprintf("cannot read request body:%s", err)
		http.Error(w, status, http.StatusInternalServerError)
		return
	}

	result := &common.Configuration{}
	if err := yaml.Unmarshal(body, result); err != nil {
		status := fmt.Sprintf("cannot unmarshal request body:%s", err)
		http.Error(w, status, http.StatusInternalServerError)
		return
	}

	if !reflect.DeepEqual(valid, result) {
		status := fmt.Sprintf("error in http handler:\nwant:%s\nhave:%s\n",
			util.ToYAMLOrError(valid), util.ToYAMLOrError(result))
		http.Error(w, status, http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Esempio n. 4
0
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))
			}
		}
	}
}
Esempio n. 5
0
// NewService encapsulates code to open an HTTP server on the given address:port that serves the
// expansion API using the given Expander backend to do the actual expansion.  After calling
// NewService, call ListenAndServe to start the returned service.
func NewService(address string, port int, backend Expander) *Service {

	restful.EnableTracing(true)
	webService := new(restful.WebService)
	webService.Consumes(restful.MIME_JSON)
	webService.Produces(restful.MIME_JSON)
	handler := func(req *restful.Request, resp *restful.Response) {
		util.LogHandlerEntry("expansion service", req.Request)
		request := &ServiceRequest{}
		if err := req.ReadEntity(&request); err != nil {
			badRequest(resp, err.Error())
			return
		}

		reqMsg := fmt.Sprintf("\nhandling request:\n%s\n", util.ToYAMLOrError(request))
		util.LogHandlerText("expansion service", reqMsg)
		response, err := backend.ExpandChart(request)
		if err != nil {
			badRequest(resp, fmt.Sprintf("error expanding chart: %s", err))
			return
		}

		util.LogHandlerExit("expansion service", http.StatusOK, "OK", resp.ResponseWriter)
		respMsg := fmt.Sprintf("\nreturning response:\n%s\n", util.ToYAMLOrError(response.Resources))
		util.LogHandlerText("expansion service", respMsg)
		resp.WriteEntity(response)
	}
	webService.Route(
		webService.POST("/expand").
			To(handler).
			Doc("Expand a chart.").
			Reads(&ServiceRequest{}).
			Writes(&ServiceResponse{}))

	container := restful.DefaultContainer
	container.Add(webService)
	server := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", address, port),
		Handler: container,
	}

	return &Service{
		webService: webService,
		server:     server,
		container:  container,
	}
}