Exemplo n.º 1
0
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))
}
Exemplo n.º 2
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")
}
Exemplo n.º 3
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), "")
}
Exemplo n.º 4
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")
}
Exemplo n.º 5
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")

}
Exemplo n.º 6
0
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)
}
Exemplo n.º 7
0
func TestClient_WaitTimeout(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

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

	setupRequestThatNeverReturns(t, l)

	client := New(socketFile)
	err := client.Wait(time.Millisecond * 100)
	tt.TestExpectError(t, err)
	tt.TestEqual(t, strings.Contains(err.Error(), "i/o timeout"), true, "Error should have contained i/o timeout", err.Error())
}
Exemplo n.º 8
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")
}
Exemplo n.º 9
0
func TestClient_WaitForSocket(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	socketFile := tt.TempFile(t)
	tt.TestExpectSuccess(t, os.Remove(socketFile))

	client := New(socketFile)
	err := client.WaitForSocket(10 * time.Millisecond)
	tt.TestExpectError(t, err)

	f, err := os.Create(socketFile)
	tt.TestExpectSuccess(t, err)
	f.Close()

	err = client.WaitForSocket(10 * time.Millisecond)
	tt.TestExpectSuccess(t, err)
}
Exemplo n.º 10
0
func TestGetImage(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	img, statusCode, err := GetImage("", "")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, statusCode, -1)
	tt.TestEqual(t, err.Error(), "image name is empty")

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

	img, statusCode, err = GetImage("base", "")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, statusCode, 200)
	tt.TestEqual(t, img.Name, "base")
}
Exemplo n.º 11
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.")
}
Exemplo n.º 12
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)
}
Exemplo n.º 13
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")
}
Exemplo n.º 14
0
func TestInvalidJsonResponse(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", "application/json")
		w.WriteHeader(200)
		io.WriteString(w, `"Name":"Molly","Age":45}`)
	}))
	defer server.Close()

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

	var responsePerson person
	err = client.Result(req, &responsePerson)
	tt.TestExpectError(t, err)
	tt.TestNotEqual(t, err.(*json.UnmarshalTypeError), nil, "Should have been a json unmarshal error")
}
Exemplo n.º 15
0
func TestReadLayer(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)

	r, err := img.LayerReader("deadbeef")
	tt.TestExpectSuccess(t, err)
	body, err := ioutil.ReadAll(r)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, body, []byte{0xd4, 0xe5, 0xf6})

	r, err = img.LayerReader("badcafe")
	tt.TestExpectSuccess(t, err)
	body, err = ioutil.ReadAll(r)
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, body, []byte{0xa1, 0xb2, 0xc3})

	r, err = img.LayerReader("badbad")
	tt.TestExpectError(t, err)
}
Exemplo n.º 16
0
func TestIPRangeParseBasicStringIPv4(t *testing.T) {
	//
	// success
	//

	// 192.168.1.1-100
	ipr, err := ParseIPRange("192.168.1.1-100")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.1.100")

	// 192.168.1.1-100/25
	ipr, err = ParseIPRange("192.168.1.1-100/25")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.1.100")
	oneBits, _ := ipr.Mask.Size()
	tt.TestEqual(t, oneBits, 25)

	// 192.168.1.1
	ipr, err = ParseIPRange("192.168.1.1")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.1.1")

	// 192.168.1.1-2.1
	ipr, err = ParseIPRange("192.168.1.1-2.1")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.2.1")

	// 192.168.1.1-2.1/22
	ipr, err = ParseIPRange("192.168.1.1-2.1/22")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.2.1")
	oneBits, _ = ipr.Mask.Size()
	tt.TestEqual(t, oneBits, 22)

	// 192.168.1.1/24
	ipr, err = ParseIPRange("192.168.1.1/24")
	tt.TestExpectSuccess(t, err)
	tt.TestEqual(t, ipr.Start.String(), "192.168.1.1")
	tt.TestEqual(t, ipr.End.String(), "192.168.1.1")
	oneBits, _ = ipr.Mask.Size()
	tt.TestEqual(t, oneBits, 24)

	//
	// errors
	//

	// 192.168.1.100-1
	_, err = ParseIPRange("192.168.1.100-1")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "the end of the range cannot be less than the start of the range")

	// 192.168.1.1/zz
	_, err = ParseIPRange("192.168.1.1/zz")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "failed to parse the network mask: strconv.ParseInt: parsing \"zz\": invalid syntax")

	// 192.168.1.1-255/32
	_, err = ParseIPRange("192.168.1.1-255/32")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "the provided IP ranges are not within the provided network mask")

	// 192.168.1.100-1-2
	_, err = ParseIPRange("192.168.1.100-1-2")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "unexpected number of IPs specified in the provided string")

	// 192.168.1.100-150/24/24
	_, err = ParseIPRange("192.168.1.100-150/24/24")
	tt.TestExpectError(t, err)
	tt.TestEqual(t, err.Error(), "expected only one '/' within the provided string")
}