blob: 60a2e4fe6200fe3bd884adf2a0eba363fb37a144 [file] [log] [blame]
package recent_rolls
import (
"context"
"testing"
"time"
assert "github.com/stretchr/testify/require"
"go.skia.org/infra/go/autoroll"
"go.skia.org/infra/go/deepequal"
"go.skia.org/infra/go/ds"
"go.skia.org/infra/go/ds/testutil"
"go.skia.org/infra/go/testutils"
)
// TestRecentRolls verifies that we correctly track mode history.
func TestRecentRolls(t *testing.T) {
testutils.LargeTest(t)
ctx := context.Background()
testutil.InitDatastore(t, ds.KIND_AUTOROLL_ROLL)
// Create the RecentRolls.
r, err := NewRecentRolls(ctx, "test-roller")
assert.NoError(t, err)
// Use this function for checking expectations.
check := func(current, last *autoroll.AutoRollIssue, history []*autoroll.AutoRollIssue) {
deepequal.AssertDeepEqual(t, current, r.CurrentRoll())
deepequal.AssertDeepEqual(t, last, r.LastRoll())
deepequal.AssertDeepEqual(t, history, r.GetRecentRolls())
}
// Add one issue.
now := time.Now().UTC()
ari1 := &autoroll.AutoRollIssue{
Closed: false,
Committed: false,
CommitQueue: true,
Created: now,
Issue: 1010101,
Modified: now,
Patchsets: []int64{1},
Result: autoroll.ROLL_RESULT_IN_PROGRESS,
Subject: "FAKE DEPS ROLL 1",
TryResults: []*autoroll.TryResult(nil),
}
expect := []*autoroll.AutoRollIssue{ari1}
assert.NoError(t, r.Add(ctx, ari1))
check(ari1, nil, expect)
// Try to add it again. We should log an error but not fail.
assert.NoError(t, r.Add(ctx, ari1))
check(ari1, nil, expect)
// Close the issue as successful. Ensure that it's now the last roll
// instead of the current roll.
ari1.Closed = true
ari1.Committed = true
ari1.CommitQueue = false
ari1.Result = autoroll.ROLL_RESULT_SUCCESS
assert.NoError(t, r.Update(ctx, ari1))
check(nil, ari1, expect)
// Add another issue. Ensure that it's the current roll with the
// previously-added roll as the last roll.
now = time.Now().UTC()
ari2 := &autoroll.AutoRollIssue{
Closed: false,
Committed: false,
CommitQueue: true,
Created: now,
Issue: 1010102,
Modified: now,
Patchsets: []int64{1},
Result: autoroll.ROLL_RESULT_IN_PROGRESS,
Subject: "FAKE DEPS ROLL 2",
TryResults: []*autoroll.TryResult(nil),
}
assert.NoError(t, r.Add(ctx, ari2))
expect = []*autoroll.AutoRollIssue{ari2, ari1}
check(ari2, ari1, expect)
// Try to add another active issue. We should log an error but not fail.
now = time.Now().UTC()
ari3 := &autoroll.AutoRollIssue{
Closed: false,
Committed: false,
CommitQueue: true,
Created: now,
Issue: 1010103,
Modified: now,
Patchsets: []int64{1},
Result: autoroll.ROLL_RESULT_IN_PROGRESS,
Subject: "FAKE DEPS ROLL 3",
TryResults: []*autoroll.TryResult(nil),
}
assert.NoError(t, r.Add(ctx, ari3))
expect = []*autoroll.AutoRollIssue{ari3, ari2, ari1}
check(ari3, ari2, expect)
// Close the issue as failed. Ensure that it's now the last roll
// instead of the current roll.
ari2.Closed = true
ari2.Committed = false
ari2.CommitQueue = false
ari2.Result = autoroll.ROLL_RESULT_FAILURE
assert.NoError(t, r.Update(ctx, ari2))
check(ari3, ari2, expect)
// Same with ari3.
ari3.Closed = true
ari3.Committed = false
ari3.CommitQueue = false
ari3.Result = autoroll.ROLL_RESULT_FAILURE
assert.NoError(t, r.Update(ctx, ari3))
check(nil, ari3, expect)
// Try to add a bogus issue.
now = time.Now().UTC()
bad2 := &autoroll.AutoRollIssue{
Closed: false,
Committed: true,
CommitQueue: true,
Created: now,
Issue: 1010104,
Modified: now,
Patchsets: []int64{1},
Result: autoroll.ROLL_RESULT_FAILURE,
Subject: "FAKE DEPS ROLL 4",
TryResults: []*autoroll.TryResult(nil),
}
assert.Error(t, r.Add(ctx, bad2))
// Add one more roll. Ensure that it's the current roll.
now = time.Now().UTC()
ari4 := &autoroll.AutoRollIssue{
Closed: false,
Committed: false,
CommitQueue: true,
Created: now,
Issue: 1010105,
Modified: now,
Patchsets: []int64{1},
Result: autoroll.ROLL_RESULT_IN_PROGRESS,
Subject: "FAKE DEPS ROLL 5",
TryResults: []*autoroll.TryResult(nil),
}
assert.NoError(t, r.Add(ctx, ari4))
expect = []*autoroll.AutoRollIssue{ari4, ari3, ari2, ari1}
check(ari4, ari3, expect)
}