github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/pkg/status/configconnector_test.go (about)

     1  // Copyright 2021 The kpt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package status
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"k8s.io/apimachinery/pkg/api/errors"
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  	"sigs.k8s.io/cli-utils/pkg/kstatus/polling/testutil"
    25  	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
    26  	"sigs.k8s.io/cli-utils/pkg/object"
    27  	fakemapper "sigs.k8s.io/cli-utils/pkg/testutil"
    28  )
    29  
    30  func TestSupports(t *testing.T) {
    31  	testCases := map[string]struct {
    32  		gk       schema.GroupKind
    33  		supports bool
    34  	}{
    35  		"matches config connector group": {
    36  			gk: schema.GroupKind{
    37  				Group: "sql.cnrm.cloud.google.com",
    38  				Kind:  "SQLDatabase",
    39  			},
    40  			supports: true,
    41  		},
    42  		"doesn't match other resources": {
    43  			gk: schema.GroupKind{
    44  				Group: "apps",
    45  				Kind:  "StatefulSet",
    46  			},
    47  			supports: false,
    48  		},
    49  	}
    50  
    51  	for tn, tc := range testCases {
    52  		t.Run(tn, func(t *testing.T) {
    53  			fakeMapper := fakemapper.NewFakeRESTMapper()
    54  
    55  			statusReader := &ConfigConnectorStatusReader{
    56  				Mapper: fakeMapper,
    57  			}
    58  
    59  			supports := statusReader.Supports(tc.gk)
    60  
    61  			assert.Equal(t, tc.supports, supports)
    62  		})
    63  	}
    64  }
    65  
    66  func TestReadStatus(t *testing.T) {
    67  	testCases := map[string]struct {
    68  		resource       string
    69  		gvk            schema.GroupVersionKind
    70  		expectedStatus status.Status
    71  		deleted        bool
    72  	}{
    73  		"Resource with deletionTimestap is Terminating": {
    74  			resource: `
    75  apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
    76  kind: Service
    77  metadata:
    78    name: pubsub.googleapis.com
    79    namespace: cnrm
    80    generation: 42
    81    deletionTimestamp: "2020-01-09T20:56:25Z"
    82  `,
    83  			gvk: schema.GroupVersionKind{
    84  				Group:   "serviceusage.cnrm.cloud.google.com",
    85  				Version: "v1beta1",
    86  				Kind:    "Service",
    87  			},
    88  			expectedStatus: status.TerminatingStatus,
    89  		},
    90  		"Resource where observedGeneration doesn't match generation is InProgress": {
    91  			resource: `
    92  apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
    93  kind: Service
    94  metadata:
    95    name: pubsub.googleapis.com
    96    namespace: cnrm
    97    generation: 42
    98  status:
    99    observedGeneration: 41
   100    conditions:
   101    - type: Ready
   102      status: "False"
   103      reason: UpdateFailed
   104      message: "Resource couldn't be updated"
   105  `,
   106  			gvk: schema.GroupVersionKind{
   107  				Group:   "serviceusage.cnrm.cloud.google.com",
   108  				Version: "v1beta1",
   109  				Kind:    "Service",
   110  			},
   111  			expectedStatus: status.InProgressStatus,
   112  		},
   113  		"Resource with reason UpdateFailed is Failed": {
   114  			resource: `
   115  apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
   116  kind: Service
   117  metadata:
   118    name: pubsub.googleapis.com
   119    namespace: cnrm
   120    generation: 42
   121  status:
   122    observedGeneration: 42
   123    conditions:
   124    - type: Ready
   125      status: "False"
   126      reason: UpdateFailed
   127      message: "Resource couldn't be updated"
   128  `,
   129  			gvk: schema.GroupVersionKind{
   130  				Group:   "serviceusage.cnrm.cloud.google.com",
   131  				Version: "v1beta1",
   132  				Kind:    "Service",
   133  			},
   134  			expectedStatus: status.FailedStatus,
   135  		},
   136  
   137  		"Resource has been deleted": {
   138  			resource: `
   139  apiVersion: storage.cnrm.cloud.google.com/v1beta1
   140  kind: StorageBucket
   141  metadata:
   142    name: fake-bucket
   143  `,
   144  			gvk: schema.GroupVersionKind{
   145  				Group:   "storage.cnrm.cloud.google.com",
   146  				Version: "v1beta1",
   147  				Kind:    "StorageBucket",
   148  			},
   149  			expectedStatus: status.NotFoundStatus,
   150  			deleted:        true,
   151  		},
   152  
   153  		"ConfigConnectorContext resource with status.healthy set to true": {
   154  			resource: `
   155  apiVersion: core.cnrm.cloud.google.com/v1beta1
   156  kind: ConfigConnectorContext
   157  metadata:
   158    name: ccc
   159    namespace: cnrm
   160    generation: 42
   161  status:
   162    healthy: true
   163  `,
   164  			gvk: schema.GroupVersionKind{
   165  				Group:   "core.cnrm.cloud.google.com",
   166  				Version: "v1beta1",
   167  				Kind:    "ConfigConnectorContext",
   168  			},
   169  			expectedStatus: status.CurrentStatus,
   170  			deleted:        false,
   171  		},
   172  
   173  		"ConfigConnectorContext resource with status.healthy set to false": {
   174  			resource: `
   175  apiVersion: core.cnrm.cloud.google.com/v1beta1
   176  kind: ConfigConnectorContext
   177  metadata:
   178    name: ccc
   179    namespace: cnrm
   180    generation: 42
   181  status:
   182    healthy: false
   183  `,
   184  			gvk: schema.GroupVersionKind{
   185  				Group:   "core.cnrm.cloud.google.com",
   186  				Version: "v1beta1",
   187  				Kind:    "ConfigConnectorContext",
   188  			},
   189  			expectedStatus: status.InProgressStatus,
   190  			deleted:        false,
   191  		},
   192  	}
   193  
   194  	for tn, tc := range testCases {
   195  		t.Run(tn, func(t *testing.T) {
   196  			obj := testutil.YamlToUnstructured(t, tc.resource)
   197  
   198  			fakeClusterReader := &fakeClusterReader{
   199  				getResource: obj,
   200  			}
   201  			// Return not found error if we want the resource to be deleted.
   202  			if tc.deleted {
   203  				fakeClusterReader.getResource = nil
   204  				fakeClusterReader.getErr = errors.NewNotFound(schema.GroupResource{Group: tc.gvk.Group, Resource: tc.gvk.Kind}, "fake-name")
   205  			}
   206  
   207  			fakeMapper := fakemapper.NewFakeRESTMapper(tc.gvk)
   208  			statusReader := &ConfigConnectorStatusReader{
   209  				Mapper: fakeMapper,
   210  			}
   211  
   212  			res, err := statusReader.ReadStatus(context.Background(), fakeClusterReader, object.UnstructuredToObjMetadata(obj))
   213  			assert.NoError(t, err)
   214  			assert.Equal(t, tc.expectedStatus, res.Status)
   215  		})
   216  	}
   217  }