github.com/iter8-tools/iter8@v1.1.2/controllers/k8sclient/fake/simple_test.go (about)

     1  package fake
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"k8s.io/apimachinery/pkg/runtime"
    11  	"k8s.io/apimachinery/pkg/runtime/schema"
    12  )
    13  
    14  const (
    15  	myns    = "myns"
    16  	myname  = "myname"
    17  	myname2 = "myname2"
    18  	hello   = "hello"
    19  	world   = "world"
    20  )
    21  
    22  func TestNew(t *testing.T) {
    23  	var tests = []struct {
    24  		a []runtime.Object
    25  		b bool
    26  	}{
    27  		{nil, true},
    28  		{[]runtime.Object{
    29  			&unstructured.Unstructured{
    30  				Object: map[string]interface{}{
    31  					"apiVersion": "iter8.tools",
    32  					"kind":       "v1",
    33  					"metadata": map[string]interface{}{
    34  						"namespace": myns,
    35  						"name":      myname,
    36  					},
    37  				},
    38  			},
    39  		}, true},
    40  	}
    41  
    42  	for _, e := range tests {
    43  		client := New(nil, e.a)
    44  		assert.NotNil(t, client)
    45  	}
    46  }
    47  
    48  func TestPatch(t *testing.T) {
    49  	gvr := schema.GroupVersionResource{
    50  		Group:    "apps",
    51  		Version:  "v1",
    52  		Resource: "deployments",
    53  	}
    54  
    55  	client := New(nil, []runtime.Object{
    56  		&unstructured.Unstructured{
    57  			Object: map[string]interface{}{
    58  				"apiVersion": "apps/v1",
    59  				"kind":       "Deployment",
    60  				"metadata": map[string]interface{}{
    61  					"namespace": myns,
    62  					"name":      myname,
    63  				},
    64  			},
    65  		},
    66  	})
    67  
    68  	myDeployment, err := client.FakeDynamicClient.Resource(gvr).Namespace(myns).Get(context.TODO(), myname, v1.GetOptions{})
    69  	assert.NoError(t, err)
    70  	assert.NotNil(t, myDeployment)
    71  	// myDeployment should not have the hello: world label yet
    72  	assert.Equal(t, "", myDeployment.GetLabels()[hello])
    73  
    74  	// Create a copy of myDeployment and add the hello: world label
    75  	copiedDeployment := myDeployment.DeepCopy()
    76  	copiedDeployment.SetLabels(map[string]string{
    77  		hello: world,
    78  	})
    79  	newDeploymentBytes, err := copiedDeployment.MarshalJSON()
    80  	assert.NoError(t, err)
    81  	assert.NotNil(t, newDeploymentBytes)
    82  
    83  	// Patch myDeployment
    84  	patchedDeployment, err := client.Patch(gvr, myns, myname, newDeploymentBytes)
    85  	assert.NoError(t, err)
    86  	assert.NotNil(t, patchedDeployment)
    87  	// Patched myDeployment should now have the hello: world label
    88  	assert.Equal(t, world, patchedDeployment.GetLabels()[hello])
    89  }