Exemplo n.º 1
0
Arquivo: log_test.go Projeto: kacf/log
func TestSyslog(t *testing.T) {
	setupLogging(t)

	if AddSyslogHook() != nil {
		// If we cannot connect to syslog we have no choice but to skip
		// the test. It's perfectly legitimate that it's not available.
		cleanupLogging(t)
		t.Skip("Skip syslog test because syslog is not available")
	}

	Log.Formatter.(*logrus.TextFormatter).ForceColors = true

	rand.Seed(time.Now().UTC().UnixNano())

	// In order to not get false passes because the syslog has entries from
	// the previous run.
	testrand := rand.Int()

	SetLevel(DebugLevel)

	Log.Errorf("For syslog testing: Error with no module: %d", testrand)
	Log.PushModule("test1")
	Log.Warnf("For syslog testing: Warning with test1 module: %d", testrand)
	Log.Debugf("For syslog testing: Debug with test1 module: %d", testrand)
	Log.PopModule()

	syslog := "/var/log/syslog"
	if _, err := os.Stat(syslog); err != nil {
		syslog = "/var/log/messages"
		if _, err = os.Stat(syslog); err != nil {
			t.Fatal("Could not locate syslog, cannot test")
		}
	}
	cmd := exec.Command("tail", syslog)
	output, err := cmd.Output()
	if err != nil {
		t.Fatalf("Error tailing syslog: %s", err.Error())
	}

	// Make sure there are no colors in the syslog, even if we forced them
	// for the console.
	var checkString string
	// Should show.
	checkString = fmt.Sprintf("level=error msg=\"For syslog testing: Error with no module: "+
		"%d\" module=\"log_test\"", testrand)
	mt.AssertTrue(t, strings.Index(string(output), checkString) >= 0)
	checkString = fmt.Sprintf("level=warning msg=\"For syslog testing: Warning with test1 module: "+
		"%d\" module=test1", testrand)
	mt.AssertTrue(t, strings.Index(string(output), checkString) >= 0)
	// Should not show.
	checkString = fmt.Sprintf("level=debug msg=\"For syslog testing: Debug with test1 module: "+
		"%d\" module=test1", testrand)
	mt.AssertTrue(t, strings.Index(string(output), checkString) < 0)

	cleanupLogging(t)

	// Get rid of the syslog hook again.
	Log = New()
}
Exemplo n.º 2
0
Arquivo: log_test.go Projeto: kacf/log
func verifyLogging(t *testing.T, checkString string) {
	fd, err := os.Open("output.log")
	mt.AssertTrue(t, err == nil)
	var buf [4096]byte
	n, err := fd.Read(buf[:])
	mt.AssertTrue(t, err == nil)
	mt.AssertTrue(t, n < 4096)

	mt.AssertStringEqual(t, string(buf[0:n]), checkString)
}
Exemplo n.º 3
0
func TestTooMuchPopping(t *testing.T) {
	var s ScopeStack
	s.Push("1")
	mt.AssertTrue(t, s.Pop().(string) == "1")
	defer func() {
		if recover() == nil {
			t.Fatal("Unbalanced Pop() did not panic.")
		}
	}()
	mt.AssertTrue(t, s.Pop().(string) == "should never happen")
	t.Fatal("Should never get here")
}
Exemplo n.º 4
0
func TestRootfsUpdateShrunkPartition(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Now try to shrink one partition, it should now fail.

	file := baseMountDevice + "3"
	part_fd, err := os.Create(file)
	if err != nil {
		t.Fatalf("Could not open '%s': %s", file, err.Error())
	}
	err = part_fd.Truncate(2048)
	if err != nil {
		t.Fatalf("Could not open '%s': %s", file, err.Error())
	}

	newRunner := &testRunnerMulti{}

	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"fw_setenv upgrade_available 1",
		"fw_setenv boot_part 3",
		"fw_setenv bootcount 0")

	mount_output :=
		baseMountDevice + "2 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	newRunner.outputs = []string{
		mount_output,
		"boot_part=2",
		mount_output,
		"boot_part=2",
		"",
		"",
		""}

	newRunner.ret_codes = []int{
		0,
		0,
		0,
		0,
		0,
		0,
		0}

	runner = newRunner
	if err := doMain([]string{"-rootfs", dummy}); err == nil {
		t.Fatal("Updating image should have failed " +
			"(partition too small)")
	}
	mt.AssertTrue(t, !checkFileOverlapEqual(t, baseMountDevice+"3", dummy))
}
Exemplo n.º 5
0
Arquivo: log_test.go Projeto: kacf/log
func setupLogging(t *testing.T) {
	fd, err := os.Create("output.log")
	mt.AssertTrue(t, err == nil)
	SetOutput(fd)

	// How you run the test influences whether the output is connected to a
	// terminal or not (-v gives you a terminal, otherwise not). So disable
	// that moving target for this test.
	Log.Formatter.(*logrus.TextFormatter).DisableColors = true

	// Also disable timestamps, which are not predictable.
	Log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true

	SetLevel(logrus.DebugLevel)
}
Exemplo n.º 6
0
// Test various ways to upgrade using a file. See each block for comments about
// each section.
func TestRootfsUpdate(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Try to execute rootfs operation with the dummy file.
	newRunner := &testRunnerMulti{}
	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"fw_setenv upgrade_available 1",
		"fw_setenv boot_part 3",
		"fw_setenv bootcount 0")

	mount_output :=
		baseMountDevice + "2 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	newRunner.outputs = []string{
		mount_output,
		"boot_part=2",
		mount_output,
		"boot_part=2",
		"",
		"",
		""}

	newRunner.ret_codes = []int{
		0,
		0,
		0,
		0,
		0,
		0,
		0}

	runner = newRunner
	if err := doMain([]string{"-rootfs", dummy}); err != nil {
		t.Fatalf("Updating image failed: %s", err.Error())
	}
	mt.AssertTrue(t, checkFileOverlapEqual(t, baseMountDevice+"3", dummy))
}
Exemplo n.º 7
0
func TestRootfsUpdateUBootDisagreement(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Try to query active partition again, when U-Boot and mount don't
	// agree.

	newRunner := &testRunnerMulti{}

	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part")

	mount_output :=
		baseMountDevice + "2 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	newRunner.outputs = []string{
		mount_output,
		"boot_part=3"}

	newRunner.ret_codes = []int{
		0,
		0}

	runner = newRunner
	err := doMain([]string{"-rootfs", dummy})
	if err == nil {
		t.Fatal("Updating image should have failed " +
			"(mount and U-Boot don't agree on boot " +
			"partition)")
	}
	mt.AssertTrue(t, !checkFileOverlapEqual(t, baseMountDevice+"3", dummy))
	mt.AssertErrorSubstring(t, err,
		"agree")
}
Exemplo n.º 8
0
func TestLoggingOptions(t *testing.T) {
	if err := doMain([]string{"-commit", "-log-level", "crap"}); err == nil {
		t.Fatal("'crap' log level should have given error")
	} else {
		// Should have a reference to log level.
		mt.AssertErrorSubstring(t, err, "Level")
	}

	if err := doMain([]string{"-info", "-log-level", "debug"}); err == nil {
		t.Fatal("Incompatible log levels should have given error")
	} else {
		mt.AssertErrorSubstring(t, err,
			errMsgIncompatibleLogOptions.Error())
	}

	var buf bytes.Buffer
	oldOutput := log.Log.Out
	log.SetOutput(&buf)
	defer log.SetOutput(oldOutput)

	// Ignore errors for now, we just want to know if the logging level was
	// applied.
	log.SetLevel(log.DebugLevel)
	doMain([]string{"-log-level", "panic"})
	log.Debugln("Should not show")
	doMain([]string{"-debug"})
	log.Debugln("Should show")
	doMain([]string{"-info"})
	log.Debugln("Should also not show")

	mt.AssertTrue(t, strings.Index(buf.String(), "Should show") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(), "Should not show") < 0)
	mt.AssertTrue(t, strings.Index(buf.String(), "Should also not show") < 0)

	doMain([]string{"-log-modules", "main_test,MyModule"})
	log.Errorln("Module filter should show main_test")
	log.PushModule("MyModule")
	log.Errorln("Module filter should show MyModule")
	log.PushModule("MyOtherModule")
	log.Errorln("Module filter should not show MyOtherModule")
	log.PopModule()
	log.PopModule()

	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should show main_test") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should show MyModule") >= 0)
	mt.AssertTrue(t, strings.Index(buf.String(),
		"Module filter should not show MyOtherModule") < 0)

	doMain([]string{"-log-file", "test.log"})
	log.Errorln("Should be in log file")
	fd, err := os.Open("test.log")
	mt.AssertTrue(t, err == nil)

	var bytebuf [4096]byte
	n, err := fd.Read(bytebuf[:])
	mt.AssertTrue(t, err == nil)
	mt.AssertTrue(t, strings.Index(string(bytebuf[0:n]),
		"Should be in log file") >= 0)

	err = doMain([]string{"-no-syslog"})
	// Just check that the flag can be specified.
	mt.AssertTrue(t, err != nil)
	mt.AssertTrue(t, strings.Index(err.Error(), "syslog") < 0)
}
Exemplo n.º 9
0
func executeNetworkTest(t *testing.T, mode int) {
	imageFd, err := os.OpenFile(dummy, os.O_WRONLY|mode, 0777)
	mt.AssertNoError(t, err)

	const imageString string = "CORRECT UPDATE"
	n, err := imageFd.Write([]byte(imageString))
	mt.AssertNoError(t, err)
	mt.AssertTrue(t, n == len(imageString))
	imageFd.Close()

	newRunner := &testRunnerMulti{}
	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"fw_setenv upgrade_available 1",
		"fw_setenv boot_part 3",
		"fw_setenv bootcount 0")

	mount_output :=
		baseMountDevice + "2 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	newRunner.outputs = []string{
		mount_output,
		"boot_part=2",
		mount_output,
		"boot_part=2",
		"",
		"",
		""}

	newRunner.ret_codes = []int{
		0,
		0,
		0,
		0,
		0,
		0,
		0}

	runner = newRunner
	httpString := fmt.Sprintf("http://localhost:%s/%s", testPortString,
		dummy)
	err = doMain([]string{"-rootfs", httpString})
	if err != nil {
		if mode == os.O_TRUNC {
			// This update should fail.
			mt.AssertErrorSubstring(t, err, "Less than")
			return
		}
		t.Fatalf("Updating image failed: %s", err.Error())
	} else {
		if mode == os.O_TRUNC {
			t.Fatal("Update should have failed")
		}
	}
	mt.AssertTrue(t, checkFileOverlapEqual(t, baseMountDevice+"3", dummy))

	fd, err := os.Open(baseMountDevice + "3")
	mt.AssertNoError(t, err)
	buf := new([len(imageString)]byte)
	n, err = fd.Read(buf[:])
	mt.AssertNoError(t, err)
	mt.AssertTrue(t, n == len(imageString))
	mt.AssertStringEqual(t, string(buf[:]), imageString)

	fd.Close()
}
Exemplo n.º 10
0
func TestPartitionsAPI(t *testing.T) {
	prepareMockDevices(t)
	defer cleanupMockDevices()

	// Test various parts of the partitions API.

	newRunner := &testRunnerMulti{}

	newRunner.cmdlines = StringPointerList(
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part",
		"mount ",
		"fw_printenv boot_part")

	mount_output3 :=
		baseMountDevice + "3 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	mount_output4 :=
		baseMountDevice + "4 on / type ext4 (rw)\n" +
			"proc on /proc type proc (rw,noexec,nosuid,nodev)\n" +
			baseMountDevice + "1 on /boot type ext4 (rw)\n"
	newRunner.outputs = []string{
		mount_output3,
		"boot_part=3",
		mount_output3,
		"boot_part=3",
		mount_output4,
		"boot_part=4",
		mount_output4,
		"boot_part=3",
		mount_output4,
		"boot_part=4",
		mount_output4,
		"boot_part=3"}

	newRunner.ret_codes = []int{
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0}

	runner = newRunner

	statter.(*testStatter).active = "3"
	defer func() { statter.(*testStatter).active = "2" }()

	part, err := getActivePartition()
	mt.AssertTrue(t, err == nil)
	mt.AssertStringEqual(t, part, baseMountDevice+"3")

	part, err = getInactivePartition()
	mt.AssertTrue(t, err == nil)
	mt.AssertStringEqual(t, part, baseMountDevice+"2")

	statter.(*testStatter).active = "4"

	part, err = getActivePartition()
	mt.AssertTrue(t, err != nil)

	part, err = getActivePartition()
	mt.AssertTrue(t, err != nil)

	part, err = getInactivePartition()
	mt.AssertTrue(t, err != nil)

	part, err = getInactivePartition()
	mt.AssertTrue(t, err != nil)

	rootStat, err := statter.Stat("/")
	if err != nil {
		t.Fatal("Should never happen")
	}
	root := rootStat.Sys().(*syscall.Stat_t)
	mt.AssertTrue(t, isMountedRoot("no-such-file", root) == false)
}
Exemplo n.º 11
0
func TestPushPop(t *testing.T) {
	var s ScopeStack
	s.Push("1")
	mt.AssertTrue(t, s.Pop().(string) == "1")
}