Skip to content

Commit

Permalink
Merge pull request #287 from clmccart/patch/rg_controller_tests
Browse files Browse the repository at this point in the history
Add tests to ResourceGroup_Controller
  • Loading branch information
clmccart authored Oct 15, 2019
2 parents 2da53fd + 1e0a0c6 commit 897a888
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions controllers/resourcegroup_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
/*
Copyright 2019 microsoft.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
"context"

"fmt"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/helpers"
"net/http"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -45,12 +31,12 @@ var _ = Describe("ResourceGroup Controller", func() {
// test Kubernetes API server, which isn't the goal here.

Context("Create and Delete", func() {
It("should create and delete resource groups in k8s", func() {
It("should create and delete resource group instances", func() {
resourceGroupName := "t-rg-dev-" + helpers.RandomString(10)

var err error

// Create the Resourcegroup object and expect the Reconcile to be created
// Create the ResourceGroup object and expect the Reconcile to be created
resourceGroupInstance := &azurev1alpha1.ResourceGroup{
ObjectMeta: metav1.ObjectMeta{
Name: resourceGroupName,
Expand All @@ -61,26 +47,61 @@ var _ = Describe("ResourceGroup Controller", func() {
},
}

// create rg
err = tc.k8sClient.Create(context.Background(), resourceGroupInstance)
Expect(apierrors.IsInvalid(err)).To(Equal(false))
Expect(err).NotTo(HaveOccurred())

resourceGroupNamespacedName := types.NamespacedName{Name: resourceGroupName, Namespace: "default"}

// verify sure rg has a finalizer
Eventually(func() bool {
_ = tc.k8sClient.Get(context.Background(), resourceGroupNamespacedName, resourceGroupInstance)
return resourceGroupInstance.HasFinalizer(resourceGroupFinalizerName)
}, tc.timeout,
).Should(BeTrue())

// verify rg gets submitted
Eventually(func() bool {
_ = tc.k8sClient.Get(context.Background(), resourceGroupNamespacedName, resourceGroupInstance)
return resourceGroupInstance.IsSubmitted()
}, tc.timeout,
).Should(BeTrue())

// verify rg exists in azure
Eventually(func() bool {
_, err := tc.resourceGroupManager.CheckExistence(context.Background(), resourceGroupName)
return err == nil
}, tc.timeout,
).Should(BeTrue())

// delete rg
err = tc.k8sClient.Delete(context.Background(), resourceGroupInstance)
Expect(err).NotTo(HaveOccurred())

// verify rg is being deleted
Eventually(func() bool {
_ = tc.k8sClient.Get(context.Background(), resourceGroupNamespacedName, resourceGroupInstance)
return resourceGroupInstance.IsBeingDeleted()
}, tc.timeout,
).Should(BeTrue())

// verify rg is gone from kubernetes
Eventually(func() bool {
err := tc.k8sClient.Get(context.Background(), resourceGroupNamespacedName, resourceGroupInstance)
if err == nil {
err = fmt.Errorf("")
}
return strings.Contains(err.Error(), "not found")
}, tc.timeout,
).Should(BeTrue())

// verify rg is gone from Azure
Eventually(func() bool {
result, _ := tc.resourceGroupManager.CheckExistence(context.Background(), resourceGroupName)
return result.Response.StatusCode == http.StatusNotFound
}, tc.timeout,
).Should(BeTrue())
})
})
})

0 comments on commit 897a888

Please sign in to comment.