Skip to content

Commit dfa584d

Browse files
dpeckettDamian Peckett
andauthored
[METAL-2804] Use new cloudish sql mock (kloeckner-i#5)
Co-authored-by: Damian Peckett <damian.peckett@kloeckner.com>
1 parent d839462 commit dfa584d

File tree

15 files changed

+96
-498
lines changed

15 files changed

+96
-498
lines changed

.github/workflows/build-and-test.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Check Code Style
1818
uses: golangci/golangci-lint-action@v2
1919
with:
20-
version: v1.30.0
20+
version: v1.40.1
2121

2222
build:
2323
runs-on: ubuntu-latest
@@ -51,6 +51,12 @@ jobs:
5151
uses: docker/setup-qemu-action@v1
5252
- name: Set up Docker Buildx
5353
uses: docker/setup-buildx-action@v1
54+
- name: Login to GitHub Container Registry
55+
uses: docker/login-action@v1
56+
with:
57+
registry: ghcr.io
58+
username: ${{ github.actor }}
59+
password: ${{ secrets.GITHUB_TOKEN }}
5460
- name: Download artifact
5561
uses: actions/download-artifact@v2
5662
with:
@@ -60,14 +66,14 @@ jobs:
6066
run: docker load --input /tmp/db-auth-gateway.tar
6167
- name: Start Google Cloud SQL Mock and Dependencies
6268
run: |
63-
docker network create db-auth-gateway-test
64-
docker run -d --network=db-auth-gateway-test --network-alias=postgres --name=postgres -e "POSTGRES_PASSWORD=mysecretpassword" postgres:13
65-
docker run -d --network=db-auth-gateway-test --name=cloudsql_mock -p 127.0.0.1:3307:3307 -p 127.0.0.1:8080:8080 kloeckner-i/db-auth-gateway:${{ github.sha }} mock --db-address=postgres:5432 --instance=my-project:my-region:my-database
69+
make start_mock
6670
- name: Test
6771
env:
6872
MOCK_ADDRESS: 127.0.0.1
6973
run: |
70-
make test e2e
74+
# Separated to ensure the reset_mock target is executed between stages.
75+
make test
76+
make e2e
7177
- name: Upload Test Logs
7278
uses: actions/upload-artifact@v2
7379
if: failure()

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ linters-settings:
66
statements: 50
77
nestif:
88
min-complexity: 10
9+
cyclop:
10+
max-complexity: 20
11+
912

1013
issues:
1114
exclude-rules:

Makefile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@ $(BIN): $(SRC)
55
@mkdir -p target
66
@go build -o $@ cmd/main.go
77

8-
test: $(SRC)
8+
test: $(SRC) reset_mock
99
@go test ./...
1010

11-
e2e: $(SRC) $(BIN)
11+
e2e: $(SRC) $(BIN) reset_mock
1212
@go test -tags=e2e ./test/...
1313

1414
start_mock: $(SRC)
1515
@-docker-compose down
1616
@docker-compose build
1717
@docker-compose up -d
1818

19+
reset_mock:
20+
@docker-compose stop mock
21+
@docker-compose rm -f -v mock
22+
@docker-compose up -d
23+
@sleep 2
24+
1925
lint: $(SRC)
2026
@go mod tidy
2127
@gofumpt -s -l -w $^
2228
@gci -w $^
23-
@golint ./...
24-
@golangci-lint run --timeout 5m0s --enable-all -D gochecknoglobals -D gomnd ./...
29+
@golangci-lint run --timeout 5m0s --enable-all \
30+
-D gochecknoglobals -D exhaustivestruct -D wrapcheck -D interfacer -D maligned -D scopelint -D golint -D gomnd -D paralleltest ./...
2531

2632
clean:
2733
@-rm -Rf target/*
2834
@go clean -testcache
2935
@-docker-compose down
3036

31-
.PHONY: test e2e start_mock lint clean
37+
.PHONY: test e2e start_mock reset_mock lint clean

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ PGPASSWORD=mysecretpassword psql -h localhost -p 5432 -d postgres postgres
6969
* GNU Make
7070
* [golangci-lint v1.30+](https://golangci-lint.run/usage/install/)
7171
* Additional Go tools:
72-
* [golint](https://github.com/golang/lint)
7372
* [gofumpt](https://github.com/mvdan/gofumpt)
7473
* [gofumports](https://github.com/mvdan/gofumpt)
7574
* [gci](https://github.com/daixiang0/gci)

cmd/main.go

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
"context"
21-
"crypto/x509/pkix"
2221
"net/http"
2322
"os"
2423
"os/signal"
@@ -28,8 +27,6 @@ import (
2827
pkg "github.com/kloeckner-i/db-auth-gateway/internal"
2928
"github.com/kloeckner-i/db-auth-gateway/internal/api"
3029
"github.com/kloeckner-i/db-auth-gateway/internal/config"
31-
"github.com/kloeckner-i/db-auth-gateway/internal/mock"
32-
"github.com/kloeckner-i/db-auth-gateway/internal/pubkey"
3330
"github.com/prometheus/client_golang/prometheus/promhttp"
3431
log "github.com/sirupsen/logrus"
3532
"github.com/spf13/cobra"
@@ -83,32 +80,7 @@ func execute() error {
8380
return err
8481
}
8582

86-
mockCmd := &cobra.Command{
87-
Use: "mock",
88-
Short: "Run a local Google managed database mock (for testing)",
89-
RunE: startMock,
90-
}
91-
92-
mockCmd.Flags().String("instance", "", "Fully qualified database instance to connect to (project:region:name)")
93-
mockCmd.Flags().String("db-address", "", "Address of the database for which to proxy")
94-
mockCmd.Flags().Int("api-port", 8080, "Port on which to serve the mocked sqladmin API")
95-
mockCmd.Flags().Int("proxy-port", 3307, "Port on which to serve the database proxy")
96-
97-
if err := mockCmd.MarkFlagRequired("instance"); err != nil {
98-
return err
99-
}
100-
101-
if err := mockCmd.MarkFlagRequired("db-address"); err != nil {
102-
return err
103-
}
104-
105-
rootCmd.AddCommand(mockCmd)
106-
107-
if err := rootCmd.Execute(); err != nil {
108-
return err
109-
}
110-
111-
return nil
83+
return rootCmd.Execute()
11284
}
11385

11486
func startGateway(cmd *cobra.Command, args []string) error {
@@ -206,54 +178,3 @@ func startGateway(cmd *cobra.Command, args []string) error {
206178

207179
return gateway.Run(ctx)
208180
}
209-
210-
func startMock(cmd *cobra.Command, args []string) error {
211-
instance, err := cmd.Flags().GetString("instance")
212-
if err != nil {
213-
return err
214-
}
215-
216-
dbAddress, err := cmd.Flags().GetString("db-address")
217-
if err != nil {
218-
return err
219-
}
220-
221-
apiPort, err := cmd.Flags().GetInt("api-port")
222-
if err != nil {
223-
return err
224-
}
225-
226-
proxyPort, err := cmd.Flags().GetInt("proxy-port")
227-
if err != nil {
228-
return err
229-
}
230-
231-
authority, err := pubkey.NewAuthority(pkix.Name{
232-
CommonName: "Mock CA",
233-
}, time.Hour)
234-
if err != nil {
235-
log.Fatal(err)
236-
}
237-
238-
dbProxy, err := mock.NewDatabaseProxy(authority, proxyPort, instance, dbAddress)
239-
if err != nil {
240-
log.Fatal(err)
241-
}
242-
243-
sqlAdminAPI, err := mock.NewSQLAdminAPI(dbProxy, authority, apiPort)
244-
if err != nil {
245-
log.Fatal(err)
246-
}
247-
248-
go func() {
249-
if err := dbProxy.Run(); err != nil {
250-
log.Fatal(err)
251-
}
252-
}()
253-
254-
if err := sqlAdminAPI.Run(); err != nil {
255-
log.Fatal(err)
256-
}
257-
258-
return nil
259-
}

docker-compose.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ services:
88
POSTGRES_PASSWORD: mysecretpassword
99

1010
mock:
11-
build:
12-
context: .
13-
dockerfile: Dockerfile
14-
command:
15-
- mock
16-
- --db-address=postgres:5432
17-
- --instance=my-project:my-region:my-database
11+
image: ghcr.io/kloeckner-i/cloudish-sql:v1.0.0
1812
ports:
13+
- "127.0.0.1:8080:8080"
1914
- "127.0.0.1:3307:3307"
20-
- "127.0.0.1:8080:8080"
15+
environment:
16+
LOG_LEVEL: "DEBUG"
17+
command:
18+
- --db-address=postgres:5432

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.15
44

55
require (
66
cloud.google.com/go v0.78.0 // indirect
7-
github.com/gorilla/mux v1.8.0
87
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
98
github.com/jackc/pgx/v4 v4.11.0
109
github.com/magefile/mage v1.11.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
201201
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
202202
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
203203
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
204-
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
205-
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
206204
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
207205
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
208206
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=

internal/api/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func NewClientFromCredentialFile(ctx context.Context, credentialFile, apiEndpoin
6969
}
7070

7171
if credentialFile == DisabledCredentialFile {
72-
opts = append(opts, option.WithHTTPClient(oauth2.NewClient(ctx, &disabledTokenSource{})))
72+
opts = append(opts, option.WithHTTPClient(oauth2.NewClient(ctx, &DisabledTokenSource{})))
7373
} else if cfg, err := goauth.JWTConfigFromJSON(credentialJSON, sqladmin.SqlserviceAdminScope); err == nil {
7474
opts = append(opts, option.WithHTTPClient(cfg.Client(ctx)))
7575
} else {
@@ -133,11 +133,11 @@ func (c *Client) CreateClientCertificate(instance string, pubKey crypto.PublicKe
133133
return req.Do()
134134
}
135135

136-
// disabledTokenSource is a mocked oauth token source for local testing.
137-
type disabledTokenSource struct{}
136+
// DisabledTokenSource is a mocked oauth token source for local testing.
137+
type DisabledTokenSource struct{}
138138

139139
// Token issues a mocked bearer token for local testing.
140-
func (ts *disabledTokenSource) Token() (*oauth2.Token, error) {
140+
func (ts *DisabledTokenSource) Token() (*oauth2.Token, error) {
141141
return &oauth2.Token{
142142
AccessToken: "let-me-in-pls",
143143
TokenType: "Bearer",

internal/config/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,43 @@ package config_test
1919
import (
2020
"context"
2121
"crypto/x509"
22+
"log"
23+
"os"
2224
"testing"
2325
"time"
2426

2527
"github.com/kloeckner-i/db-auth-gateway/internal/api"
2628
"github.com/kloeckner-i/db-auth-gateway/internal/config"
2729
"github.com/kloeckner-i/db-auth-gateway/internal/util"
2830
"github.com/stretchr/testify/assert"
31+
"golang.org/x/oauth2"
32+
"google.golang.org/api/option"
33+
sqladmin "google.golang.org/api/sqladmin/v1beta4"
2934
)
3035

36+
func TestMain(m *testing.M) {
37+
ctx := context.Background()
38+
39+
opts := []option.ClientOption{
40+
option.WithEndpoint("http://localhost:8080"),
41+
option.WithHTTPClient(oauth2.NewClient(ctx, &api.DisabledTokenSource{})),
42+
}
43+
44+
sqladminService, err := sqladmin.NewService(ctx, opts...)
45+
if err != nil {
46+
log.Fatal("error occurs during getting sqladminService", err)
47+
}
48+
49+
_, err = sqladminService.Instances.Insert("my-project", &sqladmin.DatabaseInstance{
50+
Name: "my-region~my-database",
51+
}).Do()
52+
if err != nil {
53+
log.Fatal("error occurs during getting sqladminService", err)
54+
}
55+
56+
os.Exit(m.Run())
57+
}
58+
3159
func TestGetPrimaryAddress(t *testing.T) {
3260
configProvider, err := newConfigProvider()
3361
if err != nil {

0 commit comments

Comments
 (0)