From ef07133a5ee82724c6b757f182e04bd6a3040ca9 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sat, 20 Nov 2021 14:02:44 +0800 Subject: [PATCH 1/2] Run `go fmt ./...` and `go mod tidy -go=1.17` Signed-off-by: Eng Zer Jun --- config/config.go | 5 ++--- go.mod | 19 +++++++++++-------- main.go | 13 +++++-------- metadata.go | 6 ++++-- protect/queue.go | 2 +- s3_test.go | 10 ++++++---- selenoid.go | 8 ++++---- selenoid_test.go | 12 +++++------- service/docker.go | 8 ++++---- service/driver.go | 16 ++++++++-------- service/driver_unix.go | 7 ++++--- service/driver_windows.go | 1 + service_test.go | 17 +++++++++-------- session/session.go | 3 ++- upload/s3.go | 14 ++++++++------ upload/uploader.go | 5 +++-- utils_test.go | 8 +++----- 17 files changed, 80 insertions(+), 74 deletions(-) diff --git a/config/config.go b/config/config.go index 9e555cdf..23647cb1 100644 --- a/config/config.go +++ b/config/config.go @@ -1,17 +1,16 @@ package config import ( - "log" - "encoding/json" "fmt" "io/ioutil" + "log" "strings" "sync" + "time" "github.com/aerokube/selenoid/session" "github.com/docker/docker/api/types/container" - "time" ) // Session - session id and vnc flag diff --git a/go.mod b/go.mod index d32140f3..4656a159 100644 --- a/go.mod +++ b/go.mod @@ -3,28 +3,31 @@ module github.com/aerokube/selenoid go 1.17 require ( - github.com/Microsoft/go-winio v0.4.12 // indirect github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e github.com/aerokube/ggr v0.0.0-20181215175518-4a2e23fa1769 github.com/aerokube/util v0.0.0-20190701120823-161c21b50f69 github.com/aws/aws-sdk-go v1.20.12 - github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v0.7.3-0.20190629173937-e105a74c5419 github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.4.0 - github.com/gogo/protobuf v1.2.1 // indirect - github.com/golang/protobuf v1.2.0 // indirect github.com/gorilla/websocket v1.4.2 github.com/imdario/mergo v0.3.6 + github.com/mafredri/cdp v0.21.0 + github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c + github.com/pkg/errors v0.8.1 + golang.org/x/net v0.0.0-20190311183353-d8887717615a +) + +require ( + github.com/Microsoft/go-winio v0.4.12 // indirect + github.com/docker/distribution v2.7.1+incompatible // indirect + github.com/gogo/protobuf v1.2.1 // indirect + github.com/golang/protobuf v1.2.0 // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect - github.com/mafredri/cdp v0.21.0 github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect - github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c - github.com/pkg/errors v0.8.1 github.com/sirupsen/logrus v1.4.2 // indirect - golang.org/x/net v0.0.0-20190311183353-d8887717615a golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 // indirect google.golang.org/grpc v1.21.1 // indirect diff --git a/main.go b/main.go index 531e6df8..9759211b 100644 --- a/main.go +++ b/main.go @@ -4,27 +4,22 @@ import ( "context" "encoding/json" "flag" - "github.com/aerokube/selenoid/jsonerror" - "github.com/pkg/errors" + "fmt" "log" "net" "net/http" "os" "os/signal" "path" + "path/filepath" "strconv" "strings" "syscall" "time" - "golang.org/x/net/websocket" - - "fmt" - - "path/filepath" - ggr "github.com/aerokube/ggr/config" "github.com/aerokube/selenoid/config" + "github.com/aerokube/selenoid/jsonerror" "github.com/aerokube/selenoid/protect" "github.com/aerokube/selenoid/service" "github.com/aerokube/selenoid/session" @@ -32,6 +27,8 @@ import ( "github.com/aerokube/util" "github.com/aerokube/util/docker" "github.com/docker/docker/client" + "github.com/pkg/errors" + "golang.org/x/net/websocket" ) var ( diff --git a/metadata.go b/metadata.go index 2481a3e6..8d226e34 100644 --- a/metadata.go +++ b/metadata.go @@ -1,15 +1,17 @@ +//go:build metadata // +build metadata package main import ( "encoding/json" - "github.com/aerokube/selenoid/event" - "github.com/aerokube/selenoid/session" "io/ioutil" "log" "path/filepath" "time" + + "github.com/aerokube/selenoid/event" + "github.com/aerokube/selenoid/session" ) const metadataFileExtension = ".json" diff --git a/protect/queue.go b/protect/queue.go index abbddaf9..d6c3f28c 100644 --- a/protect/queue.go +++ b/protect/queue.go @@ -2,12 +2,12 @@ package protect import ( "errors" - "github.com/aerokube/selenoid/jsonerror" "log" "math" "net/http" "time" + "github.com/aerokube/selenoid/jsonerror" "github.com/aerokube/util" ) diff --git a/s3_test.go b/s3_test.go index c6e62a54..b8e14866 100644 --- a/s3_test.go +++ b/s3_test.go @@ -1,13 +1,10 @@ +//go:build s3 // +build s3 package main import ( "context" - . "github.com/aandryashin/matchers" - "github.com/aerokube/selenoid/event" - "github.com/aerokube/selenoid/session" - "github.com/aerokube/selenoid/upload" "io/ioutil" "net" "net/http" @@ -15,6 +12,11 @@ import ( "strings" "testing" "time" + + . "github.com/aandryashin/matchers" + "github.com/aerokube/selenoid/event" + "github.com/aerokube/selenoid/session" + "github.com/aerokube/selenoid/upload" ) var ( diff --git a/selenoid.go b/selenoid.go index 835584ae..12713670 100644 --- a/selenoid.go +++ b/selenoid.go @@ -9,10 +9,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/aerokube/selenoid/event" - "github.com/aerokube/selenoid/jsonerror" - "github.com/aerokube/selenoid/service" - "github.com/imdario/mergo" "io" "io/ioutil" "log" @@ -28,10 +24,14 @@ import ( "sync" "time" + "github.com/aerokube/selenoid/event" + "github.com/aerokube/selenoid/jsonerror" + "github.com/aerokube/selenoid/service" "github.com/aerokube/selenoid/session" "github.com/aerokube/util" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" + "github.com/imdario/mergo" "golang.org/x/net/websocket" ) diff --git a/selenoid_test.go b/selenoid_test.go index c9172dfa..9983d8d2 100644 --- a/selenoid_test.go +++ b/selenoid_test.go @@ -3,26 +3,24 @@ package main import ( "bytes" "context" + "encoding/json" "fmt" - "github.com/mafredri/cdp" - "github.com/mafredri/cdp/rpcc" "io/ioutil" "log" "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" "time" - "github.com/aerokube/selenoid/config" - - "encoding/json" - "path/filepath" - . "github.com/aandryashin/matchers" . "github.com/aandryashin/matchers/httpresp" ggr "github.com/aerokube/ggr/config" + "github.com/aerokube/selenoid/config" + "github.com/mafredri/cdp" + "github.com/mafredri/cdp/rpcc" ) var _ = func() bool { diff --git a/service/docker.go b/service/docker.go index a102f9cf..447309bc 100644 --- a/service/docker.go +++ b/service/docker.go @@ -3,11 +3,13 @@ package service import ( "context" "fmt" - "github.com/docker/go-units" "log" "net" "net/url" + "os" + "path/filepath" "strconv" + "strings" "time" "github.com/aerokube/selenoid/config" @@ -20,9 +22,7 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/pkg/stdcopy" "github.com/docker/go-connections/nat" - "os" - "path/filepath" - "strings" + "github.com/docker/go-units" ) const ( diff --git a/service/driver.go b/service/driver.go index 342b2de6..587d37cd 100644 --- a/service/driver.go +++ b/service/driver.go @@ -1,18 +1,18 @@ package service import ( + "errors" "fmt" "log" "net" "net/url" + "os" "os/exec" + "path/filepath" "time" - "errors" "github.com/aerokube/selenoid/session" "github.com/aerokube/util" - "os" - "path/filepath" ) // Driver - driver processes manager @@ -77,11 +77,11 @@ func (d *Driver) StartWithCancel() (*StartedService, error) { } log.Printf("[%d] [PROCESS_STARTED] [%d] [%.2fs]", requestId, cmd.Process.Pid, util.SecondsSince(s)) log.Printf("[%d] [PROXY_TO] [%s]", requestId, u.String()) - hp := session.HostPort{} - if d.Caps.VNC { - hp.VNC = "127.0.0.1:5900" - } - return &StartedService{Url: u, HostPort: hp, Cancel: func() { d.stopProcess(cmd) }}, nil + hp := session.HostPort{} + if d.Caps.VNC { + hp.VNC = "127.0.0.1:5900" + } + return &StartedService{Url: u, HostPort: hp, Cancel: func() { d.stopProcess(cmd) }}, nil } func (d *Driver) stopProcess(cmd *exec.Cmd) { diff --git a/service/driver_unix.go b/service/driver_unix.go index e0ff87f2..8f95e3d0 100644 --- a/service/driver_unix.go +++ b/service/driver_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package service @@ -8,7 +9,7 @@ import ( ) func stopProc(cmd *exec.Cmd) error { - exitCode := cmd.Process.Signal(syscall.SIGINT) - cmd.Wait() - return exitCode + exitCode := cmd.Process.Signal(syscall.SIGINT) + cmd.Wait() + return exitCode } diff --git a/service/driver_windows.go b/service/driver_windows.go index cff64adb..782bb1ab 100644 --- a/service/driver_windows.go +++ b/service/driver_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package service diff --git a/service_test.go b/service_test.go index 9d34cd68..40d3d51c 100644 --- a/service_test.go +++ b/service_test.go @@ -3,14 +3,6 @@ package main import ( "bytes" "fmt" - . "github.com/aandryashin/matchers" - "github.com/aerokube/selenoid/config" - "github.com/aerokube/selenoid/service" - "github.com/aerokube/selenoid/session" - "github.com/aerokube/util" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/client" - "golang.org/x/net/websocket" "io" "io/ioutil" "net" @@ -21,6 +13,15 @@ import ( "sync" "testing" "time" + + . "github.com/aandryashin/matchers" + "github.com/aerokube/selenoid/config" + "github.com/aerokube/selenoid/service" + "github.com/aerokube/selenoid/session" + "github.com/aerokube/util" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/client" + "golang.org/x/net/websocket" ) var ( diff --git a/session/session.go b/session/session.go index 3aa53936..3327d77e 100644 --- a/session/session.go +++ b/session/session.go @@ -1,10 +1,11 @@ package session import ( - "github.com/imdario/mergo" "net/url" "sync" "time" + + "github.com/imdario/mergo" ) // Caps - user capabilities diff --git a/upload/s3.go b/upload/s3.go index 01ed6dd8..b54dfbf1 100644 --- a/upload/s3.go +++ b/upload/s3.go @@ -1,3 +1,4 @@ +//go:build s3 // +build s3 package upload @@ -5,18 +6,19 @@ package upload import ( "flag" "fmt" - "github.com/aerokube/selenoid/event" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/credentials" - awssession "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/s3/s3manager" - "github.com/pkg/errors" "log" "mime" "os" "path/filepath" "strings" "time" + + "github.com/aerokube/selenoid/event" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + awssession "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3/s3manager" + "github.com/pkg/errors" ) func init() { diff --git a/upload/uploader.go b/upload/uploader.go index 7ce3d949..3bbb70b1 100644 --- a/upload/uploader.go +++ b/upload/uploader.go @@ -1,10 +1,11 @@ package upload import ( - "github.com/aerokube/selenoid/event" - "github.com/aerokube/util" "log" "time" + + "github.com/aerokube/selenoid/event" + "github.com/aerokube/util" ) var ( diff --git a/utils_test.go b/utils_test.go index d40a0457..160afdd6 100644 --- a/utils_test.go +++ b/utils_test.go @@ -5,8 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/aerokube/selenoid/protect" - "github.com/gorilla/websocket" "log" "net/http" "net/http/httptest" @@ -14,14 +12,14 @@ import ( "strconv" "strings" "sync" - - "time" - "testing" + "time" . "github.com/aandryashin/matchers" + "github.com/aerokube/selenoid/protect" "github.com/aerokube/selenoid/service" "github.com/aerokube/selenoid/session" + "github.com/gorilla/websocket" "github.com/pborman/uuid" ) From 417f0d15eac780e37687b88589b3c78f35ab1819 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sat, 20 Nov 2021 14:15:18 +0800 Subject: [PATCH 2/2] Move from io/ioutil to io and os packages The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun --- config/config.go | 4 ++-- config_test.go | 3 +-- metadata.go | 4 ++-- s3_test.go | 4 ++-- selenoid.go | 5 ++--- selenoid_test.go | 20 ++++++++++---------- service_test.go | 3 +-- 7 files changed, 20 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index 23647cb1..92dd8bda 100644 --- a/config/config.go +++ b/config/config.go @@ -3,8 +3,8 @@ package config import ( "encoding/json" "fmt" - "io/ioutil" "log" + "os" "strings" "sync" "time" @@ -85,7 +85,7 @@ func NewConfig() *Config { } func loadJSON(filename string, v interface{}) error { - buf, err := ioutil.ReadFile(filename) + buf, err := os.ReadFile(filename) if err != nil { return fmt.Errorf("read error: %v", err) } diff --git a/config_test.go b/config_test.go index f848afb0..fbd200dc 100644 --- a/config_test.go +++ b/config_test.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "testing" @@ -15,7 +14,7 @@ import ( const testLogConf = "config/container-logs.json" func configfile(s string) string { - tmp, err := ioutil.TempFile("", "config") + tmp, err := os.CreateTemp("", "config") if err != nil { log.Fatal(err) } diff --git a/metadata.go b/metadata.go index 8d226e34..b8ed2768 100644 --- a/metadata.go +++ b/metadata.go @@ -5,8 +5,8 @@ package main import ( "encoding/json" - "io/ioutil" "log" + "os" "path/filepath" "time" @@ -39,7 +39,7 @@ func (mp *MetadataProcessor) OnSessionStopped(stoppedSession event.StoppedSessio return } filename := filepath.Join(logOutputDir, stoppedSession.SessionId+metadataFileExtension) - err = ioutil.WriteFile(filename, data, 0644) + err = os.WriteFile(filename, data, 0644) if err != nil { log.Printf("[%d] [METADATA] [%s] [Failed to save to %s: %v]", stoppedSession.RequestId, stoppedSession.SessionId, filename, err) return diff --git a/s3_test.go b/s3_test.go index b8e14866..7ba0fe69 100644 --- a/s3_test.go +++ b/s3_test.go @@ -5,10 +5,10 @@ package main import ( "context" - "io/ioutil" "net" "net/http" "net/http/httptest" + "os" "strings" "testing" "time" @@ -64,7 +64,7 @@ func TestS3Uploader(t *testing.T) { ReducedRedundancy: true, } uploader.Init() - f, _ := ioutil.TempFile("", "some-file") + f, _ := os.CreateTemp("", "some-file") input := event.CreatedFile{ Event: event.Event{ RequestId: 4342, diff --git a/selenoid.go b/selenoid.go index 12713670..8e1f3aa3 100644 --- a/selenoid.go +++ b/selenoid.go @@ -10,7 +10,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -112,7 +111,7 @@ func create(w http.ResponseWriter, r *http.Request) { sessionStartTime := time.Now() requestId := serial() user, remote := util.RequestInfo(r) - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() if err != nil { log.Printf("[%d] [ERROR_READING_REQUEST] [%v]", requestId, err) @@ -680,7 +679,7 @@ func logs(w http.ResponseWriter, r *http.Request) { } func listFilesAsJson(requestId uint64, w http.ResponseWriter, dir string, errStatus string) { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { log.Printf("[%d] [%s] [%s]", requestId, errStatus, fmt.Sprintf("Failed to list directory %s: %v", logOutputDir, err)) w.WriteHeader(http.StatusInternalServerError) diff --git a/selenoid_test.go b/selenoid_test.go index 9983d8d2..d15c1e5b 100644 --- a/selenoid_test.go +++ b/selenoid_test.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -34,8 +34,8 @@ var ( func init() { enableFileUpload = true - videoOutputDir, _ = ioutil.TempDir("", "selenoid-test") - logOutputDir, _ = ioutil.TempDir("", "selenoid-test") + videoOutputDir, _ = os.MkdirTemp("", "selenoid-test") + logOutputDir, _ = os.MkdirTemp("", "selenoid-test") saveAllLogs = true gitRevision = "test-revision" ggrHost = &ggr.Host{ @@ -652,7 +652,7 @@ func TestFileUpload(t *testing.T) { f, err := os.Open(jsonResponse["value"]) AssertThat(t, err, Is{nil}) - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) AssertThat(t, err, Is{nil}) AssertThat(t, string(content), EqualTo{"Hello World!"}) @@ -720,7 +720,7 @@ func TestPing(t *testing.T) { AssertThat(t, rsp.Body, Is{Not{nil}}) var data map[string]interface{} - bt, readErr := ioutil.ReadAll(rsp.Body) + bt, readErr := io.ReadAll(rsp.Body) AssertThat(t, readErr, Is{nil}) jsonErr := json.Unmarshal(bt, &data) AssertThat(t, jsonErr, Is{nil}) @@ -743,7 +743,7 @@ func TestStatus(t *testing.T) { AssertThat(t, rsp.Body, Is{Not{nil}}) var data map[string]interface{} - bt, readErr := ioutil.ReadAll(rsp.Body) + bt, readErr := io.ReadAll(rsp.Body) AssertThat(t, readErr, Is{nil}) jsonErr := json.Unmarshal(bt, &data) AssertThat(t, jsonErr, Is{nil}) @@ -760,7 +760,7 @@ func TestStatus(t *testing.T) { func TestServeAndDeleteVideoFile(t *testing.T) { fileName := "testfile" filePath := filepath.Join(videoOutputDir, fileName) - ioutil.WriteFile(filePath, []byte("test-data"), 0644) + os.WriteFile(filePath, []byte("test-data"), 0644) rsp, err := http.Get(With(srv.URL).Path("/video/testfile")) AssertThat(t, err, Is{nil}) @@ -787,7 +787,7 @@ func TestServeAndDeleteVideoFile(t *testing.T) { func TestServeAndDeleteLogFile(t *testing.T) { fileName := "logfile.log" filePath := filepath.Join(logOutputDir, fileName) - ioutil.WriteFile(filePath, []byte("test-data"), 0644) + os.WriteFile(filePath, []byte("test-data"), 0644) rsp, err := http.Get(With(srv.URL).Path("/logs/logfile.log")) AssertThat(t, err, Is{nil}) @@ -822,7 +822,7 @@ func TestFileDownload(t *testing.T) { rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/download/%s/testfile", sess["sessionId"]))) AssertThat(t, err, Is{nil}) AssertThat(t, rsp, Code{http.StatusOK}) - data, err := ioutil.ReadAll(rsp.Body) + data, err := io.ReadAll(rsp.Body) AssertThat(t, err, Is{nil}) AssertThat(t, string(data), EqualTo{"test-data"}) @@ -848,7 +848,7 @@ func TestClipboard(t *testing.T) { rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/clipboard/%s", sess["sessionId"]))) AssertThat(t, err, Is{nil}) AssertThat(t, rsp, Code{http.StatusOK}) - data, err := ioutil.ReadAll(rsp.Body) + data, err := io.ReadAll(rsp.Body) AssertThat(t, err, Is{nil}) AssertThat(t, string(data), EqualTo{"test-clipboard-value"}) diff --git a/service_test.go b/service_test.go index 40d3d51c..b59f6289 100644 --- a/service_test.go +++ b/service_test.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -223,7 +222,7 @@ func testConfig(env *service.Environment) *config.Config { } func testEnvironment() *service.Environment { - logOutputDir, _ = ioutil.TempDir("", "selenoid-test") + logOutputDir, _ = os.MkdirTemp("", "selenoid-test") return &service.Environment{ CPU: int64(0), Memory: int64(0),