示例#1
0
func compareConfigurations(t *testing.T, name, g interface{}, w interface{}) {
	var got, want bytes.Buffer
	prettyPrint(t, &got, g)
	prettyPrint(t, &want, w)

	if got.String() != want.String() {
		t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff (want -> got), %s:\n%s",
			name, &got, &want, name, test.Diff(want.Bytes(), got.Bytes()))
	}
}
示例#2
0
func testConfig(name string, t *testing.T) {
	wantedError := func() error {
		slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1))
		if os.IsNotExist(err) {
			return nil
		}
		if err != nil {
			t.Fatalf("Error reading .err file: %v", err)
		}
		return errors.New(string(slurp))
	}
	b, err := replaceRingPath(name)
	if err != nil {
		t.Fatalf("Could not read %s: %v", name, err)
	}
	b = backslashEscape(b)
	var hiLevelConf serverconfig.Config
	if err := json.Unmarshal(b, &hiLevelConf); err != nil {
		t.Fatalf("Could not unmarshal %s into a serverconfig.Config: %v", name, err)
	}

	lowLevelConf, err := serverinit.GenLowLevelConfig(&hiLevelConf)
	if g, w := strings.TrimSpace(fmt.Sprint(err)), strings.TrimSpace(fmt.Sprint(wantedError())); g != w {
		t.Fatalf("test %s: got GenLowLevelConfig error %q; want %q", name, g, w)
	}
	if err != nil {
		return
	}
	if err := (&jsonconfig.ConfigParser{}).CheckTypes(lowLevelConf.Obj); err != nil {
		t.Fatalf("Error while parsing low-level conf generated from %v: %v", name, err)
	}

	wantFile := strings.Replace(name, ".json", "-want.json", 1)
	wantConf, err := configParser().ReadFile(wantFile)
	if err != nil {
		t.Fatalf("test %s: ReadFile: %v", name, err)
	}
	var got, want bytes.Buffer
	prettyPrint(t, &got, lowLevelConf.Obj)
	prettyPrint(t, &want, wantConf)
	if *updateGolden {
		contents, err := json.MarshalIndent(lowLevelConf.Obj, "", "\t")
		if err != nil {
			t.Fatal(err)
		}
		contents = canonicalizeGolden(t, contents)
		if err := ioutil.WriteFile(wantFile, contents, 0644); err != nil {
			t.Fatal(err)
		}
	}
	if got.String() != want.String() {
		t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff (want -> got), %s:\n%s",
			name, &got, &want, name, test.Diff(want.Bytes(), got.Bytes()))
	}
}
示例#3
0
func TestHandler(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
		return
	}
	seen := map[string]bool{}
	defer SetTestHookBug121(func() {})
	for _, tt := range handlerTests {
		if seen[tt.name] {
			t.Fatalf("duplicate test named %q", tt.name)
		}
		seen[tt.name] = true
		SetTestHookBug121(func() {})

		fakeIndex := test.NewFakeIndex()
		idx := tt.setup(fakeIndex)

		indexOwner := owner
		if io, ok := idx.(indexOwnerer); ok {
			indexOwner = io.IndexOwner()
		}
		h := NewHandler(idx, indexOwner)

		req, err := http.NewRequest("GET", "/camli/search/"+tt.query, nil)
		if err != nil {
			t.Fatalf("%s: bad query: %v", tt.name, err)
		}
		req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:])

		rr := httptest.NewRecorder()
		rr.Body = new(bytes.Buffer)

		h.ServeHTTP(rr, req)

		got := rr.Body.Bytes()
		want, _ := json.MarshalIndent(tt.want, "", "  ")
		trim := bytes.TrimSpace

		if bytes.Equal(trim(got), trim(want)) {
			continue
		}

		// Try with re-encoded got, since the JSON ordering doesn't matter
		// to the test,
		gotj := parseJSON(string(got))
		got2, _ := json.MarshalIndent(gotj, "", "  ")
		if bytes.Equal(got2, want) {
			continue
		}
		diff := test.Diff(want, got2)

		t.Errorf("test %s:\nwant: %s\n got: %s\ndiff:\n%s", tt.name, want, got, diff)
	}
}
示例#4
0
func testConfig(name string, t *testing.T) {
	obj, err := configParser().ReadFile(name)
	if err != nil {
		t.Fatal(err)
	}
	wantedError := func() error {
		slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1))
		if os.IsNotExist(err) {
			return nil
		}
		if err != nil {
			t.Fatalf("Error reading .err file: %v", err)
		}
		return errors.New(string(slurp))
	}
	lowLevelConf, err := serverconfig.GenLowLevelConfig(&serverconfig.Config{Obj: obj})
	if g, w := strings.TrimSpace(fmt.Sprint(err)), strings.TrimSpace(fmt.Sprint(wantedError())); g != w {
		t.Fatalf("test %s: got GenLowLevelConfig error %q; want %q", name, g, w)
	}
	if err != nil {
		return
	}

	wantFile := strings.Replace(name, ".json", "-want.json", 1)
	wantConf, err := configParser().ReadFile(wantFile)
	if err != nil {
		t.Fatalf("test %s: ReadFile: %v", name, err)
	}
	var got, want bytes.Buffer
	prettyPrint(t, &got, lowLevelConf.Obj, 0)
	prettyPrint(t, &want, wantConf, 0)
	if got.String() != want.String() {
		if *updateGolden {
			contents, err := json.MarshalIndent(lowLevelConf.Obj, "", "\t")
			if err != nil {
				t.Fatal(err)
			}
			contents = canonicalizeGolden(t, contents)
			if err := ioutil.WriteFile(wantFile, contents, 0644); err != nil {
				t.Fatal(err)
			}
		}
		t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff (got -> want), %s:\n%s",
			name, &got, &want, name, test.Diff(got.Bytes(), want.Bytes()))
	}
}
func (ht handlerTest) test(t *testing.T) {
	SetTestHookBug121(func() {})

	fakeIndex := test.NewFakeIndex()
	idx := ht.setup(fakeIndex)

	indexOwner := owner
	if io, ok := idx.(indexOwnerer); ok {
		indexOwner = io.IndexOwner()
	}
	h := NewHandler(idx, indexOwner)

	var body io.Reader
	var method = "GET"
	if ht.postBody != "" {
		method = "POST"
		body = strings.NewReader(ht.postBody)
	}
	req, err := http.NewRequest(method, "/camli/search/"+ht.query, body)
	if err != nil {
		t.Fatalf("%s: bad query: %v", ht.name, err)
	}
	req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:])

	rr := httptest.NewRecorder()
	rr.Body = new(bytes.Buffer)

	h.ServeHTTP(rr, req)
	got := rr.Body.Bytes()

	if len(ht.wantDescribed) > 0 {
		dr := new(DescribeResponse)
		if err := json.NewDecoder(bytes.NewReader(got)).Decode(dr); err != nil {
			t.Fatalf("On test %s: Non-JSON response: %s", ht.name, got)
		}
		var gotDesc []string
		for k := range dr.Meta {
			gotDesc = append(gotDesc, k)
		}
		sort.Strings(ht.wantDescribed)
		sort.Strings(gotDesc)
		if !reflect.DeepEqual(gotDesc, ht.wantDescribed) {
			t.Errorf("On test %s: described blobs:\n%v\nwant:\n%v\n",
				ht.name, gotDesc, ht.wantDescribed)
		}
		if ht.want == nil {
			return
		}
	}

	want, _ := json.MarshalIndent(ht.want, "", "  ")
	trim := bytes.TrimSpace

	if bytes.Equal(trim(got), trim(want)) {
		return
	}

	// Try with re-encoded got, since the JSON ordering doesn't matter
	// to the test,
	gotj := parseJSON(string(got))
	got2, _ := json.MarshalIndent(gotj, "", "  ")
	if bytes.Equal(got2, want) {
		return
	}
	diff := test.Diff(want, got2)

	t.Errorf("test %s:\nwant: %s\n got: %s\ndiff:\n%s", ht.name, want, got, diff)
}