Skip to content

Commit

Permalink
Merge pull request #221 from troy0820/troy0820/update-unit-tests
Browse files Browse the repository at this point in the history
Update unit tests
  • Loading branch information
schristoff authored Aug 7, 2023
2 parents 26f9e86 + 67c4d20 commit 4232163
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/v1/agentconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,8 @@ func TestAgentConfigSpecAdapter_GetRetryLimit(t *testing.T) {
})
}
}

func TestHashString(t *testing.T) {
str := hashString("fake-string")
assert.Equal(t, "ab19e45285992b247dd281213f803479", str)
}
108 changes: 108 additions & 0 deletions controllers/agentconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,114 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestShouldDelete(t *testing.T) {
now := metav1.Now()
tests := map[string]struct {
wantTrue bool
delTimeStamp *metav1.Time
}{
"true": {wantTrue: true, delTimeStamp: &now},
"false": {wantTrue: false, delTimeStamp: nil},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
inst := &porterv1.AgentConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-namespace",
Finalizers: []string{porterv1.FinalizerName},
DeletionTimestamp: test.delTimeStamp,
},
}
acAdapter := &porterv1.AgentConfigAdapter{
AgentConfig: *inst,
}
rec := setupAgentConfigController()
isTrue := rec.shouldDelete(acAdapter)
if test.wantTrue {
assert.True(t, isTrue)
}
if !test.wantTrue {
assert.False(t, isTrue)
}
})
}
}

func TestCleanup(t *testing.T) {
ctx := context.Background()
inst := &porterv1.AgentConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-namespace",
Finalizers: []string{porterv1.FinalizerName},
},
}
plugins := map[string]porterv1.Plugin{
"kubernetes": {
FeedURL: "https://not-a-feed-url.com",
URL: "https://not-a-real-website.com",
Mirror: "fake-mirror",
Version: "0.0.1",
},
}
ps := porterv1.NewPluginsList(plugins)
pv := corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-pv",
Namespace: "fake-namespace",
},
}
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "porter-f181fa67e59422c667a37d45da7481fb",
Namespace: "fake-namespace",
},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "fake-pv",
},
}
acAdap := &porterv1.AgentConfigAdapter{
AgentConfig: *inst,
Spec: porterv1.AgentConfigSpecAdapter{
Plugins: ps,
},
}
rec := setupAgentConfigController(&pvc, &pv, inst)
err := rec.cleanup(ctx, rec.Log, acAdap)
assert.NoError(t, err)
}

func TestGetPVC(t *testing.T) {
ctx := context.Background()
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "porter-f181fa67e59422c667a37d45da7481fb",
Namespace: "fake-namespace",
},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "fake-pv",
},
}
rec := setupAgentConfigController(&pvc)
_, _, err := rec.getPersistentVolumeClaim(ctx, "fake-namespace", "porter-f181fa67e59422c667a37d45da7481fb")
assert.NoError(t, err)
}

func TestGetPV(t *testing.T) {
ctx := context.Background()
pvc := corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "porter-pv",
Namespace: "fake-namespace",
},
}
rec := setupAgentConfigController(&pvc)
_, err := rec.getPersistentVolume(ctx, "fake-namespace", "porter-pv")
assert.NoError(t, err)
}

func TestAgentConfigReconciler_Reconcile(t *testing.T) {
// long test is long
// Run through a full resource lifecycle: create, update, delete
Expand Down
19 changes: 19 additions & 0 deletions controllers/credentialset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func TestRetryForCredentialSet(t *testing.T) {
ctx := context.Background()
inst := &porterv1.CredentialSet{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
action := &porterv1.AgentAction{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-action",
Namespace: "fake-ns",
},
}
rec := setupCredentialSetController(inst, action)
err := rec.retry(ctx, rec.Log, inst, action)
assert.NoError(t, err)
}

func TestCredentialSetReconiler_Reconcile(t *testing.T) {
ctx := context.Background()

Expand Down
67 changes: 67 additions & 0 deletions controllers/installation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,73 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestShouldInstall(t *testing.T) {
now := metav1.Now()
tests := map[string]struct {
wantTrue bool
delTimeStamp *metav1.Time
}{
"true": {wantTrue: true, delTimeStamp: &now},
"false": {wantTrue: false, delTimeStamp: nil},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
inst := &porterv1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-ns",
Finalizers: []string{porterv1.FinalizerName},
DeletionTimestamp: test.delTimeStamp,
},
}
rec := setupInstallationController(inst)
isTrue := rec.shouldUninstall(inst)
if test.wantTrue {
assert.True(t, isTrue)
}
if !test.wantTrue {
assert.False(t, isTrue)
}
})
}
}

func TestUninstallInstallation(t *testing.T) {
ctx := context.Background()
inst := &porterv1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
rec := setupInstallationController(inst)
err := rec.uninstallInstallation(ctx, rec.Log, inst)
assert.NoError(t, err)
gotInstall := &porterv1.Installation{}
rec.Get(ctx, types.NamespacedName{Name: "fake-install", Namespace: "fake-ns"}, gotInstall)
assert.NotEmpty(t, gotInstall.Status)
assert.Equal(t, porterv1.PhaseUnknown, gotInstall.Status.Phase)
}

func TestRetry(t *testing.T) {
ctx := context.Background()
inst := &porterv1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
action := &porterv1.AgentAction{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-action",
Namespace: "fake-ns",
},
}
rec := setupInstallationController(inst, action)
err := rec.retry(ctx, rec.Log, inst, action)
assert.NoError(t, err)
}

func TestInstallationReconciler_Reconcile(t *testing.T) {
// long test is long
// Run through a full resource lifecycle: create, update, delete
Expand Down
19 changes: 19 additions & 0 deletions controllers/parameterset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func TestRetryForParameterSet(t *testing.T) {
ctx := context.Background()
inst := &porterv1.ParameterSet{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
action := &porterv1.AgentAction{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-action",
Namespace: "fake-ns",
},
}
rec := setupParameterSetController(inst, action)
err := rec.retry(ctx, rec.Log, inst, action)
assert.NoError(t, err)
}

func TestParameterSetReconiler_Reconcile(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit 4232163

Please sign in to comment.