// Tests the processor in conjunction with the vcs.
func TestGoldProcessor(t *testing.T) {

	// Set up mock VCS and run a servcer with the given data directory.
	vcs := ingestion.MockVCS(TEST_COMMITS)
	server, serverAddr := ingestion.StartTraceDBTestServer(t, TRACE_DB_FILENAME)
	defer server.Stop()
	defer testutils.Remove(t, TRACE_DB_FILENAME)

	ingesterConf := &sharedconfig.IngesterConfig{
		ExtraParams: map[string]string{
			CONFIG_TRACESERVICE: serverAddr,
		},
	}

	// Set up the processor.
	processor, err := newGoldProcessor(vcs, ingesterConf, nil)
	assert.Nil(t, err)

	// Load the example file and process it.
	fsResult, err := ingestion.FileSystemResult(TEST_INGESTION_FILE, "./")
	assert.Nil(t, err)
	err = processor.Process(fsResult)
	assert.Nil(t, err)

	// Steal the traceDB used by the processor to verify the results.
	traceDB := processor.(*goldProcessor).traceDB

	startTime := time.Now().Add(-time.Hour * 24 * 10)
	commitIDs, err := traceDB.List(startTime, time.Now())
	assert.Nil(t, err)

	assert.Equal(t, 1, len(FilterCommitIDs(commitIDs, "master")))
	assert.Equal(t, 0, len(FilterCommitIDs(commitIDs, TEST_CODE_REVIEW_URL)))

	assert.Equal(t, 1, len(commitIDs))
	assert.Equal(t, &tracedb.CommitID{
		Timestamp: TEST_COMMITS[0].Timestamp.Unix(),
		ID:        TEST_COMMITS[0].Hash,
		Source:    "master",
	}, commitIDs[0])

	// Get a tile and make sure we have the right number of traces.
	tile, _, err := traceDB.TileFromCommits(commitIDs)
	assert.Nil(t, err)

	traces := tile.Traces
	assert.Equal(t, len(TEST_ENTRIES), len(traces))

	for _, testEntry := range TEST_ENTRIES {
		found, ok := traces[testEntry.key]
		assert.True(t, ok)
		goldTrace, ok := found.(*types.GoldenTrace)
		assert.True(t, ok)
		assert.Equal(t, 1, len(goldTrace.Values))
		assert.Equal(t, testEntry.value, goldTrace.Values[0])
	}

	assert.Equal(t, "master", commitIDs[0].Source)
	assert.Nil(t, traceDB.Close())
}
func TestTrybotPerfIngestion(t *testing.T) {
	b, err := ioutil.ReadFile(filepath.Join("testdata", "rietveld_response.txt"))
	assert.NoError(t, err)
	m := mockhttpclient.NewURLMock()
	m.Mock("https://codereview.chromium.org/api/1467533002/1", b)

	server, serverAddr := ingestion.StartTraceDBTestServer(t, "./trybot_test_trace.db")
	defer server.Stop()
	defer testutils.Remove(t, "./trybot_test_trace.db")

	ingesterConf := &sharedconfig.IngesterConfig{
		ExtraParams: map[string]string{
			CONFIG_TRACESERVICE: serverAddr,
		},
	}
	processor, err := newPerfTrybotProcessor(nil, ingesterConf, nil)
	assert.Nil(t, err)

	processor.(*perfTrybotProcessor).review = rietveld.New("https://codereview.chromium.org", m.Client())

	fsResult, err := ingestion.FileSystemResult(filepath.Join(TEST_DATA_DIR, "trybot.json"), TEST_DATA_DIR)
	assert.Nil(t, err)
	err = processor.Process(fsResult)
	assert.Nil(t, err)
	assert.Equal(t, 1, len(processor.(*perfTrybotProcessor).cache))

	// Steal the traceDB used by the processor to verify the results.
	traceDB := processor.(*perfTrybotProcessor).traceDB

	startTime := time.Time{}
	commitIDs, err := traceDB.List(startTime, time.Now())
	assert.Nil(t, err)

	assert.Equal(t, 1, len(commitIDs))
	assert.Equal(t, &tracedb.CommitID{
		Timestamp: 1448036640,
		ID:        "1",
		Source:    "https://codereview.chromium.org/1467533002",
	}, commitIDs[0])

	// Get a tile and make sure we have the right number of traces.
	tile, _, err := traceDB.TileFromCommits(commitIDs)
	assert.Nil(t, err)

	traces := tile.Traces
	assert.Equal(t, 2, len(traces))

	assert.Nil(t, traceDB.Close())
}
func TestDiff(t *testing.T) {
	fds := getTestFileDiffStore(t, "", true)
	diffFilePath := filepath.Join(fds.localDiffDir, TEST_DIGEST1[0:2], TEST_DIGEST1[2:4], fmt.Sprintf("%s-%s.%s", TEST_DIGEST1, TEST_DIGEST2, DIFF_EXTENSION))
	defer testutils.Remove(t, diffFilePath)
	diffMetrics, err := fds.diff(TEST_DIGEST1, TEST_DIGEST2)
	if err != nil {
		t.Error("Unexpected error: ", err)
	}
	// Assert that the diff file was created.
	if _, err := os.Stat(diffFilePath); err != nil {
		t.Errorf("Diff file %s was not created!", diffFilePath)
	}
	// Assert that the DiffMetrics are as expected.
	assert.Equal(t, relExpectedDiffMetrics1_2, diffMetrics)
}
func TestCacheImageFromGS(t *testing.T) {
	fds := getTestFileDiffStore(t, TESTDATA_DIR, true)
	imgFilePath := filepath.Join(fds.localImgDir, TEST_DIGEST3[0:2], TEST_DIGEST3[2:4], fmt.Sprintf("%s.%s", TEST_DIGEST3, IMG_EXTENSION))
	defer testutils.Remove(t, imgFilePath)

	err := fds.cacheImageFromGS(TEST_DIGEST3)
	assert.Nil(t, err)

	if _, err := os.Stat(imgFilePath); err != nil {
		t.Errorf("File %s was not created!", imgFilePath)
	}
	assert.Equal(t, int64(1), downloadSuccessCount.Count())

	// Test error and assert the download failures map.
	for i := 1; i < 6; i++ {
		if err := fds.cacheImageFromGS(MISSING_DIGEST); err == nil {
			t.Error("Was expecting 404 error for missing digest")
		}
		assert.Equal(t, int64(1), downloadSuccessCount.Count())
		assert.Equal(t, int64(i), downloadFailureCount.Count())
	}
}
func TestGet_e2e(t *testing.T) {
	// Empty digests to compare too.
	fdsEmpty := getTestFileDiffStore(t, TESTDATA_DIR, true)
	diffMetricsMapEmpty, err := fdsEmpty.Get(TEST_DIGEST1, []string{})
	assert.Nil(t, err)
	assert.Equal(t, 0, len(diffMetricsMapEmpty))

	// 2 files that exist locally, diffmetrics exists locally as well.
	fds1 := getTestFileDiffStore(t, TESTDATA_DIR, false)
	diffMetricsMap1, err := fds1.Get(TEST_DIGEST1, []string{TEST_DIGEST2})
	if err != nil {
		t.Error("Unexpected error: ", err)
	}
	assert.Equal(t, 1, len(diffMetricsMap1))
	assert.Equal(t, expectedDiffMetrics1_2, diffMetricsMap1[TEST_DIGEST2])
	assert.Equal(t, int64(0), downloadSuccessCount.Count())
	assert.Equal(t, int64(0), downloadFailureCount.Count())

	// 2 files that exist locally but diffmetrics does not exist.
	fds2 := getTestFileDiffStore(t, TESTDATA_DIR, false)
	diffBasename := fmt.Sprintf("%s-%s", TEST_DIGEST1, TEST_DIGEST2)
	diffFilePath := filepath.Join(fds2.localDiffDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFF_EXTENSION))
	diffMetricsFilePath := filepath.Join(fds2.localDiffMetricsDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFFMETRICS_EXTENSION))
	diffMetricsMap2, err := fds2.Get(TEST_DIGEST1, []string{TEST_DIGEST2})
	if err != nil {
		t.Error("Unexpected error: ", err)
	}

	defer testutils.Remove(t, diffFilePath)
	defer testutils.Remove(t, diffMetricsFilePath)

	awaitDiffMetricsGoroutine(diffMetricsFilePath, 3*time.Second)
	// Verify that the diff and the diffmetrics files were created.
	assertFileExists(diffFilePath, t)
	assertFileExists(diffMetricsFilePath, t)
	assert.Equal(t, 1, len(diffMetricsMap2))
	assert.Equal(t, expectedDiffMetrics1_2, diffMetricsMap2[TEST_DIGEST2])
	assert.Equal(t, int64(0), downloadSuccessCount.Count())
	assert.Equal(t, int64(0), downloadFailureCount.Count())

	// 1 file that exists locally, 1 file that exists in Google Storage, 1
	// file that does not exist.
	fds3 := getTestFileDiffStore(t, TESTDATA_DIR, false)
	newImageFilePath := filepath.Join(fds3.localImgDir, TEST_DIGEST3[0:2], TEST_DIGEST3[2:4], fmt.Sprintf("%s.%s", TEST_DIGEST3, IMG_EXTENSION))
	diffBasename = fmt.Sprintf("%s-%s", TEST_DIGEST3, TEST_DIGEST1)
	diffFilePath = filepath.Join(fds3.localDiffDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFF_EXTENSION))
	diffMetricsFilePath = filepath.Join(fds3.localDiffMetricsDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFFMETRICS_EXTENSION))
	diffMetricsMap3, err := fds3.Get(TEST_DIGEST1, []string{TEST_DIGEST3, MISSING_DIGEST})
	if err != nil {
		t.Error("Unexpected error: ", err)
	}

	defer testutils.Remove(t, newImageFilePath)

	awaitDiffMetricsGoroutine(diffMetricsFilePath, 3*time.Second)
	// Verify that the image was downloaded successfully from Google Storage and
	// that the diff and diffmetrics files were created.
	assertFileExists(newImageFilePath, t)
	assertFileExists(diffFilePath, t)
	assertFileExists(diffMetricsFilePath, t)
	assert.Equal(t, 1, len(diffMetricsMap3))
	assert.Equal(t, expectedDiffMetrics1_3, diffMetricsMap3[TEST_DIGEST3])
	assert.Equal(t, int64(1), downloadSuccessCount.Count())
	assert.Equal(t, int64(1), downloadFailureCount.Count())

	// Call Get with multiple digests.
	fds5 := getTestFileDiffStore(t, TESTDATA_DIR, false)
	newImageFilePath = filepath.Join(fds5.localImgDir, TEST_DIGEST3[0:2], TEST_DIGEST3[2:4], fmt.Sprintf("%s.%s", TEST_DIGEST3, IMG_EXTENSION))
	diffBasename = fmt.Sprintf("%s-%s", TEST_DIGEST3, TEST_DIGEST1)
	diffFilePath = filepath.Join(fds5.localDiffDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFF_EXTENSION))
	diffMetricsFilePath = filepath.Join(fds5.localDiffMetricsDir, diffBasename[0:2], diffBasename[2:4], fmt.Sprintf("%s.%s", diffBasename, DIFFMETRICS_EXTENSION))
	diffMetricsMap5, err := fds5.Get(TEST_DIGEST1, []string{TEST_DIGEST2, TEST_DIGEST3, MISSING_DIGEST})
	if err != nil {
		t.Error("Unexpected error: ", err)
	}

	awaitDiffMetricsGoroutine(diffMetricsFilePath, 3*time.Second)
	// Verify that the image was downloaded successfully from Google Storage and
	// that the diff and diffmetrics files were created.
	assertFileExists(newImageFilePath, t)
	assertFileExists(diffFilePath, t)
	assertFileExists(diffMetricsFilePath, t)
	assert.Equal(t, 2, len(diffMetricsMap5))
	assert.Equal(t, expectedDiffMetrics1_2, diffMetricsMap5[TEST_DIGEST2])
	assert.Equal(t, expectedDiffMetrics1_3, diffMetricsMap5[TEST_DIGEST3])
	assert.Equal(t, int64(1), downloadFailureCount.Count())

	// diffFilePath, diffMetricsFilePath, and newImageFilePath will be removed
	// by the deferred testutils.Remove calls above.
}
Beispiel #6
0
// Tests the processor in conjunction with the vcs.
func TestTrybotGoldProcessor(t *testing.T) {
	// Set up mock VCS and run a servcer with the given data directory.
	vcs := ingestion.MockVCS(TEST_COMMITS)
	server, serverAddr := ingestion.StartTraceDBTestServer(t, TRYBOT_TRACE_DB_FILE)
	defer server.Stop()
	defer testutils.Remove(t, TRYBOT_TRACE_DB_FILE)

	ingesterConf := &sharedconfig.IngesterConfig{
		ExtraParams: map[string]string{
			CONFIG_TRACESERVICE:    serverAddr,
			CONFIG_CODE_REVIEW_URL: TEST_CODE_REVIEW_URL,
		},
	}

	// Set up the processor.
	processor, err := newGoldTrybotProcessor(vcs, ingesterConf)
	assert.Nil(t, err)

	// Load the example file and process it.
	fsResult, err := ingestion.FileSystemResult(TRYBOT_INGESTION_FILE)
	assert.Nil(t, err)
	err = processor.Process(fsResult)
	assert.Nil(t, err)

	// Steal the traceDB used by the processor to verify the results.
	traceDB := processor.(*goldTrybotProcessor).traceDB

	// The timestamp for the issue/patchset in the testfile is 1443718869.
	startTime := time.Unix(1443718868, 0)
	commitIDs, err := traceDB.List(startTime, time.Now())
	assert.Nil(t, err)

	assert.Equal(t, 1, len(FilterCommitIDs(commitIDs, TRYBOT_SRC)))
	assert.Equal(t, 0, len(FilterCommitIDs(commitIDs, VCS_SRC)))

	assert.Equal(t, 1, len(commitIDs))
	assert.Equal(t, &tracedb.CommitID{
		Timestamp: time.Unix(1443718869, 0),
		ID:        "1",
		Source:    string(TRYBOT_SRC) + TEST_CODE_REVIEW_URL + "/1381483003",
	}, commitIDs[0])

	// Get a tile and make sure we have the right number of traces.
	tile, err := traceDB.TileFromCommits(commitIDs)
	assert.Nil(t, err)

	traces := tile.Traces
	assert.Equal(t, len(TEST_ENTRIES), len(traces))

	for _, testEntry := range TEST_ENTRIES {
		found, ok := traces[testEntry.key]
		assert.True(t, ok)
		goldTrace, ok := found.(*types.GoldenTrace)
		assert.True(t, ok)
		assert.Equal(t, 1, len(goldTrace.Values))
		assert.Equal(t, testEntry.value, goldTrace.Values[0])
	}

	// Make sure the prefix is stripped correctly.
	StripSourcePrefix(commitIDs)
	assert.Equal(t, TEST_CODE_REVIEW_URL+"/1381483003", commitIDs[0].Source)

	assert.Nil(t, traceDB.Close())
}