Skip to content

Commit 0ed6f97

Browse files
committed
test: adding kcp env testing
1 parent bde8a75 commit 0ed6f97

File tree

11 files changed

+1406
-3
lines changed

11 files changed

+1406
-3
lines changed

.testcoverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ exclude:
44
- mocks # exclude generated mock files
55
- ^test/openfga
66
- logger/testlogger
7+
- testing/kcpenvtest
78

go.mod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ require (
1616
github.com/google/uuid v1.6.0
1717
github.com/hashicorp/golang-lru/v2 v2.0.7
1818
github.com/jellydator/ttlcache/v3 v3.4.0
19+
github.com/kcp-dev/kcp/sdk v0.27.1
1920
github.com/machinebox/graphql v0.2.2
2021
github.com/openfga/api/proto v0.0.0-20250528161632-e53c69cc5531
2122
github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20250428093642-7aeebe78bbfe
2223
github.com/openfga/openfga v1.9.0
24+
github.com/otiai10/copy v1.14.1
2325
github.com/pkg/errors v0.9.1
2426
github.com/rs/zerolog v1.34.0
2527
github.com/spf13/cobra v1.9.1
@@ -36,7 +38,9 @@ require (
3638
go.opentelemetry.io/proto/otlp v1.7.0
3739
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
3840
golang.org/x/oauth2 v0.30.0
41+
golang.org/x/sys v0.33.0
3942
google.golang.org/grpc v1.73.0
43+
gopkg.in/yaml.v3 v3.0.1
4044
k8s.io/api v0.33.0
4145
k8s.io/apimachinery v0.33.0
4246
k8s.io/client-go v0.33.0
@@ -79,6 +83,7 @@ require (
7983
github.com/inconshreveable/mousetrap v1.1.0 // indirect
8084
github.com/josharian/intern v1.0.0 // indirect
8185
github.com/json-iterator/go v1.1.12 // indirect
86+
github.com/kcp-dev/logicalcluster/v3 v3.0.5 // indirect
8287
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
8388
github.com/mailru/easyjson v0.7.7 // indirect
8489
github.com/matryer/is v1.4.1 // indirect
@@ -89,6 +94,8 @@ require (
8994
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
9095
github.com/natefinch/wrap v0.2.0 // indirect
9196
github.com/oklog/ulid/v2 v2.1.1 // indirect
97+
github.com/onsi/gomega v1.36.1 // indirect
98+
github.com/otiai10/mint v1.6.3 // indirect
9299
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
93100
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
94101
github.com/prometheus/client_golang v1.22.0 // indirect
@@ -114,7 +121,6 @@ require (
114121
golang.org/x/crypto v0.39.0 // indirect
115122
golang.org/x/net v0.41.0 // indirect
116123
golang.org/x/sync v0.15.0 // indirect
117-
golang.org/x/sys v0.33.0 // indirect
118124
golang.org/x/term v0.32.0 // indirect
119125
golang.org/x/text v0.26.0 // indirect
120126
golang.org/x/time v0.9.0 // indirect
@@ -125,7 +131,6 @@ require (
125131
google.golang.org/protobuf v1.36.6 // indirect
126132
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
127133
gopkg.in/inf.v0 v0.9.1 // indirect
128-
gopkg.in/yaml.v3 v3.0.1 // indirect
129134
k8s.io/apiextensions-apiserver v0.33.0 // indirect
130135
k8s.io/klog/v2 v2.130.1 // indirect
131136
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmO
1919
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
2020
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2121
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
22+
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
23+
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
2224
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
2325
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
2426
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -165,6 +167,10 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
165167
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
166168
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
167169
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
170+
github.com/kcp-dev/kcp/sdk v0.27.1 h1:jBVdrZoJd5hy2RqaBnmCCzldimwOqDkf8FXtNq5HaWA=
171+
github.com/kcp-dev/kcp/sdk v0.27.1/go.mod h1:3eRgW42d81Ng60DbG1xbne0FSS2znpcN/GUx4rqJgUo=
172+
github.com/kcp-dev/logicalcluster/v3 v3.0.5 h1:JbYakokb+5Uinz09oTXomSUJVQsqfxEvU4RyHUYxHOU=
173+
github.com/kcp-dev/logicalcluster/v3 v3.0.5/go.mod h1:EWBUBxdr49fUB1cLMO4nOdBWmYifLbP1LfoL20KkXYY=
168174
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
169175
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
170176
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
@@ -231,6 +237,10 @@ github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20250428093642-7aeebe78bbfe/g
231237
github.com/openfga/openfga v1.9.0 h1:Bs5h7fZWZNCubnUhAxH5choNbtkCq1HNaoSrRRgyyXU=
232238
github.com/openfga/openfga v1.9.0/go.mod h1:NwuzRFEwrOBV6AiDCq37KxPp/v9wXdDYHRCXaePf2iU=
233239
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
240+
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
241+
github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I=
242+
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
243+
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
234244
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
235245
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
236246
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
@@ -459,8 +469,12 @@ k8s.io/apiextensions-apiserver v0.33.0 h1:d2qpYL7Mngbsc1taA4IjJPRJ9ilnsXIrndH+r9
459469
k8s.io/apiextensions-apiserver v0.33.0/go.mod h1:VeJ8u9dEEN+tbETo+lFkwaaZPg6uFKLGj5vyNEwwSzc=
460470
k8s.io/apimachinery v0.33.0 h1:1a6kHrJxb2hs4t8EE5wuR/WxKDwGN1FKH3JvDtA0CIQ=
461471
k8s.io/apimachinery v0.33.0/go.mod h1:BHW0YOu7n22fFv/JkYOEfkUYNRN0fj0BlvMFWA7b+SM=
472+
k8s.io/apiserver v0.33.0 h1:QqcM6c+qEEjkOODHppFXRiw/cE2zP85704YrQ9YaBbc=
473+
k8s.io/apiserver v0.33.0/go.mod h1:EixYOit0YTxt8zrO2kBU7ixAtxFce9gKGq367nFmqI8=
462474
k8s.io/client-go v0.33.0 h1:UASR0sAYVUzs2kYuKn/ZakZlcs2bEHaizrrHUZg0G98=
463475
k8s.io/client-go v0.33.0/go.mod h1:kGkd+l/gNGg8GYWAPr0xF1rRKvVWvzh9vmZAMXtaKOg=
476+
k8s.io/component-base v0.33.0 h1:Ot4PyJI+0JAD9covDhwLp9UNkUja209OzsJ4FzScBNk=
477+
k8s.io/component-base v0.33.0/go.mod h1:aXYZLbw3kihdkOPMDhWbjGCO6sg+luw554KP51t8qCU=
464478
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
465479
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
466480
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff h1:/usPimJzUKKu+m+TE36gUyGcf03XZEP0ZIKgKj35LS4=

test/kcp/kcpserver.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package kcp
2+
3+
import (
4+
"io"
5+
"net/url"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/platform-mesh/golang-commons/logger"
11+
"github.com/platform-mesh/golang-commons/test/kcp/process"
12+
)
13+
14+
type KCPServer struct {
15+
processState *process.State
16+
Out io.Writer
17+
Err io.Writer
18+
StartTimeout time.Duration
19+
StopTimeout time.Duration
20+
Dir string
21+
Binary string
22+
Args []string
23+
PathToRoot string
24+
25+
log *logger.Logger
26+
args *process.Arguments
27+
}
28+
29+
func NewKCPServer(baseDir string, binary string, pathToRoot string, log *logger.Logger) *KCPServer {
30+
return &KCPServer{
31+
Dir: baseDir,
32+
Binary: binary,
33+
Args: []string{"start", "-v=1"},
34+
PathToRoot: pathToRoot,
35+
log: log,
36+
}
37+
}
38+
39+
func (s *KCPServer) Start() error {
40+
if err := s.prepare(); err != nil {
41+
return err
42+
}
43+
return s.processState.Start(s.Out, s.Err, s.log)
44+
}
45+
46+
func (s *KCPServer) prepare() error {
47+
if s.Out == nil || s.Err == nil {
48+
//create file writer for the logs
49+
fileOut := filepath.Join(s.PathToRoot, "kcp.log")
50+
out, err := os.Create(fileOut)
51+
if err != nil {
52+
return err
53+
}
54+
writer := io.Writer(out)
55+
56+
if s.Out == nil {
57+
s.Out = writer
58+
}
59+
if s.Err == nil {
60+
s.Err = writer
61+
}
62+
}
63+
64+
if err := s.setProcessState(); err != nil {
65+
return err
66+
}
67+
return nil
68+
}
69+
70+
func (s *KCPServer) setProcessState() error {
71+
var err error
72+
73+
healthUrl, err := url.Parse("https://localhost:6443/clusters/root/apis/tenancy.kcp.io/v1alpha1/workspaces")
74+
if err != nil {
75+
return err
76+
}
77+
s.processState = &process.State{
78+
Dir: s.Dir,
79+
Path: s.Binary,
80+
StartTimeout: s.StartTimeout,
81+
StopTimeout: s.StopTimeout,
82+
HealthCheck: process.HealthCheck{
83+
URL: *healthUrl,
84+
PollInterval: 2 * time.Second,
85+
KcpAssetPath: filepath.Join(s.PathToRoot, ".kcp"),
86+
},
87+
}
88+
if err := s.processState.Init("kcp"); err != nil {
89+
return err
90+
}
91+
92+
s.Binary = s.processState.Path
93+
s.Dir = s.processState.Dir
94+
s.StartTimeout = s.processState.StartTimeout
95+
s.StopTimeout = s.processState.StopTimeout
96+
97+
s.processState.Args, s.Args, err = process.TemplateAndArguments(s.Args, s.Configure(), process.TemplateDefaults{ //nolint:staticcheck
98+
Data: s,
99+
Defaults: s.defaultArgs(),
100+
MinimalDefaults: map[string][]string{},
101+
})
102+
if err != nil {
103+
return err
104+
}
105+
106+
return nil
107+
}
108+
109+
func (s *KCPServer) defaultArgs() map[string][]string {
110+
args := map[string][]string{}
111+
return args
112+
}
113+
114+
func (s *KCPServer) Configure() *process.Arguments {
115+
if s.args == nil {
116+
s.args = process.EmptyArguments()
117+
}
118+
return s.args
119+
}
120+
121+
func (s *KCPServer) Stop() error {
122+
if s.processState != nil {
123+
if err := s.processState.Stop(); err != nil {
124+
return err
125+
}
126+
}
127+
return nil
128+
}

0 commit comments

Comments
 (0)