예제 #1
0
func TestSubtract(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)
	alloc := NewAllocator(ipr)
	tt.TestEqual(t, alloc.remaining, int64(10))

	// create a smaller range within the same one
	ipr2, err := ParseIPRange("192.168.1.10-14")
	tt.TestExpectSuccess(t, err)

	// subtract it
	alloc.Subtract(ipr2)

	// validate it
	tt.TestEqual(t, alloc.remaining, int64(5))
	tt.TestEqual(t, len(alloc.reserved), 5)

	// consume everything and ensure we don't get an IP in the second range.
	for {
		if alloc.Remaining() == 0 {
			break
		}

		ip := alloc.Allocate()
		tt.TestNotEqual(t, ip, nil)
		tt.TestEqual(t, ipr2.Contains(ip), false)
	}
}
예제 #2
0
파일: parser_test.go 프로젝트: wallyqs/util
func TestReadInt64(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	f := tt.WriteTempFile(t, "foo\nbar")

	_, err := ReadInt64(f)
	tt.TestExpectError(t, err)

	f = tt.WriteTempFile(t, "123\n456")

	v, err := ReadInt64(f)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, v, int64(123))

	f = tt.WriteTempFile(t, "123456789")
	v, err = ReadInt64(f)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, v, int64(123456789))

	maxInt64 := fmt.Sprintf("%d", int64(1<<63-1))
	f = tt.WriteTempFile(t, maxInt64)

	v, err = ReadInt64(f)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, v, int64(1<<63-1))

	maxInt64WithExtra := fmt.Sprintf("%d666", int64(1<<63-1))
	f = tt.WriteTempFile(t, maxInt64WithExtra)

	v, err = ReadInt64(f)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, v, int64(1<<63-1))
}
예제 #3
0
func TestNewRequest(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	method, path, body := "", "", ""
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		method = req.Method
		path = req.URL.Path

		defer req.Body.Close()
		b, err := ioutil.ReadAll(req.Body)
		if err != nil {
			t.Errorf("Error reading request: %v", err)
			w.WriteHeader(500)
			return
		}
		body = string(b)
		w.WriteHeader(200)
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewRequest("POST", "/blobs", "text/plain", strings.NewReader("I am a giant blob of bytes!"))
	err = client.Result(req, nil)
	tt.TestExpectSuccess(t, err)

	// Verify request as received by server
	tt.TestEqual(t, method, "POST")
	tt.TestEqual(t, path, "/blobs")
	tt.TestEqual(t, body, "I am a giant blob of bytes!")
}
예제 #4
0
func TestRelease(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)
	alloc := NewAllocator(ipr)

	// test releasing when empty
	alloc.Release(net.ParseIP("192.168.1.11"))
	tt.TestEqual(t, alloc.remaining, int64(10))

	// consume everything
	for {
		if alloc.Remaining() == 0 {
			break
		}
		ip := alloc.Allocate()
		tt.TestNotEqual(t, ip, nil)
	}

	// release an IP
	tt.TestEqual(t, alloc.remaining, int64(0))
	alloc.Release(net.ParseIP("192.168.1.11"))
	tt.TestEqual(t, alloc.remaining, int64(1))

	// allocate one more and should get that one
	tt.TestEqual(t, alloc.Allocate().String(), "192.168.1.11")
	tt.TestEqual(t, alloc.remaining, int64(0))
}
예제 #5
0
func TestErrorResponseNoBody(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(500)
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewFormRequest("GET", "/", nil)
	resp, err := client.Do(req)
	tt.TestExpectError(t, err)

	rerr, ok := err.(*RestError)
	tt.TestEqual(t, ok, true, "Error should be of type *RestError")
	tt.TestEqual(t, rerr.Error(), "error in response: 500 Internal Server Error")

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, string(body), "")
}
예제 #6
0
func TestErrorResult(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(500)
		io.WriteString(w, "Didn't work")
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewFormRequest("GET", "/", nil)
	err = client.Result(req, nil)
	tt.TestExpectError(t, err)

	rerr, ok := err.(*RestError)
	tt.TestEqual(t, ok, true, "Error should be of type *RestError")
	tt.TestEqual(t, rerr.Error(), "error in response: 500 Internal Server Error - Didn't work")
	tt.TestEqual(t, rerr.Body(), "Didn't work")

	rerr2 := new(RestError)
	rerr2.err = fmt.Errorf("foo bar baz wibble")
	tt.TestEqual(t, rerr2.Error(), "foo bar baz wibble")

	rerr2 = new(RestError)
	rerr2.Resp = &http.Response{
		StatusCode: 404,
	}
	rerr2.err = fmt.Errorf("foo bar baz wibble")
	tt.TestEqual(t, rerr2.Error(), "foo bar baz wibble")
}
예제 #7
0
파일: merge_test.go 프로젝트: wallyqs/util
func TestDeepMergeChildrenPropagateToSource(t *testing.T) {
	dst := make(map[string]interface{})
	src1 := map[string]interface{}{
		"foobar": map[string]interface{}{
			"one":   float64(1),
			"three": float64(3),
		},
	}
	src2 := map[string]interface{}{
		"foobar": map[string]interface{}{
			"two":  "2",
			"four": float64(4),
		},
	}
	expected := map[string]interface{}{
		"foobar": map[string]interface{}{
			"one":   float64(1),
			"two":   "2",
			"three": float64(3),
			"four":  float64(4),
		},
	}
	expectedSrc1 := map[string]interface{}{
		"foobar": map[string]interface{}{
			"one":   float64(1),
			"three": float64(3),
		},
	}
	tt.TestExpectSuccess(t, Merge(dst, src1))
	tt.TestExpectSuccess(t, Merge(dst, src2))
	tt.TestEqual(t, dst, expected)
	tt.TestEqual(t, src1, expectedSrc1)
}
예제 #8
0
func TestNewAllocator(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)

	alloc := NewAllocator(ipr)
	tt.TestEqual(t, alloc.size, int64(10))
	tt.TestEqual(t, alloc.remaining, int64(10))
}
예제 #9
0
func TestReserveOutOfRange(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)
	alloc := NewAllocator(ipr)

	// reserve an IP
	reservedIP := net.ParseIP("10.0.0.1")
	alloc.Reserve(reservedIP)
	tt.TestEqual(t, alloc.remaining, int64(10))
	tt.TestEqual(t, len(alloc.reserved), 0)
}
예제 #10
0
func TestHelper_newRequestWithParams(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	client, err := New("http://example.com/resources")
	tt.TestExpectSuccess(t, err)
	req := client.newRequest(GET, "/excellence?yeah=stuff&more=params")

	tt.TestEqual(t, req.Method, GET)
	tt.TestEqual(t, req.URL.String(), "http://example.com/resources/excellence?yeah=stuff&more=params")
	tt.TestEqual(t, req.Headers, http.Header(map[string][]string{}))
}
예제 #11
0
func TestGetImageTags(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	img, statusCode, err := GetImage("foo/bar", "")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, statusCode, 200)

	tags := img.Tags()
	sort.Strings(tags)
	tt.TestEqual(t, tags, []string{"base", "latest"})
}
예제 #12
0
파일: merge_test.go 프로젝트: wallyqs/util
func TestDeepMergeHandlesNilDestination(t *testing.T) {
	var dst map[string]interface{}
	src := map[string]interface{}{
		"two":  "2",
		"four": float64(4),
	}
	err := Merge(dst, src)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err, NilDestinationError)
	dst = make(map[string]interface{})
	tt.TestExpectSuccess(t, Merge(dst, src))
	tt.TestEqual(t, dst, src)
}
예제 #13
0
func TestAllocate(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)
	alloc := NewAllocator(ipr)

	// get the first one
	ip := alloc.Allocate()
	tt.TestEqual(t, ipr.Contains(ip), true)
	tt.TestEqual(t, alloc.Remaining(), int64(9))

	// consume the others
	for i := 0; i < 8; i++ {
		ip = alloc.Allocate()
		tt.TestEqual(t, ipr.Contains(ip), true, fmt.Sprintf("%s was not within the range", ip.String()))
	}
	tt.TestEqual(t, alloc.Remaining(), int64(1))

	// last ip
	ip = alloc.Allocate()
	tt.TestEqual(t, ipr.Contains(ip), true)
	tt.TestEqual(t, alloc.Remaining(), int64(0))

	// no more left
	ip = alloc.Allocate()
	tt.TestEqual(t, ip, nil)
	tt.TestEqual(t, alloc.Remaining(), int64(0))
}
예제 #14
0
func TestGetImageMetadata(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	img, statusCode, err := GetImage("foo/bar", "")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, statusCode, 200)

	var m1 map[string]interface{}
	err = img.Metadata("tag2", &m1)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "can't find tag 'tag2' for image 'foo/bar'")

	var m2 map[string]interface{}
	err = img.Metadata("latest", &m2)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, len(m2), 2)
	tt.TestEqual(t, m2["id"], "deadbeef")
	tt.TestEqual(t, m2["k2"], "v2")

	var m3 map[string]interface{}
	err = img.Metadata("base", &m3)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, len(m3), 2)
	tt.TestEqual(t, m3["id"], "badcafe")
	tt.TestEqual(t, m3["k1"], "v1")
}
예제 #15
0
func TestEmptyPostRequest(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	body := ""
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		defer req.Body.Close()
		b, err := ioutil.ReadAll(req.Body)
		if err != nil {
			t.Errorf("Error reading request: %v", err)
			w.WriteHeader(500)
			return
		}
		body = string(b)
		w.WriteHeader(200)
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewJsonRequest("POST", "/", nil)

	err = client.Result(req, nil)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, body, "")
}
예제 #16
0
func TestFormRequest(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	var form url.Values
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if err := req.ParseForm(); err != nil {
			t.Errorf("Error reading request: %v", err)
			w.WriteHeader(500)
			return
		}
		form = req.Form
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(200)
		io.WriteString(w, `{"foo":"bar"}`)
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewFormRequest("POST", "/", map[string]string{"name": "Tim"})
	err = client.Result(req, nil)
	tt.TestExpectSuccess(t, err)

	// Verify form data as received by server
	tt.TestEqual(t, form.Get("name"), "Tim")
}
예제 #17
0
func TestClient_Exec(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	socketFile, l := createSocketServer(t)
	defer l.Close()

	var execContent string
	readChan := setupReadRequest(t, l, &execContent, "REQUEST OK\n")

	client := New(socketFile)
	err := client.Exec(
		[]string{"/sbin/init", "foo"}, []string{"FOO=bar"}, "/a", "/b", time.Second,
	)
	tt.TestExpectSuccess(t, err)

	select {
	case <-readChan:
	case <-time.After(time.Second):
		tt.Fatalf(t, "Expected to have read client response within 1 second")
	}

	expectedRequest := "1\n4\n1\n4\nEXEC2\n10\n/sbin/init3\nfoo1\n7\nFOO=bar2\n2\n/a2\n/b"
	tt.TestEqual(t, execContent, expectedRequest)
}
예제 #18
0
파일: envmap_test.go 프로젝트: wallyqs/util
func TestEnvMapGetUnflattened(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	root := NewEnvMap()
	root.Set("VARIABLE", "foo bar $STR")
	root.Set("VAR1", "abc 123 $VAR2")
	root.Set("VAR2", "xyz")

	root.FlattenMap(false)
	envMap := root.Map()

	tt.TestEqual(t, envMap["VAR1"], "abc 123 $VAR2")
	tt.TestEqual(t, envMap["VAR2"], "xyz")
	tt.TestEqual(t, envMap["VARIABLE"], "foo bar $STR")
}
예제 #19
0
func TestClient_Start(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	socketFile, l := createSocketServer(t)
	defer l.Close()

	var startContent string
	readChan := setupReadRequest(t, l, &startContent, "REQUEST OK\n")

	client := New(socketFile)
	err := client.Start(
		"echo", []string{"123"}, "dir", []string{"FOO=bar"},
		"/a", "/b", "123", "456", time.Second,
	)
	tt.TestExpectSuccess(t, err)

	select {
	case <-readChan:
	case <-time.After(time.Second):
		tt.Fatalf(t, "Expected to have read client response within 1 second")
	}

	expectedRequest := "1\n6\n2\n5\nSTART4\necho1\n3\n1231\n3\ndir1\n7\nFOO=bar2\n2\n/a2\n/b2\n3\n1233\n456"
	tt.TestEqual(t, startContent, expectedRequest)
}
예제 #20
0
파일: hmac_test.go 프로젝트: wallyqs/util
// Make sure that HMAC Sha1 is calculated correctly
func TestComputeHmacSha1(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	hmacSha1 := ComputeHmacSha1("message", "secret")
	tt.TestEqual(t, hmacSha1, "DK9kn+7klT2Hv5A6wRdsReAo3xY=")
}
예제 #21
0
func TestClient_Stop(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// To effectively test stop, we also want to ensure a request is inflight and
	// that it makes that request return.

	socketFile, l := createSocketServer(t)
	defer l.Close()

	setupRequestThatNeverReturns(t, l)

	stopReadyCh := make(chan bool)
	waitDoneCh := make(chan bool)

	client := New(socketFile)

	go func() {
		close(stopReadyCh)
		defer close(waitDoneCh)
		err := client.Wait(time.Minute)
		if err == nil {
			t.Errorf("wait should have returned an error about the client stopping")
			return
		}
		if err.Error() != "client is stopped." {
			t.Errorf("client should have returned an error that the client was stopping, but got: %v", err)
		}
	}()

	select {
	case <-stopReadyCh:
	case <-time.After(5 * time.Second):
		tt.Fatalf(t, "the wait goroutine wasn't setup within 5 seconds")
	}

	tt.TestEqual(t, client.Stopped(), false)
	client.Stop()
	tt.TestEqual(t, client.Stopped(), true)

	select {
	case <-waitDoneCh:
	case <-time.After(5 * time.Second):
		tt.Fatalf(t, "the wait goroutine should have unblocked within 5 seconds")
	}
}
예제 #22
0
func TestIPRangeOverlap(t *testing.T) {
	ipr1, err := ParseIPRange("192.168.1.1-100")
	tt.TestExpectSuccess(t, err)
	ipr2, err := ParseIPRange("192.168.1.101-200")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), false)

	ipr2, err = ParseIPRange("192.168.1.100-200")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), true)

	ipr2, err = ParseIPRange("192.168.1.50-55")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), true)

	ipr2, err = ParseIPRange("192.168.1.1")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), true)

	ipr1, err = ParseIPRange("192.168.1.200-210")
	tt.TestExpectSuccess(t, err)
	ipr2, err = ParseIPRange("192.168.1.100-150")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), false)

	ipr2, err = ParseIPRange("192.168.1.100-250")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), true)

	ipr1, err = ParseIPRange("192.168.1.1-255/24")
	tt.TestExpectSuccess(t, err)
	ipr2, err = ParseIPRange("192.168.0.1-3.255/22")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr1.Overlaps(ipr2), true)
}
예제 #23
0
func TestGetImageHistory(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	img, statusCode, err := GetImage("foo/bar", "")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, statusCode, 200)

	h, err := img.History("tag2")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "can't find tag 'tag2' for image 'foo/bar'")

	h, err = img.History("latest")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, len(h), 2)
	tt.TestEqual(t, h[0], "deadbeef")
	tt.TestEqual(t, h[1], "badcafe")
}
예제 #24
0
파일: merge_test.go 프로젝트: wallyqs/util
func TestDeepMergeRecursiveMap(t *testing.T) {
	dst := map[string]interface{}{
		"settings": map[string]interface{}{
			"internal": map[string]interface{}{
				"foo": "bar",
				"baz": []interface{}{"arg", "agg", "ugh"},
			},
			"external": map[string]interface{}{
				"path":     "/",
				"approved": false,
				"number":   float64(123),
			},
		},
		"domain": "example.com",
	}
	src := map[string]interface{}{
		"admin":  "John",
		"domain": "example.org",
		"settings": map[string]interface{}{
			"internal": map[string]interface{}{
				"foo": "buf",
			},
			"external": map[string]interface{}{
				"path":     []interface{}{"/v1", "/v2"},
				"approved": true,
				"allowed":  false,
			},
			"wildcard": map[string]interface{}{
				"destination": "home",
				"source":      "work",
			},
		},
	}
	expected := map[string]interface{}{
		"domain": "example.org",
		"admin":  "John",
		"settings": map[string]interface{}{
			"internal": map[string]interface{}{
				"foo": "buf",
				"baz": []interface{}{"arg", "agg", "ugh"},
			},
			"external": map[string]interface{}{
				"path":     []interface{}{"/v1", "/v2"},
				"approved": true,
				"allowed":  false,
				"number":   float64(123),
			},
			"wildcard": map[string]interface{}{
				"destination": "home",
				"source":      "work",
			},
		},
	}
	tt.TestExpectSuccess(t, Merge(dst, src))
	tt.TestEqual(t, dst, expected)
}
예제 #25
0
func TestClient_WaitAfterStop(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	socketFile, l := createSocketServer(t)
	defer l.Close()

	setupRequestThatNeverReturns(t, l)

	client := New(socketFile)

	tt.TestEqual(t, client.Stopped(), false)
	client.Stop()
	tt.TestEqual(t, client.Stopped(), true)

	err := client.Wait(time.Minute)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "client is shutting down.")
}
예제 #26
0
func TestIPRangeOverlappingSubnetsInvalids(t *testing.T) {
	subnet1 := "10.0.1.0/16"
	subnet2 := "10.0.0.1/33"
	_, err := OverlappingSubnets(subnet1, subnet2)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "failed to parse the subnet 10.0.0.1/33: invalid CIDR address: 10.0.0.1/33")

	subnet1 = "10.0.1.0"
	subnet2 = "10.0.0.1/3"
	_, err = OverlappingSubnets(subnet1, subnet2)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "failed to parse the subnet 10.0.1.0: invalid CIDR address: 10.0.1.0")

	subnet1 = "256.0.1.0/6"
	subnet2 = "10.0.0.1/3"
	_, err = OverlappingSubnets(subnet1, subnet2)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "failed to parse the subnet 256.0.1.0/6: invalid CIDR address: 256.0.1.0/6")

}
예제 #27
0
func TestGetImageTagLayerID(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	img, statusCode, err := GetImage("foo/bar", "")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, statusCode, 200)

	_, err = img.TagLayerID("tag2")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "can't find tag 'tag2' for image 'foo/bar'")

	id, err := img.TagLayerID("latest")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, id, "deadbeef")

	id, err = img.TagLayerID("base")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, id, "badcafe")
}
예제 #28
0
func TestTempFile(t *testing.T) {
	r := strings.NewReader("blah")
	f, err := New(r)
	tt.TestExpectSuccess(t, err)

	b, err := ioutil.ReadAll(f)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, string(b), "blah")

	tf := f.(*unlinkOnCloseFile)
	st, err := os.Stat(tf.File.Name())
	tt.TestExpectSuccess(t, err)
	tt.TestNotEqual(t, st, nil)

	tt.TestExpectSuccess(t, f.Close())

	_, err = os.Stat(tf.File.Name())
	tt.TestExpectError(t, err)
	tt.TestEqual(t, os.IsNotExist(err), true)
}
예제 #29
0
func TestBasicJsonRequest(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	// create a test server
	method, path, body := "", "", ""
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		method = req.Method
		path = req.URL.Path

		defer req.Body.Close()
		b, err := ioutil.ReadAll(req.Body)
		if err != nil {
			t.Errorf("Error reading request: %v", err)
			w.WriteHeader(500)
			return
		}
		body = string(b)
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(200)
		io.WriteString(w, `{"foo":"bar"}`)
	}))
	defer server.Close()

	client, err := New(server.URL)
	tt.TestExpectSuccess(t, err)
	req := client.NewJsonRequest("POST", "/foo", map[string]string{"bar": "baz"})

	var res map[string]string
	err = client.Result(req, &res)
	tt.TestExpectSuccess(t, err)

	// Verify request as received by server
	tt.TestEqual(t, method, "POST")
	tt.TestEqual(t, path, "/foo")
	tt.TestEqual(t, body, `{"bar":"baz"}`+"\n")

	// Verify response was parsed by client
	tt.TestEqual(t, len(res), 1)
	tt.TestEqual(t, res["foo"], "bar")
}
예제 #30
0
func TestReserve(t *testing.T) {
	ipr, err := ParseIPRange("192.168.1.10-19")
	tt.TestExpectSuccess(t, err)
	alloc := NewAllocator(ipr)

	// reserve an IP
	reservedIP := net.ParseIP("192.168.1.11")
	alloc.Reserve(reservedIP)
	tt.TestEqual(t, alloc.remaining, int64(9))
	tt.TestEqual(t, len(alloc.reserved), 1)

	// consume everything and ensure we don't get that IP
	for {
		if alloc.Remaining() == 0 {
			break
		}

		ip := alloc.Allocate()
		tt.TestNotEqual(t, ip, nil)
		tt.TestNotEqual(t, ip, reservedIP)
	}
}