|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package functions |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + |
| 24 | + util "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" |
| 25 | + "github.com/streamnative/pulsarctl/pkg/cmdutils" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestApplyFunctionRuntimeOptionsUsesCurrentValue(t *testing.T) { |
| 30 | + funcData := &util.FunctionData{FuncConf: &util.FunctionConfig{CustomRuntimeOptions: `{"base":true}`}} |
| 31 | + runtimeOptions := cmdutils.RuntimeOptionsConfig{ |
| 32 | + CustomRuntimeOptionsInjector: func(ctx cmdutils.CustomRuntimeOptionsContext) (string, error) { |
| 33 | + assert.Equal(t, cmdutils.RuntimeResourceFunction, ctx.Resource) |
| 34 | + assert.Equal(t, cmdutils.RuntimeOperationCreate, ctx.Operation) |
| 35 | + assert.Equal(t, `{"base":true}`, ctx.Current) |
| 36 | + return `{"base":true,"extra":true}`, nil |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + err := applyFunctionRuntimeOptions(funcData, runtimeOptions, cmdutils.RuntimeOperationCreate) |
| 41 | + |
| 42 | + assert.NoError(t, err) |
| 43 | + assert.Equal(t, `{"base":true,"extra":true}`, funcData.FuncConf.CustomRuntimeOptions) |
| 44 | +} |
| 45 | + |
| 46 | +func TestApplyFunctionRuntimeOptionsCanClearValue(t *testing.T) { |
| 47 | + funcData := &util.FunctionData{FuncConf: &util.FunctionConfig{CustomRuntimeOptions: `{"base":true}`}} |
| 48 | + runtimeOptions := cmdutils.RuntimeOptionsConfig{ |
| 49 | + CustomRuntimeOptionsInjector: func(cmdutils.CustomRuntimeOptionsContext) (string, error) { |
| 50 | + return "", nil |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + err := applyFunctionRuntimeOptions(funcData, runtimeOptions, cmdutils.RuntimeOperationUpdate) |
| 55 | + |
| 56 | + assert.NoError(t, err) |
| 57 | + assert.Empty(t, funcData.FuncConf.CustomRuntimeOptions) |
| 58 | +} |
| 59 | + |
| 60 | +func TestApplyFunctionRuntimeOptionsWrapsInjectorError(t *testing.T) { |
| 61 | + funcData := &util.FunctionData{FuncConf: &util.FunctionConfig{CustomRuntimeOptions: "current"}} |
| 62 | + runtimeOptions := cmdutils.RuntimeOptionsConfig{ |
| 63 | + CustomRuntimeOptionsInjector: func(cmdutils.CustomRuntimeOptionsContext) (string, error) { |
| 64 | + return "", errors.New("boom") |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + err := applyFunctionRuntimeOptions(funcData, runtimeOptions, cmdutils.RuntimeOperationUpdate) |
| 69 | + |
| 70 | + assert.EqualError(t, err, "inject function update custom runtime options: boom") |
| 71 | + assert.Equal(t, "current", funcData.FuncConf.CustomRuntimeOptions) |
| 72 | +} |
| 73 | + |
| 74 | +func TestApplyFunctionRuntimeOptionsNoInjectorLeavesValue(t *testing.T) { |
| 75 | + funcData := &util.FunctionData{FuncConf: &util.FunctionConfig{CustomRuntimeOptions: `{"base":true}`}} |
| 76 | + |
| 77 | + err := applyFunctionRuntimeOptions(funcData, cmdutils.RuntimeOptionsConfig{}, cmdutils.RuntimeOperationCreate) |
| 78 | + |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.Equal(t, `{"base":true}`, funcData.FuncConf.CustomRuntimeOptions) |
| 81 | +} |
0 commit comments