Exemplo n.º 1
0
func TestSoftwareSignHandler(t *testing.T) {
	keyStore := trustmanager.NewKeyMemoryStore(passphraseRetriever)
	cryptoService := cryptoservice.NewCryptoService(keyStore)
	setup(signer.CryptoServiceIndex{data.ED25519Key: cryptoService, data.RSAKey: cryptoService, data.ECDSAKey: cryptoService})

	tufKey, err := cryptoService.Create("", "", data.ED25519Key)
	require.Nil(t, err)

	sigRequest := &pb.SignatureRequest{KeyID: &pb.KeyID{ID: tufKey.ID()}, Content: make([]byte, 10)}
	requestJSON, _ := json.Marshal(sigRequest)

	reader = strings.NewReader(string(requestJSON))

	request, err := http.NewRequest("POST", signBaseURL, reader)

	require.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	require.Nil(t, err)

	require.Equal(t, 200, res.StatusCode)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	require.Nil(t, err)

	var sig *pb.Signature
	err = json.Unmarshal(jsonBlob, &sig)
	require.Nil(t, err)

	require.Equal(t, tufKey.ID(), sig.KeyInfo.KeyID.ID)
}
Exemplo n.º 2
0
func TestDecodeHex(t *testing.T) {
	hexData := []byte("0123456789abcdef")
	targetBytes := make([]byte, 8)
	err := DecodeHex(targetBytes, hexData)
	require.Nil(t, err)
	assert.Equal(t, []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, targetBytes)

	hexData = []byte("0123 4567\t89Ab\ncDef")
	targetBytes = make([]byte, 8)
	err = DecodeHex(targetBytes, hexData)
	require.Nil(t, err)
	assert.Equal(t, []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, targetBytes)

	// Destination too long
	hexData = []byte("0123 4567\t89ab\ncdef")
	targetBytes = make([]byte, 10)
	err = DecodeHex(targetBytes, hexData)
	assert.NotNil(t, err)
	assert.Equal(t, make([]byte, 10), targetBytes)

	// Destination too short
	hexData = []byte("0123 4567\t89ab\ncdef")
	targetBytes = make([]byte, 7)
	err = DecodeHex(targetBytes, hexData)
	assert.NotNil(t, err)
	assert.Equal(t, make([]byte, 7), targetBytes)
}
Exemplo n.º 3
0
func TestRackUpdateStable(t *testing.T) {
	versions, err := version.All()
	require.Nil(t, err)

	stable, err := versions.Resolve("stable")
	require.Nil(t, err)

	ts := testServer(t,
		test.Http{Method: "PUT", Body: fmt.Sprintf("version=%s", stable.Version), Path: "/system", Code: 200, Response: client.System{
			Name:    "mysystem",
			Version: "ver",
			Count:   1,
			Type:    "type",
		}},
	)

	defer ts.Close()

	test.Runs(t,
		test.ExecRun{
			Command: "convox rack update",
			Exit:    0,
			Stdout:  fmt.Sprintf("Name     mysystem\nStatus   \nVersion  ver\nCount    1\nType     type\n\nUpdating to version: %s\n", stable.Version),
		},
	)
}
Exemplo n.º 4
0
func TestAddLocationTestify(t *testing.T) {
	inst, err := aetest.NewInstance(
		&aetest.Options{StronglyConsistentDatastore: true})
	require.Nil(t, err, "Error creating aetest instance: %v", err)
	defer inst.Close()
	rt := initRouter()

	loc, err := json.Marshal(&Location{
		Name: "Cambridge Fresh Pond",
		Lat:  42.5,
		Lng:  -71.5,
	})
	assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)

	req, err := inst.NewRequest("POST", "/add-location",
		ioutil.NopCloser(bytes.NewBuffer(loc)))
	require.Nil(t, err, "Error preparing request: %v", err)
	rec := httptest.NewRecorder()
	rt.ServeHTTP(rec, req)

	const expectedResponse = `{"addLocation":"success"}`
	assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
		"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))

	dbReq, err := inst.NewRequest("GET", "/", nil)
	require.Nil(t, err, "Error preparing request: %v", err)
	ctx := appengine.NewContext(dbReq)

	q := datastore.NewQuery("Location")
	numLocs, err := q.Count(ctx)
	require.Nil(t, err, "Error preparing request: %v", err)
	assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
