|
| 1 | +package heartbeat |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/golang/mock/gomock" |
| 9 | + "github.com/gorilla/mux" |
| 10 | + "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" |
| 11 | + appstatetypes "github.com/replicatedhq/replicated-sdk/pkg/appstate/types" |
| 12 | + "github.com/replicatedhq/replicated-sdk/pkg/k8sutil" |
| 13 | + "github.com/replicatedhq/replicated-sdk/pkg/store" |
| 14 | + mock_store "github.com/replicatedhq/replicated-sdk/pkg/store/mock" |
| 15 | + "github.com/replicatedhq/replicated-sdk/pkg/util" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "k8s.io/client-go/kubernetes" |
| 18 | + "k8s.io/client-go/kubernetes/fake" |
| 19 | +) |
| 20 | + |
| 21 | +func Test_SendAppHeartbeat(t *testing.T) { |
| 22 | + ctrl := gomock.NewController(t) |
| 23 | + defer ctrl.Finish() |
| 24 | + mockStore := mock_store.NewMockStore(ctrl) |
| 25 | + |
| 26 | + respRecorder := httptest.NewRecorder() |
| 27 | + mockRouter := mux.NewRouter() |
| 28 | + mockServer := httptest.NewServer(mockRouter) |
| 29 | + defer mockServer.Close() |
| 30 | + mockRouter.Methods("POST").Path("/kots_metrics/license_instance/info").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + respRecorder.Write([]byte("received heartbeat")) |
| 32 | + w.WriteHeader(http.StatusOK) |
| 33 | + }) |
| 34 | + |
| 35 | + type args struct { |
| 36 | + clientset kubernetes.Interface |
| 37 | + sdkStore store.Store |
| 38 | + } |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + args args |
| 42 | + env map[string]string |
| 43 | + isAirgap bool |
| 44 | + mockStoreExpectations func() |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "online heartbeat", |
| 48 | + args: args{ |
| 49 | + clientset: fake.NewSimpleClientset( |
| 50 | + k8sutil.CreateTestDeployment(util.GetReplicatedDeploymentName(), "test-namespace", "1", map[string]string{"app": "test-app"}), |
| 51 | + k8sutil.CreateTestReplicaSet("test-replicaset", "test-namespace", "1"), |
| 52 | + k8sutil.CreateTestPod("test-pod", "test-namespace", "test-replicaset", map[string]string{"app": "test-app"}), |
| 53 | + ), |
| 54 | + sdkStore: mockStore, |
| 55 | + }, |
| 56 | + env: map[string]string{ |
| 57 | + "DISABLE_OUTBOUND_CONNECTIONS": "false", |
| 58 | + "REPLICATED_POD_NAME": "test-pod", |
| 59 | + }, |
| 60 | + isAirgap: false, |
| 61 | + mockStoreExpectations: func() { |
| 62 | + mockStore.EXPECT().GetLicense().Return(&v1beta1.License{ |
| 63 | + Spec: v1beta1.LicenseSpec{ |
| 64 | + LicenseID: "test-license-id", |
| 65 | + Endpoint: mockServer.URL, |
| 66 | + }, |
| 67 | + }) |
| 68 | + mockStore.EXPECT().GetNamespace().Return("test-namespace") |
| 69 | + mockStore.EXPECT().GetReplicatedID().Return("test-cluster-id") |
| 70 | + mockStore.EXPECT().GetAppID().Return("test-app") |
| 71 | + mockStore.EXPECT().GetChannelID().Return("test-app-nightly") |
| 72 | + mockStore.EXPECT().GetChannelName().Return("Test Channel") |
| 73 | + mockStore.EXPECT().GetChannelSequence().Return(int64(1)) |
| 74 | + mockStore.EXPECT().GetAppStatus().Times(2).Return(appstatetypes.AppStatus{ |
| 75 | + AppSlug: "test-app", |
| 76 | + Sequence: 1, |
| 77 | + State: appstatetypes.StateMissing, |
| 78 | + ResourceStates: []appstatetypes.ResourceState{}, |
| 79 | + }) |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "airgap heartbeat", |
| 84 | + args: args{ |
| 85 | + clientset: fake.NewSimpleClientset( |
| 86 | + k8sutil.CreateTestDeployment(util.GetReplicatedDeploymentName(), "test-namespace", "1", map[string]string{"app": "test-app"}), |
| 87 | + k8sutil.CreateTestReplicaSet("test-replicaset", "test-namespace", "1"), |
| 88 | + k8sutil.CreateTestPod("test-pod", "test-namespace", "test-replicaset", map[string]string{"app": "test-app"}), |
| 89 | + ), |
| 90 | + sdkStore: mockStore, |
| 91 | + }, |
| 92 | + env: map[string]string{ |
| 93 | + "DISABLE_OUTBOUND_CONNECTIONS": "true", |
| 94 | + "REPLICATED_POD_NAME": "test-pod", |
| 95 | + }, |
| 96 | + isAirgap: true, |
| 97 | + mockStoreExpectations: func() { |
| 98 | + mockStore.EXPECT().GetLicense().Return(&v1beta1.License{ |
| 99 | + Spec: v1beta1.LicenseSpec{ |
| 100 | + LicenseID: "test-license-id", |
| 101 | + Endpoint: mockServer.URL, |
| 102 | + }, |
| 103 | + }) |
| 104 | + mockStore.EXPECT().GetNamespace().Times(2).Return("test-namespace") |
| 105 | + mockStore.EXPECT().GetReplicatedID().Return("test-cluster-id") |
| 106 | + mockStore.EXPECT().GetAppID().Return("test-app") |
| 107 | + mockStore.EXPECT().GetChannelID().Return("test-app-nightly") |
| 108 | + mockStore.EXPECT().GetChannelName().Return("Test Channel") |
| 109 | + mockStore.EXPECT().GetChannelSequence().Return(int64(1)) |
| 110 | + mockStore.EXPECT().GetAppStatus().Times(2).Return(appstatetypes.AppStatus{ |
| 111 | + AppSlug: "test-app", |
| 112 | + Sequence: 1, |
| 113 | + State: appstatetypes.StateMissing, |
| 114 | + ResourceStates: []appstatetypes.ResourceState{}, |
| 115 | + }) |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + req := require.New(t) |
| 122 | + |
| 123 | + for k, v := range tt.env { |
| 124 | + t.Setenv(k, v) |
| 125 | + } |
| 126 | + |
| 127 | + respRecorder.Body.Reset() |
| 128 | + |
| 129 | + tt.mockStoreExpectations() |
| 130 | + |
| 131 | + err := SendAppHeartbeat(tt.args.clientset, tt.args.sdkStore) |
| 132 | + req.NoError(err) |
| 133 | + |
| 134 | + if !tt.isAirgap { |
| 135 | + req.Equal("received heartbeat", respRecorder.Body.String()) |
| 136 | + } else { |
| 137 | + req.Equal("", respRecorder.Body.String()) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments