|
| 1 | +package audit |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 8 | + v3rbacpb "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3" |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "google.golang.org/protobuf/testing/protocmp" |
| 11 | + "google.golang.org/protobuf/types/known/anypb" |
| 12 | + "google.golang.org/protobuf/types/known/structpb" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExtractXdsAuditLoggersConfig(t *testing.T) { |
| 16 | + tests := map[string]struct { |
| 17 | + auditLoggingOptions *v3rbacpb.RBAC_AuditLoggingOptions |
| 18 | + wantErr string |
| 19 | + wantJsonCfg string |
| 20 | + }{ |
| 21 | + "valid std_out cfg": { |
| 22 | + auditLoggingOptions: &v3rbacpb.RBAC_AuditLoggingOptions{ |
| 23 | + AuditCondition: v3rbacpb.RBAC_AuditLoggingOptions_NONE, |
| 24 | + LoggerConfigs: []*v3rbacpb.RBAC_AuditLoggingOptions_AuditLoggerConfig{ |
| 25 | + {AuditLogger: &v3corepb.TypedExtensionConfig{ |
| 26 | + Name: "stdout_logger", TypedConfig: anyPbHelper(t, map[string]interface{}{})}, |
| 27 | + IsOptional: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + wantJsonCfg: "{\"audit_logger\":{\"name\":\"stdout_logger\",\"typed_config\":{\"type_url\":\"type.googleapis.com/google.protobuf.Struct\"}},\"is_optional\":true}", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for name, test := range tests { |
| 36 | + t.Run(name, func(t *testing.T) { |
| 37 | + gotJsonCfg, gotErr := ExtractXdsAuditLoggersConfig(test.auditLoggingOptions) |
| 38 | + if gotErr != nil && !strings.HasPrefix(gotErr.Error(), test.wantErr) { |
| 39 | + t.Fatalf("unexpected error\nwant:%v\ngot:%v", test.wantErr, gotErr) |
| 40 | + } |
| 41 | + if diff := cmp.Diff(string(gotJsonCfg[0]), test.wantJsonCfg, protocmp.Transform()); diff != "" { |
| 42 | + t.Fatalf("unexpected jsonconfig\ndiff (-want +got):\n%s", diff) |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func anyPbHelper(t *testing.T, in map[string]interface{}) *anypb.Any { |
| 49 | + t.Helper() |
| 50 | + pb, err := structpb.NewStruct(in) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + ret, err := anypb.New(pb) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + return ret |
| 59 | +} |
0 commit comments