github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/input/k8s_events_test.go (about)

     1  package input
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/observiq/carbon/operator"
     9  	"github.com/observiq/carbon/operator/helper"
    10  	"github.com/observiq/carbon/testutil"
    11  	"github.com/stretchr/testify/require"
    12  	apiv1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  	"k8s.io/apimachinery/pkg/runtime"
    15  	watch "k8s.io/apimachinery/pkg/watch"
    16  	fakev1 "k8s.io/client-go/kubernetes/typed/core/v1/fake"
    17  	fakeTest "k8s.io/client-go/testing"
    18  )
    19  
    20  var fakeTime = time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)
    21  
    22  type fakeWatch struct{}
    23  
    24  func (f *fakeWatch) Stop() {}
    25  func (f *fakeWatch) ResultChan() <-chan watch.Event {
    26  	ch := make(chan watch.Event, 1)
    27  	ch <- watch.Event{
    28  		Type: "ADDED",
    29  		Object: (&apiv1.Event{
    30  			LastTimestamp: metav1.Time{
    31  				Time: fakeTime,
    32  			},
    33  		}).DeepCopyObject(),
    34  	}
    35  	return ch
    36  }
    37  
    38  func TestWatchNamespace(t *testing.T) {
    39  	inputOp, err := helper.NewInputConfig("test_id", "k8s_event_input").Build(testutil.NewBuildContext(t))
    40  	require.NoError(t, err)
    41  
    42  	fakeAPI := &fakeTest.Fake{}
    43  	fakeAPI.AddWatchReactor("*", func(action fakeTest.Action) (handled bool, ret watch.Interface, err error) {
    44  		return true, &fakeWatch{}, nil
    45  	})
    46  
    47  	ctx, cancel := context.WithCancel(context.Background())
    48  	op := &K8sEvents{
    49  		InputOperator: inputOp,
    50  		client: &fakev1.FakeCoreV1{
    51  			Fake: fakeAPI,
    52  		},
    53  		namespaces: []string{"test_namespace"},
    54  		cancel:     cancel,
    55  	}
    56  
    57  	fake := testutil.NewFakeOutput(t)
    58  	op.OutputOperators = []operator.Operator{fake}
    59  
    60  	op.startWatchingNamespace(ctx, "test_namespace")
    61  	defer op.Stop()
    62  
    63  	select {
    64  	case entry := <-fake.Received:
    65  		require.Equal(t, entry.Timestamp, fakeTime)
    66  	case <-time.After(time.Second):
    67  		require.FailNow(t, "Timed out waiting for entry")
    68  	}
    69  }
    70  
    71  func TestListNamespaces(t *testing.T) {
    72  	fakeAPI := &fakeTest.Fake{}
    73  	fakeAPI.AddReactor("*", "*", func(action fakeTest.Action) (bool, runtime.Object, error) {
    74  		list := apiv1.NamespaceList{
    75  			Items: []apiv1.Namespace{
    76  				{
    77  					ObjectMeta: metav1.ObjectMeta{
    78  						Name: "test1",
    79  					},
    80  				},
    81  				{
    82  					ObjectMeta: metav1.ObjectMeta{
    83  						Name: "test2",
    84  					},
    85  				},
    86  			},
    87  		}
    88  		return true, list.DeepCopyObject(), nil
    89  	})
    90  	fakeClient := &fakev1.FakeCoreV1{
    91  		Fake: fakeAPI,
    92  	}
    93  
    94  	namespaces, err := listNamespaces(context.Background(), fakeClient)
    95  	require.NoError(t, err)
    96  	require.Equal(t, []string{"test1", "test2"}, namespaces)
    97  }