Skip to content

Commit

Permalink
ci: small new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 26, 2020
1 parent 888747e commit f248ce1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 44 deletions.
66 changes: 48 additions & 18 deletions pkg/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,47 @@ var (
# 127.0.0.1 second.loc
# end
`
onlyEnabled = defaultProfile + Banner + "\n" + testEnabledProfile
fullHostfile = onlyEnabled + testDisabledProfile
)

func TestNewWithFs(t *testing.T) {
t.Run("With file", func(t *testing.T) {
src := makeTempHostsFile(t, "etc_hosts")
fs := afero.NewOsFs()

m, err := NewWithFs(src.Name(), fs)
assert.NoError(t, err)

err = m.Flush()
assert.NoError(t, err)

assert.Equal(t, src.Name(), m.src.Name())
})
t.Run("With invalid file", func(t *testing.T) {
fs := afero.NewOsFs()

m, err := NewWithFs("/tmp/invalid_random_name", fs)
assert.Nil(t, m)
assert.True(t, os.IsNotExist(err))
})

t.Run("Without fs", func(t *testing.T) {
src := makeTempHostsFile(t, "etc_hosts")

m, err := NewWithFs(src.Name(), nil)
assert.NoError(t, err)

err = m.Flush()
assert.NoError(t, err)

assert.Equal(t, src.Name(), m.src.Name())
})
}

func TestNewWithFsError(t *testing.T) {
}

func TestManagerStatus(t *testing.T) {
t.Run("Get Status", func(t *testing.T) {
mem := createBasicFS(t)
Expand Down Expand Up @@ -90,9 +129,7 @@ func TestManagerRoutes(t *testing.T) {
c, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)

assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner)
assert.Contains(t, string(c), testEnabledProfile)
assert.Contains(t, string(c), onlyEnabled)
var added = `
# profile.off profile2
# 127.0.0.1 first.loc
Expand Down Expand Up @@ -121,9 +158,7 @@ func TestManagerRoutes(t *testing.T) {
c, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)

assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner)
assert.Contains(t, string(c), testEnabledProfile)
assert.Contains(t, string(c), onlyEnabled)
var added = `
# profile.on awesome
3.3.3.4 host1.loc
Expand All @@ -133,7 +168,7 @@ func TestManagerRoutes(t *testing.T) {
assert.Contains(t, string(c), added)
})

t.Run("RemoveRoutes", func(t *testing.T) {
t.Run("RemoveHostnames one", func(t *testing.T) {
mem := createBasicFS(t)

f, err := NewWithFs("/tmp/etc/hosts", mem)
Expand All @@ -151,9 +186,8 @@ func TestManagerRoutes(t *testing.T) {

c, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)
assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner)
assert.Contains(t, string(c), testEnabledProfile)
assert.Contains(t, string(c), onlyEnabled)

var added = `
# profile.off profile2
# 127.0.0.1 first.loc
Expand All @@ -162,7 +196,7 @@ func TestManagerRoutes(t *testing.T) {
assert.Contains(t, string(c), added)
})

t.Run("RemoveRoutes", func(t *testing.T) {
t.Run("RemoveHostnames multi", func(t *testing.T) {
mem := createBasicFS(t)

f, err := NewWithFs("/tmp/etc/hosts", mem)
Expand All @@ -180,9 +214,7 @@ func TestManagerRoutes(t *testing.T) {

c, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)
assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner)
assert.Contains(t, string(c), testEnabledProfile)
assert.Contains(t, string(c), onlyEnabled)
assert.NotContains(t, string(c), testDisabledProfile)
})
}
Expand All @@ -201,8 +233,7 @@ func TestManagerWrite(t *testing.T) {

c, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)
assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner+"\n"+testEnabledProfile+testDisabledProfile)
assert.Contains(t, string(c), fullHostfile)
})

