Debian Codegraph @

registry/remote/credentials/trace/trace_test.go

raw
1 /*
2 Copyright The ORAS Authors.
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
16 package trace
17
18 import (
19 "context"
20 "testing"
21 )
22
23 func TestWithExecutableTrace(t *testing.T) {
24 ctx := context.Background()
25
26 t.Run("trace is nil", func(t *testing.T) {
27 newCtx := WithExecutableTrace(ctx, nil)
28 if newCtx != ctx {
29 t.Errorf("expected context to be unchanged when trace is nil")
30 }
31 })
32
33 t.Run("adding a new trace", func(t *testing.T) {
34 trace := &ExecutableTrace{}
35 newCtx := WithExecutableTrace(ctx, trace)
36 if newCtx == ctx {
37 t.Errorf("expected context to be changed when adding a new trace")
38 }
39 if got := ContextExecutableTrace(newCtx); got != trace {
40 t.Errorf("expected trace to be added to context")
41 }
42 })
43
44 t.Run("adding a new emtpy trace with existing trace", func(t *testing.T) {
45 oldExecStartCount := 0
46 oldExecDoneCount := 0
47 oldTrace := &ExecutableTrace{
48 ExecuteStart: func(executableName, action string) {
49 oldExecStartCount++
50 },
51 ExecuteDone: func(executableName, action string, err error) {
52 oldExecDoneCount++
53 },
54 }
55 ctx = WithExecutableTrace(ctx, oldTrace)
56
57 newTrace := &ExecutableTrace{}
58 newCtx := WithExecutableTrace(ctx, newTrace)
59
60 // verify new trace
61 gotTrace1 := ContextExecutableTrace(newCtx)
62 if gotTrace1 != newTrace {
63 t.Error("expected new trace to be added to context")
64 }
65
66 // verify old trace
67 gotTrace2 := ContextExecutableTrace(newCtx)
68 if gotTrace2 == oldTrace {
69 t.Errorf("expected old trace to be composed with new trace")
70 }
71 gotTrace2.ExecuteStart("oldExec", "oldAction")
72 if want := 1; oldExecStartCount != want {
73 t.Errorf("oldExecStartCount: got %d, expected: %v", oldExecStartCount, want)
74 }
75 gotTrace2.ExecuteDone("oldExec", "oldAction", nil)
76 if want := 1; oldExecDoneCount != want {
77 t.Errorf("oldExecDoneCount: got %d, expected: %v", oldExecDoneCount, want)
78 }
79 })
80
81 t.Run("adding a new trace with existing trace", func(t *testing.T) {
82 oldExecStartCount := 0
83 oldExecDoneCount := 0
84 oldTrace := &ExecutableTrace{
85 ExecuteStart: func(executableName, action string) {
86 oldExecStartCount++
87 },
88 ExecuteDone: func(executableName, action string, err error) {
89 oldExecDoneCount++
90 },
91 }
92 ctx = WithExecutableTrace(ctx, oldTrace)
93
94 newExecStartCount := 0
95 newExecDoneCount := 0
96 newTrace := &ExecutableTrace{
97 ExecuteStart: func(executableName, action string) {
98 newExecStartCount++
99 },
100 ExecuteDone: func(executableName, action string, err error) {
101 newExecDoneCount++
102 },
103 }
104 newCtx := WithExecutableTrace(ctx, newTrace)
105
106 // verify new trace
107 gotTrace1 := ContextExecutableTrace(newCtx)
108 if gotTrace1 != newTrace {
109 t.Error("expected new trace to be added to context")
110 }
111 gotTrace1.ExecuteStart("newExec", "newAction")
112 if want := 1; newExecStartCount != want {
113 t.Errorf("newExecStartCount: got %d, expected: %v", newExecStartCount, want)
114 }
115 gotTrace1.ExecuteDone("newExec", "newAction", nil)
116 if want := 1; newExecDoneCount != want {
117 t.Errorf("newExecDoneCount: got %d, expected: %v", newExecDoneCount, want)
118 }
119
120 // verify old trace
121 gotTrace2 := ContextExecutableTrace(newCtx)
122 if gotTrace2 == oldTrace {
123 t.Errorf("expected old trace to be composed with new trace")
124 }
125 gotTrace2.ExecuteStart("oldExec", "oldAction")
126 if want := 2; oldExecStartCount != want {
127 t.Errorf("oldExecStartCount: got %d, expected: %v", oldExecStartCount, want)
128 }
129 gotTrace2.ExecuteDone("oldExec", "oldAction", nil)
130 if want := 2; oldExecDoneCount != want {
131 t.Errorf("oldExecDoneCount: got %d, expected: %v", oldExecDoneCount, want)
132 }
133 })
134 }