github.com/confluentinc/cli@v1.100.0/test/scoped_id_test.go (about) 1 package test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/confluentinc/cli/internal/cmd/cluster" 14 ) 15 16 func (s *CLITestSuite) TestClusterScopedId() { 17 // everything 18 cpIdURL1 := serveClusterScopedId(&cluster.ScopedId{ 19 ID: "crn://md01.example.com/kafka=kafkaCluster1/connect=connectClusterA", 20 Scope: &cluster.Scope{ 21 Path: []string{"This", "Is", "Ignored"}, 22 Clusters: map[string]string{"kafka-cluster": "kafkaCluster1", "connect-cluster": "connectClusterA"}, 23 }, 24 }, s.T()).URL 25 26 // no id 27 cpIdURL2 := serveClusterScopedId(&cluster.ScopedId{ 28 ID: "", 29 Scope: &cluster.Scope{ 30 Path: []string{}, 31 Clusters: map[string]string{"kafka-cluster": "kafkaCluster1", "connect-cluster": "connectClusterA"}, 32 }, 33 }, s.T()).URL 34 35 // just kafka 36 cpIdURL3 := serveClusterScopedId(&cluster.ScopedId{ 37 ID: "crn://md01.example.com/kafka=kafkaCluster1/connect=connectClusterA", 38 Scope: &cluster.Scope{ 39 Path: []string{}, 40 Clusters: map[string]string{"kafka-cluster": "kafkaCluster1"}, 41 }, 42 }, s.T()).URL 43 44 // old versions of CP without the cluster metadata id endpoint respond with 401 45 cpIdURL4 := serveClusterScopedIdError().URL 46 47 tests := []CLITest{ 48 {args: fmt.Sprintf("cluster describe --url %s", cpIdURL1), fixture: "scoped_id1.golden"}, 49 {args: fmt.Sprintf("cluster describe --url %s", cpIdURL2), fixture: "scoped_id2.golden"}, 50 {args: fmt.Sprintf("cluster describe --url %s", cpIdURL3), fixture: "scoped_id3.golden"}, 51 {args: fmt.Sprintf("cluster describe --url %s", cpIdURL4), fixture: "scoped_id4.golden", wantErrCode: 1}, 52 } 53 for _, tt := range tests { 54 s.runConfluentTest(tt, "") 55 } 56 } 57 58 func serveClusterScopedId(meta *cluster.ScopedId, t *testing.T) *httptest.Server { 59 router := http.NewServeMux() 60 router.HandleFunc("/v1/metadata/id", func(w http.ResponseWriter, r *http.Request) { 61 b, err := json.Marshal(meta) 62 require.NoError(t, err) 63 _, err = io.WriteString(w, string(b)) 64 require.NoError(t, err) 65 }) 66 return httptest.NewServer(router) 67 } 68 69 func serveClusterScopedIdError() *httptest.Server { 70 router := http.NewServeMux() 71 router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 72 w.Header()["WWW-Authenticate"] = []string{"Bearer realm=\"\""} 73 w.Header()["Cache-Control"] = []string{"must-revalidate,no-cache,no-store"} 74 w.WriteHeader(http.StatusUnauthorized) 75 }) 76 return httptest.NewServer(router) 77 }