Exemplo n.º 1
0
func TestConf(t *testing.T) {
	assert := assert.New(t)

	env.ConfFile = "../test/data/test.conf"
	env.VarFile = "../test/data/test_server.conf"
	conf := NewConfFromEnv()
	assert.Equal("APIKEY", conf.ApiKey)
	assert.Equal(2, len(conf.Files), "len of files")
	dpkg := conf.Files[0]
	assert.Equal("/var/lib/dpkg/available", dpkg.Path, "file path")

	gemfile := conf.Files[1]
	assert.Equal("/path/to/Gemfile.lock", gemfile.Path, "file path")

	assert.Equal("123456", conf.ServerConf.UUID)
}
Exemplo n.º 2
0
// does the callback get fired when the directory
// the file is in gets renamed?
// TODO: replace with tempfiles.
func TestWatchFileRenameDirectory(t *testing.T) {
	assert := assert.New(t)

	folder := "/tmp/CANARYTEST"
	file_name := folder + "/test1.gems"

	os.Mkdir(folder, 0777)
	ioutil.WriteFile(file_name, []byte("tst"), 0644)

	cbInvoked := make(chan bool, 10)

	mutex := &sync.Mutex{}
	counter := 0
	testcb := func(wfile *WatchedFile) {
		mutex.Lock()
		counter++
		mutex.Unlock()
		cbInvoked <- true
	}

	wfile := NewWatchedFile(file_name, testcb)

	// file gets read on hook add
	wfile.StartListener()
	defer wfile.StopListening()
	<-cbInvoked

	// aight. let's rename the folder it's in.
	// let's create a tmp path we can rename to.
	folder2 := "/tmp/CANARYTEST2"
	os.Rename(folder, folder2)

	// file should now be missing.
	time.Sleep(TEST_POLL_SLEEP)

	assert.False(wfile.GetBeingWatched())

	// let's then recreate a new file w/same path
	// recreate the old folderm
	os.Mkdir(folder, 0777)
	// write new file
	ioutil.WriteFile(file_name, []byte("tst2"), 0644)

	time.Sleep(TEST_POLL_SLEEP)
	// this file should be different, thus triggering
	// another callback

	mutex.Lock()
	assert.Equal(2, counter)
	mutex.Unlock()

	os.RemoveAll(folder)
	os.RemoveAll(folder2)
}
Exemplo n.º 3
0
func TestAgent(t *testing.T) {
	assert := assert.New(t)

	// setup
	InitEnv("test")
	conf := NewConfFromEnv()

	conf.Files[0].Path = DEV_CONF_PATH + "/dpkg/available"

	client := &MockClient{}
	agent := NewAgent("test", conf, client)

	// let's ensure our server is unregistered
	agent.server.UUID = ""

	assert.Equal(true, agent.FirstRun())
	server_uuid := "123456"

	client.On("CreateServer").Return(server_uuid)
	agent.RegisterServer()

	// registering the server actually set the right val
	assert.Equal(server_uuid, agent.server.UUID)

	// Let's ensure that the client gets exercised.
	client.On("SendFile").Return(nil).Once()
	agent.StartWatching()

	client.On("Heartbeat").Return(nil).Once()
	agent.Heartbeat()

	// the filewatcher needs enough time to
	// actually be able to start watching
	// the file. This is clunky, but less clunky
	// than hacking some channel into this.
	<-time.After(200 * time.Millisecond)
	// close the hooks before asserting expectations
	// since the SendFiles happen in a go routine
	defer agent.CloseWatches()
	defer client.AssertExpectations(t)
}
Exemplo n.º 4
0
// create a tempfile, add a hook, see if hook gets called
// when file changes. TODO: test all other fs events.
func TestWatchFile(t *testing.T) {
	assert := assert.New(t)

	file_content := "tst1"
	tf, _ := ioutil.TempFile("", "gems.lock")
	tf.Write([]byte(file_content))
	tf.Close()

	timer := time.Tick(5 * time.Second)
	cbInvoked := make(chan bool)
	testcb := func(nop *WatchedFile) {
		cbInvoked <- true
	}

	wfile := NewWatchedFile(tf.Name(), testcb)

	wfile.StartListener()

	// let's make sure the file got written to
	read_contents, _ := wfile.Contents()
	assert.Equal(file_content, string(read_contents))

	// but really we want to know if the
	// callback was ever invoked
	select {
	case invoked := <-cbInvoked:
		assert.True(invoked)

	case _ = <-timer:
		assert.True(false)
	}

	// solid. on boot it worked. But what
	// if we changed the file contents?

	newContents := []byte("HelloWorld\n")
	err := ioutil.WriteFile(tf.Name(), newContents, 0777)
	assert.Nil(err)

	// let's wait again just in case.
	select {
	case invoked := <-cbInvoked:
		assert.True(invoked)

	case _ = <-timer:
		assert.True(false)
	}

	wfile.StopListening()
}