-
Notifications
You must be signed in to change notification settings - Fork 2
Implement unit-tests for the codebase #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrajagopal
wants to merge
27
commits into
main
Choose a base branch
from
mrajagopal-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b28f5db
Issue 40: Unit-test for CRD
mrajagopal b1f649c
Issue 40: Unit-test for CRD
mrajagopal ee263e6
Merge branch 'main' into mrajagopal-unit-tests
mrajagopal 7bb7833
feat: Create unit tests for NIC, NIM, NGF, and NGX
mrajagopal 29de4ac
Merge branch 'main' into mrajagopal-unit-tests
mrajagopal 33dabbd
Chore: Update to reflect changes in return parameters
mrajagopal d53e7ff
chore: replace deprecated routine filepath.HasPrefix with strings.Has…
mrajagopal 2d9225d
Update pkg/jobs/ngf_job_list_test.go
mrajagopal ecb3c62
Update pkg/jobs/ngf_job_list_test.go
mrajagopal 6d7b09d
Update pkg/data_collector/data_collector.go
mrajagopal 6ef2f3e
Update pkg/jobs/nic_job_list_test.go
mrajagopal e124a26
Update pkg/jobs/job_test.go
mrajagopal 1922e67
Update pkg/jobs/nic_job_list_test.go
mrajagopal 2c2bd5c
Update pkg/jobs/common_job_list_test.go
mrajagopal 3170f31
Fix: Address issues from common_job_list tests
mrajagopal d954b2e
Fix: Implement a mock data_collector
mrajagopal 5e2bd87
Fix: Use the mock data_collector in various tests
mrajagopal 6eef435
Fix merge-conflicts from main onto branch
mrajagopal 625c0be
Fix: merge conflict from main
mrajagopal 8e5c792
Fix: incorporate test coverage report
mrajagopal ba84c4e
Fix: Use library function for a pointer
mrajagopal 0f1c9e6
Review fixes
mrajagopal 7beddf9
Merge branch 'main' into mrajagopal-unit-tests
mrajagopal 043d54d
Update pkg/mock/mock_data_collector.go
mrajagopal 9e5a541
Update pkg/mock/mock_data_collector.go
mrajagopal eda9364
Review: Read mock CRD objects via a YAML declaration
mrajagopal 7757474
Review: Read mock objects via a YAML declaration
mrajagopal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package crds | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetNICCRDList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want []Crd | ||
}{ | ||
{ | ||
name: "Correct CRD list", | ||
want: []Crd{ | ||
{ | ||
Resource: "apdoslogconfs", | ||
Group: "appprotectdos.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "apdospolicies", | ||
Group: "appprotectdos.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "dosprotectedresources", | ||
Group: "appprotectdos.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "aplogconfs", | ||
Group: "appprotect.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "appolicies", | ||
Group: "appprotect.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "apusersigs", | ||
Group: "appprotect.f5.com", | ||
Version: "v1beta1", | ||
}, | ||
{ | ||
Resource: "globalconfigurations", | ||
Group: "k8s.nginx.org", | ||
Version: "v1", | ||
}, | ||
{ | ||
Resource: "policies", | ||
Group: "k8s.nginx.org", | ||
Version: "v1", | ||
}, | ||
{ | ||
Resource: "transportservers", | ||
Group: "k8s.nginx.org", | ||
Version: "v1", | ||
}, | ||
{ | ||
Resource: "virtualserverroutes", | ||
Group: "k8s.nginx.org", | ||
Version: "v1", | ||
}, | ||
{ | ||
Resource: "virtualservers", | ||
Group: "k8s.nginx.org", | ||
Version: "v1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetNICCRDList(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetNICCRDList() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package data_collector | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func TestNewDataCollector_Success(t *testing.T) { | ||
dc := &DataCollector{Namespaces: []string{"default"}} | ||
err := NewDataCollector(dc) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if dc.BaseDir == "" { | ||
t.Error("BaseDir should be set") | ||
} | ||
if dc.Logger == nil { | ||
t.Error("Logger should be set") | ||
} | ||
if dc.LogFile == nil { | ||
t.Error("LogFile should be set") | ||
} | ||
if dc.K8sCoreClientSet == nil { | ||
t.Error("K8sCoreClientSet should be set") | ||
} | ||
if dc.K8sCrdClientSet == nil { | ||
t.Error("K8sCrdClientSet should be set") | ||
} | ||
if dc.K8sMetricsClientSet == nil { | ||
t.Error("K8sMetricsClientSet should be set") | ||
} | ||
if dc.K8sHelmClientSet == nil { | ||
t.Error("K8sHelmClientSet should be set") | ||
} | ||
} | ||
|
||
func TestWrapUp_CreatesTarball(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
logFile, _ := os.Create(filepath.Join(tmpDir, "supportpkg.log")) | ||
dc := &DataCollector{ | ||
BaseDir: tmpDir, | ||
LogFile: logFile, | ||
Logger: log.New(io.Discard, "", 0), | ||
} | ||
product := "nginx" | ||
tarball, err := dc.WrapUp(product) | ||
if err != nil { | ||
t.Fatalf("WrapUp failed: %v", err) | ||
} | ||
if _, err := os.Stat(tarball); err != nil { | ||
t.Errorf("tarball not created: %v", err) | ||
} | ||
_ = os.Remove(tarball) | ||
} | ||
|
||
func TestRealPodExecutor_ReturnsOutput(t *testing.T) { | ||
dc := &DataCollector{ | ||
K8sCoreClientSet: fake.NewSimpleClientset(), | ||
K8sRestConfig: &rest.Config{}, | ||
} | ||
// Replace RealPodExecutor with a mock for testing | ||
dc.PodExecutor = func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) { | ||
return []byte("output"), nil | ||
} | ||
out, err := dc.PodExecutor("default", "pod", "container", []string{"echo", "hello"}, context.TODO()) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if !bytes.Contains(out, []byte("output")) { | ||
t.Errorf("expected output, got %s", string(out)) | ||
} | ||
} | ||
|
||
func TestRealQueryCRD_ReturnsErrorOnInvalidConfig(t *testing.T) { | ||
dc := &DataCollector{ | ||
K8sRestConfig: &rest.Config{}, | ||
} | ||
crd := crds.Crd{Group: "test", Version: "v1", Resource: "foos"} | ||
_, err := dc.RealQueryCRD(crd, "default", context.TODO()) | ||
if err == nil { | ||
t.Error("expected error for invalid config") | ||
} | ||
} | ||
|
||
func TestAllNamespacesExist_AllExist(t *testing.T) { | ||
client := fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "default"}}) | ||
dc := &DataCollector{ | ||
Namespaces: []string{"default"}, | ||
K8sCoreClientSet: client, | ||
Logger: log.New(io.Discard, "", 0), | ||
} | ||
if !dc.AllNamespacesExist() { | ||
t.Error("expected all namespaces to exist") | ||
} | ||
} | ||
|
||
func TestAllNamespacesExist_NotExist(t *testing.T) { | ||
client := fake.NewSimpleClientset() | ||
dc := &DataCollector{ | ||
Namespaces: []string{"missing"}, | ||
K8sCoreClientSet: client, | ||
Logger: log.New(io.Discard, "", 0), | ||
} | ||
if dc.AllNamespacesExist() { | ||
t.Error("expected namespaces to not exist") | ||
} | ||
} | ||
|
||
func TestWrapUp_ErrorOnLogFileClose(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
logFile, _ := os.Create(filepath.Join(tmpDir, "supportpkg.log")) | ||
logFile.Close() // Already closed | ||
dc := &DataCollector{ | ||
BaseDir: tmpDir, | ||
LogFile: logFile, | ||
Logger: log.New(io.Discard, "", 0), | ||
} | ||
_, err := dc.WrapUp("nginx") | ||
if err == nil { | ||
t.Error("expected error on closing already closed log file") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.