Skip to content

Commit

Permalink
inject rollout crd into test cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
dnguy078 committed Aug 18, 2023
1 parent b222805 commit 7a4a1d9
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 10 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate envtest ## Run tests.
go mod download
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v $(shell go list ./... | grep -v 'e2e') -coverprofile cover.out -covermode=atomic -race

##@ Build
Expand Down
146 changes: 140 additions & 6 deletions internal/builders/podaccessbuilder/create_access_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

rolloutsv1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -22,12 +23,14 @@ import (
var _ = Describe("RequestReconciler", Ordered, func() {
Context("CreateAccessResources()", func() {
var (
ctx = context.Background()
ns *corev1.Namespace
deployment *appsv1.Deployment
request *v1alpha1.PodAccessRequest
template *v1alpha1.PodAccessTemplate
builder = PodAccessBuilder{}
ctx = context.Background()
ns *corev1.Namespace
deployment *appsv1.Deployment
request *v1alpha1.PodAccessRequest
rolloutRequest *v1alpha1.PodAccessRequest
template *v1alpha1.PodAccessTemplate
rolloutTemplate *v1alpha1.PodAccessTemplate
builder = PodAccessBuilder{}
)

BeforeAll(func() {
Expand Down Expand Up @@ -72,6 +75,39 @@ var _ = Describe("RequestReconciler", Ordered, func() {
err = k8sClient.Create(ctx, deployment)
Expect(err).To(Not(HaveOccurred()))

By("Creating a Rollout to reference for the test")
rollout := &rolloutsv1alpha1.Rollout{
ObjectMeta: metav1.ObjectMeta{
Name: "rollout-test",
Namespace: ns.Name,
},
Spec: rolloutsv1alpha1.RolloutSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"testLabel": "testValue",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"testLabel": "testValue",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
Image: "nginx:latest",
},
},
},
},
},
}

err = k8sClient.Create(ctx, rollout)
Expect(err).To(Not(HaveOccurred()))

By("Should have an PodAccessTemplate to test against")
cpuReq, _ := resource.ParseQuantity("1")
template = &v1alpha1.PodAccessTemplate{
Expand Down Expand Up @@ -108,6 +144,40 @@ var _ = Describe("RequestReconciler", Ordered, func() {
err = k8sClient.Create(ctx, template)
Expect(err).ToNot(HaveOccurred())

rolloutTemplate = &v1alpha1.PodAccessTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: utils.RandomString(8),
Namespace: ns.GetName(),
},
Spec: v1alpha1.PodAccessTemplateSpec{
AccessConfig: v1alpha1.AccessConfig{
AllowedGroups: []string{"testGroupA"},
DefaultDuration: "1h",
MaxDuration: "2h",
},
ControllerTargetRef: &v1alpha1.CrossVersionObjectReference{
APIVersion: "argoproj.io/v1alpha1",
Kind: "Rollout",
Name: "rollout-test",
},
ControllerTargetMutationConfig: &v1alpha1.PodTemplateSpecMutationConfig{
DefaultContainerName: "test",
Command: &[]string{"/bin/sleep"},
Args: &[]string{"100"},
Env: []corev1.EnvVar{
{Name: "FOO", Value: "BAR"},
},
Resources: corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
"cpu": cpuReq,
},
},
},
},
}
err = k8sClient.Create(ctx, rolloutTemplate)
Expect(err).ToNot(HaveOccurred())

By("Should have an PodAccessRequest built to test against")
request = &v1alpha1.PodAccessRequest{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -120,6 +190,19 @@ var _ = Describe("RequestReconciler", Ordered, func() {
}
err = k8sClient.Create(ctx, request)
Expect(err).ToNot(HaveOccurred())

// verify podaccess request with Rollout
rolloutRequest = &v1alpha1.PodAccessRequest{
ObjectMeta: metav1.ObjectMeta{
Name: "createaccessresource-rollout-test",
Namespace: ns.GetName(),
},
Spec: v1alpha1.PodAccessRequestSpec{
TemplateName: rolloutTemplate.GetName(),
},
}
err = k8sClient.Create(ctx, rolloutRequest)
Expect(err).ToNot(HaveOccurred())
})

AfterAll(func() {
Expand Down Expand Up @@ -178,5 +261,56 @@ var _ = Describe("RequestReconciler", Ordered, func() {
Expect(foundRoleBinding.RoleRef.Name).To(Equal(foundRole.GetName()))
Expect(foundRoleBinding.Subjects[0].Name).To(Equal("testGroupA"))
})

It("CreateAccessResources() should succeed with Rollout", func() {
rolloutRequest.Status.PodName = ""

// Execute
ret, err := builder.CreateAccessResources(ctx, k8sClient, rolloutRequest, rolloutTemplate)

// VERIFY: No error returned
Expect(err).ToNot(HaveOccurred())

// VERIFY: Proper status string returned
Expect(ret).To(MatchRegexp(fmt.Sprintf(
"Success. Pod %s-.*, Role %s-.*, RoleBinding %s.* created",
rolloutRequest.GetName(),
rolloutRequest.GetName(),
rolloutRequest.GetName(),
)))

// VERIFY: Pod Created as expected
foundPod := &corev1.Pod{}
err = k8sClient.Get(ctx, types.NamespacedName{
Name: bldutil.GenerateResourceName(rolloutRequest),
Namespace: ns.GetName(),
}, foundPod)
Expect(err).ToNot(HaveOccurred())
Expect(foundPod.GetOwnerReferences()).ToNot(BeNil())
Expect(foundPod.Spec.Containers[0].Command[0]).To(Equal("/bin/sleep"))
Expect(foundPod.Spec.Containers[0].Args[0]).To(Equal("100"))

// VERIFY: Role Created as expected
foundRole := &rbacv1.Role{}
err = k8sClient.Get(ctx, types.NamespacedName{
Name: bldutil.GenerateResourceName(rolloutRequest),
Namespace: ns.GetName(),
}, foundRole)
Expect(err).ToNot(HaveOccurred())
Expect(foundRole.GetOwnerReferences()).ToNot(BeNil())
Expect(foundRole.Rules[0].ResourceNames[0]).To(Equal(foundPod.GetName()))
Expect(foundRole.Rules[1].ResourceNames[0]).To(Equal(foundPod.GetName()))

// VERIFY: RoleBinding Created as expected
foundRoleBinding := &rbacv1.RoleBinding{}
err = k8sClient.Get(ctx, types.NamespacedName{
Name: bldutil.GenerateResourceName(rolloutRequest),
Namespace: ns.GetName(),
}, foundRoleBinding)
Expect(err).ToNot(HaveOccurred())
Expect(foundRoleBinding.GetOwnerReferences()).ToNot(BeNil())
Expect(foundRoleBinding.RoleRef.Name).To(Equal(foundRole.GetName()))
Expect(foundRoleBinding.Subjects[0].Name).To(Equal("testGroupA"))
})
})
})
20 changes: 16 additions & 4 deletions internal/builders/podaccessbuilder/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ limitations under the License.
package podaccessbuilder

import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"testing"

rolloutsv1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/zap/zapcore"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -60,12 +62,23 @@ var _ = BeforeSuite(func() {
logf.SetLogger(logger)

By("bootstrapping test environment")

var err error

// grab go mod directory with Argo rollout CRD to be installed into test environment cluster
argoRolloutPath, err := exec.Command("go", "list", "-m", "-f", "{{.Dir}}", "github.com/argoproj/argo-rollouts").Output()
Expect(err).NotTo(HaveOccurred())
argoCRDPath := fmt.Sprintf("%s/manifests/crds", string(argoRolloutPath))
argoCRDPath = strings.ReplaceAll(argoCRDPath, "\n", "")

testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "config", "crd", "bases"),
argoCRDPath,
},
ErrorIfCRDPathMissing: true,
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -78,7 +91,6 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down

0 comments on commit 7a4a1d9

Please sign in to comment.