Exemplo n.º 5
0
func TestGetGSResultFileLocations(t *testing.T) {
	testutils.SkipIfShort(t)
	storage, err := storage.New(http.DefaultClient)
	assert.Nil(t, err)

	startTS := time.Date(2014, time.December, 10, 0, 0, 0, 0, time.UTC).Unix()
	endTS := time.Date(2014, time.December, 10, 23, 59, 59, 0, time.UTC).Unix()

	// TODO(stephana): Switch this to a dedicated test bucket, so we are not
	// in danger of removing it.
	resultFiles, err := getGSResultsFileLocations(startTS, endTS, storage, "chromium-skia-gm", "dm-json-v1")
	assert.Nil(t, err)

	// Read the expected list of files and compare them.
	content, err := ioutil.ReadFile("./testdata/filelist_dec_10.txt")
	assert.Nil(t, err)
	lines := strings.Split(strings.TrimSpace(string(content)), "\n")
	sort.Strings(lines)

	resultNames := make([]string, len(resultFiles))
	for idx, rf := range resultFiles {
		resultNames[idx] = rf.Name
	}
	sort.Strings(resultNames)
	assert.Equal(t, len(lines), len(resultNames))
	assert.Equal(t, lines, resultNames)
}
Exemplo n.º 6
0
// testUnfinishedBuild verifies that we can write a build which is not yet
// finished, load the build back from the database, and update it when it
// finishes.
func testUnfinishedBuild(t *testing.T) {
	d := clearDB(t)
	defer d.Close(t)

	// Load the test repo.
	tr := util.NewTempRepo()
	defer tr.Cleanup()

	repos := gitinfo.NewRepoMap(tr.Dir)

	// Obtain and insert an unfinished build.
	httpClient = testHttpClient
	b, err := getBuildFromMaster("client.skia", "Test-Ubuntu12-ShuttleA-GTX550Ti-x86_64-Release-Valgrind", 152, repos)
	assert.Nil(t, err)
	assert.False(t, b.IsFinished(), fmt.Errorf("Unfinished build thinks it's finished!"))
	dbSerializeAndCompare(t, b, true)

	// Ensure that the build is found by getUnfinishedBuilds.
	unfinished, err := getUnfinishedBuilds()
	assert.Nil(t, err)
	found := false
	for _, u := range unfinished {
		if u.Master == b.Master && u.Builder == b.Builder && u.Number == b.Number {
			found = true
			break
		}
	}
	assert.True(t, found, "Unfinished build was not found by getUnfinishedBuilds!")

	// Add another step to the build to "finish" it, ensure that we can
	// retrieve it as expected.
	b.Finished = b.Started + 1000
	b.Times[1] = b.Finished
	stepStarted := b.Started + 500
	s := &BuildStep{
		BuildID:    b.Id,
		Name:       "LastStep",
		Times:      []float64{stepStarted, b.Finished},
		Number:     len(b.Steps),
		Results:    0,
		ResultsRaw: []interface{}{0.0, []interface{}{}},
		Started:    b.Started + 500.0,
		Finished:   b.Finished,
	}
	b.Steps = append(b.Steps, s)
	assert.True(t, b.IsFinished(), "Finished build thinks it's unfinished!")
	dbSerializeAndCompare(t, b, true)

	// Ensure that the finished build is NOT found by getUnfinishedBuilds.
	unfinished, err = getUnfinishedBuilds()
	assert.Nil(t, err)
	found = false
	for _, u := range unfinished {
		if u.Master == b.Master && u.Builder == b.Builder && u.Number == b.Number {
			found = true
			break
		}
	}
	assert.False(t, found, "Finished build was found by getUnfinishedBuilds!")
}
// TestItemsAreInLongTermStorage - make sure that each tar file is
// stored in our S3 test storage bucket, with correct metadata.
func TestItemsAreInLongTermStorage(t *testing.T) {
	if !apt_testutil.ShouldRunIntegrationTests() {
		t.Skip("Skipping integration test. Set ENV var RUN_EXCHANGE_INTEGRATION=true if you want to run them.")
	}
	_context, err := apt_testutil.GetContext("integration.json")
	require.Nil(t, err, "Could not create context")

	localClient, err := network.NewDPNRestClient(
		_context.Config.DPN.RestClient.LocalServiceURL,
		_context.Config.DPN.RestClient.LocalAPIRoot,
		_context.Config.DPN.RestClient.LocalAuthToken,
		_context.Config.DPN.LocalNode,
		_context.Config.DPN)
	require.Nil(t, err)

	// s3List lists bucket contents.
	s3List := apt_network.NewS3ObjectList(
		constants.AWSVirginia,
		_context.Config.DPN.DPNPreservationBucket,
		int64(100),
	)
	// s3Head gets metadata about specific objects in S3/Glacier.
	s3Head := apt_network.NewS3Head(_context.Config.APTrustS3Region,
		_context.Config.DPN.DPNPreservationBucket)

	for _, identifier := range dpn_testutil.BAG_IDS {
		resp := localClient.DPNBagGet(identifier)
		dpnBag := resp.Bag()
		require.NotNil(t, dpnBag)
		if dpnBag.IngestNode == _context.Config.DPN.LocalNode {
			continue // we would not have replicated our own bag
		}
		tarFileName := fmt.Sprintf("%s.tar", identifier)
		s3List.GetList(tarFileName)
		require.NotEmpty(t, s3List.Response.Contents, "%s not found in S3", tarFileName)
		object := s3List.Response.Contents[0]
		fiveMinutesAgo := time.Now().UTC().Add(-5 * time.Minute)
		require.NotNil(t, object.LastModified)
		assert.True(t, object.LastModified.After(fiveMinutesAgo))

		// Make sure each item has the expected metadata.
		// s3Head.Response.Metadata is map[string]*string.
		s3Head.Head(tarFileName)
		require.Empty(t, s3Head.ErrorMessage)
		metadata := s3Head.Response.Metadata
		require.NotNil(t, metadata)
		// Amazon library transforms first letters of keys to CAPS
		require.NotNil(t, metadata["From_node"])
		require.NotNil(t, metadata["Transfer_id"])
		require.NotNil(t, metadata["Member"])
		require.NotNil(t, metadata["Local_id"])
		require.NotNil(t, metadata["Version"])

		assert.NotEmpty(t, *metadata["From_node"])
		assert.NotEmpty(t, *metadata["Transfer_id"])
		assert.NotEmpty(t, *metadata["Member"])
		assert.NotEmpty(t, *metadata["Local_id"])
		assert.NotEmpty(t, *metadata["Version"])
	}
}
Exemplo n.º 8
0
func TestCmd(t *T) {
	c := dial(t)
	v, err := c.Cmd("echo", "Hello, World!").Str()
	require.Nil(t, err)
	assert.Equal(t, "Hello, World!", v)

	// Test that a bad command properly returns an AppErr
	r := c.Cmd("non-existant-cmd")
	assert.Equal(t, AppErr, r.typ)
	assert.NotNil(t, r.Err)

	// Test that application level errors propagate correctly
	require.Nil(t, c.Cmd("sadd", "foo", "bar").Err)
	_, err = c.Cmd("get", "foo").Str()
	assert.NotNil(t, "", err)

	// Test flattening out maps
	args := map[string]interface{}{
		"someBytes":  []byte("blah"),
		"someString": "foo",
		"someInt":    10,
		"someBool":   false,
	}
	require.Nil(t, c.Cmd("HMSET", "somestuff", args).Err)
	l, err := c.Cmd("HMGET", "somestuff", "someBytes", "someString", "someInt", "someBool").List()
	require.Nil(t, err)
	assert.Equal(t, []string{"blah", "foo", "10", "0"}, l)

}
Exemplo n.º 9
0
func TestRenameOpCustomUpdateAcrossDirs(t *testing.T) {
	oldOldDir := makeFakeBlockPointer(t)
	oldNewDir := oldOldDir
	oldNewDir.ID = fakeBlockID(42)
	renamed := oldOldDir
	renamed.ID = fakeBlockID(43)
	ro := newRenameOp(
		"old name", oldOldDir, "new name", oldNewDir,
		renamed, Exec)
	require.Equal(t, blockUpdate{Unref: oldOldDir}, ro.OldDir)
	require.Equal(t, blockUpdate{Unref: oldNewDir}, ro.NewDir)

	// Update to oldOldDir should update ro.OldDir.
	newOldDir := oldOldDir
	newOldDir.ID = fakeBlockID(44)
	ro.AddUpdate(oldOldDir, newOldDir)
	require.Nil(t, ro.Updates)
	require.Equal(t, blockUpdate{Unref: oldOldDir, Ref: newOldDir}, ro.OldDir)
	require.Equal(t, blockUpdate{Unref: oldNewDir}, ro.NewDir)

	// Update to oldNewDir should update ro.OldDir.
	newNewDir := oldNewDir
	newNewDir.ID = fakeBlockID(45)
	ro.AddUpdate(oldNewDir, newNewDir)
	require.Nil(t, ro.Updates)
	require.Equal(t, blockUpdate{Unref: oldOldDir, Ref: newOldDir}, ro.OldDir)
	require.Equal(t, blockUpdate{Unref: oldNewDir, Ref: newNewDir}, ro.NewDir)
}
Exemplo n.º 10
0
func TestAuthCode(t *testing.T) {
	var callbackURL *url.URL
	router := mux.NewRouter()
	ts := httptest.NewUnstartedServer(router)
	callbackCalled := false

	handler.SetRoutes(router, mockAuthorization("", new(jwt.Token)))
	router.HandleFunc("/remote/oauth2/auth", authHandlerMock(t, ts))
	router.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
		callbackURL = r.URL
		callbackCalled = true
	})

	ts.Start()
	defer ts.Close()

	for _, c := range []struct{ config *oauth2.Config }{{configs["working"]}} {
		config := *c.config
		config.Endpoint = oauth2.Endpoint{AuthURL: ts.URL + "/oauth2/auth?provider=mockprovider", TokenURL: ts.URL + "/oauth2/token"}
		authURL := config.AuthCodeURL(uuid.New())
		t.Logf("Auth code URL: %s", authURL)

		resp, err := http.Get(authURL)
		require.Nil(t, err)
		defer resp.Body.Close()
		require.True(t, callbackCalled)
		callbackCalled = false

		token, err := config.Exchange(oauth2.NoContext, callbackURL.Query().Get("code"))
		require.Nil(t, err)
		require.NotEmpty(t, token.AccessToken)
		require.NotEmpty(t, token.RefreshToken)
		testTokenRefresh(t, ts.URL, config.ClientID, config.ClientSecret, token.RefreshToken, true)
	}
}
Exemplo n.º 11
0
func TestMemChangeIterator(t *testing.T) {
	cl := memChangelist{}
	it, err := cl.NewIterator()
	require.Nil(t, err, "Non-nil error from NewIterator")
	require.False(t, it.HasNext(), "HasNext returns false for empty ChangeList")

	c1 := NewTUFChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1})
	cl.Add(c1)

	c2 := NewTUFChange(ActionUpdate, "t2", "target2", "test/targ2", []byte{2})
	cl.Add(c2)

	c3 := NewTUFChange(ActionUpdate, "t3", "target3", "test/targ3", []byte{3})
	cl.Add(c3)

	cs := cl.List()
	index := 0
	it, _ = cl.NewIterator()
	for it.HasNext() {
		c, err := it.Next()
		require.Nil(t, err, "Next err should be false")
		require.Equal(t, c.Action(), cs[index].Action(), "Action mismatch")
		require.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch")
		require.Equal(t, c.Type(), cs[index].Type(), "Type mismatch")
		require.Equal(t, c.Path(), cs[index].Path(), "Path mismatch")
		require.Equal(t, c.Content(), cs[index].Content(), "Content mismatch")
		index++
	}
	require.Equal(t, index, len(cs), "Iterator produced all data in ChangeList")
	_, err = it.Next()
	require.NotNil(t, err, "Next errors gracefully when exhausted")
	var iterError IteratorBoundsError
	require.IsType(t, iterError, err, "IteratorBoundsError type")
}
Exemplo n.º 12
0
// QINFO is a human readable version of QSTATUS, so the testing of it is not
// nearly as strenuous
func TestQInfo(t *T) {
	client := newClient()

	// We register a queue to make sure at least one queue shows up in the
	// results when doing a QINFO with no arguments
	emptyQueue := clients.RandQueueName()
	Dispatch(client, "qregister", []string{emptyQueue})
	readAndAssertStr(t, client, "OK")
	Dispatch(client, "qinfo", []string{})
	infos, err := read(t, client).Array()
	require.Nil(t, err)
	found := 0
	for _, infoM := range infos {
		line, err := infoM.Str()
		require.Nil(t, err)
		if strings.HasPrefix(line, emptyQueue) {
			found++
		}
	}
	assert.Equal(t, 1, found)

	// Now we make sure qinfo returns a line starting with the expected queue
	// name when we give it as an argument
	Dispatch(client, "qinfo", []string{emptyQueue})
	infos, err = read(t, client).Array()
	require.Nil(t, err)
	require.Equal(t, 1, len(infos))

	line, err := infos[0].Str()
	require.Nil(t, err)
	assert.True(t, strings.HasPrefix(line, emptyQueue))
}
Exemplo n.º 13
0
func TestQStatus(t *T) {
	client := newClient()
	queues := []string{
		clients.RandQueueName(),
		clients.RandQueueName(),
	}

	Dispatch(client, "qstatus", queues)
	readAndAssertQStatus(t, client, []queueInfo{
		queueInfo{queues[0], 0, 0, 0},
		queueInfo{queues[1], 0, 0, 0},
	})

	// Make sure that when a client is registered to a queue that it shows up in
	// the full list of queues, even if it doesn't have any items
	emptyQueue := clients.RandQueueName()
	Dispatch(client, "qregister", []string{emptyQueue})
	readAndAssertStr(t, client, "OK")
	Dispatch(client, "qstatus", []string{})
	infos, err := read(t, client).Array()
	require.Nil(t, err)
	found := 0
	for _, infoM := range infos {
		a, err := infoM.Array()
		require.Nil(t, err)
		require.True(t, len(a) == 4)
		queueName, err := a[0].Str()
		require.Nil(t, err)
		if queueName == emptyQueue {
			found++
		}
	}
	assert.Equal(t, 1, found)
}
Exemplo n.º 14
0
func TestKeyInfoHandler(t *testing.T) {
	keyStore := trustmanager.NewKeyMemoryStore(passphraseRetriever)
	cryptoService := cryptoservice.NewCryptoService(keyStore)
	setup(signer.CryptoServiceIndex{data.ED25519Key: cryptoService, data.RSAKey: cryptoService, data.ECDSAKey: cryptoService})

	tufKey, _ := cryptoService.Create("", "", data.ED25519Key)
	require.NotNil(t, tufKey)

	keyInfoURL := fmt.Sprintf("%s/%s", keyInfoBaseURL, tufKey.ID())

	request, err := http.NewRequest("GET", keyInfoURL, nil)
	require.Nil(t, err)

	res, err := http.DefaultClient.Do(request)
	require.Nil(t, err)

	jsonBlob, err := ioutil.ReadAll(res.Body)
	require.Nil(t, err)

	var pubKey *pb.PublicKey
	err = json.Unmarshal(jsonBlob, &pubKey)
	require.Nil(t, err)

	require.Equal(t, tufKey.ID(), pubKey.KeyInfo.KeyID.ID)
	require.Equal(t, 200, res.StatusCode)
}
Exemplo n.º 15
0
func TestSecondCommitMultipleSplits(t *testing.T) {
	num := 50
	cfg := defaultConfig()
	rt := New(cfg)
	items := generateRandomItems(num)

	mutable := rt.AsMutable()
	mutable.AddItems(items[:25]...)
	mutable.(*Tr).verify(mutable.(*Tr).Root, t)
	rt, err := mutable.Commit()
	rt.(*Tr).verify(rt.(*Tr).Root, t)

	result, err := rt.(*Tr).toList(itemsToValues(items...)...)
	require.Nil(t, err)
	assert.Equal(t, items[:25], result)

	mutable = rt.AsMutable()
	mutable.AddItems(items[25:]...)
	mutable.(*Tr).verify(mutable.(*Tr).Root, t)

	sort.Sort(orderedItems(items))
	result, err = mutable.(*Tr).toList(itemsToValues(items...)...)
	require.Nil(t, err)
	if !assert.Equal(t, items, result) {
		mutable.(*Tr).pprint(mutable.(*Tr).Root)
	}
}
Exemplo n.º 16
0
func TestMarshalUnmarshal(t *testing.T) {
	css := &Conditions{
		"clientIP": &CIDRCondition{CIDR: "127.0.0.1/0"},
		"owner":    &EqualsSubjectCondition{},
	}
	out, err := json.Marshal(css)
	require.Nil(t, err)
	t.Logf("%s", out)

	cs := Conditions{}
	require.Nil(t, json.Unmarshal([]byte(`{
	"owner": {
		"type": "EqualsSubjectCondition"
	},
	"clientIP": {
		"type": "CIDRCondition",
		"options": {
			"cidr": "127.0.0.1/0"
		}
	}
}`), &cs))

	require.Len(t, cs, 2)
	assert.IsType(t, &EqualsSubjectCondition{}, cs["owner"])
	assert.IsType(t, &CIDRCondition{}, cs["clientIP"])
}
Exemplo n.º 17
0
func TestJSONCodec(t *testing.T) {
	type myTestType struct {
		A int
		B string
	}

	itemCodec := JSONCodec(&myTestType{})
	testInstance := &myTestType{5, "hello"}
	jsonBytes, err := itemCodec.Encode(testInstance)
	assert.Nil(t, err)

	decodedInstance, err := itemCodec.Decode(jsonBytes)
	assert.Nil(t, err)
	assert.IsType(t, &myTestType{}, decodedInstance)
	assert.Equal(t, testInstance, decodedInstance)

	arrCodec := JSONCodec([]*myTestType{})
	testArr := []*myTestType{&myTestType{1, "1"}, &myTestType{2, "2"}}
	jsonBytes, err = arrCodec.Encode(testArr)
	assert.Nil(t, err)

	decodedArr, err := arrCodec.Decode(jsonBytes)
	assert.Nil(t, err)
	assert.IsType(t, []*myTestType{}, decodedArr)
	assert.Equal(t, testArr, decodedArr)
}
Exemplo n.º 18
0
func TestDownloadRootCASuccess(t *testing.T) {
	tc := testutils.NewTestCA(t)
	defer tc.Stop()

	// Remove the CA cert
	os.RemoveAll(tc.Paths.RootCA.Cert)

	rootCA, err := ca.DownloadRootCA(tc.Context, tc.Paths.RootCA, tc.WorkerToken, tc.Remotes)
	require.NoError(t, err)
	require.NotNil(t, rootCA.Pool)
	require.NotNil(t, rootCA.Cert)
	require.Nil(t, rootCA.Signer)
	require.False(t, rootCA.CanSign())
	require.Equal(t, tc.RootCA.Cert, rootCA.Cert)

	// Remove the CA cert
	os.RemoveAll(tc.Paths.RootCA.Cert)

	// downloading without a join token also succeeds
	rootCA, err = ca.DownloadRootCA(tc.Context, tc.Paths.RootCA, "", tc.Remotes)
	require.NoError(t, err)
	require.NotNil(t, rootCA.Pool)
	require.NotNil(t, rootCA.Cert)
	require.Nil(t, rootCA.Signer)
	require.False(t, rootCA.CanSign())
	require.Equal(t, tc.RootCA.Cert, rootCA.Cert)
}
// TestReplicationsMarkedAsStored - make sure that the ReplicationTransfer
// records are marked with stored = true on the remote nodes.
func TestReplicationsMarkedAsStored(t *testing.T) {
	if !apt_testutil.ShouldRunIntegrationTests() {
		t.Skip("Skipping integration test. Set ENV var RUN_EXCHANGE_INTEGRATION=true if you want to run them.")
	}
	_context, err := apt_testutil.GetContext("integration.json")
	require.Nil(t, err, "Could not create context")

	// Get a list of ReplicationTransfers where our local node
	// is the ToNode, and make sure we marked them all as stored
	// on the FromNode.
	localClient, err := network.NewDPNRestClient(
		_context.Config.DPN.RestClient.LocalServiceURL,
		_context.Config.DPN.RestClient.LocalAPIRoot,
		_context.Config.DPN.RestClient.LocalAuthToken,
		_context.Config.DPN.LocalNode,
		_context.Config.DPN)
	require.Nil(t, err)
	remoteClients, err := localClient.GetRemoteClients()
	require.Nil(t, err)

	xferParams := url.Values{}
	xferParams.Set("to_node", _context.Config.DPN.LocalNode)
	dpnResp := localClient.ReplicationTransferList(xferParams)
	require.Nil(t, dpnResp.Error)
	for _, xfer := range dpnResp.ReplicationTransfers() {
		remoteClient := remoteClients[xfer.FromNode]
		require.NotNil(t, remoteClient)
		resp := remoteClient.ReplicationTransferGet(xfer.ReplicationId)
		require.Nil(t, resp.Error)
		remoteXfer := resp.ReplicationTransfer()
		require.NotNil(t, remoteXfer)
		assert.True(t, remoteXfer.Stored)
	}
}
func TestParseDefaultVal(t *testing.T) {
	var conf struct {
		MySQL struct {
			Master struct {
				Address string `envconfig:"default=localhost"`
				Port    int    `envconfig:"default=3306"`
			}
			Timeout      time.Duration `envconfig:"default=1m,myTimeout"`
			LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
		}
	}

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*1, conf.MySQL.Timeout)

	os.Setenv("myTimeout", "2m")
	os.Setenv("myTimeout2", "20m")

	err = envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
	require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
