Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Unit Tests
on:
workflow_dispatch:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
merge_group:
types:
- checks_requested
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
unit-tests:
strategy:
fail-fast: false
matrix:
go-version: ['1.22.x', '1.23.x']
os: [ubuntu-latest, windows-latest]
name: Unit Tests
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run unit tests
run: make test-all
9 changes: 4 additions & 5 deletions cns/cnireconciler/statefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
)

func TestWriteObjectToFile(t *testing.T) {
name := "testdata/test"
err := os.MkdirAll(path.Dir(name), 0o666)
require.NoError(t, err)

_, err = os.Stat(name)
tmpDir := t.TempDir()
name := path.Join(tmpDir, "test")

_, err := os.Stat(name)
require.ErrorIs(t, err, os.ErrNotExist)

// create empty file
Expand Down
28 changes: 20 additions & 8 deletions cns/fsnotify/fsnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func TestAddFile(t *testing.T) {
tmpDir := t.TempDir()
validPath := tmpDir + "/we/want"
badPath := "/nonexistent/bad/path"

type args struct {
podInterfaceID string
containerID string
Expand All @@ -23,7 +27,7 @@ func TestAddFile(t *testing.T) {
args: args{
podInterfaceID: "123",
containerID: "67890",
path: "/bad/path",
path: badPath,
},
wantErr: true,
},
Expand All @@ -32,15 +36,17 @@ func TestAddFile(t *testing.T) {
args: args{
podInterfaceID: "345",
containerID: "12345",
path: "/path/we/want",
path: validPath,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.MkdirAll("/path/we/want", 0o777)
require.NoError(t, err)
if tt.args.path == validPath {
err := os.MkdirAll(validPath, 0o777)
require.NoError(t, err)
}
if err := AddFile(tt.args.podInterfaceID, tt.args.containerID, tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("WatcherAddFile() error = %v, wantErr %v", err, tt.wantErr)
}
Expand All @@ -49,6 +55,10 @@ func TestAddFile(t *testing.T) {
}

func TestWatcherRemoveFile(t *testing.T) {
tmpDir := t.TempDir()
validPath := tmpDir + "/we/want"
badPath := "/nonexistent/bad/path"

type args struct {
containerID string
path string
Expand All @@ -62,23 +72,25 @@ func TestWatcherRemoveFile(t *testing.T) {
name: "remove file fail",
args: args{
containerID: "12345",
path: "/bad/path",
path: badPath,
},
wantErr: true,
},
{
name: "no such directory, add fail",
args: args{
containerID: "67890",
path: "/path/we/want",
path: validPath,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.MkdirAll("/path/we/want/67890", 0o777)
require.NoError(t, err)
if tt.args.path == validPath {
err := os.MkdirAll(validPath+"/67890", 0o777)
require.NoError(t, err)
}
if err := removeFile(tt.args.containerID, tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("WatcherRemoveFile() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
3 changes: 2 additions & 1 deletion cns/ipampool/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ipampool
import (
"context"
"errors"
"os"
"testing"
"time"

Expand Down Expand Up @@ -53,7 +54,7 @@ type testState struct {
}

func initFakes(state testState, nnccli nodeNetworkConfigSpecUpdater) (*fakes.HTTPServiceFake, *fakes.RequestControllerFake, *Monitor) {
logger.InitLogger("testlogs", 0, 0, "./")
logger.InitLogger("testlogs", 0, 0, os.TempDir())

scalarUnits := v1alpha.Scaler{
BatchSize: state.batch,
Expand Down
7 changes: 4 additions & 3 deletions cns/restserver/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v2
import (
"net"
"net/url"
"os"
"testing"
"time"

Expand All @@ -21,7 +22,7 @@ import (
func TestStartServerWithCNSPort(t *testing.T) {
var err error

logger.InitLogger("testlogs", 0, 0, "./")
logger.InitLogger("testlogs", 0, 0, os.TempDir())
cnsPort := "8000"

// Create the service with -p 8000
Expand All @@ -33,7 +34,7 @@ func TestStartServerWithCNSPort(t *testing.T) {
func TestStartServerWithCNSURL(t *testing.T) {
var err error

logger.InitLogger("testlogs", 0, 0, "./")
logger.InitLogger("testlogs", 0, 0, os.TempDir())

// Create the service with -c "localhost:8000"
cnsURL := "tcp://localhost:8500"
Expand Down Expand Up @@ -89,7 +90,7 @@ func startService(cnsPort, cnsURL string) error {
urls = "localhost:" + port
}

_, err = net.DialTimeout("tcp", urls, 10*time.Millisecond)
_, err = net.DialTimeout("tcp", urls, 1*time.Second)
Comment on lines -92 to +93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot why did you make this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the timeout from 10ms to 1s because the original 10ms timeout was too aggressive for CI environments, causing flaky test failures. CI runners can have higher latency and slower response times, so the 1-second timeout makes the test more reliable without affecting the test's functionality.

if err != nil {
return errors.Wrapf(err, "Failed to check reachability to urls %+v", urls)
}
Expand Down
12 changes: 6 additions & 6 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestRotateFailure(t *testing.T) {

// Tests that the log file rotates when size limit is reached.
func TestLogFileRotatesWhenSizeLimitIsReached(t *testing.T) {
logDirectory := "" // This sets the current location for logs
logDirectory := t.TempDir() // Use temporary directory for tests
l := NewLogger(logName, LevelInfo, TargetLogfile, logDirectory)
if l == nil {
t.Fatalf("Failed to create logger.\n")
Expand All @@ -86,21 +86,21 @@ func TestLogFileRotatesWhenSizeLimitIsReached(t *testing.T) {

l.Close()

fn := l.GetLogDirectory() + logName + ".log"
fn := path.Join(l.GetLogDirectory(), logName + ".log")
_, err := os.Stat(fn)
if err != nil {
t.Errorf("Failed to find active log file.")
}
os.Remove(fn)

fn = l.GetLogDirectory() + logName + ".log.1"
fn = path.Join(l.GetLogDirectory(), logName + ".log.1")
_, err = os.Stat(fn)
if err != nil {
t.Errorf("Failed to find the 1st rotated log file.")
}
os.Remove(fn)

fn = l.GetLogDirectory() + logName + ".log.2"
fn = path.Join(l.GetLogDirectory(), logName + ".log.2")
_, err = os.Stat(fn)
if err == nil {
t.Errorf("Found the 2nd rotated log file which should have been deleted.")
Expand All @@ -109,15 +109,15 @@ func TestLogFileRotatesWhenSizeLimitIsReached(t *testing.T) {
}

func TestPid(t *testing.T) {
logDirectory := "" // This sets the current location for logs
logDirectory := t.TempDir() // Use temporary directory for tests
l := NewLogger(logName, LevelInfo, TargetLogfile, logDirectory)
if l == nil {
t.Fatalf("Failed to create logger.")
}

l.Printf("LogText %v", 1)
l.Close()
fn := l.GetLogDirectory() + logName + ".log"
fn := path.Join(l.GetLogDirectory(), logName + ".log")
defer os.Remove(fn)

logBytes, err := os.ReadFile(fn)
Expand Down
4 changes: 2 additions & 2 deletions netlink/netlink_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

//go:build linux
// +build linux
//go:build linux && integration
// +build linux,integration

package netlink

Expand Down
2 changes: 1 addition & 1 deletion npm/cmd/parseiptable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestParseIPTableCmd(t *testing.T) {
{
name: "Iptables information from Kernel",
args: baseArgs,
wantErr: false,
wantErr: true, // This test requires root permissions and should fail in unit test environment
},
}

Expand Down
3 changes: 3 additions & 0 deletions npm/pkg/controlplane/controllers/v1/controllers_v1_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package controllers

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2018 Microsoft. All rights reserved.
// MIT License

//go:build integration
// +build integration

package controllers

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2018 Microsoft. All rights reserved.
// MIT License

//go:build integration
// +build integration

package controllers

import (
Expand Down
3 changes: 3 additions & 0 deletions npm/pkg/controlplane/controllers/v1/parsePolicy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package controllers

import (
Expand Down
3 changes: 3 additions & 0 deletions npm/pkg/controlplane/controllers/v1/parseSelector_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package controllers

import (
Expand Down
3 changes: 3 additions & 0 deletions npm/pkg/controlplane/controllers/v1/podController_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

// Copyright 2018 Microsoft. All rights reserved.
// MIT License
package controllers
Expand Down
3 changes: 3 additions & 0 deletions npm/pkg/controlplane/controllers/v1/translatePolicy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package controllers

import (
Expand Down
16 changes: 8 additions & 8 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ func TestReportToBytes(t *testing.T) {
}

func TestSendReport(t *testing.T) {
tb, closeTBServer := createTBServer(t)
_, closeTBServer, socketPath := createTBServer(t)
defer closeTBServer()

err := tb.Connect()
require.NoError(t, err)
client := connectToTestSocket(t, socketPath)
defer client.Close()

reportManager := &ReportManager{}
for _, tt := range telemetryTests {
tt := tt
reportManager.Report = tt.Data
t.Run(tt.name, func(t *testing.T) {
err := reportManager.SendReport(tb)
err := reportManager.SendReport(client)
if tt.wantErr {
require.Error(t, err)
return
Expand All @@ -99,11 +99,11 @@ func TestSendReport(t *testing.T) {
}

func TestSendCNIMetric(t *testing.T) {
tb, closeTBServer := createTBServer(t)
_, closeTBServer, socketPath := createTBServer(t)
defer closeTBServer()

err := tb.Connect()
require.NoError(t, err)
client := connectToTestSocket(t, socketPath)
defer client.Close()

tests := []struct {
name string
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestSendCNIMetric(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err := SendCNIMetric(tt.metric, tb)
err := SendCNIMetric(tt.metric, client)
if tt.wantErr {
require.Error(t, err)
return
Expand Down
Loading
Loading