Skip to content

Commit

Permalink
Add integration test for AgentConfig deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Yingrong Zhao <[email protected]>
  • Loading branch information
VinozzZ committed Dec 5, 2022
1 parent 858c072 commit e9bb870
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/v1/agentconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestAgentConfigSpec_MergeConfig(t *testing.T) {
VolumeSize: "2Mi",
PullPolicy: v1.PullAlways,
InstallationServiceAccount: "override",
Plugin: Plugin{Name: "test-plugin", FeedURL: "localhost:5000"},
}

config, err := systemConfig.MergeConfig(nsConfig, instConfig)
Expand All @@ -112,5 +113,6 @@ func TestAgentConfigSpec_MergeConfig(t *testing.T) {
assert.Equal(t, "2Mi", config.VolumeSize)
assert.Equal(t, v1.PullAlways, config.PullPolicy)
assert.Equal(t, "override", config.InstallationServiceAccount)
assert.Equal(t, Plugin{Name: "test-plugin", FeedURL: "localhost:5000"}, config.Plugin)
})
}
85 changes: 85 additions & 0 deletions tests/integration/agentconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//go:build integration

package integration_test

import (
"context"

. "github.com/onsi/ginkgo"
"github.com/tidwall/gjson"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

porterv1 "get.porter.sh/operator/api/v1"
. "github.com/onsi/gomega"
)

var _ = Describe("AgentConfig delete", func() {
Context("when an existing AgentConfig is delete", func() {
It("should delete AgentConfig and remove owner reference from all volumes it's associated with", func() {
By("creating an agent action", func() {
ctx := context.Background()
ns := createTestNamespace(ctx)

agentCfg := NewTestAgentCfg()
agentCfg.Namespace = ns

Expect(k8sClient.Create(ctx, agentCfg)).Should(Succeed())
Expect(waitForPorter(ctx, agentCfg, 1, "waiting for plugins to be installed")).Should(Succeed())
validateResourceConditions(agentCfg)

Log("verify it's created")
jsonOut := runAgentAction(ctx, "create-check-plugins-list", ns, []string{"plugins", "list", "-o", "json"})
firstName := gjson.Get(jsonOut, "0.name").String()
numPluginsInstalled := gjson.Get(jsonOut, "#").Int()
Expect(int64(1)).To(Equal(numPluginsInstalled))
Expect(agentCfg.Spec.Plugin.Name).To(Equal(firstName))

Log("delete a agent config")
Expect(k8sClient.Delete(ctx, agentCfg)).Should(Succeed())
Expect(waitForResourceDeleted(ctx, agentCfg)).Should(Succeed())

Log("verify persistent volums and claims no longer has the agent config in their owner reference")
results := &corev1.PersistentVolumeClaimList{}
Expect(k8sClient.List(ctx, results, client.InNamespace(agentCfg.Namespace), client.MatchingLabels(agentCfg.Spec.GetPluginsLabels()))).Should(Succeed())
for _, pvc := range results.Items {
for _, ow := range pvc.OwnerReferences {
if ow.Kind == "AgentConfig" {

Expect(ow.Name).ShouldNot(Equal(agentCfg.Name))
}
}
key := client.ObjectKey{Namespace: agentCfg.Namespace, Name: pvc.Spec.VolumeName}
pv := &corev1.PersistentVolume{}
Expect(k8sClient.Get(ctx, key, pv)).Should(Succeed())
for _, ow := range pv.OwnerReferences {
if ow.Kind == "AgentConfig" {

Expect(ow.Name).ShouldNot(Equal(agentCfg.Name))
}
}
}
})
})
})
})

// NewTestCredSet minimal CredentialSet CRD for tests
func NewTestAgentCfg() *porterv1.AgentConfig {
cs := &porterv1.AgentConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "porter.sh/v1",
Kind: "AgentConfig",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "porter-test-me-",
},
Spec: porterv1.AgentConfigSpec{
Plugin: porterv1.Plugin{
Name: "kubernetes",
},
},
}
return cs
}
6 changes: 6 additions & 0 deletions tests/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ var _ = BeforeSuite(func(done Done) {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&controllers.AgentConfigReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AgentConfig"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

go func() {
err = k8sManager.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred())
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var resourceTypeMap = map[string]string{
"CredentialSet": "credentialsets",
"Installation": "installations",
"AgentAction": "agentactions",
"AgentConfig": "agentconfigs",
}

var gvrVersion = "v1"
Expand Down Expand Up @@ -282,7 +283,9 @@ func getAgentActionCmdOut(action *porterv1.AgentAction, aaOut string) string {
return strings.SplitAfterN(strings.Replace(aaOut, "\n", "", -1), strings.Join(action.Spec.Args, " "), 2)[1]
}

/* Fully execute an agent action and return the associated result of the command executed. For example an agent action
/*
Fully execute an agent action and return the associated result of the command executed. For example an agent action
that does "porter credentials list" will return just the result of the porter command from the job logs. This can be
used to run porter commands inside the cluster to validate porter state
*/
Expand Down

0 comments on commit e9bb870

Please sign in to comment.