Exemplo n.º 21
0
func (s *TestSuite) TestAddLocation() {
	t := s.T()

	loc, err := json.Marshal(&Location{
		Name: "Cambridge Fresh Pond",
		Lat:  42.5,
		Lng:  -71.5,
	})
	assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)

	req, err := s.inst.NewRequest("POST", "/add-location",
		ioutil.NopCloser(bytes.NewBuffer(loc)))
	require.Nil(t, err, "Error preparing request: %v", err)
	rec := httptest.NewRecorder()
	s.rt.ServeHTTP(rec, req)

	const expectedResponse = `{"addLocation":"success"}`
	assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
		"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))

	dbReq, err := s.inst.NewRequest("GET", "/", nil)
	require.Nil(t, err, "Error preparing request: %v", err)
	ctx := appengine.NewContext(dbReq)

	q := datastore.NewQuery("Location")
	numLocs, err := q.Count(ctx)
	require.Nil(t, err, "Error preparing request: %v", err)
	assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
Exemplo n.º 22
0
func TestLuaEval(t *T) {
	c1, err := redis.Dial("tcp", "127.0.0.1:6379")
	require.Nil(t, err)
	c2, err := cluster.New("127.0.0.1:7000")
	require.Nil(t, err)

	cs := []Cmder{c1, c2}

	for _, c := range cs {
		script, key, val := randTestScript()
		s, err := LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		// The second time the command will be hashed
		script, key, val = randTestScript()
		s, err = LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		s, err = c.Cmd("GET", key).Str()
		require.Nil(t, err)
		assert.Equal(t, val, s)
	}
}
Exemplo n.º 23
0
func testRasterizer(t *testing.T, rasterizer Rasterizer, expectation string) {
	assert.True(t, rasterizer.Enabled(), "%s.Enabled() failed.", rasterizer.String())

	testDataDir, err := testutils.TestDataDir()
	assert.Nil(t, err, "TestDataDir missing: %v", err)

	tempDir, err := ioutil.TempDir("", "pdf_test_")
	assert.Nil(t, err, "ioutil.TempDir failed")
	defer util.RemoveAll(tempDir)

	pdfSrcPath := path.Join(testDataDir, "minimal.pdf")
	assert.True(t, fileutil.FileExists(pdfSrcPath), "Path '%s' does not exist", pdfSrcPath)
	pdfInputPath := path.Join(tempDir, "minimal.pdf")

	err = os.Symlink(pdfSrcPath, pdfInputPath)
	assert.Nil(t, err, "Symlink failed")
	assert.True(t, fileutil.FileExists(pdfInputPath), "Path '%s' does not exist", pdfInputPath)

	outputFileName := path.Join(tempDir, "test.png")

	badPath := path.Join(tempDir, "this_file_should_really_not_exist.pdf")

	if err := rasterizer.Rasterize(badPath, outputFileName); err == nil {
		t.Errorf(": Got '%v' Want '%v'", err, nil)
	}

	if err := rasterizer.Rasterize(pdfInputPath, outputFileName); err != nil {
		t.Errorf(": Got '%v' Want '!nil'", err)
	}

	expectedOutput := path.Join(testDataDir, expectation)
	assert.True(t, filesEqual(outputFileName, expectedOutput), "png output not correct")
}
Exemplo n.º 24
0
func assertFixture(t *testing.T, name string) {
	orig := manifest.ManifestRandomPorts
	manifest.ManifestRandomPorts = false
	defer func() { manifest.ManifestRandomPorts = orig }()

	app := models.App{
		Name: "httpd",
		Tags: map[string]string{
			"Name":   "httpd",
			"Type":   "app",
			"System": "convox",
			"Rack":   "convox-test",
		},
	}

	data, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s.yml", name))
	require.Nil(t, err)

	manifest, err := manifest.Load(data)
	require.Nil(t, err)

	formation, err := app.Formation(*manifest)
	require.Nil(t, err)

	pretty, err := models.PrettyJSON(formation)
	require.Nil(t, err)

	data, err = ioutil.ReadFile(fmt.Sprintf("fixtures/%s.json", name))
	require.Nil(t, err)

	diff1 := strings.Split(strings.TrimSpace(string(data)), "\n")
	diff2 := strings.Split(strings.TrimSpace(pretty), "\n")

	diff := difflib.Diff(diff1, diff2)
	diffs := []string{}

	// bigger than max
	prev := 1000000

	for l, d := range diff {
		switch d.Delta {
		case difflib.LeftOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
			prev = l
		case difflib.RightOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
			prev = l
		}
	}

	if len(diffs) > 0 {
		t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
	}
}
Exemplo n.º 25
0
func TestGetLocalResultFileLocations(t *testing.T) {
	testutils.SkipIfShort(t)

	err := testutils.DownloadTestDataArchive(t, TEST_DATA_STORAGE_PATH, TEST_DATA_DIR)
	assert.Nil(t, err)

	startTS := time.Date(2015, time.May, 5, 0, 0, 0, 0, time.UTC).Unix()
	endTS := time.Date(2015, time.May, 17, 23, 59, 59, 0, time.UTC).Unix()

	resultFiles, err := getLocalResultsFileLocations(startTS, endTS, filepath.Join(TEST_DATA_DIR, "nano-json-v1"))
	assert.Nil(t, err)

	// Read the expected list of files and compare them.
	content, err := ioutil.ReadFile("./testdata/local_ingest_files.txt")
	assert.Nil(t, err)
	lines := strings.Split(strings.TrimSpace(string(content)), "\n")
	sort.Strings(lines)

	resultNames := make([]string, len(resultFiles))
	for idx, rf := range resultFiles {
		resultNames[idx] = rf.Name
	}
	sort.Strings(resultNames)
	assert.Equal(t, len(lines), len(resultNames))
	assert.Equal(t, lines, resultNames)
}
Exemplo n.º 26
0
func TestUserGroups(t *testing.T) {
	err := RegisterGroup("a")
	require.Nil(t, err)

	err = RegisterGroup("b")
	require.Nil(t, err)

	err = RegisterGroup("c")
	require.Nil(t, err)

	u, err := NewUser("foo", "bar", "*****@*****.**", "secretpassword")
	require.Nil(t, err)
	require.NotNil(t, u)

	u.AddGroup("a", "a", "a", "b", "c", "b")
	require.Len(t, u.Groups, 3)

	u.RemoveGroup("a", "b")
	require.Len(t, u.Groups, 1)

	u.Groups = append(u.Groups, "a", "a", "b", "b", "b")
	require.Len(t, u.Groups, 6)

	u.RemoveGroup("a", "b")
	require.Len(t, u.Groups, 1)
}
Exemplo n.º 27
0
func TestPipeline(t *T) {
	c := dial(t)
	// Do this multiple times to make sure pipeline resetting happens correctly
	for i := 0; i < 10; i++ {
		c.PipeAppend("echo", "foo")
		c.PipeAppend("echo", "bar")
		c.PipeAppend("echo", "zot")

		v, err := c.PipeResp().Str()
		require.Nil(t, err)
		assert.Equal(t, "foo", v)

		v, err = c.PipeResp().Str()
		require.Nil(t, err)
		assert.Equal(t, "bar", v)

		v, err = c.PipeResp().Str()
		require.Nil(t, err)
		assert.Equal(t, "zot", v)

		r := c.PipeResp()
		assert.Equal(t, AppErr, r.typ)
		assert.Equal(t, ErrPipelineEmpty, r.Err)
	}
}
Exemplo n.º 28
0
func TestDeleteMultipleLevelsWithCommit(t *testing.T) {
	num := 20
	cfg := defaultConfig()
	rt := New(cfg)
	mutable := rt.AsMutable()
	items := generateRandomItems(num)
	mutable.AddItems(items...)
	rt, _ = mutable.Commit()

	rt, _ = Load(cfg.Persister, rt.ID(), comparator)
	result, err := rt.(*Tr).toList(itemsToValues(items...)...)
	require.NoError(t, err)
	assert.Equal(t, items, result)
	mutable = rt.AsMutable()

	for _, c := range items[:10] {
		_, err := mutable.DeleteItems(c.Value)
		require.Nil(t, err)
	}

	result, err = mutable.(*Tr).toList(itemsToValues(items[10:]...)...)
	require.Nil(t, err)
	assert.Equal(t, items[10:], result)
	mutable.(*Tr).verify(mutable.(*Tr).Root, t)

	result, err = rt.(*Tr).toList(itemsToValues(items...)...)
	require.NoError(t, err)
	assert.Equal(t, items, result)
	rt.(*Tr).verify(rt.(*Tr).Root, t)
}
Exemplo n.º 29
0
func TestSpaceRequest(t *testing.T) {
	const (
		container1 = "cont-1"
		container2 = "cont-2"
		universe   = "10.32.0.0/12"
	)
	allocs, router, subnet := makeNetworkOfAllocators(1, universe)
	defer stopNetworkOfAllocators(allocs, router)
	alloc1 := allocs[0]

	addr, err := alloc1.Allocate(container1, subnet, true, returnFalse)
	require.Nil(t, err, "")
	// free it again so the donation splits the range neatly
	err = alloc1.Free(container1, addr)
	require.Nil(t, err, "")
	require.Equal(t, cidrRanges(universe), alloc1.OwnedRanges(), "")

	// Start a new peer
	alloc2, _ := makeAllocator("02:00:00:02:00:00", universe, 2)
	alloc2.SetInterfaces(router.Connect(alloc2.ourName, alloc2))
	alloc2.Start()
	defer alloc2.Stop()
	alloc2.Allocate(container2, subnet, true, returnFalse)

	// Test whether the universe has been split into two equal halves (GH #2009)
	require.Equal(t, cidrRanges("10.32.0.0/13"), alloc1.OwnedRanges(), "")
	require.Equal(t, cidrRanges("10.40.0.0/13"), alloc2.OwnedRanges(), "")
}
Exemplo n.º 30
0
func TestGenerate(t *testing.T) {
	k, err := Generate(GenerateOpts{
		Issuer:      "SnakeOil",
		AccountName: "*****@*****.**",
	})
	require.NoError(t, err, "generate basic TOTP")
	require.Equal(t, "SnakeOil", k.Issuer(), "Extracting Issuer")
	require.Equal(t, "*****@*****.**", k.AccountName(), "Extracting Account Name")
	require.Equal(t, 16, len(k.Secret()), "Secret is 16 bytes long as base32.")

	k, err = Generate(GenerateOpts{
		Issuer:      "SnakeOil",
		AccountName: "*****@*****.**",
		SecretSize:  20,
	})
	require.NoError(t, err, "generate larger TOTP")
	require.Equal(t, 32, len(k.Secret()), "Secret is 32 bytes long as base32.")

	k, err = Generate(GenerateOpts{
		Issuer:      "",
		AccountName: "*****@*****.**",
	})
	require.Equal(t, otp.ErrGenerateMissingIssuer, err, "generate missing issuer")
	require.Nil(t, k, "key should be nil on error.")

	k, err = Generate(GenerateOpts{
		Issuer:      "Foobar, Inc",
		AccountName: "",
	})
	require.Equal(t, otp.ErrGenerateMissingAccountName, err, "generate missing account name.")
	require.Nil(t, k, "key should be nil on error.")
}