Exemple #1
0
func cpDir(t *testing.T, src string, dst string) {
	assert.NoError(t, os.MkdirAll(dst, 0755))

	assert.NoError(t, filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
		assert.NoError(t, err)
		name, fErr := filepath.Rel(src, path)
		assert.NoError(t, fErr)

		dstPath := filepath.Join(dst, name)

		if info.IsDir() {
			assert.NoError(t, os.MkdirAll(dstPath, info.Mode()))
		} else if info.Mode()&os.ModeSymlink > 0 {
			dest, fErr := os.Readlink(path)
			assert.NoError(t, fErr)

			assert.NoError(t, os.Symlink(dest, dstPath))
		} else if info.Mode().IsRegular() {
			df, fErr := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY, info.Mode())
			assert.NoError(t, fErr)
			defer df.Close()

			sf, fErr := os.Open(path)
			assert.NoError(t, fErr)
			defer sf.Close()

			_, fErr = io.Copy(df, sf)
			assert.NoError(t, fErr)
		} else {
			return fmt.Errorf("not regular, not symlink, not dir, what is it? %s", path)
		}

		return nil
	}))
}
Exemple #2
0
func Test_CopyContainer(t *testing.T) {
	mainDir, err := ioutil.TempDir("", "copycontainer")
	assert.NoError(t, err)
	defer os.RemoveAll(mainDir)

	src := path.Join(mainDir, "src")
	dst := path.Join(mainDir, "dst")
	makeTestDir(t, src, testDirSettings{
		seed: 0x91738,
		entries: []testDirEntry{
			{path: "subdir/file-1", seed: 0x1},
			{path: "file-1", seed: 0x2},
			{path: "file-2", seed: 0x3},
		},
	})

	container, err := tlc.WalkAny(src, nil)
	assert.NoError(t, err)

	inPool := fspool.New(container, src)
	outPool := fspool.New(container, dst)

	err = CopyContainer(container, outPool, inPool, &state.Consumer{})
	assert.NoError(t, err)

	assert.NoError(t, inPool.Close())
}
Exemple #3
0
func Test_OpenRemoteDownloadBuild(t *testing.T) {
	fakeData := []byte("aaaabbbb")

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("content-length", fmt.Sprintf("%d", len(fakeData)))
		w.WriteHeader(200)
		w.Write(fakeData)
	}))
	defer server.CloseClientConnections()

	tfs := &testfs{server.URL}
	assert.NoError(t, RegisterHandler(tfs))
	assert.Error(t, RegisterHandler(tfs))
	defer DeregisterHandler(tfs)

	f, err := Open("nofs:///not-quite")
	assert.Error(t, err)

	f, err = Open("testfs:///now/we/are/talking")
	assert.NoError(t, err)

	stats, err := f.Stat()
	assert.NoError(t, err)

	assert.EqualValues(t, len(fakeData), stats.Size())
	assert.NoError(t, f.Close())
}
Exemple #4
0
func TestParseTCPAddrList(t *testing.T) {
	p := parserMixin{}
	_ = p.TCPList()
	err := p.value.Set("127.0.0.1:1234")
	assert.NoError(t, err)
	err = p.value.Set("127.0.0.1:1235")
	assert.NoError(t, err)
	assert.Equal(t, "127.0.0.1:1234,127.0.0.1:1235", p.value.String())
}
Exemple #5
0
func TestParseTCPAddr(t *testing.T) {
	p := parserMixin{}
	v := p.TCP()
	err := p.value.Set("127.0.0.1:1234")
	assert.NoError(t, err)
	expected, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234")
	assert.NoError(t, err)
	assert.Equal(t, *expected, **v)
}
Exemple #6
0
func Test_Writer(t *testing.T) {
	dropSize := 16

	numShort := 0
	shortSize := 0
	validate := func(buf []byte) error {
		switch {
		case len(buf) == dropSize:
			if numShort > 0 {
				return fmt.Errorf("got full after short")
			}
			return nil
		case len(buf) < dropSize:
			if numShort > 0 {
				return fmt.Errorf("got second short (%d)", len(buf))
			}
			numShort++
			shortSize = len(buf)
		default:
			return fmt.Errorf("drop too large (%d > %d)", len(buf), dropSize)
		}

		return nil
	}

	buf := make([]byte, dropSize)
	countingWriter := counter.NewWriter(nil)

	dw := &Writer{
		Buffer:   buf,
		Validate: validate,
		Writer:   countingWriter,
	}

	rbuf := make([]byte, 128)

	write := func(l int) {
		written, wErr := dw.Write(rbuf[0:l])
		assert.Equal(t, l, written)
		assert.NoError(t, wErr)
	}

	write(12)
	write(4)
	write(10)
	write(6)
	write(16)
	write(64)
	write(5)

	assert.NoError(t, dw.Close())
	assert.Equal(t, 5, shortSize)
	assert.Equal(t, int64(12+4+10+6+16+64+5), countingWriter.Count())
}
Exemple #7
0
func TestGetFlagAndOverrideDefault(t *testing.T) {
	app := newTestApp()
	a := app.Flag("a", "").Default("default").String()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "default", *a)
	app.GetFlag("a").Default("new")
	_, err = app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "new", *a)
}
Exemple #8
0
func Test_FanOut(t *testing.T) {
	t.Logf("Testing fail fast...")

	ts := &TestSink{
		FailingBlock: BlockLocation{
			FileIndex:  2,
			BlockIndex: 2,
		},
	}
	fos, err := NewFanOutSink(ts, 8)
	assert.NoError(t, err)

	fos.Start()

	hadError := false

	for i := 0; i < 8; i++ {
		for j := 0; j < 8; j++ {
			loc := BlockLocation{
				FileIndex:  int64(i),
				BlockIndex: int64(j),
			}
			sErr := fos.Store(loc, []byte{})
			if sErr != nil {
				hadError = true
			}
		}
	}

	assert.True(t, hadError)

	err = fos.Close()
	assert.NoError(t, err)

	t.Logf("Testing tail errors...")

	fos, err = NewFanOutSink(ts, 8)
	assert.NoError(t, err)

	fos.Start()

	// Store shouldn't err, just queue it...
	err = fos.Store(ts.FailingBlock, []byte{})
	assert.NoError(t, err)

	// but close should catch the error
	err = fos.Close()
	assert.NotNil(t, err)
}
Exemple #9
0
func TestNestedCommandsWithFlags(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Flag("aaa", "").Short('a').String()
	b := cmd.Flag("bbb", "").Short('b').String()
	err := app.init()
	assert.NoError(t, err)
	context := tokenize(strings.Split("a b --aaa x -b x", " "), false)
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "x", *a)
	assert.Equal(t, "x", *b)
}
Exemple #10
0
func TestCommander(t *testing.T) {
	c := newTestApp()
	ping := c.Command("ping", "Ping an IP address.")
	pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration()

	selected, err := c.Parse([]string{"ping"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 5*time.Second, *pingTTL)

	selected, err = c.Parse([]string{"ping", "--ttl=10s"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 10*time.Second, *pingTTL)
}
Exemple #11
0
func cpFile(t *testing.T, src string, dst string) {
	sf, fErr := os.Open(src)
	assert.NoError(t, fErr)
	defer sf.Close()

	info, fErr := sf.Stat()
	assert.NoError(t, fErr)

	df, fErr := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, info.Mode())
	assert.NoError(t, fErr)
	defer df.Close()

	_, fErr = io.Copy(df, sf)
	assert.NoError(t, fErr)
}
Exemple #12
0
func TestTooManyArgsAfterCommand(t *testing.T) {
	a := newTestApp()
	a.Command("a", "")
	assert.NoError(t, a.init())
	_, err := a.Parse([]string{"a", "b"})
	assert.Error(t, err)
}
Exemple #13
0
func TestParserExpandFromFile(t *testing.T) {
	f, err := ioutil.TempFile("", "")
	assert.NoError(t, err)
	defer os.Remove(f.Name())
	f.WriteString("hello\nworld\n")
	f.Close()

	app := New("test", "")
	arg0 := app.Arg("arg0", "").String()
	arg1 := app.Arg("arg1", "").String()

	_, err = app.Parse([]string{"@" + f.Name()})
	assert.NoError(t, err)
	assert.Equal(t, "hello", *arg0)
	assert.Equal(t, "world", *arg1)
}
Exemple #14
0
func TestFloat32(t *testing.T) {
	p := parserMixin{}
	v := p.Float32()
	err := p.value.Set("123.45")
	assert.NoError(t, err)
	assert.InEpsilon(t, 123.45, *v, 0.001)
}
Exemple #15
0
func TestCounter(t *testing.T) {
	app := New("", "")
	c := app.Flag("f", "").Counter()
	_, err := app.Parse([]string{"--f", "--f", "--f"})
	assert.NoError(t, err)
	assert.Equal(t, 3, *c)
}
Exemple #16
0
func TestDoOnComplete(t *testing.T) {
	complete := false
	a, err := EmptyInt().DoOnComplete(func() { complete = true }).ToArrayWithError()
	assert.NoError(t, err)
	assert.Equal(t, []int{}, a)
	assert.True(t, complete)
}
Exemple #17
0
func TestToOneWithError(t *testing.T) {
	_, err := FromInts(1, 2).ToOneWithError()
	assert.Error(t, err)
	value, err := FromInts(3).ToOneWithError()
	assert.NoError(t, err)
	assert.Equal(t, 3, value)
}
Exemple #18
0
func TesArgtMultipleValuesDefault(t *testing.T) {
	app := New("test", "")
	a := app.Arg("a", "").Default("default1", "default2").Strings()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, []string{"default1", "default2"}, *a)
}
Exemple #19
0
func TestCombinedShortFlagArg(t *testing.T) {
	a := newTestApp()
	n := a.Flag("short", "").Short('s').Int()
	_, err := a.Parse([]string{"-s10"})
	assert.NoError(t, err)
	assert.Equal(t, 10, *n)
}
Exemple #20
0
func TestShortFlag(t *testing.T) {
	app := newTestApp()
	f := app.Flag("long", "").Short('s').Bool()
	_, err := app.Parse([]string{"-s"})
	assert.NoError(t, err)
	assert.True(t, *f)
}
Exemple #21
0
func TestHexBytes(t *testing.T) {
	app := newTestApp()
	actual := app.Arg("bytes", "").HexBytes()
	_, err := app.Parse([]string{"01020aff"})
	assert.NoError(t, err)
	assert.Equal(t, []byte{0x01, 0x02, 0x0a, 0xff}, *actual)
}
Exemple #22
0
func TestBool(t *testing.T) {
	app := newTestApp()
	b := app.Flag("b", "").Bool()
	_, err := app.Parse([]string{"--b"})
	assert.NoError(t, err)
	assert.True(t, *b)
}
Exemple #23
0
func TestFlagMultipleValuesDefault(t *testing.T) {
	app := newTestApp()
	a := app.Flag("a", "").Default("default1", "default2").Strings()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, []string{"default1", "default2"}, *a)
}
Exemple #24
0
func TestTopLevelArgWorks(t *testing.T) {
	c := newTestApp()
	s := c.Arg("arg", "help").String()
	_, err := c.Parse([]string{"foo"})
	assert.NoError(t, err)
	assert.Equal(t, "foo", *s)
}
Exemple #25
0
func TestFlagMultipleValuesDefaultEnvarNonRepeatable(t *testing.T) {
	c := newTestApp()
	a := c.Flag("foo", "foo").Envar("TEST_MULTIPLE_VALUES_NON_REPEATABLE").String()
	os.Setenv("TEST_MULTIPLE_VALUES_NON_REPEATABLE", "123\n456")
	_, err := c.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "123\n456", *a)
}
Exemple #26
0
func TestFlagMultipleValuesDefaultEnvarWindows(t *testing.T) {
	app := newTestApp()
	a := app.Flag("a", "").Envar("TEST_MULTIPLE_VALUES").Strings()
	os.Setenv("TEST_MULTIPLE_VALUES", "123\r\n456\r\n")
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, []string{"123", "456"}, *a)
}
Exemple #27
0
func TestEnvarOverrideDefault(t *testing.T) {
	os.Setenv("TEST_ENVAR", "123")
	app := newTestApp()
	flag := app.Flag("t", "").Default("default").Envar("TEST_ENVAR").String()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "123", *flag)
}
Exemple #28
0
func TestIPv6Addr(t *testing.T) {
	app := newTestApp()
	flag := app.Flag("addr", "").ResolvedIP()
	_, err := app.Parse([]string{"--addr", net.IPv6interfacelocalallnodes.String()})
	assert.NoError(t, err)
	assert.NotNil(t, *flag)
	assert.Equal(t, net.IPv6interfacelocalallnodes, *flag)
}
Exemple #29
0
func TestRequiredWithEnvar(t *testing.T) {
	os.Setenv("TEST_ENVAR", "123")
	app := newTestApp()
	flag := app.Flag("t", "").Envar("TEST_ENVAR").Required().Int()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, 123, *flag)
}
Exemple #30
0
func TestSelectedCommand(t *testing.T) {
	app := newTestApp()
	c0 := app.Command("c0", "")
	c0.Command("c1", "")
	s, err := app.Parse([]string{"c0", "c1"})
	assert.NoError(t, err)
	assert.Equal(t, "c0 c1", s)
}