Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ws-manager] add tests for createPVCForWorkspacePod #10165

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/ws-manager/pkg/manager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (m *Manager) createPVCForWorkspacePod(startContext *startWorkspaceContext)
prefix = "ws"
}

PVCConfig := m.Config.WorkspaceClasses[""].PVC
PVCConfig := m.Config.WorkspaceClasses[config.DefaultWorkspaceClass].PVC
if startContext.Class != nil {
PVCConfig = startContext.Class.PVC
}
Expand Down
121 changes: 121 additions & 0 deletions components/ws-manager/pkg/manager/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,124 @@ func TestCreateDefiniteWorkspacePod(t *testing.T) {
}
test.Run()
}

func TestCreatePVCForWorkspacePod(t *testing.T) {
type WorkspaceClass struct {
PVCConfig *config.PVCConfiguration `json:"pvcConfig,omitempty"`
ResourceRequests *config.ResourceConfiguration `json:"resourceRequests,omitempty"`
ResourceLimits *config.ResourceConfiguration `json:"resourceLimits,omitempty"`
}
type fixture struct {
WorkspaceClass

Spec *json.RawMessage `json:"spec,omitempty"` // *api.StartWorkspaceSpec
Request *json.RawMessage `json:"request,omitempty"` // *api.StartWorkspaceRequest
Context *startWorkspaceContext `json:"context,omitempty"`
CACertSecret string `json:"caCertSecret,omitempty"`
Classes map[string]WorkspaceClass `json:"classes,omitempty"`

EnforceAffinity bool `json:"enforceAffinity,omitempty"`
}
type gold struct {
PVC corev1.PersistentVolumeClaim `json:"reason,omitempty"`
Error string `json:"error,omitempty"`
}

test := ctesting.FixtureTest{
T: t,
Path: "testdata/cpwp_*.json",
Test: func(t *testing.T, input interface{}) interface{} {
fixture := input.(*fixture)

mgmtCfg := forTestingOnlyManagerConfig()
mgmtCfg.WorkspaceCACertSecret = fixture.CACertSecret

if fixture.Classes == nil {
fixture.Classes = make(map[string]WorkspaceClass)
}

if _, exists := fixture.Classes[config.DefaultWorkspaceClass]; !exists {
if fixture.WorkspaceClass.ResourceLimits != nil || fixture.WorkspaceClass.ResourceRequests != nil {
// there's no default class in the fixture. If there are limits configured, use those
fixture.Classes[config.DefaultWorkspaceClass] = fixture.WorkspaceClass
}
}

for n, cls := range fixture.Classes {
var cfgCls config.WorkspaceClass
cfgCls.Container.Requests = cls.ResourceRequests
cfgCls.Container.Limits = cls.ResourceLimits
if cls.PVCConfig != nil {
cfgCls.PVC = *cls.PVCConfig
}

mgmtCfg.WorkspaceClasses[n] = &cfgCls
}

manager := &Manager{Config: mgmtCfg}

if fixture.Context == nil {
var req api.StartWorkspaceRequest
if fixture.Request == nil {
if fixture.Spec == nil {
t.Errorf("fixture has neither context, nor request, nor spec")
return nil
}

var spec api.StartWorkspaceSpec
err := protojson.Unmarshal([]byte(*fixture.Spec), &spec)
if err != nil {
t.Errorf("cannot unmarshal StartWorkspaceSpec: %v", err)
return nil
}

req = api.StartWorkspaceRequest{
Type: api.WorkspaceType_REGULAR,
Id: "test",
Metadata: &api.WorkspaceMetadata{
Owner: "tester",
MetaId: "foobar",
},
ServicePrefix: "foobarservice",
Spec: &spec,
}
} else {
err := protojson.Unmarshal([]byte(*fixture.Request), &req)
if err != nil {
t.Errorf("cannot unmarshal StartWorkspaceReq: %v", err)
return nil
}
}

if req.Spec.Class == "" {
fmt.Println()
}
Comment on lines +269 to +271
Copy link
Contributor

@jenting jenting May 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason printing out this empty line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy paste error from the function above. :(


ctx, err := manager.newStartWorkspaceContext(context.Background(), &req)
if err != nil {
t.Errorf("cannot create startWorkspaceContext: %v", err)
return nil
}

// tie down values that would otherwise change for each test
ctx.CLIAPIKey = "Ab=5=rRA*9:C'T{;RRB\u003e]vK2p6`fFfrS"
ctx.OwnerToken = "%7J'[Of/8NDiWE+9F,I6^Jcj_1\u0026}-F8p"

fixture.Context = ctx
}

pvc, serr := manager.createPVCForWorkspacePod(fixture.Context)
result := gold{}
if serr != nil {
result.Error = serr.Error()
return &result
}
result.PVC = *pvc

return &result
},
Fixture: func() interface{} { return &fixture{} },
Gold: func() interface{} { return &gold{} },
}
test.Run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
jenting marked this conversation as resolved.
Show resolved Hide resolved
"reason": {
"metadata": {
"name": "ws-test",
"namespace": "default",
"creationTimestamp": null
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "30Gi"
}
},
"storageClassName": "test-storage"
},
"status": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"classes": {
"gitpodio-pvc": {
"resourceRequests": {"cpu": "900m"},
"resourceLimits": {"cpu": "900m"},
"pvcConfig": {
"size": "30Gi",
"storageClass": "test-storage",
"snapshotClass": "test-snapshot"
}
}
},
"spec": {
"ideImage": {
"webRef": "eu.gcr.io/gitpod-core-dev/buid/theia-ide:someversion"
},
"workspaceImage": "eu.gcr.io/gitpod-dev/workspace-images/ac1c0755007966e4d6e090ea821729ac747d22ac/eu.gcr.io/gitpod-dev/workspace-base-images/github.com/typefox/gitpod:80a7d427a1fcd346d420603d80a31d57cf75a7af",
"initializer": {
"snapshot": {
"snapshot": "workspaces/cryptic-id-goes-herg/fd62804b-4cab-11e9-843a-4e645373048e.tar@gitpod-dev-user-christesting"
}
},
"ports": [
{
"port": 8080
}
],
"envvars": [
{
"name": "foo",
"value": "bar"
}
],
"git": {
"username": "usernameGoesHere",
"email": "some@user.com"
},
"class": "gitpodio-pvc"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"reason": {
"metadata": {
"name": "ws-test",
"namespace": "default",
"creationTimestamp": null
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "30Gi"
}
},
"storageClassName": ""
},
"status": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"classes": {
"gitpodio-pvc": {
"resourceRequests": {"cpu": "900m"},
"resourceLimits": {"cpu": "900m"},
"pvcConfig": {
"size": "30Gi",
"storageClass": "",
"snapshotClass": ""
}
}
},
"spec": {
"ideImage": {
"webRef": "eu.gcr.io/gitpod-core-dev/buid/theia-ide:someversion"
},
"workspaceImage": "eu.gcr.io/gitpod-dev/workspace-images/ac1c0755007966e4d6e090ea821729ac747d22ac/eu.gcr.io/gitpod-dev/workspace-base-images/github.com/typefox/gitpod:80a7d427a1fcd346d420603d80a31d57cf75a7af",
"initializer": {
"snapshot": {
"snapshot": "workspaces/cryptic-id-goes-herg/fd62804b-4cab-11e9-843a-4e645373048e.tar@gitpod-dev-user-christesting"
}
},
"ports": [
{
"port": 8080
}
],
"envvars": [
{
"name": "foo",
"value": "bar"
}
],
"git": {
"username": "usernameGoesHere",
"email": "some@user.com"
},
"class": "gitpodio-pvc"
}
}
21 changes: 21 additions & 0 deletions components/ws-manager/pkg/manager/testdata/cpwp_no_class.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"reason": {
"metadata": {
"name": "ws-test",
"namespace": "default",
"creationTimestamp": null
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "0"
}
},
"storageClassName": ""
},
"status": {}
}
}
28 changes: 28 additions & 0 deletions components/ws-manager/pkg/manager/testdata/cpwp_no_class.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"spec": {
"ideImage": {
"webRef": "eu.gcr.io/gitpod-core-dev/buid/theia-ide:someversion"
},
"workspaceImage": "eu.gcr.io/gitpod-dev/workspace-images/ac1c0755007966e4d6e090ea821729ac747d22ac/eu.gcr.io/gitpod-dev/workspace-base-images/github.com/typefox/gitpod:80a7d427a1fcd346d420603d80a31d57cf75a7af",
"initializer": {
"snapshot": {
"snapshot": "workspaces/cryptic-id-goes-herg/fd62804b-4cab-11e9-843a-4e645373048e.tar@gitpod-dev-user-christesting"
}
},
"ports": [
{
"port": 8080
}
],
"envvars": [
{
"name": "foo",
"value": "bar"
}
],
"git": {
"username": "usernameGoesHere",
"email": "some@user.com"
}
}
}
21 changes: 21 additions & 0 deletions components/ws-manager/pkg/manager/testdata/cpwp_no_pvc.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"reason": {
"metadata": {
"name": "ws-test",
"namespace": "default",
"creationTimestamp": null
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "0"
}
},
"storageClassName": ""
},
"status": {}
}
}
35 changes: 35 additions & 0 deletions components/ws-manager/pkg/manager/testdata/cpwp_no_pvc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"classes": {
"gitpodio-pvc": {
"resourceRequests": {"cpu": "900m"},
"resourceLimits": {"cpu": "900m"}
}
},
"spec": {
"ideImage": {
"webRef": "eu.gcr.io/gitpod-core-dev/buid/theia-ide:someversion"
},
"workspaceImage": "eu.gcr.io/gitpod-dev/workspace-images/ac1c0755007966e4d6e090ea821729ac747d22ac/eu.gcr.io/gitpod-dev/workspace-base-images/github.com/typefox/gitpod:80a7d427a1fcd346d420603d80a31d57cf75a7af",
"initializer": {
"snapshot": {
"snapshot": "workspaces/cryptic-id-goes-herg/fd62804b-4cab-11e9-843a-4e645373048e.tar@gitpod-dev-user-christesting"
}
},
"ports": [
{
"port": 8080
}
],
"envvars": [
{
"name": "foo",
"value": "bar"
}
],
"git": {
"username": "usernameGoesHere",
"email": "some@user.com"
},
"class": "gitpodio-pvc"
}
}