@@ -9,10 +9,12 @@ import "reflect-metadata";
9
9
import { Container } from 'inversify' ;
10
10
import { suite , test } from "@testdeck/mocha" ;
11
11
import * as chai from 'chai' ;
12
- import { WorkspaceManagerClientProvider } from "./client-provider" ;
12
+ import { IWorkspaceClusterStartSet , WorkspaceManagerClientProvider } from "./client-provider" ;
13
13
import { WorkspaceManagerClientProviderCompositeSource , WorkspaceManagerClientProviderSource } from "./client-provider-source" ;
14
14
import { WorkspaceCluster , WorkspaceClusterWoTLS } from "@gitpod/gitpod-protocol/lib/workspace-cluster" ;
15
15
import { User , Workspace , WorkspaceInstance } from "@gitpod/gitpod-protocol" ;
16
+ import { PromisifiedWorkspaceManagerClient } from "." ;
17
+ import { Constraint , constraintNewWorkspaceCluster , ExtendedUser , intersect , invert } from "./constraints" ;
16
18
const expect = chai . expect ;
17
19
18
20
@suite
@@ -32,12 +34,7 @@ class TestClientProvider {
32
34
{ name : "a2" , govern : true , maxScore : 100 , score : 50 , state : "available" , url : "" , admissionConstraints : [ ] } ,
33
35
{ name : "a3" , govern : false , maxScore : 100 , score : 50 , state : "available" , url : "" , admissionConstraints : [ ] } ,
34
36
{
35
- name : "con1" , govern : true , maxScore : 100 , score : 0 , state : "available" , url : "" , admissionConstraints : [
36
- { type : "has-feature-preview" } ,
37
- ]
38
- } ,
39
- {
40
- name : "con2" , govern : true , maxScore : 100 , score : 50 , state : "available" , url : "" , admissionConstraints : [
37
+ name : "con1" , govern : true , maxScore : 100 , score : 50 , state : "available" , url : "" , admissionConstraints : [
41
38
{ type : "has-permission" , permission : "new-workspace-cluster" } ,
42
39
]
43
40
} ,
@@ -50,28 +47,72 @@ class TestClientProvider {
50
47
} ;
51
48
} ) . inSingletonScope ( ) ;
52
49
this . provider = c . get ( WorkspaceManagerClientProvider ) ;
50
+
51
+ // we don't actually want to try and connect here
52
+ this . provider . get = async ( name : string , grpcOptions ?: object ) : Promise < PromisifiedWorkspaceManagerClient > => { return { } as PromisifiedWorkspaceManagerClient } ;
53
53
}
54
54
55
55
@test
56
- public async testGetStarterWorkspaceCluster ( ) {
57
- this . expectInstallations ( [ "a1" , "a2" , "a3" ] , await this . provider . getAvailableStartCluster ( { } as User , { } as Workspace , { } as WorkspaceInstance ) ) ;
58
- this . expectInstallations ( [ "a1" , "a2" , "a3" , "con1" ] , await this . provider . getAvailableStartCluster ( {
59
- additionalData : { featurePreview : true }
60
- } as User , { } as Workspace , { } as WorkspaceInstance ) ) ;
61
- this . expectInstallations ( [ "a1" , "a2" , "a3" , "con2" ] , await this . provider . getAvailableStartCluster ( {
56
+ public async getStartClusterSets ( ) {
57
+ await this . expectInstallations ( [ "a1" , "a2" , "a3" ] , await this . provider . getStartClusterSets ( { } as User , { } as Workspace , { } as WorkspaceInstance ) , "default case" ) ;
58
+ this . expectInstallations ( [ "a1" , "a2" , "a3" , "con1" ] , await this . provider . getStartClusterSets ( {
62
59
rolesOrPermissions : [ "new-workspace-cluster" ]
63
- } as User , { } as Workspace , { } as WorkspaceInstance ) ) ;
60
+ } as User , { } as Workspace , { } as WorkspaceInstance ) , "new workspace cluster" ) ;
61
+ }
62
+
63
+ @test
64
+ public async testInvert ( ) {
65
+ expect ( materializeConstraint ( invert ( everything ) ) ) . to . be . empty ;
66
+ expect ( materializeConstraint ( invert ( nothing ) ) ) . to . be . eql ( materializeConstraint ( everything ) ) ;
67
+ }
68
+
69
+ @test
70
+ public testIntersect ( ) {
71
+ expect ( materializeConstraint ( intersect ( everything , nothing ) ) , "eveything U nothing == nothing" ) . to . be . empty ;
72
+ expect ( materializeConstraint ( intersect ( everything , everything , nothing ) ) , "eveything U everything U nothing == nothing" ) . to . be . empty ;
73
+ expect ( materializeConstraint ( intersect ( everything , everything ) ) , "eveything U everything == everything" ) . to . be . eql ( materializeConstraint ( everything ) ) ;
74
+
75
+ const something = ( all : WorkspaceClusterWoTLS [ ] , user : ExtendedUser , workspace : Workspace , instance : WorkspaceInstance ) => all . filter ( c => c . name === "a1" ) ;
76
+ expect ( materializeConstraint ( intersect ( something , nothing ) ) , "something U nothing == nothing" ) . to . be . empty ;
77
+ expect ( materializeConstraint ( intersect ( everything , something ) ) , "eveything U something == something" ) . to . be . eql ( materializeConstraint ( something ) ) ;
78
+ expect ( materializeConstraint ( intersect ( everything , something , nothing ) ) , "everything U something U nothing == nothing" ) . to . be . empty ;
79
+
80
+ expect ( materializeConstraint ( intersect ( everything , invert ( nothing ) ) ) , "everything U invert(nothing) == everything" ) . to . be . eql ( materializeConstraint ( everything ) ) ;
64
81
}
65
82
66
- public async getStartManager ( ) {
67
- const { installation } = await this . provider . getStartManager ( { } as User , { } as Workspace , { } as WorkspaceInstance , [ "a2" , "a3" ] ) ;
68
- expect ( installation ) . to . be . eql ( "a1" ) ;
83
+ @test
84
+ public testConstraintNewWorkspaceCluster ( ) {
85
+ const clusters : WorkspaceClusterWoTLS [ ] = [
86
+ { name : "a1" , admissionConstraints : [ { type : "has-permission" , permission : "new-workspace-cluster" } ] } as WorkspaceClusterWoTLS ,
87
+ { name : "b1" } as WorkspaceClusterWoTLS ,
88
+ ]
89
+ expect ( constraintNewWorkspaceCluster ( clusters , { } as ExtendedUser , { } as Workspace , { } as WorkspaceInstance ) . map ( c => c . name ) ) . to . be . empty ;
90
+ expect ( constraintNewWorkspaceCluster ( clusters , { rolesOrPermissions :[ "new-workspace-cluster" ] } as ExtendedUser , { } as Workspace , { } as WorkspaceInstance ) . map ( c => c . name ) ) . to . be . eql ( [ "a1" ] ) ;
69
91
}
70
92
71
- private expectInstallations ( expected : string [ ] , actual : WorkspaceClusterWoTLS [ ] ) {
72
- expect ( actual . map ( e => e . name ) . sort ( ) ) . to . be . eql ( expected ) ;
93
+ private async expectInstallations ( expected : string [ ] , actual : IWorkspaceClusterStartSet , msg : string ) {
94
+ const a : string [ ] = [ ] ;
95
+ for await ( const c of actual ) {
96
+ a . push ( c . installation ) ;
97
+ }
98
+
99
+ expect ( a . sort ( ) , msg ) . to . be . eql ( expected ) ;
73
100
}
74
101
75
102
}
76
103
104
+ const everything = ( all : WorkspaceClusterWoTLS [ ] , user : ExtendedUser , workspace : Workspace , instance : WorkspaceInstance ) => all ;
105
+ const nothing = ( all : WorkspaceClusterWoTLS [ ] , user : ExtendedUser , workspace : Workspace , instance : WorkspaceInstance ) => [ ] ;
106
+
107
+ function materializeConstraint ( c : Constraint ) : string [ ] {
108
+ const cluster : WorkspaceClusterWoTLS [ ] = [
109
+ { name : "a1" } as WorkspaceClusterWoTLS ,
110
+ { name : "a2" } as WorkspaceClusterWoTLS ,
111
+ { name : "a3" } as WorkspaceClusterWoTLS ,
112
+ { name : "a4" } as WorkspaceClusterWoTLS ,
113
+ ] ;
114
+
115
+ return c ( cluster , { } as ExtendedUser , { } as Workspace , { } as WorkspaceInstance ) . map ( cluster => cluster . name ) ;
116
+ }
117
+
77
118
module . exports = new TestClientProvider ( )
0 commit comments