go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/invocations/graph/traverse_test.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package graph contains methods to explore reachable invocations. 16 package graph 17 18 import ( 19 "testing" 20 21 . "github.com/smartystreets/goconvey/convey" 22 "go.chromium.org/luci/resultdb/internal/invocations" 23 "go.chromium.org/luci/resultdb/internal/spanutil" 24 "go.chromium.org/luci/resultdb/internal/testutil" 25 "go.chromium.org/luci/resultdb/internal/testutil/insert" 26 "go.chromium.org/luci/resultdb/pbutil" 27 pb "go.chromium.org/luci/resultdb/proto/v1" 28 ) 29 30 func TestFindInheritSourcesDescendants(t *testing.T) { 31 32 Convey(`FindInheritSourcesDescendants`, t, func() { 33 ctx := testutil.SpannerTestContext(t) 34 sources := spanutil.Compressed(pbutil.MustMarshal(&pb.Sources{ 35 GitilesCommit: &pb.GitilesCommit{Host: "testHost"}, 36 IsDirty: false, 37 })) 38 Convey(`Includes inherited and non-inherted: a->b, a->c`, func() { 39 testutil.MustApply(ctx, testutil.CombineMutations( 40 insert.InvocationWithInclusions("a", pb.Invocation_FINALIZING, map[string]any{"Sources": sources}, "b", "c"), 41 insert.InvocationWithInclusions("b", pb.Invocation_ACTIVE, map[string]any{"InheritSources": true}), 42 insert.InvocationWithInclusions("c", pb.Invocation_FINALIZING, map[string]any{"Sources": sources}), 43 )...) 44 45 invs, err := FindInheritSourcesDescendants(ctx, "a") 46 So(err, ShouldBeNil) 47 So(invs, ShouldResemble, invocations.NewIDSet("a", "b")) 48 }) 49 50 Convey(`Includes indirectly inherited a->b->c`, func() { 51 testutil.MustApply(ctx, testutil.CombineMutations( 52 insert.InvocationWithInclusions("a", pb.Invocation_FINALIZING, map[string]any{"Sources": sources}, "b"), 53 insert.InvocationWithInclusions("b", pb.Invocation_ACTIVE, map[string]any{"InheritSources": true}, "c"), 54 insert.InvocationWithInclusions("c", pb.Invocation_FINALIZING, map[string]any{"InheritSources": true}), 55 )...) 56 57 invs, err := FindInheritSourcesDescendants(ctx, "a") 58 So(err, ShouldBeNil) 59 So(invs, ShouldResemble, invocations.NewIDSet("a", "b", "c")) 60 }) 61 62 Convey(`Cycle with one node: a->b<->b`, func() { 63 testutil.MustApply(ctx, testutil.CombineMutations( 64 insert.InvocationWithInclusions("a", pb.Invocation_FINALIZING, map[string]any{"Sources": sources}, "b"), 65 insert.InvocationWithInclusions("b", pb.Invocation_ACTIVE, map[string]any{"InheritSources": true}, "b"), 66 )...) 67 68 invs, err := FindInheritSourcesDescendants(ctx, "a") 69 So(err, ShouldBeNil) 70 So(invs, ShouldResemble, invocations.NewIDSet("a", "b")) 71 }) 72 73 Convey(`Cycle with two: a->b<->c`, func() { 74 testutil.MustApply(ctx, testutil.CombineMutations( 75 insert.InvocationWithInclusions("a", pb.Invocation_FINALIZING, map[string]any{"Sources": sources}, "b"), 76 insert.InvocationWithInclusions("b", pb.Invocation_ACTIVE, map[string]any{"InheritSources": true}, "c"), 77 insert.InvocationWithInclusions("c", pb.Invocation_FINALIZING, map[string]any{"InheritSources": true}, "b"), 78 )...) 79 80 invs, err := FindInheritSourcesDescendants(ctx, "a") 81 So(err, ShouldBeNil) 82 So(invs, ShouldResemble, invocations.NewIDSet("a", "b", "c")) 83 }) 84 }) 85 }