package issuers import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" acmapi "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/apis/anthoscertmanager/v1" v1 "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/apis/meta/v1" clientset "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/client/clientset/versioned" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" restclient "k8s.io/client-go/rest" ) func getClientServer(h func(http.ResponseWriter, *http.Request)) (*clientset.Clientset, *httptest.Server, error) { srv := httptest.NewServer(http.HandlerFunc(h)) cl, err := clientset.NewForConfig(&restclient.Config{ Host: srv.URL, }) if err != nil { srv.Close() return nil, nil, err } return cl, srv, nil } func TestUpdateIssuerStatus(t *testing.T) { new := &acmapi.Issuer{ ObjectMeta: metav1.ObjectMeta{ Name: "selfsigned-user-issuer", Namespace: "anthoscertmanager", }, Status: acmapi.IssuerStatus{ Conditions: []acmapi.IssuerCondition{ { Type: acmapi.IssuerConditionReady, Status: v1.ConditionTrue, }, }, }, } cl, srv, err := getClientServer(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", runtime.ContentTypeJSON) desiredPath := "/apis/anthos-cert-manager.io/v1/namespaces/anthoscertmanager/issuers/selfsigned-user-issuer/status" if r.URL.Path != desiredPath { t.Errorf("Patch request got path %s. wanted %s", r.URL.Path, desiredPath) } res, _ := json.Marshal(new) w.Write(res) }) defer srv.Close() if err != nil { t.Errorf("Unable to create the fake HTTP server with error: %v", err) } controller := &controller{acmClient: cl} old := &acmapi.Issuer{} if err := controller.updateIssuerStatus(context.TODO(), old, new); err != nil { t.Errorf("Unable to update the issuer due to the error : %v", err) } }