Skip to content

Commit

Permalink
Introduce test and dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
troydai committed Nov 28, 2024
1 parent fa00824 commit 74ee02a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Use the official Golang image to build the Go application
FROM golang:1.23-alpine AS builder

# Set the working directory inside the container
WORKDIR /src

# Copy the Go module files and download dependencies
COPY go.mod go.sum ./
RUN go mod download

# Copy the rest of the application code
COPY . .

# Build the Go application
RUN go build -o /app/main main.go

# Use a minimal image for the final container
FROM alpine:latest

# Install dumb-init
RUN apk --no-cache add dumb-init

# Set the working directory inside the container
WORKDIR /app

# Copy the built Go application from the builder stage
COPY --from=builder /app/main .

# Expose the port the application runs on
EXPOSE 8080

# Use dumb-init as the entrypoint to handle PID 1 correctly
ENTRYPOINT ["/usr/bin/dumb-init", "--"]

# Command to run the Go application
CMD ["/app/main"]
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build-image
build-image:
docker build -t http-crash .

.PHONY: run-container
run-container:
docker run --rm -p 8080:8080 http-crash
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/troydai/http-crash

go 1.23.2

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 41 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"io"
"log/slog"
"net/http"
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"
)

func TestServer(t *testing.T) {
s := &server{
counter: &atomic.Uint64{},
frequency: 10,
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
s.handler(&fakeResponseWriter{}, randomRequest())
assert.Equal(t, uint64(1), s.counter.Load())
}

func randomRequest() *http.Request {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
panic(err)
}
return req
}

type fakeResponseWriter struct{}

func (frw *fakeResponseWriter) Header() http.Header {
return http.Header{}
}

func (frw *fakeResponseWriter) Write(data []byte) (int, error) {
return len(data), nil
}

func (frw *fakeResponseWriter) WriteHeader(statusCode int) {}

0 comments on commit 74ee02a

Please sign in to comment.