t.Run("WriteTo", func(t *testing.T) {
Expand All @@ -218,8 +249,7 @@ func TestManagerWrite(t *testing.T) {

c, err := ioutil.ReadFile(f.Name())
assert.NoError(t, err)
assert.Contains(t, string(c), defaultProfile)
assert.Contains(t, string(c), Banner+"\n"+testEnabledProfile+testDisabledProfile)
assert.Contains(t, string(c), fullHostfile)
})

t.Run("writeBanner", func(t *testing.T) {
Expand Down
34 changes: 19 additions & 15 deletions pkg/file/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,22 @@ import (
func makeTempHostsFile(t *testing.T, pattern string) *os.File {
t.Helper()

var (
defaultProfile = "127.0.0.1 localhost\n"
file, err := ioutil.TempFile("/tmp", pattern+"_")
if err != nil {
t.Fatal(err)
}

testEnabledProfile = `
_, _ = file.WriteString(`
127.0.0.1 localhost
# profile.on profile1
127.0.0.1 first.loc
127.0.0.1 second.loc
# end
`
testDisabledProfile = `
# profile.off profile2
# 127.0.0.1 first.loc
# 127.0.0.1 second.loc
# end
`
)

file, err := ioutil.TempFile("/tmp", pattern+"_")
if err != nil {
t.Fatal(err)
}

_, _ = file.WriteString(defaultProfile + testEnabledProfile + testDisabledProfile)
`)
defer file.Close()

return file
Expand All @@ -48,7 +41,18 @@ func createBasicFS(t *testing.T) afero.Fs {
f, _ := appFS.Create("/tmp/etc/hosts")
defer f.Close()

_, _ = f.WriteString(defaultProfile + Banner + testEnabledProfile + testDisabledProfile)
_, _ = f.WriteString(`
127.0.0.1 localhost
` + Banner + `
# profile.on profile1
127.0.0.1 first.loc
127.0.0.1 second.loc
# end
# profile.off profile2
# 127.0.0.1 first.loc
# 127.0.0.1 second.loc
# end
`)

return appFS
}
31 changes: 20 additions & 11 deletions pkg/profile/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func NewProfileFromDockerCompose(opts *DockerOptions) (*types.Profile, error) {
func NewProfileFromDocker(opts *DockerOptions) (*types.Profile, error) {
p := &types.Profile{}

err := checkCli(opts)
if err != nil {
return nil, err
}

containers, err := getContainerList(opts)
if err != nil {
return nil, err
Expand All @@ -57,34 +62,38 @@ func NewProfileFromDocker(opts *DockerOptions) (*types.Profile, error) {
return p, err
}

func getContainerList(opts *DockerOptions) ([]dtypes.Container, error) {
var (
networkID string
err error
)

func checkCli(opts *DockerOptions) error {
cli := opts.Cli
if cli == nil {
cli, err = client.NewEnvClient()
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
return err
}

defer cli.Close()
opts.Cli = cli
}

return nil
}

func getContainerList(opts *DockerOptions) ([]dtypes.Container, error) {
var (
networkID string
err error
)

ctx := context.Background()

if opts.NetworkID == "" && opts.Network != "" {
networkID, err = docker.GetNetworkID(ctx, cli, opts.Network)
networkID, err = docker.GetNetworkID(ctx, opts.Cli, opts.Network)
if err != nil {
return nil, err
}

opts.NetworkID = networkID
}

return docker.GetContainerList(ctx, cli, networkID)
return docker.GetContainerList(ctx, opts.Cli, networkID)
}

func addFromContainer(profile *types.Profile, containers []dtypes.Container, opts *DockerOptions) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/profile/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
opts := &DockerOptions{
Domain: "test",
}
err := checkCli(opts)
assert.NoError(t, err)
assert.NotNil(t, opts)
assert.NotNil(t, opts.Cli)
}

func TestNewProfileFromDocker(t *testing.T) {
t.Run("All containers", func(t *testing.T) {
c := newClientWithResponse(t, map[string]string{
Expand Down

0 comments on commit f248ce1

Please sign in to comment.