Skip to content

Commit

Permalink
Fix build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ksclarke committed Jan 6, 2025
1 parent d2e995f commit a24e124
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
16 changes: 11 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARG SERVICE_NAME="validation-service"
##
## STEP 1 - BUILD
##
FROM golang:1.23.3-alpine3.20 AS build
FROM golang:1.23.4-alpine3.20 AS build

ARG SERVICE_NAME
ENV SERVICE_NAME=${SERVICE_NAME}
Expand Down Expand Up @@ -38,17 +38,23 @@ RUN apk add --no-cache curl
# Create a non--root user
RUN addgroup -S "${SERVICE_NAME}" && adduser -S "${SERVICE_NAME}" -G "${SERVICE_NAME}"

# Copy the executable from the build stage
COPY --from=build --chown="${SERVICE_NAME}":"${SERVICE_NAME}" --chmod=0700 "/${SERVICE_NAME}" "/sbin/${SERVICE_NAME}"
# Copy the executable from the build stage (BuildKit required, but not working with TestContainers-Go)
# COPY --from=build --chown="${SERVICE_NAME}":"${SERVICE_NAME}" --chmod=0700 "/${SERVICE_NAME}" "/sbin/${SERVICE_NAME}"

# Copy the file without --chown or --chmod (BuildKit not required)
COPY --from=build "/${SERVICE_NAME}" "/sbin/${SERVICE_NAME}"

# Now, modify ownership and permissions in a separate RUN step
RUN chown "${SERVICE_NAME}":"${SERVICE_NAME}" "/sbin/${SERVICE_NAME}" && chmod 0700 "/sbin/${SERVICE_NAME}"

# Expose the port on which the application will run
EXPOSE 8888

# Create a non-root user
USER "${SERVICE_NAME}"

# Specify the command to be used when the image is used to start a container
# Specify the command to be used when the image is used to start a container; use shell to support ENV name
ENTRYPOINT [ "sh", "-c", "exec /sbin/${SERVICE_NAME}" ]

# Confirm the service started as expected
HEALTHCHECK CMD curl -f http://localhost:8888/ || exit 1
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:8888/ || exit 1
24 changes: 7 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
# Variables
DOCKER_IMAGE := validation-service
DOCKER_TAG := latest
SERVICE_NAME := validation-service

# Do a full build of the project
all: lint build test docker-build docker-test
all: lint build test docker-test

# Build the Go project
build:
go build -o $(DOCKER_IMAGE)
go build -o $(SERVICE_NAME)

# Run Go tests
test:
go test ./... -v
go test -tags=unit ./... -v

# Lint the code
lint:
golangci-lint run

# Build the Docker container
docker-build:
docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) .

# Run tests inside the Docker container
docker-test: docker-build
docker run -d --name $(DOCKER_IMAGE)-test -p 8888:8888 $(DOCKER_IMAGE):$(DOCKER_TAG)
go test ./... -v
docker stop $(DOCKER_IMAGE)-test
docker rm $(DOCKER_IMAGE)-test
docker-test:
go test -tags=functional ./... -v

# Clean up all artifacts of the build
clean:
rm -rf $(DOCKER_IMAGE)
docker rm -f $(DOCKER_IMAGE)-test
docker rmi -f $(DOCKER_IMAGE)
rm -rf $(SERVICE_NAME)
2 changes: 2 additions & 0 deletions csv_location_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unit

package main

import (
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unit

package main

import (
Expand Down
16 changes: 10 additions & 6 deletions testcontainer_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//go:build functional

package main

import (
"bytes"
"context"
"fmt"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -15,9 +16,13 @@ import (
)

func TestApp(t *testing.T) {

// Define the container request
req := testcontainers.ContainerRequest{
Image: "validation-service",
FromDockerfile: testcontainers.FromDockerfile{
Context: ".",
Dockerfile: "Dockerfile",
},
ExposedPorts: []string{"8888/tcp"},
WaitingFor: wait.ForHTTP("/").WithPort("8888/tcp"),
}
Expand Down Expand Up @@ -58,19 +63,18 @@ func TestApp(t *testing.T) {
}

// Make requests to the containerized app and assert the responses
// Example: Make an HTTP request to the root endpoint
resp, err := client.Get("http://" + host + ":" + strconv.Itoa(port.Int()) + "/")
resp, err := client.Get(fmt.Sprintf("http://%s:%d/", host, port.Int()))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode)

// Any requeset body will work since POST requests are not currently allowed
// Any request body will work since POST requests are not currently allowed
requestBody := []byte(`{"key": "value"}`)

resp, err = client.Post("http://" + host + ":" + strconv.Itoa(port.Int()) + "/", "application/json",
resp, err = client.Post(fmt.Sprintf("http://%s:%d/", host, port.Int()), "application/json",
bytes.NewBuffer(requestBody))
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit a24e124

Please sign in to comment.