github.com/kyma-project/kyma-environment-broker@v0.0.1/cmd/subscriptioncleanup/job/cleaner_test.go (about)

     1  package job
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/cmd/subscriptioncleanup/cloudprovider/mocks"
     8  	"github.com/kyma-project/kyma-environment-broker/cmd/subscriptioncleanup/model"
     9  	"github.com/kyma-project/kyma-environment-broker/common/gardener"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/mock"
    12  	"github.com/stretchr/testify/require"
    13  	v1 "k8s.io/api/core/v1"
    14  	machineryv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    16  	"k8s.io/apimachinery/pkg/runtime/schema"
    17  	"k8s.io/client-go/kubernetes/fake"
    18  )
    19  
    20  var (
    21  	namespace        = "test_gardener"
    22  	shootGVK         = schema.GroupVersionKind{Group: "core.gardener.cloud", Version: "v1beta1", Kind: "Shoot"}
    23  	secretBindingGVK = schema.GroupVersionKind{Group: "core.gardener.cloud", Version: "v1beta1", Kind: "SecretBinding"}
    24  )
    25  
    26  func TestCleanerJob(t *testing.T) {
    27  	t.Run("should return secret binding to the secrets pool", func(t *testing.T) {
    28  		//given
    29  		secret := &v1.Secret{
    30  			ObjectMeta: machineryv1.ObjectMeta{
    31  				Name: "secret1", Namespace: namespace,
    32  			},
    33  			Data: map[string][]byte{
    34  				"credentials":    []byte("secret1"),
    35  				"clientID":       []byte("tenant1"),
    36  				"clientSecret":   []byte("secret"),
    37  				"subscriptionID": []byte("12344"),
    38  				"tenantID":       []byte("tenant1"),
    39  			},
    40  		}
    41  		secretBinding := &unstructured.Unstructured{
    42  			Object: map[string]interface{}{
    43  				"metadata": map[string]interface{}{
    44  					"name":      "secretBinding1",
    45  					"namespace": namespace,
    46  					"labels": map[string]interface{}{
    47  						"tenantName":      "tenant1",
    48  						"hyperscalerType": "azure",
    49  						"dirty":           "true",
    50  					},
    51  				},
    52  				"secretRef": map[string]interface{}{
    53  					"name":      "secret1",
    54  					"namespace": namespace,
    55  				},
    56  			},
    57  		}
    58  		secretBinding.SetGroupVersionKind(secretBindingGVK)
    59  
    60  		mockClient := fake.NewSimpleClientset(secret)
    61  
    62  		gardenerFake := gardener.NewDynamicFakeClient(secretBinding)
    63  		mockSecretBindings := gardenerFake.Resource(gardener.SecretBindingResource).Namespace(namespace)
    64  		mockShoots := gardenerFake.Resource(gardener.ShootResource).Namespace(namespace)
    65  
    66  		resCleaner := &azureMockResourceCleaner{}
    67  		providerFactory := &mocks.ProviderFactory{}
    68  		providerFactory.On("New", model.Azure, mock.Anything).Return(resCleaner, nil)
    69  
    70  		cleaner := NewCleaner(context.Background(), mockClient, mockSecretBindings, mockShoots, providerFactory)
    71  
    72  		//when
    73  		err := cleaner.Do()
    74  
    75  		//then
    76  		require.NoError(t, err)
    77  		cleanedSecretBinding, err := mockSecretBindings.Get(context.Background(), secretBinding.GetName(), machineryv1.GetOptions{})
    78  		require.NoError(t, err)
    79  
    80  		assert.Equal(t, "", cleanedSecretBinding.GetLabels()["dirty"])
    81  		assert.Equal(t, "", cleanedSecretBinding.GetLabels()["tenantName"])
    82  	})
    83  
    84  	t.Run("should not return secret binding to the secrets pool when secret is still in use", func(t *testing.T) {
    85  		//given
    86  		secret := &v1.Secret{
    87  			ObjectMeta: machineryv1.ObjectMeta{
    88  				Name: "secret1", Namespace: namespace,
    89  			},
    90  			Data: map[string][]byte{
    91  				"credentials":    []byte("secret1"),
    92  				"clientID":       []byte("tenant1"),
    93  				"clientSecret":   []byte("secret"),
    94  				"subscriptionID": []byte("12344"),
    95  				"tenantID":       []byte("tenant1"),
    96  			},
    97  		}
    98  		secretBinding := &unstructured.Unstructured{
    99  			Object: map[string]interface{}{
   100  				"metadata": map[string]interface{}{
   101  					"name":      "secretBinding1",
   102  					"namespace": namespace,
   103  					"labels": map[string]interface{}{
   104  						"tenantName":      "tenant1",
   105  						"hyperscalerType": "azure",
   106  						"dirty":           "true",
   107  					},
   108  				},
   109  				"secretRef": map[string]interface{}{
   110  					"name":      "secret1",
   111  					"namespace": namespace,
   112  				},
   113  			},
   114  		}
   115  		secretBinding.SetGroupVersionKind(secretBindingGVK)
   116  
   117  		shoot := &unstructured.Unstructured{
   118  			Object: map[string]interface{}{
   119  				"metadata": map[string]interface{}{
   120  					"name":      "some-name",
   121  					"namespace": namespace,
   122  				},
   123  				"spec": map[string]interface{}{
   124  					"secretBindingName": secretBinding.GetName(),
   125  				},
   126  				"status": map[string]interface{}{},
   127  			},
   128  		}
   129  		shoot.SetGroupVersionKind(shootGVK)
   130  
   131  		mockClient := fake.NewSimpleClientset(secret)
   132  
   133  		gardenerFake := gardener.NewDynamicFakeClient(secretBinding, shoot)
   134  		mockSecretBindings := gardenerFake.Resource(gardener.SecretBindingResource).Namespace(namespace)
   135  		mockShoots := gardenerFake.Resource(gardener.ShootResource).Namespace(namespace)
   136  
   137  		resCleaner := &azureMockResourceCleaner{}
   138  		providerFactory := &mocks.ProviderFactory{}
   139  		providerFactory.On("New", model.Azure, mock.Anything).Return(resCleaner, nil)
   140  
   141  		cleaner := NewCleaner(context.Background(), mockClient, mockSecretBindings, mockShoots, providerFactory)
   142  
   143  		//when
   144  		err := cleaner.Do()
   145  
   146  		//then
   147  		require.NoError(t, err)
   148  		cleanedSecretBinding, err := mockSecretBindings.Get(context.Background(), secretBinding.GetName(), machineryv1.GetOptions{})
   149  		require.NoError(t, err)
   150  
   151  		assert.Equal(t, "true", cleanedSecretBinding.GetLabels()["dirty"])
   152  		assert.Equal(t, "tenant1", cleanedSecretBinding.GetLabels()["tenantName"])
   153  	})
   154  }
   155  
   156  type azureMockResourceCleaner struct {
   157  	error error
   158  }
   159  
   160  func (am *azureMockResourceCleaner) Do() error {
   161  	return am.error
   162  }