|
18 | 18 | package spec |
19 | 19 |
|
20 | 20 | import ( |
| 21 | + "strings" |
21 | 22 | "testing" |
22 | 23 |
|
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
23 | 26 | "github.com/streamnative/function-mesh/api/v1alpha1" |
24 | 27 |
|
25 | 28 | "github.com/stretchr/testify/assert" |
@@ -170,3 +173,76 @@ func TestGetSourceRunnerImage(t *testing.T) { |
170 | 173 | image = getSourceRunnerImage(&spec) |
171 | 174 | assert.Equal(t, image, "streamnative/pulsar-io-test:2.7.1") |
172 | 175 | } |
| 176 | + |
| 177 | +func TestMakeGoFunctionCommand(t *testing.T) { |
| 178 | + function := makeGoFunctionSample(TestFunctionName) |
| 179 | + commands := MakeGoFunctionCommand("", "/pulsar/go-func", function) |
| 180 | + assert.Equal(t, commands[0], "sh") |
| 181 | + assert.Equal(t, commands[1], "-c") |
| 182 | + assert.True(t, strings.HasPrefix(commands[2], "SHARD_ID=${POD_NAME##*-} && echo shardId=${SHARD_ID}")) |
| 183 | + innerCommands := strings.Split(commands[2], "&&") |
| 184 | + assert.Equal(t, innerCommands[0], "SHARD_ID=${POD_NAME##*-} ") |
| 185 | + assert.Equal(t, innerCommands[1], " echo shardId=${SHARD_ID} ") |
| 186 | + assert.True(t, strings.HasPrefix(innerCommands[2], " GO_FUNCTION_CONF")) |
| 187 | + assert.Equal(t, innerCommands[3], " goFunctionConfigs=${GO_FUNCTION_CONF} ") |
| 188 | + assert.Equal(t, innerCommands[4], " echo goFunctionConfigs=\"'${goFunctionConfigs}'\" ") |
| 189 | + assert.Equal(t, innerCommands[5], " chmod +x /pulsar/go-func ") |
| 190 | + assert.Equal(t, innerCommands[6], " exec /pulsar/go-func -instance-conf ${goFunctionConfigs}") |
| 191 | +} |
| 192 | + |
| 193 | +const TestClusterName string = "test-pulsar" |
| 194 | +const TestFunctionName string = "test-function" |
| 195 | +const TestNameSpace string = "default" |
| 196 | + |
| 197 | +func makeSampleObjectMeta(name string) *metav1.ObjectMeta { |
| 198 | + return &metav1.ObjectMeta{ |
| 199 | + Name: name, |
| 200 | + Namespace: TestNameSpace, |
| 201 | + UID: "dead-beef", // uid not generate automatically with fake k8s |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func makeGoFunctionSample(functionName string) *v1alpha1.Function { |
| 206 | + maxPending := int32(1000) |
| 207 | + replicas := int32(1) |
| 208 | + maxReplicas := int32(5) |
| 209 | + trueVal := true |
| 210 | + return &v1alpha1.Function{ |
| 211 | + TypeMeta: metav1.TypeMeta{ |
| 212 | + Kind: "Function", |
| 213 | + APIVersion: "compute.functionmesh.io/v1alpha1", |
| 214 | + }, |
| 215 | + ObjectMeta: *makeSampleObjectMeta(functionName), |
| 216 | + Spec: v1alpha1.FunctionSpec{ |
| 217 | + Name: functionName, |
| 218 | + Tenant: "public", |
| 219 | + ClusterName: TestClusterName, |
| 220 | + Input: v1alpha1.InputConf{ |
| 221 | + Topics: []string{ |
| 222 | + "persistent://public/default/go-function-input-topic", |
| 223 | + }, |
| 224 | + }, |
| 225 | + Output: v1alpha1.OutputConf{ |
| 226 | + Topic: "persistent://public/default/go-function-output-topic", |
| 227 | + }, |
| 228 | + LogTopic: "persistent://public/default/go-function-logs", |
| 229 | + Timeout: 0, |
| 230 | + MaxMessageRetry: 0, |
| 231 | + ForwardSourceMessageProperty: &trueVal, |
| 232 | + Replicas: &replicas, |
| 233 | + MaxReplicas: &maxReplicas, |
| 234 | + AutoAck: &trueVal, |
| 235 | + MaxPendingAsyncRequests: &maxPending, |
| 236 | + Messaging: v1alpha1.Messaging{ |
| 237 | + Pulsar: &v1alpha1.PulsarMessaging{ |
| 238 | + PulsarConfig: TestClusterName, |
| 239 | + }, |
| 240 | + }, |
| 241 | + Runtime: v1alpha1.Runtime{ |
| 242 | + Golang: &v1alpha1.GoRuntime{ |
| 243 | + Go: "/pulsar/go-func", |
| 244 | + }, |
| 245 | + }, |
| 246 | + }, |
| 247 | + } |
| 248 | +} |
0 commit comments