-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestDataBuilder.cs
More file actions
186 lines (173 loc) · 5.91 KB
/
Copy pathTestDataBuilder.cs
File metadata and controls
186 lines (173 loc) · 5.91 KB
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
using k8s.Models;
using SqlServerOperator.Entities.V1Alpha1;
using System.Text;
namespace OperatorTemplate.Operator.UnitTests.Helpers;
public static class TestDataBuilder
{
public static V1Alpha1SQLServer CreateSqlServer(string name = "test-sqlserver", string namespaceName = "default")
{
return new V1Alpha1SQLServer
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1SQLServer.V1Alpha1SQLServerSpec
{
Image = "mcr.microsoft.com/mssql/server:2022-latest",
StorageClass = "standard",
StorageSize = "1Gi",
SecretName = $"{name}-secret",
ServiceType = "NodePort"
}
};
}
public static V1Alpha1ExternalSQLServer CreateExternalSqlServer(string name = "test-external-sql", string namespaceName = "default")
{
return new V1Alpha1ExternalSQLServer
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1ExternalSQLServer.V1Alpha1ExternalSQLServerSpec
{
Host = "localhost",
Port = 1433,
SecretName = "external-secret",
UseEncryption = false,
TrustServerCertificate = true
}
};
}
public static V1Alpha1SQLServerDatabase CreateDatabase(string name = "test-database", string instanceName = "test-sqlserver", string namespaceName = "default")
{
return new V1Alpha1SQLServerDatabase
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1SQLServerDatabase.V1Alpha1SQLServerDatabaseSpec
{
InstanceName = instanceName,
DatabaseName = "TestDB"
}
};
}
public static V1Alpha1SQLServerLogin CreateLogin(string name = "test-login", string sqlServerName = "test-sqlserver", string namespaceName = "default")
{
return new V1Alpha1SQLServerLogin
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1SQLServerLogin.V1Alpha1SQLServerLoginSpec
{
SqlServerName = sqlServerName,
LoginName = "testuser",
AuthenticationType = "SQL",
SecretName = "login-secret"
}
};
}
public static V1Alpha1DatabaseUser CreateDatabaseUser(
string name = "test-user",
string loginName = "testuser",
string databaseName = "TestDB",
string sqlServerName = "test-sqlserver",
string namespaceName = "default")
{
return new V1Alpha1DatabaseUser
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1DatabaseUser.V1Alpha1DatabaseUserSpec
{
SqlServerName = sqlServerName,
DatabaseName = databaseName,
LoginName = loginName,
Roles = new List<string> { "db_datareader", "db_datawriter" }
}
};
}
public static V1Alpha1SQLServerSchema CreateSchema(string name = "test-schema", string instanceName = "test-sqlserver", string namespaceName = "default")
{
return new V1Alpha1SQLServerSchema
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1Alpha1SQLServerSchema.V1Alpha1SQLServerSchemaSpec
{
InstanceName = instanceName,
DatabaseName = "TestDB",
SchemaName = "TestSchema",
SchemaOwner = "dbo"
}
};
}
public static V1Secret CreateSecret(string name, string namespaceName, string password, string? username = null)
{
var data = new Dictionary<string, byte[]>
{
["password"] = Encoding.UTF8.GetBytes(password)
};
if (username != null)
{
data["username"] = Encoding.UTF8.GetBytes(username);
}
return new V1Secret
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Data = data
};
}
public static V1Service CreateService(string name, string namespaceName, string serviceType = "ClusterIP", string? externalIP = null)
{
var service = new V1Service
{
Metadata = new V1ObjectMeta
{
Name = name,
NamespaceProperty = namespaceName
},
Spec = new V1ServiceSpec
{
Type = serviceType,
Ports = new List<V1ServicePort>
{
new V1ServicePort { Port = 1433, TargetPort = 1433 }
}
}
};
if (serviceType == "LoadBalancer" && externalIP != null)
{
service.Status = new V1ServiceStatus
{
LoadBalancer = new V1LoadBalancerStatus
{
Ingress = new List<V1LoadBalancerIngress>
{
new V1LoadBalancerIngress { Ip = externalIP }
}
}
};
}
return service;
}
}