1313// limitations under the License.
1414package root
1515
16- import "testing"
16+ import (
17+ "regexp"
18+ "testing"
19+ )
1720
1821func TestBuildAuditLogQuery (t * testing.T ) {
1922 tests := []struct {
@@ -25,13 +28,13 @@ func TestBuildAuditLogQuery(t *testing.T) {
2528 username string
2629 fromTime string
2730 toTime string
28- expected string
31+ expectedRx string // regex pattern to match (for times that vary)
2932 wantErr bool
3033 }{
3134 {
32- name : "returns base query only" ,
33- baseQuery : "operation=push" ,
34- expected : " operation=push" ,
35+ name : "returns base query only" ,
36+ baseQuery : "operation=push" ,
37+ expectedRx : "^ operation=push$ " ,
3538 },
3639 {
3740 name : "builds query with convenience filters" ,
@@ -40,16 +43,21 @@ func TestBuildAuditLogQuery(t *testing.T) {
4043 resourceType : "artifact" ,
4144 resource : "library/nginx" ,
4245 username : "admin" ,
43- expected : " operation_result=true,operation=create_artifact,resource_type=artifact,resource=library/nginx,username=admin" ,
46+ expectedRx : "^ operation_result=true,operation=create_artifact,resource_type=artifact,resource=library/nginx,username=admin$ " ,
4447 },
4548 {
46- name : "builds range query with normalized times" ,
47- fromTime : "2025-01-01T01:02:03Z" ,
48- toTime : "2025-01-01 05:06:07" ,
49- expected : "op_time=[2025-01-01 01:02:03~2025-01-01 05:06:07]" ,
49+ name : "builds range query with both times specified" ,
50+ fromTime : "2025-01-01T01:02:03Z" ,
51+ toTime : "2025-01-01 05:06:07" ,
52+ expectedRx : "^op_time=\\ [2025-01-01 01:02:03~2025-01-01 05:06:07\\ ]$" ,
53+ },
54+ {
55+ name : "from-time alone defaults to-time to current time" ,
56+ fromTime : "2025-01-01T01:02:03Z" ,
57+ expectedRx : "^op_time=\\ [2025-01-01 01:02:03~.*\\ ]$" , // matches any end time
5058 },
5159 {
52- name : "fails when one range bound is missing " ,
60+ name : "to-time alone is rejected " ,
5361 toTime : "2025-01-01 05:06:07" ,
5462 wantErr : true ,
5563 },
@@ -59,6 +67,12 @@ func TestBuildAuditLogQuery(t *testing.T) {
5967 toTime : "2025-01-01 05:06:07" ,
6068 wantErr : true ,
6169 },
70+ {
71+ name : "fails for invalid to time" ,
72+ fromTime : "2025-01-01T01:02:03Z" ,
73+ toTime : "invalid-time" ,
74+ wantErr : true ,
75+ },
6276 }
6377
6478 for _ , tt := range tests {
@@ -84,8 +98,9 @@ func TestBuildAuditLogQuery(t *testing.T) {
8498 t .Fatalf ("unexpected error: %v" , err )
8599 }
86100
87- if query != tt .expected {
88- t .Fatalf ("expected query %q, got %q" , tt .expected , query )
101+ matched , _ := regexp .MatchString (tt .expectedRx , query )
102+ if ! matched {
103+ t .Fatalf ("expected query to match regex %q, got %q" , tt .expectedRx , query )
89104 }
90105 })
91106 }
0 commit comments