Skip to content

Commit

Permalink
chore: update dockerfile, add tests for dockerfile
Browse files Browse the repository at this point in the history
Added tests for dockerfile to ensure that GoVersions in dockerfiles stay in sync with Go.mod version.
Updated dockerfile to use a more reliable base image.
Install ca-certificates and curl
  • Loading branch information
Sean Connole committed Oct 22, 2024
1 parent 4798d96 commit d3f1dd8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 83 deletions.
17 changes: 13 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
FROM debian
ARG GO_VERSION=1.22
FROM golang:${GO_VERSION} AS build

RUN apt update && apt install -y ca-certificates curl
RUN mkdir -p /go/src/github.com/contentsquare/chproxy
WORKDIR /go/src/github.com/contentsquare/chproxy
COPY . ./
ARG EXT_BUILD_TAG
ENV EXT_BUILD_TAG=${EXT_BUILD_TAG}
RUN make release-build
RUN ls -al /go/src/github.com/contentsquare/chproxy

COPY chproxy /
FROM alpine
RUN apk add --no-cache curl ca-certificates
COPY --from=build /go/src/github.com/contentsquare/chproxy/chproxy* /

EXPOSE 9090

ENTRYPOINT ["/chproxy"]
ENTRYPOINT [ "/chproxy" ]
CMD [ "--help" ]
10 changes: 6 additions & 4 deletions Dockerfile_boringcrypto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM ${UBUNTU_IMAGE} AS build

ENV GOPATH=/gocode
ENV PATH=$PATH:$GOPATH/bin
ENV GOVERSION=1.21
ENV GO_VERSION=1.22

RUN mkdir /scr
WORKDIR /src
Expand All @@ -17,21 +17,23 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-ins
&& add-apt-repository -y ppa:longsleep/golang-backports \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
golang-$GOVERSION-go \
golang-$GO_VERSION-go \
golang-golang-x-tools \
&& apt-get autoremove -y \
&& apt-get remove -y \
apt-utils \
software-properties-common

# Create symbolic link
RUN ln -s /usr/lib/go-$GOVERSION /gocode
RUN ln -s /usr/lib/go-$GO_VERSION /gocode

# tools
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
git \
make \
tzdata
tzdata \
curl \
ca-certificates

FROM build

Expand Down
18 changes: 0 additions & 18 deletions Dockerfile_build

This file was deleted.

66 changes: 66 additions & 0 deletions dockerfile_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"bufio"
"os"
"regexp"
"testing"
)

func getVersionFromFile(filename string, searchPhrase string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()

scanner := bufio.NewScanner(file)
re := regexp.MustCompile(searchPhrase) // Regex to capture Go version
for scanner.Scan() {
line := scanner.Text()
matches := re.FindStringSubmatch(line)
if len(matches) > 1 {
return matches[1], nil
}
}

return "", scanner.Err()
}

// Test function to check if Go version in go.mod matches Dockerfile
func TestGoVersionMatching(t *testing.T) {
goModVersionSearchPhrase := `^go\s+(\d+\.\d+)`
goModVersion, err := getVersionFromFile("go.mod", goModVersionSearchPhrase)
if err != nil {
t.Fatalf("Error getting Go version from go.mod: %v", err)
}

dockerFileVersionSearchPhrase := "GO_VERSION=(.*)"
dockerfileVersion, err := getVersionFromFile("Dockerfile", dockerFileVersionSearchPhrase)
if err != nil {
t.Fatalf("Error getting Go version from Dockerfile: %v", err)
}

if goModVersion != dockerfileVersion {
t.Errorf("Go version mismatch: go.mod (%s), Dockerfile (%s)", goModVersion, dockerfileVersion)
}
}

// Test function to check if Go version in go.mod matches Dockerfile_boringcrypto
func TestGoVersionMatchingBoringCrypto(t *testing.T) {
goModVersionSearchPhrase := `^go\s+(\d+\.\d+)`
goModVersion, err := getVersionFromFile("go.mod", goModVersionSearchPhrase)
if err != nil {
t.Fatalf("Error getting Go version from go.mod: %v", err)
}

dockerFileVersionSearchPhrase := "GO_VERSION=(.*)"
dockerfileVersion, err := getVersionFromFile("Dockerfile_boringcrypto", dockerFileVersionSearchPhrase)
if err != nil {
t.Fatalf("Error getting Go version from Dockerfile_boringcrypto: %v", err)
}

if goModVersion != dockerfileVersion {
t.Errorf("Go version mismatch: go.mod (%s), Dockerfile (%s)", goModVersion, dockerfileVersion)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/contentsquare/chproxy

go 1.19
go 1.22

require (
github.com/alicebob/miniredis/v2 v2.21.0
Expand Down
Loading

0 comments on commit d3f1dd8

Please sign in to comment.