...

Source file src/sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders/generic_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package statusreaders
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  	"k8s.io/apimachinery/pkg/runtime/schema"
    14  	fakecr "sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader/fake"
    15  	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
    16  	"sigs.k8s.io/cli-utils/pkg/object"
    17  	fakemapper "sigs.k8s.io/cli-utils/pkg/testutil"
    18  )
    19  
    20  var (
    21  	customGVK = schema.GroupVersionKind{
    22  		Group:   "custom.io",
    23  		Version: "v1beta1",
    24  		Kind:    "Custom",
    25  	}
    26  	name      = "Foo"
    27  	namespace = "default"
    28  )
    29  
    30  func TestGenericStatusReader(t *testing.T) {
    31  	testCases := map[string]struct {
    32  		result             *status.Result
    33  		err                error
    34  		expectedIdentifier object.ObjMetadata
    35  		expectedStatus     status.Status
    36  	}{
    37  		"successfully computes status": {
    38  			result: &status.Result{
    39  				Status:  status.InProgressStatus,
    40  				Message: "this is a test",
    41  			},
    42  			expectedIdentifier: object.ObjMetadata{
    43  				GroupKind: customGVK.GroupKind(),
    44  				Name:      name,
    45  				Namespace: namespace,
    46  			},
    47  			expectedStatus: status.InProgressStatus,
    48  		},
    49  		"computing status fails": {
    50  			err: fmt.Errorf("this error is a test"),
    51  			expectedIdentifier: object.ObjMetadata{
    52  				GroupKind: customGVK.GroupKind(),
    53  				Name:      name,
    54  				Namespace: namespace,
    55  			},
    56  			expectedStatus: status.UnknownStatus,
    57  		},
    58  	}
    59  
    60  	for tn, tc := range testCases {
    61  		t.Run(tn, func(t *testing.T) {
    62  			fakeReader := fakecr.NewNoopClusterReader()
    63  			fakeMapper := fakemapper.NewFakeRESTMapper()
    64  			resourceStatusReader := &genericStatusReader{
    65  				mapper: fakeMapper,
    66  				statusFunc: func(u *unstructured.Unstructured) (*status.Result, error) {
    67  					return tc.result, tc.err
    68  				},
    69  			}
    70  
    71  			o := &unstructured.Unstructured{}
    72  			o.SetGroupVersionKind(customGVK)
    73  			o.SetName(name)
    74  			o.SetNamespace(namespace)
    75  
    76  			resourceStatus, err := resourceStatusReader.ReadStatusForObject(context.Background(), fakeReader, o)
    77  
    78  			assert.NoError(t, err)
    79  			assert.Equal(t, tc.expectedIdentifier, resourceStatus.Identifier)
    80  			assert.Equal(t, tc.expectedStatus, resourceStatus.Status)
    81  		})
    82  	}
    83  }
    84  

View as plain text