Skip to content

Commit

Permalink
make testpackage linter pass (#252)
Browse files Browse the repository at this point in the history
- enable linter in configuration
- move tests to their own package
- change tests to only inspect external behavior (e.g., no labels returned for deleted Pod, not that arrays are empty)

Signed-off-by: Etai Lev Ran <[email protected]>
  • Loading branch information
elevran authored Jan 7, 2024
1 parent 9942267 commit 510ca51
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ linters:
- tenv
- testableexamples
# - thelper
# - testpackage
- testpackage
- tparallel
- unconvert
- unparam
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/k8s/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (p *Platform) DeleteService(name, host string) {

// GetLabelsFromIP return all the labels for specific ip.
func (p *Platform) GetLabelsFromIP(ip string) map[string]string {
return p.podReconciler.getLabelsFromIP(ip)
return p.podReconciler.GetLabelsFromIP(ip)
}

// NewPlatform returns a new Kubernetes platform.
Expand Down
23 changes: 14 additions & 9 deletions pkg/platform/k8s/pod_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ type PodReconciler struct {
logger *logrus.Entry
}

// CreatePodReconciler returns a new PodReconciler with the given client and logger.
func CreatePodReconciler(c client.Client, l *logrus.Entry) *PodReconciler {
return &PodReconciler{
Client: c,
ipToPod: make(map[string]types.NamespacedName),
podList: make(map[types.NamespacedName]podInfo),
logger: l,
}
}

// Reconcile watches all pods events and updates the PodReconciler.
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var pod corev1.Pod
Expand Down Expand Up @@ -85,8 +95,8 @@ func (r *PodReconciler) updatePod(pod corev1.Pod) {
}
}

// getLabelsFromIP return all the labels for specific ip.
func (r *PodReconciler) getLabelsFromIP(ip string) map[string]string {
// GetLabelsFromIP returns the labels associated with Pod with the specified IP address.
func (r *PodReconciler) GetLabelsFromIP(ip string) map[string]string {
r.lock.RLock()
defer r.lock.RUnlock()

Expand All @@ -108,16 +118,11 @@ func (r *PodReconciler) setupWithManager(mgr *ctrl.Manager) error {
// NewPodReconciler creates pod reconciler for monitoring pods in the cluster.
func NewPodReconciler(mgr *ctrl.Manager) (*PodReconciler, error) {
logger := logrus.WithField("component", "platform.k8s.podReconciler")
r := PodReconciler{
Client: (*mgr).GetClient(),
ipToPod: make(map[string]types.NamespacedName),
podList: make(map[types.NamespacedName]podInfo),
logger: logger,
}
r := CreatePodReconciler((*mgr).GetClient(), logger)

if err := r.setupWithManager(mgr); err != nil {
return nil, err
}
r.logger.Info("start podReconciler")
return &r, nil
return r, nil
}
20 changes: 9 additions & 11 deletions pkg/platform/k8s/pod_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package k8s
package k8s_test

import (
"context"
Expand All @@ -26,6 +26,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/clusterlink-net/clusterlink/pkg/platform/k8s"
)

const (
Expand All @@ -41,12 +43,8 @@ func TestPodReconciler(t *testing.T) {
client, err := getFakeClient()
require.NoError(t, err)
ctx := context.Background()
podReconciler := &PodReconciler{
Client: client,
ipToPod: make(map[string]types.NamespacedName),
podList: make(map[types.NamespacedName]podInfo),
logger: logger,
}
podReconciler := k8s.CreatePodReconciler(client, logger)

req := ctrl.Request{NamespacedName: types.NamespacedName{
Name: TestPodName,
Namespace: TestPodNameSpace,
Expand All @@ -59,7 +57,7 @@ func TestPodReconciler(t *testing.T) {
require.NoError(t, err)
_, err = podReconciler.Reconcile(ctx, req)
require.NoError(t, err)
actualLabels := podReconciler.getLabelsFromIP(TestPodIP)[TestPodKeyLabel]
actualLabels := podReconciler.GetLabelsFromIP(TestPodIP)[TestPodKeyLabel]
expectedLabels := createLabel
require.Equal(t, expectedLabels, actualLabels, "Labels should be equal")

Expand All @@ -70,7 +68,7 @@ func TestPodReconciler(t *testing.T) {
require.NoError(t, err)
_, err = podReconciler.Reconcile(ctx, req)
require.NoError(t, err)
actualLabels = podReconciler.getLabelsFromIP(TestPodIP)[TestPodKeyLabel]
actualLabels = podReconciler.GetLabelsFromIP(TestPodIP)[TestPodKeyLabel]
expectedLabels = updateLabel
require.Equal(t, expectedLabels, actualLabels, "Labels should be equal")

Expand All @@ -79,8 +77,8 @@ func TestPodReconciler(t *testing.T) {
require.NoError(t, err)
_, err = podReconciler.Reconcile(ctx, req)
require.NoError(t, err)
require.Empty(t, podReconciler.ipToPod, "ipToPod map should be empty")
require.Empty(t, podReconciler.podList, "podList map should be empty")
labels := podReconciler.GetLabelsFromIP(TestPodIP)[TestPodKeyLabel]
require.Empty(t, labels)
}

func getFakeClient(initObjs ...client.Object) (client.WithWatch, error) {
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package k8s
package k8s_test

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/clusterlink-net/clusterlink/tests/e2e/k8s"
)

// TestE2EK8S runs all k8s e2e tests.
func TestE2EK8S(t *testing.T) {
suite.Run(t, &TestSuite{})
suite.Run(t, &k8s.TestSuite{})
}

0 comments on commit 510ca51

Please sign in to comment.