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

Commit

Permalink
Merge pull request #55 from rancher-sandbox/fixes-linter-test
Browse files Browse the repository at this point in the history
Fixes linter for test files
  • Loading branch information
Nino-K authored Oct 27, 2023
2 parents 130b4f5 + 7ec8a2b commit cefd79e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 72 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.10.0-rc.1 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
Expand Down Expand Up @@ -54,6 +55,7 @@ require (
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/onsi/gomega v1.27.6 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1SMSibvLzxjeJLnrYEVLULFNiHY9YfQ=
Expand Down
63 changes: 32 additions & 31 deletions pkg/tracker/apitracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/docker/go-connections/nat"
"github.com/rancher-sandbox/rancher-desktop-agent/pkg/tracker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -46,7 +47,7 @@ func TestBasicAdd(t *testing.T) {

mux.HandleFunc("/services/forwarder/expose", func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&expectedExposeReq)
assert.NoError(t, err)
require.NoError(t, err)
})

testSrv := httptest.NewServer(mux)
Expand All @@ -62,7 +63,7 @@ func TestBasicAdd(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, expectedExposeReq.Local, ipPortBuilder(hostIP, hostPort))
assert.Equal(t, expectedExposeReq.Remote, ipPortBuilder(hostSwitchIP, hostPort))
Expand All @@ -81,7 +82,7 @@ func TestAddOverride(t *testing.T) {
mux.HandleFunc("/services/forwarder/expose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.ExposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
expectedExposeReq = append(expectedExposeReq, tmpReq)
})

Expand All @@ -104,7 +105,7 @@ func TestAddOverride(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping)
assert.NoError(t, err)
require.NoError(t, err)

assert.ElementsMatch(t, expectedExposeReq,
[]*types.ExposeRequest{
Expand Down Expand Up @@ -139,7 +140,7 @@ func TestAddOverride(t *testing.T) {
},
}
err = apiTracker.Add(containerID, portMapping2)
assert.NoError(t, err)
require.NoError(t, err)

assert.ElementsMatch(t, expectedExposeReq,
[]*types.ExposeRequest{
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestAddWithError(t *testing.T) {
mux.HandleFunc("/services/forwarder/expose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.ExposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
if tmpReq.Local == ipPortBuilder(hostIP2, hostPort) {
http.Error(w, "Bad API error", http.StatusRequestTimeout)

Expand Down Expand Up @@ -197,7 +198,7 @@ func TestAddWithError(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping)
assert.Error(t, err)
require.Error(t, err)

errPortBinding := nat.PortBinding{
HostIP: hostIP2,
Expand All @@ -208,7 +209,7 @@ func TestAddWithError(t *testing.T) {
fmt.Errorf("exposing %+v failed: %w", errPortBinding, nestedErr),
}
expectedErr := fmt.Errorf("%w: %+v", tracker.ErrExposeAPI, errs)
assert.EqualError(t, err, expectedErr.Error())
require.EqualError(t, err, expectedErr.Error())

assert.Len(t, expectedExposeReq, 2)
assert.ElementsMatch(t, expectedExposeReq,
Expand Down Expand Up @@ -236,7 +237,7 @@ func TestAddWithError(t *testing.T) {
HostIP: hostIP2,
HostPort: hostPort,
})
assert.Equal(t, actualPortMapping["80/tcp"],
assert.Equal(t,
[]nat.PortBinding{
{
HostIP: hostIP,
Expand All @@ -246,7 +247,7 @@ func TestAddWithError(t *testing.T) {
HostIP: hostIP3,
HostPort: hostPort,
},
})
}, actualPortMapping["80/tcp"])
}

func TestGet(t *testing.T) {
Expand Down Expand Up @@ -276,7 +277,7 @@ func TestGet(t *testing.T) {

apiTracker := tracker.NewAPITracker(testSrv.URL, true)
err := apiTracker.Add(containerID, portMapping)
assert.NoError(t, err)
require.NoError(t, err)

actualPortMappings := apiTracker.Get(containerID)
assert.Len(t, actualPortMappings, len(portMapping))
Expand All @@ -296,7 +297,7 @@ func TestRemove(t *testing.T) {

mux.HandleFunc("/services/forwarder/unexpose", func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&expectedUnexposeReq)
assert.NoError(t, err)
require.NoError(t, err)
})

testSrv := httptest.NewServer(mux)
Expand Down Expand Up @@ -324,12 +325,12 @@ func TestRemove(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping1)
assert.NoError(t, err)
require.NoError(t, err)
err = apiTracker.Add(containerID2, portMapping2)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.Remove(containerID)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, expectedUnexposeReq.Local, ipPortBuilder(hostIP, hostPort))

Expand All @@ -354,7 +355,7 @@ func TestRemoveWithError(t *testing.T) {
mux.HandleFunc("/services/forwarder/unexpose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.UnexposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
if tmpReq.Local == ipPortBuilder(hostIP2, hostPort) {
http.Error(w, "Test API error", http.StatusRequestTimeout)

Expand Down Expand Up @@ -385,10 +386,10 @@ func TestRemoveWithError(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.Remove(containerID)
assert.Error(t, err)
require.Error(t, err)

errPortBinding := nat.PortBinding{
HostIP: hostIP2,
Expand All @@ -399,7 +400,7 @@ func TestRemoveWithError(t *testing.T) {
fmt.Errorf("unexposing %+v failed: %w", errPortBinding, nestedErr),
}
expectedErr := fmt.Errorf("%w: %+v", tracker.ErrUnexposeAPI, errs)
assert.EqualError(t, err, expectedErr.Error())
require.EqualError(t, err, expectedErr.Error())

assert.ElementsMatch(t, expectedUnexposeReq, []*types.UnexposeRequest{
{Local: ipPortBuilder(hostIP, hostPort)},
Expand Down Expand Up @@ -449,13 +450,13 @@ func TestRemoveAll(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping1)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.Add(containerID2, portMapping2)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.RemoveAll()
assert.NoError(t, err)
require.NoError(t, err)

expectedPortMapping1 := apiTracker.Get(containerID)
assert.Nil(t, expectedPortMapping1)
Expand All @@ -478,7 +479,7 @@ func TestRemoveAllWithError(t *testing.T) {
mux.HandleFunc("/services/forwarder/unexpose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.UnexposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
if tmpReq.Local == ipPortBuilder(hostIP2, hostPort2) {
http.Error(w, "RemoveAll API error", http.StatusRequestTimeout)

Expand Down Expand Up @@ -513,13 +514,13 @@ func TestRemoveAllWithError(t *testing.T) {
},
}
err := apiTracker.Add(containerID, portMapping1)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.Add(containerID2, portMapping2)
assert.NoError(t, err)
require.NoError(t, err)

err = apiTracker.RemoveAll()
assert.Error(t, err)
require.Error(t, err)

errPortBinding := nat.PortBinding{
HostIP: hostIP2,
Expand All @@ -530,7 +531,7 @@ func TestRemoveAllWithError(t *testing.T) {
fmt.Errorf("RemoveAll unexposing %+v failed: %w", errPortBinding, nestedErr),
}
expectedErr := fmt.Errorf("%w: %+v", tracker.ErrUnexposeAPI, errs)
assert.EqualError(t, err, expectedErr.Error())
require.EqualError(t, err, expectedErr.Error())

assert.ElementsMatch(t, expectedUnexposeReq, []*types.UnexposeRequest{
{Local: ipPortBuilder(hostIP, hostPort)},
Expand All @@ -554,7 +555,7 @@ func TestNonAdminInstall(t *testing.T) {
mux.HandleFunc("/services/forwarder/expose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.ExposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
expectedExposeReq = append(expectedExposeReq, tmpReq)
})

Expand All @@ -563,7 +564,7 @@ func TestNonAdminInstall(t *testing.T) {
mux.HandleFunc("/services/forwarder/unexpose", func(w http.ResponseWriter, r *http.Request) {
var tmpReq *types.UnexposeRequest
err := json.NewDecoder(r.Body).Decode(&tmpReq)
assert.NoError(t, err)
require.NoError(t, err)
expectedUnexposeReq = append(expectedUnexposeReq, tmpReq)
})

Expand All @@ -582,7 +583,7 @@ func TestNonAdminInstall(t *testing.T) {
}

err := apiTracker.Add(containerID, portMapping)
assert.NoError(t, err)
require.NoError(t, err)

assert.ElementsMatch(t, expectedExposeReq,
[]*types.ExposeRequest{
Expand All @@ -594,7 +595,7 @@ func TestNonAdminInstall(t *testing.T) {
)

err = apiTracker.Remove(containerID)
assert.NoError(t, err)
require.NoError(t, err)

assert.ElementsMatch(t, expectedUnexposeReq,
[]*types.UnexposeRequest{
Expand Down
10 changes: 5 additions & 5 deletions pkg/tracker/listenertracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"testing"

"github.com/rancher-sandbox/rancher-desktop-agent/pkg/tracker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestListenerTracker(t *testing.T) {
Expand All @@ -46,16 +46,16 @@ func TestListenerTracker(t *testing.T) {
t.Run(fmt.Sprintf("Should create listener with port: %d", testCase.testPort), func(t *testing.T) {
t.Parallel()
err := listenerTracker.AddListener(ctx, testIPAddr, testCase.testPort)
assert.Nil(t, err)
require.NoError(t, err)

_, err = net.Dial("tcp", ipPortToAddr(testIPAddr, testCase.testPort))
assert.Nil(t, err)
require.NoError(t, err)

err = listenerTracker.RemoveListener(ctx, testIPAddr, testCase.testPort)
assert.Nil(t, err)
require.NoError(t, err)

_, err = net.Dial("tcp", ipPortToAddr(testIPAddr, testCase.testPort))
assert.ErrorIs(t, err, syscall.ECONNREFUSED)
require.ErrorIs(t, err, syscall.ECONNREFUSED)
})
}
}
Expand Down
Loading

0 comments on commit cefd79e

Please sign in to comment.