func TestLoadLotsOfHardware(t *testing.T) {
	context := make(map[string]interface{})

	context[constants.CTX_HARDWARE_FOLDERS] = []string{"downloaded_board_manager_stuff", "downloaded_hardware", filepath.Join("..", "hardware"), "hardware", "user_hardware"}

	loader := builder.HardwareLoader{}
	err := loader.Run(context)
	NoError(t, err)

	packages := context[constants.CTX_HARDWARE].(map[string]*types.Package)

	if runtime.GOOS == "windows" {
		//a package is a symlink, and windows does not support them
		require.Equal(t, 4, len(packages))
	} else {
		require.Equal(t, 5, len(packages))
	}

	require.NotNil(t, packages["arduino"])
	require.NotNil(t, packages["my_avr_platform"])

	require.Equal(t, 3, len(packages["arduino"].Platforms))
	require.Equal(t, 20, len(packages["arduino"].Platforms["avr"].Boards))
	require.Equal(t, 2, len(packages["arduino"].Platforms["sam"].Boards))
	require.Equal(t, 2, len(packages["arduino"].Platforms["samd"].Boards))

	require.Equal(t, 1, len(packages["my_avr_platform"].Platforms))
	require.Equal(t, 2, len(packages["my_avr_platform"].Platforms["avr"].Boards))

	if runtime.GOOS != "windows" {
		require.Equal(t, 1, len(packages["my_symlinked_avr_platform"].Platforms))
		require.Equal(t, 2, len(packages["my_symlinked_avr_platform"].Platforms["avr"].Boards))
	}
}
Example #2
0
func TestTracingPropagates(t *testing.T) {
	WithVerifiedServer(t, nil, func(ch *Channel, hostPort string) {
		handler := &traceHandler{t: t, ch: ch}
		json.Register(ch, json.Handlers{
			"call": handler.call,
		}, handler.onError)

		ctx, cancel := json.NewContext(time.Second)
		defer cancel()

		peer := ch.Peers().GetOrAdd(ch.PeerInfo().HostPort)
		var response TracingResponse
		require.NoError(t, json.CallPeer(ctx, peer, ch.PeerInfo().ServiceName, "call", &TracingRequest{
			ForwardCount: 1,
		}, &response))

		clientSpan := CurrentSpan(ctx)
		require.NotNil(t, clientSpan)
		assert.Equal(t, uint64(0), clientSpan.ParentID())

		assert.Equal(t, uint64(0), clientSpan.ParentID())
		assert.NotEqual(t, uint64(0), clientSpan.TraceID())
		assert.Equal(t, clientSpan.TraceID(), response.TraceID)
		assert.Equal(t, clientSpan.SpanID(), response.ParentID)
		assert.Equal(t, response.TraceID, response.SpanID, "traceID = spanID for root span")

		nestedResponse := response.Child
		require.NotNil(t, nestedResponse)
		assert.Equal(t, clientSpan.TraceID(), nestedResponse.TraceID)
		assert.Equal(t, response.SpanID, nestedResponse.ParentID)
		assert.NotEqual(t, response.SpanID, nestedResponse.SpanID)
	})
}
Example #3
0
// verifyCacheFactoryCluster performs assertions over a ClusterInfo structure,
// based on the values and structure generated by cacheFactory.
func verifyCacheFactoryCluster(clinfo *ClusterInfo, t *testing.T) {
	assert := assert.New(t)
	assert.NotNil(clinfo.Nodes["hostname2"])
	node2 := clinfo.Nodes["hostname2"]
	assert.NotEmpty(node2.Metrics)
	assert.Len(node2.FreeContainers, 1)
	assert.NotNil(node2.FreeContainers["free_container1"])

	assert.NotNil(clinfo.Nodes["hostname3"])
	node3 := clinfo.Nodes["hostname3"]
	assert.NotEmpty(node3.Metrics)

	assert.NotNil(clinfo.Namespaces["test"])
	namespace := clinfo.Namespaces["test"]

	assert.NotNil(namespace.Pods)
	pod1_ptr := namespace.Pods["pod1"]
	require.NotNil(t, pod1_ptr)
	assert.Equal(pod1_ptr, node2.Pods["pod1"])
	assert.Len(pod1_ptr.Containers, 2)
	pod2_ptr := namespace.Pods["pod2"]
	require.NotNil(t, pod2_ptr)
	assert.Equal(pod2_ptr, node3.Pods["pod2"])
	assert.Len(pod2_ptr.Containers, 2)
}
// TestListWorkItemType tests if we can find the work item types
// "person" and "animal" in the list of work item types
func (s *workItemTypeSuite) TestListWorkItemType() {
	defer cleaner.DeleteCreatedEntities(s.DB)()

	// Create the work item type first and try to read it back in
	_, witAnimal := s.createWorkItemTypeAnimal()
	require.NotNil(s.T(), witAnimal)
	_, witPerson := s.createWorkItemTypePerson()
	require.NotNil(s.T(), witPerson)

	// Fetch a single work item type
	// Paging in the format <start>,<limit>"
	page := "0,-1"
	_, witCollection := test.ListWorkitemtypeOK(s.T(), nil, nil, s.typeCtrl, &page)

	require.NotNil(s.T(), witCollection)
	require.Nil(s.T(), witCollection.Validate())

	// Check the number of found work item types
	require.Condition(s.T(), func() bool {
		return (len(witCollection) >= 2)
	}, "At least two work item types must exist (animal and person), but only %d exist.", len(witCollection))

	// Search for the work item types that must exist at minimum
	toBeFound := 2
	for i := 0; i < len(witCollection) && toBeFound > 0; i++ {
		if witCollection[i].Name == "person" || witCollection[i].Name == "animal" {
			s.T().Log("Found work item type in collection: ", witCollection[i].Name)
			toBeFound--
		}
	}
	require.Exactly(s.T(), 0, toBeFound, "Not all required work item types (animal and person) where found.")
}
Example #5
0
func TestParseMutationAndQuery(t *testing.T) {
	query := `
		mutation {
			set {
				<name> <is> <something> .
				<hometown> <is> <san francisco> .
			}
			delete {
				<name> <is> <something-else> .
			}
		}
		query {
			me(_xid_: tomhanks) {
				name
				hometown
			}
		}
	`
	gq, mu, err := Parse(query)
	require.NoError(t, err)
	require.NotNil(t, mu)
	require.NotEqual(t, strings.Index(mu.Set, "<name> <is> <something> ."), -1)
	require.NotEqual(t, strings.Index(mu.Set, "<hometown> <is> <san francisco> ."), -1)
	require.NotEqual(t, strings.Index(mu.Del, "<name> <is> <something-else> ."), -1)

	require.NotNil(t, gq)
	require.Equal(t, gq.XID, "tomhanks")
	require.Equal(t, childAttrs(gq), []string{"name", "hometown"})
}
Example #6
0
func TestReferencePolicyConfigurationNamespaces(t *testing.T) {
	ref, tmpDir := refToTempOCI(t)
	defer os.RemoveAll(tmpDir)
	// We don't really know enough to make a full equality test here.
	ns := ref.PolicyConfigurationNamespaces()
	require.NotNil(t, ns)
	assert.True(t, len(ns) >= 2)
	assert.Equal(t, tmpDir, ns[0])
	assert.Equal(t, filepath.Dir(tmpDir), ns[1])

	// Test with a known path which should exist. Test just one non-canonical
	// path, the various other cases are tested in explicitfilepath.ResolvePathToFullyExplicit.
	//
	// It would be nice to test a deeper hierarchy, but it is not obvious what
	// deeper path is always available in the various distros, AND is not likely
	// to contains a symbolic link.
	for _, path := range []string{"/etc/skel", "/etc/skel/./."} {
		_, err := os.Lstat(path)
		require.NoError(t, err)
		ref, err := NewReference(path, "sometag")
		require.NoError(t, err)
		ns := ref.PolicyConfigurationNamespaces()
		require.NotNil(t, ns)
		assert.Equal(t, []string{"/etc/skel", "/etc"}, ns)
	}

	// "/" as a corner case.
	ref, err := NewReference("/", "tag3")
	require.NoError(t, err)
	assert.Equal(t, []string{}, ref.PolicyConfigurationNamespaces())
}
Example #7
0
func TestContainerStatsToPoints(t *testing.T) {
	// Given
	storage, err := createTestStorage()
	require.Nil(t, err)
	require.NotNil(t, storage)

	ref, stats := createTestStats()
	require.Nil(t, err)
	require.NotNil(t, stats)

	// When
	points := storage.containerStatsToPoints(*ref, stats)

	// Then
	assert.NotEmpty(t, points)
	assert.Len(t, points, 10+len(stats.Cpu.Usage.PerCpu))

	assertContainsPointWithValue(t, points, serCpuUsageTotal, stats.Cpu.Usage.Total)
	assertContainsPointWithValue(t, points, serCpuUsageSystem, stats.Cpu.Usage.System)
	assertContainsPointWithValue(t, points, serCpuUsageUser, stats.Cpu.Usage.User)
	assertContainsPointWithValue(t, points, serMemoryUsage, stats.Memory.Usage)
	assertContainsPointWithValue(t, points, serLoadAverage, stats.Cpu.LoadAverage)
	assertContainsPointWithValue(t, points, serMemoryWorkingSet, stats.Memory.WorkingSet)
	assertContainsPointWithValue(t, points, serRxBytes, stats.Network.RxBytes)
	assertContainsPointWithValue(t, points, serRxErrors, stats.Network.RxErrors)
	assertContainsPointWithValue(t, points, serTxBytes, stats.Network.TxBytes)
	assertContainsPointWithValue(t, points, serTxBytes, stats.Network.TxErrors)

	for _, cpu_usage := range stats.Cpu.Usage.PerCpu {
		assertContainsPointWithValue(t, points, serCpuUsagePerCpu, cpu_usage)
	}
}
// TestListWorkItemLinkTypeOK tests if we can find the work item link types
// "test-bug-blocker" and "test-related" in the list of work item link types
func (s *workItemLinkTypeSuite) TestListWorkItemLinkTypeOK() {
	bugBlockerPayload := s.createDemoLinkType("test-bug-blocker")
	_, bugBlockerType := test.CreateWorkItemLinkTypeCreated(s.T(), nil, nil, s.linkTypeCtrl, bugBlockerPayload)
	require.NotNil(s.T(), bugBlockerType)

	relatedPayload := CreateWorkItemLinkType("test-related", workitem.SystemBug, workitem.SystemBug, bugBlockerType.Data.Relationships.LinkCategory.Data.ID)
	_, relatedType := test.CreateWorkItemLinkTypeCreated(s.T(), nil, nil, s.linkTypeCtrl, relatedPayload)
	require.NotNil(s.T(), relatedType)

	// Fetch a single work item link type
	_, linkTypeCollection := test.ListWorkItemLinkTypeOK(s.T(), nil, nil, s.linkTypeCtrl)
	require.NotNil(s.T(), linkTypeCollection)
	require.Nil(s.T(), linkTypeCollection.Validate())
	// Check the number of found work item link types
	require.NotNil(s.T(), linkTypeCollection.Data)
	require.Condition(s.T(), func() bool {
		return (len(linkTypeCollection.Data) >= 2)
	}, "At least two work item link types must exist (bug-blocker and related), but only %d exist.", len(linkTypeCollection.Data))
	// Search for the work item types that must exist at minimum
	toBeFound := 2
	for i := 0; i < len(linkTypeCollection.Data) && toBeFound > 0; i++ {
		if *linkTypeCollection.Data[i].Attributes.Name == "test-bug-blocker" || *linkTypeCollection.Data[i].Attributes.Name == "test-related" {
			s.T().Log("Found work item link type in collection: ", *linkTypeCollection.Data[i].Attributes.Name)
			toBeFound--
		}
	}
	require.Exactly(s.T(), 0, toBeFound, "Not all required work item link types (bug-blocker and related) where found.")

	// Check that the link categories are included in the response in the "included" array
	require.Len(s.T(), linkTypeCollection.Included, 1, "The work item link type should include it's work item link category.")
	categoryData, ok := linkTypeCollection.Included[0].(*app.WorkItemLinkCategoryData)
	require.True(s.T(), ok)
	require.Equal(s.T(), "test-user", *categoryData.Attributes.Name, "The work item link type's category should have the name 'test-user'.")
}
// The SetupSuite method will run before the tests in the suite are run.
// It sets up a database connection for all the tests in this suite without polluting global space.
func (s *workItemLinkTypeSuite) SetupSuite() {
	var err error

	if err = configuration.Setup(""); err != nil {
		panic(fmt.Errorf("Failed to setup the configuration: %s", err.Error()))
	}

	s.db, err = gorm.Open("postgres", configuration.GetPostgresConfigString())

	if err != nil {
		panic("Failed to connect database: " + err.Error())
	}

	// Make sure the database is populated with the correct types (e.g. bug etc.)
	if err := models.Transactional(DB, func(tx *gorm.DB) error {
		return migration.PopulateCommonTypes(context.Background(), tx, workitem.NewWorkItemTypeRepository(tx))
	}); err != nil {
		panic(err.Error())
	}

	svc := goa.New("workItemLinkTypeSuite-Service")
	require.NotNil(s.T(), svc)
	s.linkTypeCtrl = NewWorkItemLinkTypeController(svc, gormapplication.NewGormDB(DB))
	require.NotNil(s.T(), s.linkTypeCtrl)
	s.linkCatCtrl = NewWorkItemLinkCategoryController(svc, gormapplication.NewGormDB(DB))
	require.NotNil(s.T(), s.linkCatCtrl)
	//	s.typeCtrl = NewWorkitemtypeController(svc, gormapplication.NewGormDB(DB))
	//	require.NotNil(s.T(), s.typeCtrl)
}
func TestManifestBuildDPNIdentifierEvent(t *testing.T) {
	nsqMessage := testutil.MakeNsqMessage("999")
	manifest := models.NewDPNIngestManifest(nsqMessage)
	manifest.StorageURL = "https://example.com/my_bag.tar"

	// Should give error, since DPNBag is nil
	event, err := manifest.BuildDPNIdentifierEvent()
	require.NotNil(t, err)

	manifest.DPNBag = testutil.MakeDPNBag()
	event, err = manifest.BuildDPNIdentifierEvent()
	require.Nil(t, err)
	require.NotNil(t, event)

	assert.True(t, util.LooksLikeUUID(event.Identifier))
	assert.Equal(t, constants.EventIdentifierAssignment, event.EventType)
	assert.False(t, event.DateTime.IsZero())
	assert.Equal(t, "Item assigned DPN UUID", event.Detail)
	assert.Equal(t, constants.StatusSuccess, event.Outcome)
	assert.Equal(t, manifest.DPNBag.UUID, event.OutcomeDetail)
	assert.Equal(t, "APTrust exchange using Satori go.uuid", event.Object)
	assert.Equal(t, "https://github.com/satori/go.uuid", event.Agent)
	assert.True(t, strings.Contains(event.OutcomeInformation, manifest.DPNBag.UUID))
	assert.Equal(t, manifest.DPNBag.LocalId, event.IntellectualObjectIdentifier)
}
func (s *workItemTypeRepoBlackBoxTest) TestCreateLoadWITWithList() {
	bt := "string"
	wit, err := s.repo.Create(context.Background(), nil, "foo_bar", map[string]app.FieldDefinition{
		"foo": {
			Required: true,
			Type: &app.FieldType{
				ComponentType: &bt,
				Kind:          string(workitem.KindList),
			},
		},
	})
	assert.Nil(s.T(), err)
	assert.NotNil(s.T(), wit)

	wit3, err := s.repo.Create(context.Background(), nil, "foo_bar", map[string]app.FieldDefinition{})
	assert.IsType(s.T(), errors.BadParameterError{}, errs.Cause(err))
	assert.Nil(s.T(), wit3)

	wit2, err := s.repo.Load(context.Background(), "foo_bar")
	assert.Nil(s.T(), err)
	require.NotNil(s.T(), wit2)
	field := wit2.Fields["foo"]
	require.NotNil(s.T(), field)
	assert.Equal(s.T(), string(workitem.KindList), field.Type.Kind)
	assert.Equal(s.T(), true, field.Required)
	assert.Nil(s.T(), field.Type.BaseType)
	assert.Nil(s.T(), field.Type.Values)
}
func TestManifestBuildReplicationTransfer(t *testing.T) {
	nsqMessage := testutil.MakeNsqMessage("999")
	manifest := models.NewDPNIngestManifest(nsqMessage)
	fromNode := "aptrust"
	toNode := "chron"
	link := "[email protected]:outbound/1234567.tar"
	xfer, err := manifest.BuildReplicationTransfer(fromNode, toNode, link)
	// No DPNBag attached to manifest. Should get error.
	require.NotNil(t, err)

	manifest.DPNBag = testutil.MakeDPNBag()
	xfer, err = manifest.BuildReplicationTransfer(fromNode, toNode, link)
	require.Nil(t, err)
	require.NotNil(t, xfer)

	assert.Equal(t, fromNode, xfer.FromNode)
	assert.Equal(t, toNode, xfer.ToNode)
	assert.Equal(t, manifest.DPNBag.UUID, xfer.Bag)
	assert.True(t, util.LooksLikeUUID(xfer.ReplicationId))

	assert.Equal(t, constants.AlgSha256, xfer.FixityAlgorithm)
	assert.Nil(t, xfer.FixityNonce)
	assert.Nil(t, xfer.FixityValue)
	assert.Equal(t, "rsync", xfer.Protocol)
	assert.Equal(t, link, xfer.Link)
	assert.False(t, xfer.CreatedAt.IsZero())
	assert.False(t, xfer.UpdatedAt.IsZero())
}
Example #13
0
func TestBuildIngestChecksums(t *testing.T) {
	gf := testutil.MakeGenericFile(0, 0, "test.edu/test_bag/file.txt")
	assert.Equal(t, 0, len(gf.Checksums))
	err := gf.BuildIngestChecksums()
	assert.Nil(t, err)
	assert.Equal(t, 2, len(gf.Checksums))
	md5 := gf.GetChecksumByAlgorithm(constants.AlgMd5)
	sha256 := gf.GetChecksumByAlgorithm(constants.AlgSha256)
	require.NotNil(t, md5)
	require.NotNil(t, sha256)

	assert.Equal(t, md5.GenericFileId, gf.Id)
	assert.Equal(t, constants.AlgMd5, md5.Algorithm)
	assert.False(t, md5.DateTime.IsZero())
	assert.Equal(t, 32, len(md5.Digest))

	assert.Equal(t, sha256.GenericFileId, gf.Id)
	assert.Equal(t, constants.AlgSha256, sha256.Algorithm)
	assert.False(t, sha256.DateTime.IsZero())
	assert.Equal(t, 64, len(sha256.Digest))

	// Calling this function again should not generate new checksums
	// when all the checksums are already present.
	err = gf.BuildIngestChecksums()
	assert.Nil(t, err)
	assert.Equal(t, 2, len(gf.Checksums))
}
Example #14
0
func TestGenerate(t *testing.T) {
	cg := HMACSHAEnigma{
		GlobalSecret: []byte("12345678901234567890"),
	}

	token, signature, err := cg.Generate([]byte("09876543210987654321"))
	require.Nil(t, err, "%s", err)
	require.NotEmpty(t, token)
	require.NotEmpty(t, signature)
	t.Logf("Token: %s\n Signature: %s", token, signature)

	validateSignature, err := cg.Validate([]byte("09876543210987654321"), token)
	require.Nil(t, err, "%s", err)
	assert.Equal(t, signature, validateSignature)

	_, err = cg.Validate([]byte("bar"), token)
	require.NotNil(t, err, "%s", err)

	_, err = cg.Validate([]byte("baz"), token)
	require.NotNil(t, err, "%s", err)

	cg.GlobalSecret = []byte("baz")
	_, err = cg.Validate([]byte("bar"), token)
	require.NotNil(t, err, "%s", err)
}
Example #15
0
func TestRefreshOperations(t *testing.T) {
	client := &osin.DefaultClient{Id: "4", Secret: "secret", RedirectUri: "http://localhost/", UserData: ""}
	type test struct {
		access *osin.AccessData
	}

	for k, c := range []*test{
		{
			access: &osin.AccessData{
				Client: client,
				AuthorizeData: &osin.AuthorizeData{
					Client:      client,
					Code:        uuid.New(),
					ExpiresIn:   int32(60),
					Scope:       "scope",
					RedirectUri: "http://localhost/",
					State:       "state",
					CreatedAt:   time.Now().Round(time.Second),
					UserData:    userDataMock,
				},
				AccessData:   nil,
				AccessToken:  uuid.New(),
				RefreshToken: uuid.New(),
				ExpiresIn:    int32(60),
				Scope:        "scope",
				RedirectUri:  "https://localhost/",
				CreatedAt:    time.Now().Round(time.Second),
				UserData:     userDataMock,
			},
		},
	} {
		createClient(t, store, client)
		require.Nil(t, store.SaveAuthorize(c.access.AuthorizeData), "Case %d", k)
		require.Nil(t, store.SaveAccess(c.access), "Case %d", k)

		result, err := store.LoadRefresh(c.access.RefreshToken)
		require.Nil(t, err)
		require.Equal(t, c.access.CreatedAt.Unix(), result.CreatedAt.Unix())
		require.Equal(t, c.access.AuthorizeData.CreatedAt.Unix(), result.AuthorizeData.CreatedAt.Unix())
		c.access.CreatedAt = result.CreatedAt
		c.access.AuthorizeData.CreatedAt = result.AuthorizeData.CreatedAt
		require.Equal(t, c.access, result, "Case %d", k)

		require.Nil(t, store.RemoveRefresh(c.access.RefreshToken))
		_, err = store.LoadRefresh(c.access.RefreshToken)

		require.NotNil(t, err, "Case %d", k)
		require.Nil(t, store.RemoveAccess(c.access.AccessToken), "Case %d", k)
		require.Nil(t, store.SaveAccess(c.access), "Case %d", k)

		_, err = store.LoadRefresh(c.access.RefreshToken)
		require.Nil(t, err, "Case %d", k)

		require.Nil(t, store.RemoveAccess(c.access.AccessToken), "Case %d", k)
		_, err = store.LoadRefresh(c.access.RefreshToken)
		require.NotNil(t, err, "Case %d", k)

	}
	removeClient(t, store, client)
}
Example #16
0
func TestGenerate(t *testing.T) {
	cg := HMACSHAEnigma{
		GlobalSecret: []byte("12345678901234567890"),
	}

	challenge, err := cg.GenerateChallenge([]byte("09876543210987654321"))
	require.Nil(t, err, "%s", err)
	require.NotNil(t, challenge)
	t.Logf("%s.%s", challenge.Key, challenge.Signature)

	err = cg.ValidateChallenge([]byte("09876543210987654321"), challenge)
	require.Nil(t, err, "%s", err)

	challenge.FromString(challenge.String())

	err = cg.ValidateChallenge([]byte("09876543210987654321"), challenge)
	require.Nil(t, err, "%s", err)

	err = cg.ValidateChallenge([]byte("bar"), challenge)
	require.NotNil(t, err, "%s", err)

	err = cg.ValidateChallenge([]byte("baz"), challenge)
	require.NotNil(t, err, "%s", err)

	cg.GlobalSecret = []byte("baz")
	err = cg.ValidateChallenge([]byte("bar"), challenge)
	require.NotNil(t, err, "%s", err)
}
Example #17
0
func TestDockerFilesystemStats(t *testing.T) {
	fm := framework.New(t)
	defer fm.Cleanup()

	storageDriver := fm.Docker().StorageDriver()
	switch storageDriver {
	case framework.Aufs:
	case framework.Overlay:
	default:
		t.Skip("skipping filesystem stats test")
	}
	// Wait for the container to show up.
	containerId := fm.Docker().RunBusybox("/bin/sh", "-c", "dd if=/dev/zero of=/file count=1 bs=1M & ping www.google.com")
	waitForContainer(containerId, fm)
	time.Sleep(time.Minute)
	request := &v2.RequestOptions{
		IdType: v2.TypeDocker,
		Count:  1,
	}
	containerInfo, err := fm.Cadvisor().ClientV2().Stats(containerId, request)
	time.Sleep(time.Minute)
	require.NoError(t, err)
	require.True(t, len(containerInfo) == 1)
	var info v2.ContainerInfo
	for _, cInfo := range containerInfo {
		info = cInfo
	}
	sanityCheckV2(containerId, info, t)
	require.NotNil(t, info.Stats[0].Filesystem.BaseUsageBytes)
	assert.True(t, *info.Stats[0].Filesystem.BaseUsageBytes > (1<<6), "expected base fs usage to be greater than 1MB")
	require.NotNil(t, info.Stats[0].Filesystem.TotalUsageBytes)
	assert.True(t, *info.Stats[0].Filesystem.TotalUsageBytes > (1<<6), "expected total fs usage to be greater than 1MB")
}
Example #18
0
func TestCreateFeed(t *testing.T) {
	feedTitle := fmt.Sprintf("test-feed-%d", rand.Intn(1000000000))
	url := _url("/feeds/" + feedTitle)

	res, err := http.Get(url)
	require.Nil(t, err)
	require.Equal(t, res.StatusCode, 404)

	entryStr := "<entry><title>foo</title><content>bar</content></entry>"
	buf := bytes.NewBufferString(entryStr)
	res, err = http.Post(url, "application/atom+xml", buf)
	require.Nil(t, err)
	require.Equal(t, res.StatusCode, 201, "created feed")

	res, err = http.Get(url)
	require.Nil(t, err)
	require.Equal(t, res.StatusCode, 200, "got feed")

	feed, err := atom.DecodeFeed(res.Body)
	require.Nil(t, err, "parsed feed")
	assert.Equal(t, feed.Title.Raw, feedTitle, "feed title")
	require.NotNil(t, feed.Updated, "feed.updated")
	assert.Regexp(t, timeRe, *feed.Updated, "feed.updated")

	require.Equal(t, len(feed.Entries), 1, "got 1 entry")
	entry := feed.Entries[0]
	assert.Equal(t, entry.Content.Raw, "bar", "entry content")
	require.NotNil(t, entry.Updated, "entry.updated")
	assert.Regexp(t, timeRe, *entry.Updated, "entry.updated")
}
Example #19
0
func TestParseGenericTimestamp(t *testing.T) {
	p := &Parser{
		Patterns: []string{`\[%{HTTPDATE:ts:ts}\] response_time=%{POSINT:response_time:int} mymetric=%{NUMBER:metric:float}`},
	}
	assert.NoError(t, p.Compile())

	metricA, err := p.ParseLine(`[09/Jun/2016:03:37:03 +0000] response_time=20821 mymetric=10890.645`)
	require.NotNil(t, metricA)
	assert.NoError(t, err)
	assert.Equal(t,
		map[string]interface{}{
			"response_time": int64(20821),
			"metric":        float64(10890.645),
		},
		metricA.Fields())
	assert.Equal(t, map[string]string{}, metricA.Tags())
	assert.Equal(t, time.Unix(1465443423, 0).UTC(), metricA.Time().UTC())

	metricB, err := p.ParseLine(`[09/Jun/2016:03:37:04 +0000] response_time=20821 mymetric=10890.645`)
	require.NotNil(t, metricB)
	assert.NoError(t, err)
	assert.Equal(t,
		map[string]interface{}{
			"response_time": int64(20821),
			"metric":        float64(10890.645),
		},
		metricB.Fields())
	assert.Equal(t, map[string]string{}, metricB.Tags())
	assert.Equal(t, time.Unix(1465443424, 0).UTC(), metricB.Time().UTC())
}
Example #20
0
func snapEnumerate(t *testing.T, ctx *Context) {
	fmt.Println("snapEnumerate")

	snaps, err := ctx.SnapEnumerate(nil, nil)
	require.NoError(t, err, "Failed in snapEnumerate")
	require.NotNil(t, snaps, "Nil snaps")
	require.Equal(t, 1, len(snaps), "Expect 1 snaps actual %v snaps", len(snaps))
	require.Equal(t, snaps[0].Id, ctx.snapID, "Expect snapID %v actual %v", ctx.snapID, snaps[0].Id)
	labels := snaps[0].Locator.VolumeLabels

	snaps, err = ctx.SnapEnumerate([]string{ctx.volID}, nil)
	require.NoError(t, err, "Failed in snapEnumerate")
	require.NotNil(t, snaps, "Nil snaps")
	require.Equal(t, len(snaps), 1, "Expect 1 snap actual %v snaps", len(snaps))
	require.Equal(t, snaps[0].Id, ctx.snapID, "Expect snapID %v actual %v", ctx.snapID, snaps[0].Id)

	snaps, err = ctx.SnapEnumerate([]string{string("shouldNotExist")}, nil)
	require.Equal(t, len(snaps), 0, "Expect 0 snap actual %v snaps", len(snaps))

	snaps, err = ctx.SnapEnumerate(nil, labels)
	require.NoError(t, err, "Failed in snapEnumerate")
	require.NotNil(t, snaps, "Nil snaps")
	require.Equal(t, len(snaps), 1, "Expect 1 snap actual %v snaps", len(snaps))
	require.Equal(t, snaps[0].Id, ctx.snapID, "Expect snapID %v actual %v", ctx.snapID, snaps[0].Id)
}
Example #21
0
func TestService_RegisterVerification(t *testing.T) {
	// Testing whether we sign correctly the SkipBlocks
	onet.RegisterNewService("ServiceVerify", newServiceVerify)
	local := onet.NewLocalTest()
	defer local.CloseAll()
	hosts, el, s1 := makeHELS(local, 3)
	VerifyTest := VerifierID(uuid.NewV5(uuid.NamespaceURL, "Test1"))
	ver := make(chan bool, 3)
	verifier := func(msg []byte, s *SkipBlock) bool {
		ver <- true
		return true
	}
	for _, h := range hosts {
		s := h.GetService(ServiceName).(*Service)
		log.ErrFatal(s.RegisterVerification(VerifyTest, verifier))
	}
	sb, err := makeGenesisRosterArgs(s1, el, nil, VerifyTest, 1, 1)
	log.ErrFatal(err)
	require.NotNil(t, sb.Data)
	require.Equal(t, 3, len(ver))

	sb, err = makeGenesisRosterArgs(s1, el, nil, ServiceVerifier, 1, 1)
	log.ErrFatal(err)
	require.NotNil(t, sb.Data)
	require.Equal(t, 3, len(ServiceVerifierChan))
}
Example #22
0
func BenchmarkAppendLog(b *testing.B) {
	bucketName := "bucketName"
	ownerName := "ownerName"
	artifactName := "artifactName"

	if testing.Short() {
		b.Skip("Skipping end-to-end test in short mode.")
	}

	client := setup(b)

	bucket, _ := client.NewBucket(bucketName, ownerName, 31)
	require.NotNil(b, bucket)

	artifact, _ := bucket.NewChunkedArtifact(artifactName)
	require.NotNil(b, artifact)

	// Typical logchunk of size ~4KB
	str := gen4KString()

	// Each 4K chunks sent takes ~1.3ms on a 4-core laptop (with synchronous_commit=off,fsync=off in
	// Postgres)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		artifact.AppendLog(str)
	}
	// Make sure all logchunks are sent to API/DB.
	artifact.Flush()
}
func TestObjBuildIngestChecksums(t *testing.T) {
	// Make intel obj with 5 files, no events, checksums or tags
	obj := testutil.MakeIntellectualObject(5, 0, 0, 0)
	assert.Equal(t, 5, len(obj.GenericFiles))
	assert.Equal(t, 0, len(obj.PremisEvents))

	err := obj.BuildIngestChecksums()
	assert.Nil(t, err)

	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 2, len(gf.Checksums))
		md5 := gf.GetChecksumByAlgorithm(constants.AlgMd5)
		sha256 := gf.GetChecksumByAlgorithm(constants.AlgSha256)
		require.NotNil(t, md5)
		require.NotNil(t, sha256)

		assert.Equal(t, md5.GenericFileId, gf.Id)
		assert.Equal(t, constants.AlgMd5, md5.Algorithm)
		assert.False(t, md5.DateTime.IsZero())
		assert.Equal(t, 32, len(md5.Digest))

		assert.Equal(t, sha256.GenericFileId, gf.Id)
		assert.Equal(t, constants.AlgSha256, sha256.Algorithm)
		assert.False(t, sha256.DateTime.IsZero())
		assert.Equal(t, 64, len(sha256.Digest))
	}

	// Calling this function again should not generate new checksums
	// when all the checksums are already present.
	err = obj.BuildIngestChecksums()
	assert.Nil(t, err)
	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 2, len(gf.Checksums))
	}
}
Example #24
0
func TestFileManager(t *testing.T) {
	mongo, dbName, err := tests.RandomTestMongoUp()
	if err != nil {
		t.Fatal(err)
	}
	defer tests.RandomTestMongoDown(mongo, dbName)
	println("Up test mongodb", dbName)

	mgr := New(mongo.DB(dbName))

	data := []byte("data")

	meta, err := mgr.Files.Create(bytes.NewBuffer(data), &file.Meta{Name: "file1.txt", ContentType: "text/plain"})

	require.NoError(t, err)
	require.NotNil(t, meta)
	assert.Equal(t, len(data), meta.Size)
	assert.Equal(t, "file1.txt", meta.Name)
	assert.Equal(t, "text/plain", meta.ContentType)

	f, err := mgr.Files.GetById(meta.Id)
	require.NoError(t, err)
	defer f.Close()
	require.NotNil(t, f)
	require.Equal(t, meta, f.Meta)

	_, err = mgr.Files.GetById("bad id")
	require.Error(t, err)
}
Example #25
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)
}
Example #26
0
func TestGetContribs(t *testing.T) {
	server := httptest.NewServer(
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, contribs)
		}),
	)
	defer server.Close()

	cfg := libs.NewConfig()
	gh := libs.NewGithub(cfg)
	require.NotNil(t, gh)

	gh.DocGetter = func(c *libs.Config) (*goquery.Document, error) {
		return goquery.NewDocument(server.URL)
	}

	gc, err := gh.GetContribs()
	require.Nil(t, err)
	require.NotNil(t, gc)

	b, err := json.Marshal(gc)
	require.Nil(t, err)
	require.NotEmpty(t, b)

	var dst bytes.Buffer
	json.Indent(&dst, b, "", "  ")

	assert.Equal(t, githubContribs, dst.String())
}
Example #27
0
// Check that torrent Info is obtained from the metainfo file cache.
func TestAddTorrentMetainfoInCache(t *testing.T) {
	cfg := TestingConfig
	cfg.DisableMetainfoCache = false
	cfg.ConfigDir, _ = ioutil.TempDir(os.TempDir(), "")
	defer os.RemoveAll(cfg.ConfigDir)
	cl, err := NewClient(&cfg)
	require.NoError(t, err)
	defer cl.Close()
	dir, mi := testutil.GreetingTestTorrent()
	defer os.RemoveAll(dir)
	tt, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
	require.NoError(t, err)
	require.True(t, new)
	require.NotNil(t, tt.Info())
	_, err = os.Stat(filepath.Join(cfg.ConfigDir, "torrents", fmt.Sprintf("%x.torrent", mi.Info.Hash)))
	require.NoError(t, err)
	// Contains only the infohash.
	var ts TorrentSpec
	missinggo.CopyExact(&ts.InfoHash, mi.Info.Hash)
	_, ok := cl.Torrent(ts.InfoHash)
	require.True(t, ok)
	tt.Drop()
	_, ok = cl.Torrent(ts.InfoHash)
	require.False(t, ok)
	tt, new, err = cl.AddTorrentSpec(&ts)
	require.NoError(t, err)
	require.True(t, new)
	// Obtained from the metainfo cache.
	require.NotNil(t, tt.Info())
}
Example #28
0
func TestGetRepos(t *testing.T) {
	mux := http.NewServeMux()
	server := httptest.NewServer(mux)
	defer server.Close()

	mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, repos)
	})

	cfg := libs.NewConfig()
	cfg.Github.Username = "******"
	cfg.Github.Token = "xxx"

	gh := libs.NewGithub(cfg)
	require.NotNil(t, gh)

	gh.Client = github.NewClient(nil)
	require.NotNil(t, gh.Client)

	url, _ := url.Parse(server.URL)
	gh.Client.BaseURL = url

	gr, err := gh.GetRepos()
	require.Nil(t, err)
	require.NotNil(t, gr)

	b, err := json.Marshal(gr)
	require.Nil(t, err)
	require.NotEmpty(t, b)

	var dst bytes.Buffer
	json.Indent(&dst, b, "", "  ")

	assert.Equal(t, githubRepos, dst.String())
}
// 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)
	}
}
// TestListWorkItemLinkCategoryOK tests if we can find the work item link categories
// "test-system" and "test-user" in the list of work item link categories
func (s *workItemLinkCategorySuite) TestListWorkItemLinkCategoryOK() {
	_, linkCatSystem := s.createWorkItemLinkCategorySystem()
	require.NotNil(s.T(), linkCatSystem)
	_, linkCatUser := s.createWorkItemLinkCategoryUser()
	require.NotNil(s.T(), linkCatUser)

	// Fetch a single work item link category
	_, linkCatCollection := test.ListWorkItemLinkCategoryOK(s.T(), nil, nil, s.linkCatCtrl)

	require.NotNil(s.T(), linkCatCollection)
	require.Nil(s.T(), linkCatCollection.Validate())

	// Check the number of found work item link categories
	require.NotNil(s.T(), linkCatCollection.Data)
	require.Condition(s.T(), func() bool {
		return (len(linkCatCollection.Data) >= 2)
	}, "At least two work item link categories must exist (system and user), but only %d exist.", len(linkCatCollection.Data))

	// Search for the work item types that must exist at minimum
	toBeFound := 2
	for i := 0; i < len(linkCatCollection.Data) && toBeFound > 0; i++ {
		if *linkCatCollection.Data[i].Attributes.Name == "test-system" || *linkCatCollection.Data[i].Attributes.Name == "test-user" {
			s.T().Log("Found work item link category in collection: ", *linkCatCollection.Data[i].Attributes.Name)
			toBeFound--
		}
	}
	require.Exactly(s.T(), 0, toBeFound, "Not all required work item link categories (system and user) where found.")
}