github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/reconciler/client_test.go (about) 1 package reconciler 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "path/filepath" 10 "strconv" 11 "testing" 12 "time" 13 14 reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb" 15 "github.com/sirupsen/logrus" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 var clusterJSONFile = filepath.Join(".", "test", "cluster.json") 21 22 const fixReconcilerURL = "reconciler-url:8080" 23 24 func Test_RegisterCluster(t *testing.T) { 25 // given 26 fixClusterID := "1" 27 fixClusterVersion := int64(1) 28 fixConfigVersion := int64(1) 29 requestedCluster := fixCluster(t, fixClusterID, fixClusterVersion) 30 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 31 //then 32 assert.Equal(t, "/v1/clusters", r.URL.Path) 33 assert.Equal(t, http.MethodPost, r.Method) 34 err := json.NewEncoder(w).Encode(reconcilerApi.HTTPClusterResponse{ 35 Cluster: requestedCluster.RuntimeID, 36 ClusterVersion: fixClusterVersion, 37 ConfigurationVersion: fixConfigVersion, 38 Status: reconcilerApi.StatusReconcilePending, 39 StatusURL: fmt.Sprintf("%s/v1/clusters/%s/configs/%s/status", fixReconcilerURL, requestedCluster.RuntimeID, strconv.FormatInt(fixConfigVersion, 10)), 40 }) 41 require.NoError(t, err) 42 })) 43 defer ts.Close() 44 45 client := NewReconcilerClient(http.DefaultClient, logrus.New().WithField("client", "reconciler"), &Config{URL: ts.URL}) 46 47 // when 48 response, err := client.ApplyClusterConfig(*requestedCluster) 49 50 // then 51 require.NoError(t, err) 52 assert.Equal(t, requestedCluster.RuntimeID, response.Cluster) 53 assert.Equal(t, fixClusterVersion, response.ClusterVersion) 54 assert.Equal(t, fixConfigVersion, response.ConfigurationVersion) 55 assert.Equal(t, reconcilerApi.StatusReconcilePending, response.Status) 56 assert.Equal(t, fmt.Sprintf("%s/v1/clusters/%s/configs/%d/status", fixReconcilerURL, fixClusterID, fixConfigVersion), response.StatusURL) 57 } 58 59 func Test_DeleteCluster(t *testing.T) { 60 // given 61 fixClusterID := "1" 62 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 63 //then 64 assert.Equal(t, fmt.Sprintf("/v1/clusters/%s", fixClusterID), r.URL.Path) 65 assert.Equal(t, http.MethodDelete, r.Method) 66 err := json.NewEncoder(w).Encode("") 67 require.NoError(t, err) 68 })) 69 defer ts.Close() 70 71 client := NewReconcilerClient(http.DefaultClient, logrus.New().WithField("client", "reconciler"), &Config{URL: ts.URL}) 72 73 // when 74 err := client.DeleteCluster(fixClusterID) 75 76 // then 77 require.NoError(t, err) 78 } 79 80 func Test_GetCluster(t *testing.T) { 81 // given 82 fixClusterID := "1" 83 fixClusterVersion := int64(1) 84 fixConfigVersion := int64(2) 85 requestedCluster := fixCluster(t, fixClusterID, fixClusterVersion) 86 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 87 //then 88 assert.Equal(t, fmt.Sprintf("/v1/clusters/%s/configs/%d/status", fixClusterID, fixConfigVersion), r.URL.Path) 89 assert.Equal(t, http.MethodGet, r.Method) 90 err := json.NewEncoder(w).Encode(reconcilerApi.HTTPClusterResponse{ 91 Cluster: requestedCluster.RuntimeID, 92 ClusterVersion: fixClusterVersion, 93 ConfigurationVersion: fixConfigVersion, 94 Status: reconcilerApi.StatusReconcilePending, 95 }) 96 require.NoError(t, err) 97 })) 98 defer ts.Close() 99 100 client := NewReconcilerClient(http.DefaultClient, logrus.New().WithField("client", "reconciler"), &Config{URL: ts.URL}) 101 102 // when 103 response, err := client.GetCluster(fixClusterID, fixConfigVersion) 104 105 // then 106 require.NoError(t, err) 107 assert.Equal(t, requestedCluster.RuntimeID, response.Cluster) 108 assert.Equal(t, fixClusterVersion, response.ClusterVersion) 109 assert.Equal(t, fixConfigVersion, response.ConfigurationVersion) 110 assert.Equal(t, reconcilerApi.StatusReconcilePending, response.Status) 111 } 112 113 func Test_GetLatestCluster(t *testing.T) { 114 // given 115 fixClusterID := "1" 116 fixClusterVersion := int64(1) 117 fixConfigVersion := int64(2) 118 requestedCluster := fixCluster(t, fixClusterID, fixClusterVersion) 119 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 120 //then 121 assert.Equal(t, fmt.Sprintf("/v1/clusters/%s/status", fixClusterID), r.URL.Path) 122 assert.Equal(t, http.MethodGet, r.Method) 123 err := json.NewEncoder(w).Encode(reconcilerApi.HTTPClusterResponse{ 124 Cluster: requestedCluster.RuntimeID, 125 ClusterVersion: fixClusterVersion, 126 ConfigurationVersion: fixConfigVersion, 127 Status: reconcilerApi.StatusReconcilePending, 128 }) 129 require.NoError(t, err) 130 })) 131 defer ts.Close() 132 133 client := NewReconcilerClient(http.DefaultClient, logrus.New().WithField("client", "reconciler"), &Config{URL: ts.URL}) 134 135 // when 136 response, err := client.GetLatestCluster(fixClusterID) 137 138 // then 139 require.NoError(t, err) 140 assert.Equal(t, requestedCluster.RuntimeID, response.Cluster) 141 assert.Equal(t, fixClusterVersion, response.ClusterVersion) 142 assert.Equal(t, fixConfigVersion, response.ConfigurationVersion) 143 assert.Equal(t, reconcilerApi.StatusReconcilePending, response.Status) 144 } 145 146 func Test_GetStatusChange(t *testing.T) { 147 // given 148 fixClusterID := "1" 149 fixOffset := "1h" 150 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 151 //then 152 assert.Equal(t, fmt.Sprintf("/v1/clusters/%s/statusChanges/%s", fixClusterID, fixOffset), r.URL.Path) 153 assert.Equal(t, http.MethodGet, r.Method) 154 err := json.NewEncoder(w).Encode([]*reconcilerApi.StatusChange{ 155 { 156 Status: reconcilerApi.StatusReady, 157 Duration: int64(40 * time.Second), 158 }, 159 { 160 Status: reconcilerApi.StatusReconciling, 161 Duration: int64(10 * time.Second), 162 }, 163 { 164 Status: reconcilerApi.StatusReconcilePending, 165 Duration: int64(30 * time.Second), 166 }, 167 }) 168 require.NoError(t, err) 169 })) 170 defer ts.Close() 171 172 client := NewReconcilerClient(http.DefaultClient, logrus.New().WithField("client", "reconciler"), &Config{URL: ts.URL}) 173 174 // when 175 response, err := client.GetStatusChange(fixClusterID, fixOffset) 176 177 // then 178 require.NoError(t, err) 179 assert.Len(t, response, 3) 180 } 181 182 func fixCluster(t *testing.T, runtimeID string, clusterVersion int64) *reconcilerApi.Cluster { 183 cluster := &reconcilerApi.Cluster{} 184 data, err := ioutil.ReadFile(clusterJSONFile) 185 require.NoError(t, err) 186 err = json.Unmarshal(data, cluster) 187 require.NoError(t, err) 188 189 cluster.RuntimeID = runtimeID 190 cluster.RuntimeInput.Name = fmt.Sprintf("runtimeName%d", clusterVersion) 191 cluster.Metadata.GlobalAccountID = fmt.Sprintf("globalAccountId%d", clusterVersion) 192 cluster.KymaConfig.Profile = fmt.Sprintf("kymaProfile%d", clusterVersion) 193 cluster.KymaConfig.Version = fmt.Sprintf("kymaVersion%d", clusterVersion) 194 cluster.Kubeconfig = "fake kubeconfig" 195 196 return cluster 197 }