Skip to content

Commit

Permalink
Enable golangci-lint (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Wang <[email protected]>
  • Loading branch information
henry118 authored Aug 22, 2024
1 parent c414734 commit 183581a
Show file tree
Hide file tree
Showing 198 changed files with 724 additions and 424 deletions.
71 changes: 71 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# The sections in this file are ordered in the order presented in https://golangci-lint.run/usage/configuration/.
# The nested fields are ordered alphabetically.

linters-settings:
goheader:
template-path: copyright_header
goimports:
local-prefixes: github.com/runfinch/finch-daemon
gosec:
config:
G306: "0o644"
excludes:
- G304
- G101
- G104
lll:
line-length: 250
tab-width: 4
makezero:
always: true
nolintlint:
require-explanation: true
require-specific: true
stylecheck:
checks: ["all", "-ST1003", "-ST1000", "-ST1001", "-ST1021"]

linters:
enable:
# - errname
# - errorlint
- exportloopref
# - forcetypeassert
# - gocritic
- godot
# - gofumpt
- goheader
# - goimports
- gosec
- lll
# - makezero
- misspell
# - nilerr
# - nilnil
- nolintlint
- nosprintfhostport
# - paralleltest
- predeclared
- reassign
# - revive
- testableexamples
- unconvert
# - unparam
- usestdlibvars
- wastedassign
- whitespace
- stylecheck
disable:
- errcheck

issues:
exclude-rules:
- linters:
- lll
# A go:generate statement has to be in the same line: https://github.com/golang/go/issues/46050.
source: "^//go:generate "

# Some checks enabled in the stylecheck setting are disabled by default
# (e.g., https://golangci-lint.run/usage/false-positives/#exc0013),
# so we need to enable them explicitly here.
exclude-use-default: false
# fix: true
11 changes: 7 additions & 4 deletions cmd/finch-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ import (
"os/signal"
"sync"
"syscall"
"time"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/config"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/runfinch/finch-daemon/pkg/api/router"
"github.com/runfinch/finch-daemon/pkg/archive"
"github.com/runfinch/finch-daemon/pkg/backend"
Expand All @@ -31,9 +36,6 @@ import (
"github.com/runfinch/finch-daemon/pkg/service/network"
"github.com/runfinch/finch-daemon/pkg/service/system"
"github.com/runfinch/finch-daemon/pkg/service/volume"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -99,7 +101,8 @@ func run(options *DaemonOptions) error {
return fmt.Errorf("failed to chown the finch-daemon socket: %w", err)
}
server := &http.Server{
Handler: r,
Handler: r,
ReadHeaderTimeout: 5 * time.Minute,
}
handleSignal(options.socketAddr, server, logger)

Expand Down
2 changes: 2 additions & 0 deletions copyright_header
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
2 changes: 1 addition & 1 deletion e2e/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
)

// NewClient creates a new http client that connects to the finch-daemon server
// NewClient creates a new http client that connects to the finch-daemon server.
func NewClient(socketPath string) *http.Client {
if socketPath == "" {
panic("socketPath is empty")
Expand Down
1 change: 1 addition & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/tests"
)

Expand Down
7 changes: 5 additions & 2 deletions e2e/tests/container_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
)

Expand Down Expand Up @@ -115,10 +116,11 @@ func ContainerAttach(opt *option.Option) {
time.Sleep(1 * time.Second)
res, err := uClient.Post(client.ConvertToFinchUrl(version, relativeUrl+opts),
"application/json", nil)
Expect(err).Should(BeNil())
Expect(err).ShouldNot(HaveOccurred())

time.Sleep(4 * time.Second)
body, err := io.ReadAll(res.Body)
Expect(err).ShouldNot(HaveOccurred())
_ = res.Body.Close()

// make assertions
Expand Down Expand Up @@ -148,10 +150,11 @@ func ContainerAttach(opt *option.Option) {
time.Sleep(1 * time.Second)
res, err := uClient.Post(client.ConvertToFinchUrl(version, relativeUrl+opts),
"application/json", nil)
Expect(err).Should(BeNil())
Expect(err).ShouldNot(HaveOccurred())

time.Sleep(4 * time.Second)
body, err := io.ReadAll(res.Body)
Expect(err).ShouldNot(HaveOccurred())
_ = res.Body.Close()

// make assertions
Expand Down
14 changes: 7 additions & 7 deletions e2e/tests/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
"github.com/docker/go-connections/nat"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/ffs"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/types"
)
Expand Down Expand Up @@ -87,7 +87,7 @@ func ContainerCreate(opt *option.Option) {
Expect(ctr.ID).ShouldNot(BeEmpty())

// create container with duplicate name
statusCode, ctr = createContainer(uClient, url, testContainerName, options)
statusCode, _ = createContainer(uClient, url, testContainerName, options)
Expect(statusCode).Should(Equal(http.StatusInternalServerError))
})
It("should fail to create a container for a nonexistent image", func() {
Expand Down Expand Up @@ -209,7 +209,7 @@ func ContainerCreate(opt *option.Option) {
It("should create a container with a directory mounted from the host", func() {
fileContent := "hello world"
hostFilepath := ffs.CreateTempFile("test-file", fileContent)
ginkgo.DeferCleanup(os.RemoveAll, filepath.Dir(hostFilepath))
DeferCleanup(os.RemoveAll, filepath.Dir(hostFilepath))
ctrFilepath := "/tmp/test-mount/test-file"

// define options
Expand Down Expand Up @@ -237,7 +237,7 @@ func ContainerCreate(opt *option.Option) {
It("should create a container with a directory mounted from the host with read-only permissions", func() {
fileContent := "hello world"
hostFilepath := ffs.CreateTempFile("test-file", fileContent)
ginkgo.DeferCleanup(os.RemoveAll, filepath.Dir(hostFilepath))
DeferCleanup(os.RemoveAll, filepath.Dir(hostFilepath))
ctrFilepath := "/tmp/test-mount/test-file"

// define options
Expand Down Expand Up @@ -388,12 +388,12 @@ func ContainerCreate(opt *option.Option) {
})
}

// creates a container with given options and returns http status code and response
// creates a container with given options and returns http status code and response.
func createContainer(client *http.Client, url, ctrName string, ctrOptions types.ContainerCreateRequest) (int, containerCreateResponse) {
// send create request
reqBody, err := json.Marshal(ctrOptions)
Expect(err).Should(BeNil())
url = url + fmt.Sprintf("?name=%s", ctrName)
url += fmt.Sprintf("?name=%s", ctrName)
res, err := client.Post(url, "application/json", bytes.NewReader(reqBody))
Expect(err).Should(BeNil())

Expand All @@ -404,7 +404,7 @@ func createContainer(client *http.Client, url, ctrName string, ctrOptions types.
return res.StatusCode, ctr
}

// verifies that the container is connected to the network specified
// verifies that the container is connected to the network specified.
func verifyNetworkSettings(opt *option.Option, ctrName, network string) {
// inspect network
resp := command.Stdout(opt, "network", "inspect", network)
Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/types"
)
Expand Down Expand Up @@ -231,7 +232,7 @@ func ContainerList(opt *option.Option) {
})
}

// Checks that the other field for containers is non-null, then sets it to a zero value for comparison with dummy values
// Checks that the other field for containers is non-null, then sets it to a zero value for comparison with dummy values.
func filterContainerList(got []types.ContainerListItem) []types.ContainerListItem {
filtered := []types.ContainerListItem{}
for _, cont := range got {
Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/container_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ func ContainerLogs(opt *option.Option) {
// wait for container to reach steady state, then call endpoint
time.Sleep(1 * time.Second)
res, err := uClient.Get(client.ConvertToFinchUrl(version, relativeUrl+opts))
Expect(err).Should(BeNil())
Expect(err).ShouldNot(HaveOccurred())

time.Sleep(4 * time.Second)
body, err := io.ReadAll(res.Body)
Expect(err).ShouldNot(HaveOccurred())
_ = res.Body.Close()

// make assertions
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/response"
)
Expand Down Expand Up @@ -90,6 +91,5 @@ func ContainerRemove(opt *option.Option) {
Expect(err).Should(BeNil())
Expect(errResponse.Message).Should(Not(BeEmpty()))
})

})
}
1 change: 1 addition & 0 deletions e2e/tests/container_rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/response"
)
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/response"
)
Expand Down Expand Up @@ -51,6 +52,5 @@ func ContainerStart(opt *option.Option) {
Expect(err).Should(BeNil())
Expect(errResponse.Message).Should(Not(BeEmpty()))
})

})
}
5 changes: 3 additions & 2 deletions e2e/tests/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/response"
"github.com/runfinch/finch-daemon/pkg/api/types"
Expand Down Expand Up @@ -244,7 +245,7 @@ func ContainerStats(opt *option.Option) {
})
}

