Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
test: add more handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lavkesh committed Sep 12, 2023
1 parent 217b59b commit 12e2850
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
82 changes: 79 additions & 3 deletions internal/server/v1/dlq/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dlq
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -93,6 +94,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,
Expand Down Expand Up @@ -134,7 +139,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)
Expand All @@ -146,11 +150,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]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"])
}

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]interface{})
_ = json.Unmarshal([]byte(httpWriter.messages[0]), &expectedMap)
assert.Equal(t, "test-error", expectedMap["cause"])
}

0 comments on commit 12e2850

Please sign in to comment.