示例#1
0
func TestConfigServer(t *testing.T) {
	file, err := ioutil.TempFile("", "yamlconf_test_")
	if err != nil {
		t.Fatalf("Unable to create temp file: %s", err)
	}
	defer os.Remove(file.Name())

	m := &Manager{
		EmptyConfig: func() Config {
			return &TestCfg{}
		},
		FilePath:         file.Name(),
		FilePollInterval: pollInterval,
		ConfigServerAddr: ConfigSrvAddr,
	}

	_, err = m.Init()
	if err != nil {
		t.Fatalf("Unable to init manager: %s", err)
	}
	m.StartPolling()

	newNested := &Nested{
		S: "900",
		I: 900,
	}
	nny, err := yaml.Marshal(newNested)
	if err != nil {
		t.Fatalf("Unable to marshal new nested into yaml: %s", err)
	}

	_, err = http.Post(fmt.Sprintf("http://%s/N", ConfigSrvAddr), "text/yaml", bytes.NewReader(nny))
	assert.NoError(t, err, "POSTing to config server should succeed")

	updated := m.Next()

	assert.Equal(t, &TestCfg{
		Version: 2,
		N:       newNested,
	}, updated, "Nested should have been updated by POST")

	req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/N/I", ConfigSrvAddr), bytes.NewReader(nny))
	if err != nil {
		t.Fatalf("Unable to construct DELETE request: %s", err)
	}
	_, err = (&http.Client{}).Do(req)
	assert.NoError(t, err, "DELETEing to config server should succeed")

	updated = m.Next()

	assert.Equal(t, &TestCfg{
		Version: 3,
		N: &Nested{
			S: newNested.S,
			I: FIXED_I,
		},
	}, updated, "Nested I should have reverted to default value after clearing")
}
示例#2
0
func TestPackagedSettings(t *testing.T) {
	file, err := ioutil.TempFile("", ".packaged-lantern.yaml")
	defer func() {
		err := os.Remove(file.Name())
		if err != nil {
			log.Errorf("Could not remove file? %v", err)
		}
	}()
	assert.True(t, err == nil, "Should not be an error")
	file.Close()

	log.Debugf("File at: %v", file.Name())
	settings := PackagedSettings{StartupUrl: "test"}
	log.Debugf("Settings: %v", settings)

	data, er := yaml.Marshal(&settings)
	assert.True(t, er == nil, "Should not be an error")

	e := ioutil.WriteFile(file.Name(), data, 0644)
	assert.True(t, e == nil, "Should not be an error")

	path, ps, errr := readSettingsFromFile(file.Name())
	assert.Equal(t, "test", ps.StartupUrl, "Unexpected startup URL")
	assert.Equal(t, file.Name(), path, "Wrote to unexpected path")
	assert.True(t, errr == nil, "Should not be an error")

	// Now do another full round trip, writing and reading
	// Overwite local to avoid affecting actual Lantern instances
	local = file.Name()
	path, errrr := writeToDisk(ps)
	assert.True(t, errrr == nil, "Should not be an error")
	path, ps, err = readSettingsFromFile(path)
	assert.Equal(t, "test", ps.StartupUrl, "Could not read data")
	assert.Equal(t, local, path, "Wrote to unexpected path")
	assert.True(t, err == nil, "Should not be an error")

	url := "test"

	path, ps, err = readSettingsFromFile(path)

	log.Debugf("Wrote settings to: %v", path)
	assert.Equal(t, url, ps.StartupUrl, "Could not read data")
	assert.Equal(t, local, path, "Wrote to unexpected path")
	assert.True(t, err == nil, "Should not be an error")

	path, err = packagedSettingsPath()
	assert.True(t, err == nil, "Should not be an error")

	var dir string
	dir, err = filepath.Abs(filepath.Dir(os.Args[0]))
	assert.True(t, err == nil, "Should not be an error")

	if runtime.GOOS == "darwin" {
		assert.Equal(t, dir+"/../Resources/"+name, path, "Unexpected settings dir")
	} else if runtime.GOOS == "linux" {
		assert.Equal(t, dir+"/"+name, path, "Unexpected settings dir")
	}
}
示例#3
0
func saveConfig(t *testing.T, file *os.File, updated *TestCfg) {
	b, err := yaml.Marshal(updated)
	if err != nil {
		t.Fatalf("Unable to marshal updated to yaml: %s", err)
	}
	err = ioutil.WriteFile(file.Name(), b, 0644)
	if err != nil {
		t.Fatalf("Unable to save test config: %s", err)
	}
}
示例#4
0
// Save saves settings to disk.
func (s *Settings) save() {
	log.Debug("Saving settings")
	s.Lock()
	defer s.Unlock()
	if bytes, err := yaml.Marshal(s); err != nil {
		log.Errorf("Could not create yaml from settings %v", err)
	} else if err := ioutil.WriteFile(path, bytes, 0644); err != nil {
		log.Errorf("Could not write settings file %v", err)
	} else {
		log.Debugf("Saved settings to %s with contents %v", path, string(bytes))
	}
}
示例#5
0
func assertSavedConfigEquals(t *testing.T, file *os.File, expected *TestCfg) {
	b, err := yaml.Marshal(expected)
	if err != nil {
		t.Fatalf("Unable to marshal expected to yaml: %s", err)
	}
	bod, err := ioutil.ReadFile(file.Name())
	if err != nil {
		t.Errorf("Unable to read config from disk: %s", err)
	}
	if !bytes.Equal(b, bod) {
		t.Errorf("Saved config doesn't equal expected.\n---- Expected ----\n%s\n\n---- On Disk ----:\n%s\n\n", string(b), string(bod))
	}
}
示例#6
0
文件: packaged.go 项目: kidaa/lantern
func writeToDisk(ps *PackagedSettings) (string, error) {
	data, err := yaml.Marshal(ps)
	if err != nil {
		log.Errorf("Could not write to disk: %v", err)
		return "", err
	}
	err = ioutil.WriteFile(local, data, 0644)
	if err != nil {
		log.Errorf("Could not write to disk: %v", err)
		return "", err
	}
	return local, nil
}
示例#7
0
func (m *Manager) writeToDisk(cfg Config) error {
	bytes, err := yaml.Marshal(cfg)
	if err != nil {
		return fmt.Errorf("Unable to marshal config yaml: %s", err)
	}
	err = ioutil.WriteFile(m.FilePath, bytes, 0644)
	if err != nil {
		return fmt.Errorf("Unable to write config yaml to file %s: %s", m.FilePath, err)
	}
	m.fileInfo, err = os.Stat(m.FilePath)
	if err != nil {
		return fmt.Errorf("Unable to stat file %s: %s", m.FilePath, err)
	}
	return nil
}