func deployerSuccessHandler(w http.ResponseWriter, r *http.Request) {
	valid := &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 := &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)
}
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. 3
0
func TestServiceWrapper(t *testing.T) {
	backend := expander.NewExpander("../expansion/expansion.py")
	wrapper := NewService(NewExpansionHandler(backend))
	container := restful.NewContainer()
	container.ServeMux = http.NewServeMux()
	wrapper.Register(container)
	handlerTester := util.NewHandlerTester(container)
	for _, swtc := range ServiceWrapperTestCases {
		reader := GetTemplateReader(t, swtc.Description, inputFileName)
		w, err := handlerTester(swtc.HTTPMethod, swtc.ServiceURLPath, swtc.ContentType, reader)
		if err != nil {
			t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
		}

		if w.Code != http.StatusOK {
			if w.Code != swtc.StatusCode {
				message := fmt.Sprintf("test returned code:%d, status: %s", w.Code, w.Body.String())
				t.Errorf("error in test case '%s': %s\n", swtc.Description, message)
			}
		} else {
			if swtc.StatusCode != http.StatusOK {
				t.Errorf("expected error did not occur in test case '%s': want: %d have: %d\n",
					swtc.Description, swtc.StatusCode, w.Code)
			}

			body := w.Body.Bytes()
			actualResponse := &expander.ExpansionResponse{}
			if err := json.Unmarshal(body, actualResponse); err != nil {
				t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
			}

			actualResult, err := actualResponse.Unmarshal()
			if err != nil {
				t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
			}

			expectedOutput := GetOutputString(t, swtc.Description)
			expectedResult := expandOutputOrDie(t, expectedOutput, swtc.Description)

			if !reflect.DeepEqual(expectedResult, actualResult) {
				message := fmt.Sprintf("want: %s\nhave: %s\n",
					util.ToYAMLOrError(expectedResult), util.ToYAMLOrError(actualResult))
				t.Errorf("error in test case '%s':\n%s\n", swtc.Description, message)
			}
		}
	}
}
func TestExpandTemplate(t *testing.T) {
	roundTripResponse := &ExpandedTemplate{}
	if err := yaml.Unmarshal([]byte(finalExpanded), roundTripResponse); err != nil {
		panic(err)
	}

	tests := []ExpanderTestCase{
		{
			"expect success for ExpandTemplate",
			"",
			expanderSuccessHandler,
			&mockResolver{},
			getValidResponse(t, "expect success for ExpandTemplate"),
		},
		{
			"expect error for ExpandTemplate",
			"cannot expand template",
			expanderErrorHandler,
			&mockResolver{},
			nil,
		},
		{
			"expect success for ExpandTemplate with two expansions",
			"",
			roundTripHandler,
			&mockResolver{[][]*ImportFile{
				{},
				{&ImportFile{Name: "test"}},
			}, t},
			roundTripResponse,
		},
	}

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

		expander := NewExpander(ts.URL, etc.Resolver)
		actualResponse, err := expander.ExpandTemplate(validTemplateTestCaseData)
		if err != nil {
			message := err.Error()
			if etc.Error == "" {
				t.Errorf("Error in test case %s when there should not be.", etc.Description)
			}
			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))
			}
		}
	}
}