// expectValidStats ensures that the data contained in the stats object is valid
// expectValidStats ensures that the data contained in the stats object is valid.
func expectValidStats(st *types.StatsJSON, name, id string, numNetworks int) {
// verify container name and ID
Expect(st.Name).Should(Equal(name))
Expand Down Expand Up @@ -274,7 +275,7 @@ func expectValidStats(st *types.StatsJSON, name, id string, numNetworks int) {
}

// expectEmptyStats ensures that the data contained in the stats object is empty
// which is the case with containers that are not running
// which is the case with containers that are not running.
func expectEmptyStats(st *types.StatsJSON, name, id string) {
// verify container name and ID
Expect(st.Name).Should(Equal(name))
Expand Down
1 change: 1 addition & 0 deletions e2e/tests/container_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
"github.com/runfinch/finch-daemon/pkg/api/response"
)
Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
eventtype "github.com/runfinch/finch-daemon/pkg/api/events"
)

// SystemEvents tests streaming container events
// SystemEvents tests streaming container events.
func SystemEvents(opt *option.Option) {
Describe("stream container events", func() {
var (
Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
)

Expand Down Expand Up @@ -123,7 +124,7 @@ func ImagePull(opt *option.Option) {
})
}

// waitForResponse waits until the http response is closed with EOF
// waitForResponse waits until the http response is closed with EOF.
func waitForResponse(resp *http.Response) {
buf := make([]byte, 4096)
for {
Expand Down
5 changes: 3 additions & 2 deletions e2e/tests/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/runfinch/common-tests/ffs"
"github.com/runfinch/common-tests/fnet"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch-daemon/e2e/client"
)

Expand Down Expand Up @@ -106,9 +107,9 @@ func ImagePush(opt *option.Option) {
Expect(err).Should(BeNil())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
data, _ := io.ReadAll(resp.Body)
Expect(string(data[:])).Should(ContainSubstring(`"errorDetail"`))
Expect(string(data)).Should(ContainSubstring(`"errorDetail"`))
})

//TODO: add a e2e test that push an image to ecr public which requires authentication.
// TODO: add a e2e test that push an image to ecr public which requires authentication.
})
}
Loading

0 comments on commit 183581a

Please sign in to comment.