-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
276 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kong | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// AbstractConfigService handles Config in Kong. | ||
type AbstractConfigService interface { | ||
ReloadDeclarativeConfig(ctx context.Context, config any, checkHash int) error | ||
} | ||
|
||
// ConfigService handles Config in Kong. | ||
type ConfigService service | ||
|
||
// SendConfig sends out the specified config to configured Admin API endpoint | ||
func (c *ConfigService) ReloadDeclarativeConfig(ctx context.Context, config any, checkHash int) error { | ||
type sendConfigParams struct { | ||
CheckHash int `url:"check_hash"` | ||
} | ||
req, err := c.client.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHash}, config) | ||
if err != nil { | ||
return fmt.Errorf("creating new HTTP request for /config: %w", err) | ||
} | ||
|
||
resp, err := c.client.DoRAW(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed posting new config to /config: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 400 { | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed posting new config to /config: got status code %d (and failed to read the response body): %w", | ||
resp.StatusCode, err, | ||
) | ||
} | ||
|
||
return fmt.Errorf( | ||
"failed posting new config to /config: got status code %d, body: %s", | ||
resp.StatusCode, b, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package kong | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigService(t *testing.T) { | ||
RunWhenDBMode(t, "off") | ||
|
||
tests := []struct { | ||
name string | ||
config Configuration | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "basic config works", | ||
config: Configuration{ | ||
"_format_version": "1.1", | ||
"services": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
"routes": []Configuration{ | ||
{"paths": []string{"/"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing _format_version fails", | ||
config: Configuration{ | ||
"services": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
"routes": []Configuration{ | ||
{"paths": []string{"/"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid config fails", | ||
config: Configuration{ | ||
"dummy_key": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
client, err := NewTestClient(nil, nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, client) | ||
|
||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := client.Configs.ReloadDeclarativeConfig(context.Background(), tt.config, 1); (err != nil) != tt.wantErr { | ||
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewRequestBody(t *testing.T) { | ||
t.Run("body can be string", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := `{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}` | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be []byte", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a bytes.Buffer", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := bytes.NewBufferString(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a map", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := map[string]any{ | ||
"_format_version": "1.1", | ||
"services": []map[string]any{ | ||
{ | ||
"host": "example.com", | ||
"name": "foo", | ||
}, | ||
}, | ||
} | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters