1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// +build enterprise
package kong
import (
"testing"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)
func TestWorkspaceService(T *testing.T) {
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
workspace := &Workspace{
Name: String("teamA"),
Meta: map[string]interface{}{
"color": "#814CA6",
"thumbnail": nil,
},
}
createdWorkspace, err := client.Workspaces.Create(defaultCtx, workspace)
assert.Nil(err)
assert.NotNil(createdWorkspace)
workspace, err = client.Workspaces.Get(defaultCtx, createdWorkspace.ID)
assert.Nil(err)
assert.NotNil(workspace)
workspace.Comment = String("new comment")
workspace, err = client.Workspaces.Update(defaultCtx, workspace)
assert.Nil(err)
assert.NotNil(workspace)
assert.NotNil(workspace.Config)
assert.Equal("teamA", *workspace.Name)
assert.Equal("new comment", *workspace.Comment)
assert.Equal("#814CA6", workspace.Meta["color"])
err = client.Workspaces.Delete(defaultCtx, createdWorkspace.ID)
assert.Nil(err)
// ID can be specified
id := uuid.NewV4().String()
workspace = &Workspace{
Name: String("teamB"),
ID: String(id),
}
createdWorkspace, err = client.Workspaces.Create(defaultCtx, workspace)
assert.Nil(err)
assert.NotNil(createdWorkspace)
assert.Equal(id, *createdWorkspace.ID)
err = client.Workspaces.Delete(defaultCtx, createdWorkspace.ID)
assert.Nil(err)
}
func TestWorkspaceServiceList(T *testing.T) {
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
workspaceA := &Workspace{
Name: String("teamA"),
}
workspaceB := &Workspace{
Name: String("teamB"),
}
createdWorkspaceA, err := client.Workspaces.Create(defaultCtx, workspaceA)
assert.Nil(err)
createdWorkspaceB, err := client.Workspaces.Create(defaultCtx, workspaceB)
assert.Nil(err)
// paged List
page1, next, err := client.Workspaces.List(defaultCtx, &ListOpt{Size: 1})
assert.Nil(err)
assert.NotNil(next)
assert.NotNil(page1)
assert.Equal(1, len(page1))
// nil ListOpt List
workspaces, next, err := client.Workspaces.List(defaultCtx, nil)
assert.Nil(err)
assert.Nil(next)
assert.NotNil(workspaces)
// Counts default workspace
assert.Equal(3, len(workspaces))
err = client.Workspaces.Delete(defaultCtx, createdWorkspaceA.ID)
assert.Nil(err)
err = client.Workspaces.Delete(defaultCtx, createdWorkspaceB.ID)
assert.Nil(err)
}
func TestWorkspaceServiceListAll(T *testing.T) {
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
workspaceA := &Workspace{
Name: String("teamA"),
}
workspaceB := &Workspace{
Name: String("teamB"),
}
createdWorkspaceA, err := client.Workspaces.Create(defaultCtx, workspaceA)
assert.Nil(err)
createdWorkspaceB, err := client.Workspaces.Create(defaultCtx, workspaceB)
assert.Nil(err)
workspaces, err := client.Workspaces.ListAll(defaultCtx)
assert.Nil(err)
assert.NotNil(workspaces)
// Counts default workspace
assert.Equal(3, len(workspaces))
err = client.Workspaces.Delete(defaultCtx, createdWorkspaceA.ID)
assert.Nil(err)
err = client.Workspaces.Delete(defaultCtx, createdWorkspaceB.ID)
assert.Nil(err)
}
// Workspace entities
func TestWorkspaceService_Entities(T *testing.T) {
runWhenEnterprise(T, ">=0.33.0", false)
assert := assert.New(T)
client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)
workspace := &Workspace{
Name: String("teamA"),
Meta: map[string]interface{}{
"color": "#814CA6",
"thumbnail": nil,
},
}
// Create a workspace
createdWorkspace, err := client.Workspaces.Create(defaultCtx, workspace)
assert.Nil(err)
assert.NotNil(createdWorkspace)
service := &Service{
Name: String("foo"),
Host: String("upstream"),
Port: Int(42),
Path: String("/path"),
}
// Create a service
createdService, err := client.Services.Create(defaultCtx, service)
assert.Nil(err)
assert.NotNil(createdService)
// Add the service to the workspace
entities, err := client.Workspaces.AddEntities(
defaultCtx, createdWorkspace.ID, createdService.ID)
assert.Nil(err)
assert.NotNil(entities)
// List Entities attached to the workspace
entitiesAdded, err := client.Workspaces.ListEntities(defaultCtx, createdWorkspace.ID)
assert.Nil(err)
assert.NotNil(entitiesAdded)
// The two entities are records capturing the service name and id
assert.Equal(2, len(entitiesAdded))
// Delete the service from the workspace
err = client.Workspaces.DeleteEntities(defaultCtx, createdWorkspace.ID, createdService.ID)
assert.Nil(err)
// Delete the service
err = client.Services.Delete(defaultCtx, createdService.ID)
assert.Nil(err)
// Delete the workspace
err = client.Workspaces.Delete(defaultCtx, createdWorkspace.ID)
assert.Nil(err)
}
|