github.com/goharbor/go-client@v0.210.0/pkg/harbor/client_test.go (about) 1 package harbor_test 2 3 import ( 4 "context" 5 "net/http" 6 "net/url" 7 "testing" 8 9 "github.com/go-openapi/runtime" 10 "github.com/goharbor/go-client/pkg/harbor" 11 "github.com/goharbor/go-client/pkg/harbor/test" 12 v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client" 13 "github.com/goharbor/go-client/pkg/sdk/v2.0/client/health" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 var ( 18 httpsSchema = "https" 19 httpSchema = "http" 20 ) 21 22 type clientType string 23 24 var ( 25 v2Type clientType = "v2" 26 ) 27 28 type testParam struct { 29 url string 30 transport http.RoundTripper 31 authInfo runtime.ClientAuthInfoWriter 32 expectedHost string 33 expectedScheme string 34 expectedBasePath string 35 expectedTransport http.RoundTripper 36 expectedAuthInfo runtime.ClientAuthInfoWriter 37 clientType clientType 38 } 39 40 var params = []testParam{ 41 testParam{ 42 url: "//10.0.0.1:443", 43 transport: harbor.InsecureTransport, 44 authInfo: nil, 45 expectedHost: "10.0.0.1:443", 46 expectedBasePath: v2client.DefaultBasePath, 47 expectedScheme: httpsSchema, 48 expectedTransport: harbor.InsecureTransport, 49 expectedAuthInfo: nil, 50 clientType: v2Type, 51 }, 52 testParam{ 53 url: "http://10.0.0.1", 54 transport: harbor.InsecureTransport, 55 authInfo: nil, 56 expectedHost: "10.0.0.1", 57 expectedBasePath: v2client.DefaultBasePath, 58 expectedScheme: httpSchema, 59 expectedTransport: harbor.InsecureTransport, 60 expectedAuthInfo: nil, 61 clientType: v2Type, 62 }, 63 } 64 65 func TestToConfig(t *testing.T) { 66 for _, test := range params { 67 assert := assert.New(t) 68 u, err := url.Parse(test.url) 69 assert.Nil(err) 70 71 c := &harbor.Config{ 72 URL: u, 73 Transport: test.transport, 74 } 75 76 if test.clientType == v2Type { 77 v2c := c.ToV2Config() 78 assert.NotNil(v2c) 79 assert.Equal(test.expectedHost, v2c.URL.Host) 80 assert.Equal(test.expectedScheme, v2c.URL.Scheme) 81 assert.Equal(test.expectedBasePath, v2c.URL.Path) 82 assert.Equal(test.expectedTransport, v2c.Transport) 83 assert.Equal(test.expectedAuthInfo, v2c.AuthInfo) 84 } else { 85 t.Errorf("Unexpected client type %s", test.clientType) 86 } 87 } 88 } 89 90 var clientSetParams = []testParam{ 91 testParam{ 92 url: "//10.0.0.1:443", 93 transport: harbor.InsecureTransport, 94 authInfo: nil, 95 }, 96 } 97 98 func TestClientSet(t *testing.T) { 99 for _, test := range clientSetParams { 100 assert := assert.New(t) 101 102 c := &harbor.ClientSetConfig{ 103 URL: test.url, 104 Password: "a", 105 Username: "b", 106 Insecure: true, 107 } 108 109 cs, err := harbor.NewClientSet(c) 110 assert.NotNil(cs) 111 assert.Nil(err) 112 113 v2Client := cs.V2() 114 assert.NotNil(v2Client) 115 } 116 } 117 118 var ( 119 result = "yes" 120 v = "1.2" 121 username = "a" 122 password = "b" 123 ) 124 125 func TestClients(t *testing.T) { 126 assert := assert.New(t) 127 128 s := test.NewServer() 129 defer s.Close() 130 131 c := &harbor.ClientSetConfig{ 132 URL: s.URL, 133 Password: username, 134 Username: password, 135 Insecure: true, 136 } 137 138 cs, err := harbor.NewClientSet(c) 139 assert.Nil(err) 140 141 // test v2 client 142 resGetHealth, err := cs.V2().Health.GetHealth(context.TODO(), health.NewGetHealthParams()) 143 assert.Nil(err) 144 145 assert.Equal(result, resGetHealth.Payload.Status) 146 }