From c4bdb156a2331930aeed6849c7e8bab2a7a996cd Mon Sep 17 00:00:00 2001 From: lavkesh Date: Tue, 12 Sep 2023 11:33:53 +0800 Subject: [PATCH] test: add more handler tests --- go.mod | 5 +- go.sum | 8 ++- internal/server/v1/dlq/handler_test.go | 83 ++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index d7f967f..38b50e2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/goto/dex -go 1.20 +go 1.21 + +toolchain go1.21.0 require ( buf.build/gen/go/gotocompany/proton/grpc/go v1.3.0-20230910125305-da3eb6608f25.1 @@ -128,6 +130,7 @@ require ( go.mongodb.org/mongo-driver v1.12.1 // indirect go.opentelemetry.io/otel v1.17.0 // indirect go.opentelemetry.io/otel/metric v1.17.0 // indirect + go.opentelemetry.io/otel/sdk v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.17.0 // indirect go.uber.org/goleak v1.2.1 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/go.sum b/go.sum index 33441ea..b5f6dfd 100644 --- a/go.sum +++ b/go.sum @@ -549,6 +549,7 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3 github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= @@ -826,6 +827,7 @@ github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoD github.com/form3tech-oss/jwt-go v3.2.5+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -1053,6 +1055,7 @@ github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIG github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw= +github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= @@ -1278,6 +1281,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= @@ -1577,6 +1581,7 @@ github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= @@ -1794,7 +1799,8 @@ go.opentelemetry.io/otel/metric v1.17.0/go.mod h1:h4skoxdZI17AxwITdmdZjjYJQH5nzi go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc= go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs= -go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY= +go.opentelemetry.io/otel/sdk v1.16.0 h1:Z1Ok1YsijYL0CSJpHt4cS3wDDh7p572grzNrBMiMWgE= +go.opentelemetry.io/otel/sdk v1.16.0/go.mod h1:tMsIuKXuuIWPBAOrH+eHtvhTL+SntFtXF9QD68aP6p4= go.opentelemetry.io/otel/sdk/export/metric v0.20.0/go.mod h1:h7RBNMsDJ5pmI1zExLi+bJK+Dr8NQCh0qGhm1KDnNlE= go.opentelemetry.io/otel/sdk/metric v0.20.0/go.mod h1:knxiS8Xd4E/N+ZqKmUPf3gTTZ4/0TjTXukfxjzSTpHE= go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw= diff --git a/internal/server/v1/dlq/handler_test.go b/internal/server/v1/dlq/handler_test.go index 0035fdd..70e77c6 100644 --- a/internal/server/v1/dlq/handler_test.go +++ b/internal/server/v1/dlq/handler_test.go @@ -3,6 +3,7 @@ package dlq import ( "context" "encoding/json" + "fmt" "net/http" "testing" @@ -75,7 +76,6 @@ func (g *mockGcsClient) ListTopicDates(bucketInfo gcs.BucketInfo) (map[string]ma } type mockHTTPWriter struct { - mock.Mock messages []string } @@ -93,6 +93,10 @@ func (*mockHTTPWriter) WriteHeader(int) { func TestListTopicDates(t *testing.T) { eService := &mockEntropyService{} + gClient := &mockGcsClient{} + handler := NewHandler(NewService(eService, gClient)) + httpWriter := &mockHTTPWriter{} + httpRequest := &http.Request{} config := &entropy.Config{ Stopped: false, StopTime: nil, @@ -134,7 +138,6 @@ func TestListTopicDates(t *testing.T) { UpdatedBy: "", }, }, nil) - gClient := &mockGcsClient{} topicDates := make(map[string]map[string]int64) topicDates["topic-1"] = make(map[string]int64) topicDates["topic-2"] = make(map[string]int64) @@ -146,11 +149,83 @@ func TestListTopicDates(t *testing.T) { Prefix: "test-prefix", Delim: "", }).Return(topicDates, nil) + handler.listFirehoseDLQ(httpWriter, httpRequest) + expectedMap := make(map[string]map[string]map[string]int64) + _ = json.Unmarshal([]byte(httpWriter.messages[0]), &expectedMap) + assert.Equal(t, topicDates, expectedMap["dlq_list"]) +} + +func TestErrorFromGCSClient(t *testing.T) { + eService := &mockEntropyService{} + gClient := &mockGcsClient{} + handler := NewHandler(NewService(eService, gClient)) + httpWriter := &mockHTTPWriter{} + httpRequest := &http.Request{} + config := &entropy.Config{ + Stopped: false, + StopTime: nil, + Replicas: 0, + Namespace: "", + DeploymentID: "", + EnvVariables: map[string]string{ + firehose.ConfigDLQBucket: "test-bucket", + firehose.ConfigDLQDirectoryPrefix: "test-prefix", + }, + ResetOffset: "", + Limits: entropy.UsageSpec{}, + Requests: entropy.UsageSpec{}, + Telegraf: nil, + ChartValues: nil, + InitContainer: entropy.InitContainer{}, + } + configProto, _ := utils.GoValToProtoStruct(config) + eService.On( + "GetResource", + context.Background(), + mock.Anything, + []grpc.CallOption(nil)).Return( + &entropyv1beta1.GetResourceResponse{ + Resource: &entropyv1beta1.Resource{ + Urn: "", + Kind: "", + Name: "", + Project: "", + Labels: nil, + CreatedAt: nil, + UpdatedAt: nil, + Spec: &entropyv1beta1.ResourceSpec{ + Configs: configProto, + Dependencies: nil, + }, + State: nil, + CreatedBy: "", + UpdatedBy: "", + }, + }, nil) + gClient.On("ListTopicDates", gcs.BucketInfo{ + BucketName: "test-bucket", + Prefix: "test-prefix", + Delim: "", + }).Return(nil, fmt.Errorf("test-error")) + handler.listFirehoseDLQ(httpWriter, httpRequest) + expectedMap := make(map[string]interface{}) + _ = json.Unmarshal([]byte(httpWriter.messages[0]), &expectedMap) + assert.Equal(t, "test-error", expectedMap["cause"]) +} + +func TestErrorFromFirehoseResource(t *testing.T) { + eService := &mockEntropyService{} + gClient := &mockGcsClient{} handler := NewHandler(NewService(eService, gClient)) httpWriter := &mockHTTPWriter{} httpRequest := &http.Request{} + eService.On( + "GetResource", + context.Background(), + mock.Anything, + []grpc.CallOption(nil)).Return(nil, fmt.Errorf("test-error")) handler.listFirehoseDLQ(httpWriter, httpRequest) - expectedMap := make(map[string]map[string]map[string]int64) + expectedMap := make(map[string]interface{}) _ = json.Unmarshal([]byte(httpWriter.messages[0]), &expectedMap) - assert.Equal(t, topicDates, expectedMap["dlq_list"]) + assert.Equal(t, "test-error", expectedMap["cause"]) }