github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/reconciler/client_int_test.go (about) 1 //go:build reconciler_integration 2 // +build reconciler_integration 3 4 package reconciler 5 6 import ( 7 "encoding/json" 8 "io/ioutil" 9 "net/http" 10 "os" 11 "path/filepath" 12 "testing" 13 "time" 14 15 "github.com/sirupsen/logrus" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 "k8s.io/apimachinery/pkg/util/wait" 19 ) 20 21 var createClusterJSONPayload = filepath.Join(".", "test", "createCluster.json") 22 var testKubeconfig = filepath.Join(".", "test", "kubeconfig.yaml") 23 24 const reconcilerUrlEnv = "RECONCILER_URL" 25 const testKubeconfigPathKey = "TEST_KUBECONFIG_PATH" 26 27 /* 28 * 29 Those tests perform operation on the Reconciler inventory using the client. Before running any test set the following envs: 30 - RECONCILER_URL - url to locally running instance of reconciler mothership for example "http://localhost:8080" 31 - TEST_KUBECONFIG_PATH - path to kubeconfig for the cluster to reconcile 32 33 EXAMPLE USAGE: 34 kind create cluster 35 kind get kubeconfig > $(pwd)/internal/reconciler/test/kindkubeconfig.yaml 36 37 then run 'base' reconciler, reconciler-mothership (inventory API) and psql db: 38 39 gh repo clone kyma-incubator/reconciler 40 ./scripts/postgres.sh start 41 make build-darwin 42 43 1st terminal: 44 ./bin/reconciler-darwin reconciler start base --server-port=8081 --verbose 45 46 2st terminal: 47 ./bin/reconciler-darwin mothership start --reconcilers configs/component-reconcilers.json --verbose 48 49 3rd terminal: 50 export RECONCILER_URL="http://localhost:8080" 51 export TEST_KUBECONFIG_PATH=$(pwd)/internal/reconciler/test/kindkubeconfig.yaml 52 go test -v -tags=reconciler_integration ./internal/reconciler/... -run TestClient_ReconcileCluster 53 */ 54 func TestClient_ReconcileCluster(t *testing.T) { 55 // given 56 client := newClient(t) 57 reqPayload := fixPayload(t) 58 59 // when 60 response, err := client.ApplyClusterConfig(*reqPayload) 61 62 // then 63 if err != nil { 64 t.Error(err) 65 } 66 t.Logf("%#v", response) 67 68 // then 69 70 if err != nil { 71 t.Error(err) 72 } 73 err = wait.PollImmediate(10*time.Second, 2*time.Minute, func() (done bool, err error) { 74 response, callErr := client.GetLatestCluster(reqPayload.Cluster) 75 if callErr != nil { 76 return false, callErr 77 } 78 t.Logf("got status: %s", response.Status) 79 if response.Status == "ready" { 80 return true, nil 81 } 82 return false, nil 83 }) 84 assert.NoError(t, err) 85 86 } 87 88 func newClient(t *testing.T) Client { 89 t.Helper() 90 reconcilerURL, ok := os.LookupEnv(reconcilerUrlEnv) 91 if !ok { 92 reconcilerURL = "http://localhost:8080" 93 } 94 95 cfg := &Config{URL: reconcilerURL} 96 client := NewReconcilerClient(http.DefaultClient, logrus.WithField("test-client", "reconciler"), cfg) 97 98 return client 99 } 100 101 func fixPayload(t *testing.T) *Cluster { 102 cluster := &Cluster{} 103 data, err := ioutil.ReadFile(createClusterJSONPayload) 104 require.NoError(t, err) 105 err = json.Unmarshal(data, cluster) 106 require.NoError(t, err) 107 108 kubeconfigPath, ok := os.LookupEnv(testKubeconfigPathKey) 109 if !ok { 110 t.Errorf("%s not set", testKubeconfigPathKey) 111 } 112 kubeconfig, err := ioutil.ReadFile(kubeconfigPath) 113 require.NoError(t, err) 114 cluster.Kubeconfig = string(kubeconfig) 115 116 return cluster 117 }