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

Commit

Permalink
Using github.com/stretchr/testify for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Dec 31, 2023
1 parent 675a1fa commit 4015eda
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 406 deletions.
116 changes: 60 additions & 56 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
"testing"

. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/session"
assert "github.com/stretchr/testify/require"
)

const testLogConf = "config/container-logs.json"
Expand All @@ -34,31 +34,33 @@ func TestConfig(t *testing.T) {
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
AssertThat(t, err, Is{nil})
assert.NoError(t, err)
}

func TestConfigError(t *testing.T) {
confFile := configfile(`{}`)
os.Remove(confFile)
_ = os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
AssertThat(t, err.Error(), EqualTo{fmt.Sprintf("browsers config: read error: open %s: no such file or directory", confFile)})
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf("browsers config: read error: open %s: no such file or directory", confFile))
}

func TestLogConfigError(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, "some-missing-file")
AssertThat(t, err, Not{nil})
assert.Error(t, err)
}

func TestConfigParseError(t *testing.T) {
confFile := configfile(`{`)
defer os.Remove(confFile)
var conf config.Config
err := conf.Load(confFile, testLogConf)
AssertThat(t, err.Error(), EqualTo{"browsers config: parse error: unexpected end of JSON input"})
assert.Error(t, err)
assert.Equal(t, err.Error(), "browsers config: parse error: unexpected end of JSON input")
}

func TestConfigEmptyState(t *testing.T) {
Expand All @@ -68,42 +70,42 @@ func TestConfigEmptyState(t *testing.T) {
conf.Load(confFile, testLogConf)

state := conf.State(session.NewMap(), 0, 0, 0)
AssertThat(t, state.Total, EqualTo{0})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{0})
assert.Equal(t, state.Total, 0)
assert.Equal(t, state.Queued, 0)
assert.Equal(t, state.Pending, 0)
assert.Equal(t, state.Used, 0)
}

func TestConfigNonEmptyState(t *testing.T) {
confFile := configfile(`{}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_ = conf.Load(confFile, testLogConf)

sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
assert.Equal(t, state.Total, 1)
assert.Equal(t, state.Queued, 0)
assert.Equal(t, state.Pending, 0)
assert.Equal(t, state.Used, 1)
assert.Equal(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, 1)
}

func TestConfigEmptyVersions(t *testing.T) {
confFile := configfile(`{"firefox":{}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
_ = conf.Load(confFile, testLogConf)

sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
assert.Equal(t, state.Total, 1)
assert.Equal(t, state.Queued, 0)
assert.Equal(t, state.Pending, 0)
assert.Equal(t, state.Used, 1)
assert.Equal(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, 1)
}

func TestConfigNonEmptyVersions(t *testing.T) {
Expand All @@ -115,11 +117,11 @@ func TestConfigNonEmptyVersions(t *testing.T) {
sessions := session.NewMap()
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
AssertThat(t, state.Pending, EqualTo{0})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, EqualTo{1})
assert.Equal(t, state.Total, 1)
assert.Equal(t, state.Queued, 0)
assert.Equal(t, state.Pending, 0)
assert.Equal(t, state.Used, 1)
assert.Equal(t, state.Browsers["firefox"]["49.0"]["unknown"].Count, 1)
}

func TestConfigFindMissingBrowser(t *testing.T) {
Expand All @@ -129,7 +131,7 @@ func TestConfigFindMissingBrowser(t *testing.T) {
conf.Load(confFile, testLogConf)

_, _, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
assert.False(t, ok)
}

func TestConfigFindDefaultVersionError(t *testing.T) {
Expand All @@ -139,65 +141,70 @@ func TestConfigFindDefaultVersionError(t *testing.T) {
conf.Load(confFile, testLogConf)

_, _, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
assert.False(t, ok)
}

func TestConfigFindDefaultVersion(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0"}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
err := conf.Load(confFile, testLogConf)
assert.NoError(t, err)

_, v, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{false})
AssertThat(t, v, EqualTo{"49.0"})
assert.False(t, ok)
assert.Equal(t, v, "49.0")
}

func TestConfigFindFoundByEmptyPrefix(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
err := conf.Load(confFile, testLogConf)
assert.NoError(t, err)

_, v, ok := conf.Find("firefox", "")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
assert.True(t, ok)
assert.Equal(t, v, "49.0")
}

func TestConfigFindFoundByPrefix(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
err := conf.Load(confFile, testLogConf)
assert.NoError(t, err)

_, v, ok := conf.Find("firefox", "49")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
assert.True(t, ok)
assert.Equal(t, v, "49.0")
}

func TestConfigFindFoundByMatch(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
err := conf.Load(confFile, testLogConf)
assert.NoError(t, err)

_, v, ok := conf.Find("firefox", "49.0")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
assert.True(t, ok)
assert.Equal(t, v, "49.0")
}

func TestConfigFindImage(t *testing.T) {
confFile := configfile(`{"firefox":{"default":"49.0","versions":{"49.0":{"image":"image","port":"5555", "path":"/"}}}}`)
defer os.Remove(confFile)
conf := config.NewConfig()
conf.Load(confFile, testLogConf)
err := conf.Load(confFile, testLogConf)
assert.NoError(t, err)

b, v, ok := conf.Find("firefox", "49.0")
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"49.0"})
AssertThat(t, b.Image, EqualTo{"image"})
AssertThat(t, b.Port, EqualTo{"5555"})
AssertThat(t, b.Path, EqualTo{"/"})
assert.True(t, ok)
assert.Equal(t, v, "49.0")
assert.Equal(t, b.Image, "image")
assert.Equal(t, b.Port, "5555")
assert.Equal(t, b.Path, "/")
}

func TestConfigConcurrentLoad(t *testing.T) {
Expand All @@ -207,10 +214,10 @@ func TestConfigConcurrentLoad(t *testing.T) {

done := make(chan struct{})
go func() {
conf.Load(confFile, testLogConf)
_ = conf.Load(confFile, testLogConf)
done <- struct{}{}
}()
conf.Load(confFile, testLogConf)
_ = conf.Load(confFile, testLogConf)
<-done
}

Expand All @@ -219,15 +226,14 @@ func TestConfigConcurrentLoadAndRead(t *testing.T) {
defer os.Remove(confFile)
conf := config.NewConfig()
err := conf.Load(confFile, testLogConf)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
done := make(chan string)
go func() {
browser, _, _ := conf.Find("firefox", "")
done <- browser.Tmpfs["/tmp"]
}()
conf.Load(confFile, testLogConf)
err = conf.Load(confFile, testLogConf)
assert.NoError(t, err)
<-done
}

Expand All @@ -236,9 +242,7 @@ func TestConfigConcurrentRead(t *testing.T) {
defer os.Remove(confFile)
var conf config.Config
err := conf.Load(confFile, testLogConf)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
done := make(chan string)
go func() {
browser, _, _ := conf.Find("firefox", "")
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/aerokube/selenoid
go 1.21

require (
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e
github.com/aerokube/ggr v0.0.0-20221124163939-c041f40a7e45
github.com/aerokube/util v1.0.1
github.com/aws/aws-sdk-go v1.44.197
Expand All @@ -15,18 +14,22 @@ require (
github.com/mafredri/cdp v0.33.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.17.0
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -151,12 +151,14 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
Expand Down
Loading

0 comments on commit 4015eda

Please sign in to comment.