Пример #1
0
// TestGenerateClientConfig validates the client config generated for display
// by the HelpHandler.
func TestGenerateClientConfig(t *testing.T) {
	inName := filepath.Join("testdata", "gen_client_config.in")
	wantName := strings.Replace(inName, ".in", ".out", 1)

	b, err := replaceRingPath(inName)
	if err != nil {
		t.Fatalf("Failed to read high-level server config file: %v", err)
	}
	b = backslashEscape(b)
	var hiLevelConf serverconfig.Config
	if err := json.Unmarshal(b, &hiLevelConf); err != nil {
		t.Fatalf("Failed to unmarshal server config: %v", err)
	}
	lowLevelConf, err := serverinit.GenLowLevelConfig(&hiLevelConf)
	if err != nil {
		t.Fatalf("Failed to generate low-level config: %v", err)
	}
	generatedConf, err := clientconfig.GenerateClientConfig(lowLevelConf.Obj)
	if err != nil {
		t.Fatalf("Failed to generate client config: %v", err)
	}

	wb, err := replaceRingPath(wantName)
	if err != nil {
		t.Fatalf("Failed to read want config file: %v", err)
	}
	wb = backslashEscape(wb)
	var wantConf clientconfig.Config
	if err := json.Unmarshal(wb, &wantConf); err != nil {
		t.Fatalf("Failed to unmarshall want config: %v", err)
	}

	compareConfigurations(t, inName, generatedConf, wantConf)
}
Пример #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 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)
	}

	// TODO(mpl): should we stop execution (and not update golden files)
	// if the comparison fails? Currently this is not the case.
	wantFile := strings.Replace(name, ".json", "-want.json", 1)
	wantConf, err := configParser().ReadFile(wantFile)
	if err != nil {
		t.Fatalf("test %s: ReadFile: %v", name, err)
	}
	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)
		}
	}
	compareConfigurations(t, name, lowLevelConf.Obj, wantConf)
}
Пример #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 := serverinit.GenLowLevelConfig(&serverinit.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()))
	}
}