Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #391 from vania-pooh/master
Browse files Browse the repository at this point in the history
Switched to github.com/stretchr/testify in tests
  • Loading branch information
vania-pooh authored Dec 31, 2023
2 parents 5fe0f6f + 73c4590 commit 6c523a3
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 328 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ cat /etc/grid-router/quota/test.xml
</browser>
</qa:browsers>
```
***Note***: file name should correspond to user name you added to htpasswd file. For user ```test``` we added on previous steps you should create ```test.xml```.
***Note***: file name should correspond to username you added to htpasswd file. For user ```test``` we added on previous steps you should create ```test.xml```.

6) Start Ggr container:
```
Expand Down
114 changes: 57 additions & 57 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
assert "github.com/stretchr/testify/require"
"os"
"testing"

. "github.com/aandryashin/matchers"
. "github.com/aerokube/ggr/config"
)

Expand All @@ -15,32 +15,32 @@ func init() {

func TestEmptyListOfHosts(t *testing.T) {
host, index := choose(Hosts{})
AssertThat(t, host, Is{(*Host)(nil)})
AssertThat(t, index, EqualTo{-1})
assert.Nil(t, host)
assert.Equal(t, index, -1)
}

func TestNothingToChoose(t *testing.T) {
host, index := choose(Hosts{Host{Count: 0}, Host{Count: 0}})
AssertThat(t, host, Is{(*Host)(nil)})
AssertThat(t, index, EqualTo{-1})
assert.Nil(t, host)
assert.Equal(t, index, -1)
}

func TestChooseFirst(t *testing.T) {
host, index := choose(Hosts{Host{Name: "first", Count: 1}, Host{Name: "mid", Count: 0}, Host{Name: "last", Count: 0}})
AssertThat(t, host.Name, EqualTo{"first"})
AssertThat(t, index, EqualTo{0})
assert.Equal(t, host.Name, "first")
assert.Equal(t, index, 0)
}

func TestChooseMid(t *testing.T) {
host, index := choose(Hosts{Host{Name: "first", Count: 0}, Host{Name: "mid", Count: 1}, Host{Name: "last", Count: 0}})
AssertThat(t, host.Name, EqualTo{"mid"})
AssertThat(t, index, EqualTo{1})
assert.Equal(t, host.Name, "mid")
assert.Equal(t, index, 1)
}

func TestChooseLast(t *testing.T) {
host, index := choose(Hosts{Host{Name: "first", Count: 0}, Host{Name: "mid", Count: 0}, Host{Name: "last", Count: 1}})
AssertThat(t, host.Name, EqualTo{"last"})
AssertThat(t, index, EqualTo{2})
assert.Equal(t, host.Name, "last")
assert.Equal(t, index, 2)
}

var (
Expand Down Expand Up @@ -92,84 +92,84 @@ var (

func TestFindDefaultVersion(t *testing.T) {
hosts, version, _ := browsersWithMultipleVersions.find("browser", "", "", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0")
}

func TestFindVersion(t *testing.T) {
hosts, version, _ := browsersWithMultipleVersions.find("browser", "1.0", "LINUX", newSet(), newSet())
AssertThat(t, version, EqualTo{"1.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-1.0"})
assert.Equal(t, version, "1.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-1.0")
}

func TestFindVersionByPrefix(t *testing.T) {
hosts, version, _ := browsersWithMultipleVersions.find("browser", "1", "", newSet(), newSet())
AssertThat(t, version, EqualTo{"1.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-1.0"})
assert.Equal(t, version, "1.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-1.0")
}

func TestVersionNotFound(t *testing.T) {
hosts, version, _ := browsersWithMultipleVersions.find("browser", "missing", "", newSet(), newSet())
AssertThat(t, version, EqualTo{"missing"})
AssertThat(t, len(hosts), EqualTo{0})
assert.Equal(t, version, "missing")
assert.Empty(t, hosts)
}

func TestFindWithExcludedRegions(t *testing.T) {
hosts, version, _ := browsersWithMultipleRegions.find("browser", "1.0", "", newSet(), newSet("f"))
AssertThat(t, version, EqualTo{"1.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-e-1.0"})
assert.Equal(t, version, "1.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-e-1.0")
}

func TestFindWithExcludedRegionsExhausted(t *testing.T) {
hosts, _, excludedRegions := browsersWithMultipleRegions.find("browser", "1.0", "", newSet(), newSet("e", "f"))
AssertThat(t, len(hosts), EqualTo{2})
AssertThat(t, excludedRegions.size(), EqualTo{0})
assert.Len(t, hosts, 2)
assert.Equal(t, excludedRegions.size(), 0)
}

func TestFindWithExcludedHosts(t *testing.T) {
hosts, version, _ := browsersWithMultipleRegions.find("browser", "1.0", "", newSet("browser-e-1.0:4444"), newSet())
AssertThat(t, version, EqualTo{"1.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-f-1.0"})
assert.Equal(t, version, "1.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-f-1.0")
}

func TestFindWithDefaultPlatform(t *testing.T) {
hosts, version, _ := browsersWithMultiplePlatforms.find("browser", "2.0", "", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0-linux"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0-linux")
}

func TestFindWithAnyPlatform(t *testing.T) {
hosts, version, _ := browsersWithMultiplePlatforms.find("browser", "2.0", "ANY", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0-linux"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0-linux")
}

func TestFindWithPlatform(t *testing.T) {
hosts, version, _ := browsersWithMultiplePlatforms.find("browser", "2.0", "LINUX", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0-linux"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0-linux")
}

func TestFindWithPlatformLowercase(t *testing.T) {
hosts, version, _ := browsersWithMultiplePlatforms.find("browser", "2.0", "windows", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0-windows"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0-windows")
}

func TestFindWithPlatformPrefix(t *testing.T) {
hosts, version, _ := browsersWithMultiplePlatforms.find("browser", "2.0", "WIN", newSet(), newSet())
AssertThat(t, version, EqualTo{"2.0"})
AssertThat(t, len(hosts), EqualTo{1})
AssertThat(t, hosts[0].Name, EqualTo{"browser-2.0-windows"})
assert.Equal(t, version, "2.0")
assert.Len(t, hosts, 1)
assert.Equal(t, hosts[0].Name, "browser-2.0-windows")
}

func TestReadNotExistingConfig(t *testing.T) {
Expand All @@ -184,8 +184,8 @@ func TestReadNotExistingConfig(t *testing.T) {
var browsers Browsers
err = readConfig(tmp.Name(), &browsers)

AssertThat(t, err, Is{Not{nil}})
AssertThat(t, err.Error(), EqualTo{fmt.Sprintf("error reading configuration file %s: open %s: no such file or directory", tmp.Name(), tmp.Name())})
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf("error reading configuration file %s: open %s: no such file or directory", tmp.Name(), tmp.Name()))
}

func TestParseInvalidConfig(t *testing.T) {
Expand All @@ -205,8 +205,8 @@ func TestParseInvalidConfig(t *testing.T) {
var browsers Browsers
err = readConfig(tmp.Name(), &browsers)

AssertThat(t, err, Is{Not{nil}})
AssertThat(t, err.Error(), EqualTo{fmt.Sprintf("error parsing configuration file %s: EOF", tmp.Name())})
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf("error parsing configuration file %s: EOF", tmp.Name()))
}

func TestParseConfig(t *testing.T) {
Expand Down Expand Up @@ -234,20 +234,20 @@ func testParseConfig(t *testing.T, config string) {
var browsers Browsers
err = readConfig(tmp.Name(), &browsers)

AssertThat(t, err, Is{nil})
AssertThat(t, browsers.Browsers[0].Name, EqualTo{"browser"})
assert.NoError(t, err)
assert.Equal(t, browsers.Browsers[0].Name, "browser")
}

func TestConfDirDoesNotExist(t *testing.T) {
err := loadQuotaFiles("missing-dir")
AssertThat(t, err, Is{Not{nil}})
assert.Error(t, err)
}

func TestConcurrentReload(t *testing.T) {
go func() {
loadQuotaFiles("quota")
_ = loadQuotaFiles("quota")
}()
loadQuotaFiles("quota")
_ = loadQuotaFiles("quota")
}

func TestChoosingAllHosts(t *testing.T) {
Expand All @@ -258,7 +258,7 @@ func TestChoosingAllHosts(t *testing.T) {
host, _ := choose(hosts)
chosenHosts[host.Name]++
}
AssertThat(t, chosenHosts["first"] > 0, Is{true})
AssertThat(t, chosenHosts["mid"] > 0, Is{true})
AssertThat(t, chosenHosts["last"] > 0, Is{true})
assert.True(t, chosenHosts["first"] > 0)
assert.True(t, chosenHosts["mid"] > 0)
assert.True(t, chosenHosts["last"] > 0)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module github.com/aerokube/ggr
go 1.21

require (
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e
github.com/aandryashin/reloader v0.0.0-20161127125235-da4f1b43ce40
github.com/abbot/go-http-auth v0.4.1-0.20220112235402-e1cee1c72f2f
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.19.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 10 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e h1:ogUKYFNcdYUIBSLibE4+EjbTJazoHr5JsWWx21Lpn8c=
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e/go.mod h1:cbmYNkm9xeQlNoWEPtOUcvNok2gSD7ErMnYkRW+eHi8=
github.com/aandryashin/reloader v0.0.0-20161127125235-da4f1b43ce40 h1:zSKVi4h3Kv0HvNGjWPOsOEJIxtvd3PRMlVA/fcCB45g=
github.com/aandryashin/reloader v0.0.0-20161127125235-da4f1b43ce40/go.mod h1:gvg2/m9OQ4ZwK4Qk/mnfgokCb4qDN4BGyle+QGw4VOc=
github.com/abbot/go-http-auth v0.4.1-0.20220112235402-e1cee1c72f2f h1:R2ZVGCZzU95oXFJxncosHS9LsX8N4/MYUdGGWOb2cFk=
github.com/abbot/go-http-auth v0.4.1-0.20220112235402-e1cee1c72f2f/go.mod h1:l2P3JyHa+fjy5Bxol6y1u2o4DV/mv3QMBdBu2cNR53w=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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=
24 changes: 12 additions & 12 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c caps) capabilityJsonWireW3C(jsonWire, W3C string) string {
return result
}

func (c *caps) browser() string {
func (c caps) browser() string {
browserName := c.capability("browserName")
if browserName != "" {
return browserName
Expand Down Expand Up @@ -237,7 +237,7 @@ func session(ctx context.Context, h *Host, header http.Header, c caps) (map[stri
func reply(w http.ResponseWriter, msg map[string]interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(msg)
_ = json.NewEncoder(w).Encode(msg)
}

func serial() uint64 {
Expand Down Expand Up @@ -402,7 +402,7 @@ loop:
}

func secondsSince(start time.Time) float64 {
return float64(time.Now().Sub(start).Seconds())
return time.Now().Sub(start).Seconds()
}

func proxy(w http.ResponseWriter, r *http.Request) {
Expand All @@ -420,7 +420,7 @@ func proxy(w http.ResponseWriter, r *http.Request) {
if ok {
if r.Body != nil {
if body, err := io.ReadAll(r.Body); err == nil {
r.Body.Close()
_ = r.Body.Close()
var msg map[string]interface{}
if err := json.Unmarshal(body, &msg); err == nil {
delete(msg, "sessionId")
Expand Down Expand Up @@ -462,7 +462,7 @@ func ping(w http.ResponseWriter, _ *http.Request) {
lrt := lastReloadTime.Format(time.RFC3339)
confLock.RUnlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(struct {
_ = json.NewEncoder(w).Encode(struct {
Uptime string `json:"uptime"`
LastReloadTime string `json:"lastReloadTime"`
NumRequests uint64 `json:"numRequests"`
Expand All @@ -479,7 +479,7 @@ func ping(w http.ResponseWriter, _ *http.Request) {

func status(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(
_ = json.NewEncoder(w).Encode(
map[string]interface{}{
"value": map[string]interface{}{
"message": fmt.Sprintf("Ggr %s built at %s", gitRevision, buildStamp),
Expand All @@ -500,7 +500,7 @@ func host(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if len(path) < tail {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid session ID"))
_, _ = w.Write([]byte("invalid session ID"))
return
}
sum := path[head:tail]
Expand All @@ -509,12 +509,12 @@ func host(w http.ResponseWriter, r *http.Request) {
confLock.RUnlock()
if !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("unknown host"))
_, _ = w.Write([]byte("unknown host"))
return
}
log.Printf("[%d] [-] [HOST_INFO_REQUESTED] [%s] [%s] [-] [%s] [%s] [-] [-]\n", id, user, remote, h.Name, sum)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Host{Name: h.Name, Port: h.Port, Count: h.Count})
_ = json.NewEncoder(w).Encode(Host{Name: h.Name, Port: h.Port, Count: h.Count})
}

func quotaInfo(w http.ResponseWriter, r *http.Request) {
Expand All @@ -539,7 +539,7 @@ func quotaInfo(w http.ResponseWriter, r *http.Request) {
}
}
}
json.NewEncoder(w).Encode(browsers.Browsers.Browsers)
_ = json.NewEncoder(w).Encode(browsers.Browsers.Browsers)
}

func postOnly(handler http.HandlerFunc) http.HandlerFunc {
Expand Down Expand Up @@ -726,10 +726,10 @@ func proxyConn(id uint64, wsconn *websocket.Conn, conn net.Conn, err error, sess
wsconn.PayloadType = websocket.BinaryFrame
go func() {
io.Copy(wsconn, conn)
wsconn.Close()
_ = wsconn.Close()
log.Printf("[%d] [-] [WS_SESSION_CLOSED] [-] [-] [-] [%s] [%s] [-] [-]", id, address, sessionID)
}()
io.Copy(conn, wsconn)
_, _ = io.Copy(conn, wsconn)
log.Printf("[%d] [-] [WS_CLIENT_DISCONNECTED] [-] [-] [-] [%s] [%s] [-] [-]", id, address, sessionID)
}

Expand Down
Loading

0 comments on commit 6c523a3

Please sign in to comment.