Skip to content

Commit

Permalink
Enable action CI on Go Functions (apache#6053)
Browse files Browse the repository at this point in the history
Motivation
- Enable action CI on Go Functions

Modifications
- add test check for Go function
- add code style check for Go Function

* Enable action CI on Go Functions

Signed-off-by: xiaolong.ran <[email protected]>

* ignore non-go files

Signed-off-by: xiaolong.ran <[email protected]>

* remove go build

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix go mod file

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* fix ci error

Signed-off-by: xiaolong.ran <[email protected]>

* code format

Signed-off-by: xiaolong.ran <[email protected]>

* fix test case

Signed-off-by: xiaolong.ran <[email protected]>

* fix license header

Signed-off-by: xiaolong.ran <[email protected]>
  • Loading branch information
wolfstudy authored Jan 19, 2020
1 parent dc7abd8 commit f28c32f
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 39 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/ci-go-functions-style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

name: CI - Go Functions style check
on:
pull_request:
branches:
- master
paths:
- '.github/workflows/**'
- 'pulsar-function-go/**'

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: InstallTool
run: |
cd pulsar-function-go
wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.18.0
./bin/golangci-lint --version
- name: Build
run: |
cd pulsar-function-go
go build ./pf
- name: CheckStyle
run: |
cd pulsar-function-go
./bin/golangci-lint run -c ./golangci.yml ./pf
49 changes: 49 additions & 0 deletions .github/workflows/ci-go-functions-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

name: CI - Go Functions Tests
on:
pull_request:
branches:
- master
paths:
- '.github/workflows/**'
- 'pulsar-function-go/**'

jobs:

cpp-tests:
name:
runs-on: ubuntu-latest
timeout-minutes: 120

steps:
- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: checkout
uses: actions/checkout@v1

- name: run tests
run: |
cd pulsar-function-go
go test -v $(go list ./... | grep -v examples)
22 changes: 7 additions & 15 deletions pulsar-function-go/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"flag"
"io/ioutil"
"os"
"os/user"
"time"

log "github.com/apache/pulsar/pulsar-function-go/logutil"
"gopkg.in/yaml.v2"
)

const ConfigPath = "github.com/apache/pulsar/pulsar-function-go/conf/conf.yaml"
const ConfigPath = "conf/conf.yaml"

type Conf struct {
PulsarServiceURL string `json:"pulsarServiceURL" yaml:"pulsarServiceURL"`
Expand Down Expand Up @@ -100,10 +99,10 @@ func (c *Conf) GetConf() *Conf {
log.Errorf("unmarshal yaml file error:%s", err.Error())
return nil
}
} else if err != nil && os.IsNotExist(err) && confContent == "" {
} else if os.IsNotExist(err) && confContent == "" {
log.Errorf("conf file not found, no config content provided, err:%s", err.Error())
return nil
} else if err != nil && !os.IsNotExist(err) {
} else if !os.IsNotExist(err) {
log.Errorf("load conf file failed, err:%s", err.Error())
return nil
}
Expand All @@ -121,19 +120,12 @@ func (c *Conf) GetConf() *Conf {
}

func init() {
var homeDir string
usr, err := user.Current()
if err == nil {
homeDir = usr.HomeDir
var defaultPath string
if err := os.Chdir("../"); err == nil{
defaultPath = ConfigPath
}
log.Infof("The default config file path is: %s", defaultPath)

// Fall back to standard HOME environment variable that works
// for most POSIX OSes if the directory from the Go standard
// lib failed.
if err != nil || homeDir == "" {
homeDir = os.Getenv("HOME")
}
defaultPath := homeDir + "/" + ConfigPath
flag.BoolVar(&help, "help", false, "print help cmd")
flag.StringVar(&confFilePath, "instance-conf-path", defaultPath, "config conf.yml filepath")
flag.StringVar(&confContent, "instance-conf", "", "the string content of Conf struct")
Expand Down
45 changes: 45 additions & 0 deletions pulsar-function-go/golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

run:
deadline: 6m

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- gocritic
- goimports
- golint
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- lll
- prealloc
6 changes: 0 additions & 6 deletions pulsar-function-go/pf/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@ func TestContext(t *testing.T) {
defer cancel()
fc := NewFuncContext()
ctx = NewContext(ctx, fc)

ctx = context.WithValue(ctx, "pulsar", "function")

if resfc, ok := FromContext(ctx); ok {
assert.Equal(t, []string{"topic-1", "topic-2"}, resfc.GetInputTopics())
assert.Equal(t, "1.0.0", resfc.GetFuncVersion())
assert.Equal(t, "pulsar-function", resfc.GetFuncID())
assert.Equal(t, "go-function", resfc.GetFuncName())
assert.Equal(t, "topic-3", resfc.GetOutputTopic())
}
assert.Equal(t, "function", ctx.Value("pulsar"))
}
12 changes: 8 additions & 4 deletions pulsar-function-go/pf/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//

//
// This file borrows some of the implementations from {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go}
// This file borrows some of the implementations from
// {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go}
// - errorHandler
// - validateArguments
// - validateReturns
Expand Down Expand Up @@ -76,17 +77,20 @@ func validateArguments(handler reflect.Type) (bool, error) {

func validateReturns(handler reflect.Type) error {
errorType := reflect.TypeOf((*error)(nil)).Elem()
if handler.NumOut() > 2 {

switch {
case handler.NumOut() > 2:
return fmt.Errorf("function may not return more than two values")
} else if handler.NumOut() > 1 {
case handler.NumOut() > 1:
if !handler.Out(1).Implements(errorType) {
return fmt.Errorf("function returns two values, but the second does not implement error")
}
} else if handler.NumOut() == 1 {
case handler.NumOut() == 1:
if !handler.Out(0).Implements(errorType) {
return fmt.Errorf("function returns a single value, but it does not implement error")
}
}

return nil
}

Expand Down
6 changes: 4 additions & 2 deletions pulsar-function-go/pf/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//

//
// This file borrows some of the implementations from {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/function_test.go}
// This file borrows some of the implementations from
// {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/function_test.go}
// - TestInvalidFunctions
//

Expand All @@ -33,6 +34,7 @@ import (
"github.com/stretchr/testify/assert"
)

// nolint
func TestInvalidFunctions(t *testing.T) {

testCases := []struct {
Expand Down Expand Up @@ -107,7 +109,7 @@ type expected struct {
}

var (
input = []byte{102, 117, 110, 99, 116, 105, 111, 110}
input = []byte{102, 117, 110, 99, 116, 105, 111, 110}
)

func TestInvokes(t *testing.T) {
Expand Down
11 changes: 5 additions & 6 deletions pulsar-function-go/pf/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (gi *goInstance) startFunction(function function) error {
return err
}

idleDuration := getIdleTimeout(time.Millisecond * gi.context.instanceConf.killAfterIdleMs)
idleDuration := getIdleTimeout(time.Millisecond * gi.context.instanceConf.killAfterIdle)
idleTimer := time.NewTimer(idleDuration)
defer idleTimer.Stop()

Expand Down Expand Up @@ -238,7 +238,8 @@ func (gi *goInstance) processResult(msgInput pulsar.Message, output []byte) {
Payload: output,
}
// Attempt to send the message and handle the response
gi.producer.SendAsync(context.Background(), &asyncMsg, func(messageID pulsar.MessageID, message *pulsar.ProducerMessage, err error) {
gi.producer.SendAsync(context.Background(), &asyncMsg, func(messageID pulsar.MessageID,
message *pulsar.ProducerMessage, err error) {
if err != nil {
if autoAck && atLeastOnce {
gi.nackInputMessage(msgInput)
Expand All @@ -248,10 +249,8 @@ func (gi *goInstance) processResult(msgInput pulsar.Message, output []byte) {
gi.ackInputMessage(msgInput)
}
})
} else {
if autoAck && atLeastOnce {
gi.ackInputMessage(msgInput)
}
} else if autoAck && atLeastOnce {
gi.ackInputMessage(msgInput)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pulsar-function-go/pf/instanceConf.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type instanceConf struct {
port int
clusterName string
pulsarServiceURL string
killAfterIdleMs time.Duration
killAfterIdle time.Duration
}

func newInstanceConf() *instanceConf {
Expand All @@ -55,7 +55,7 @@ func newInstanceConf() *instanceConf {
port: cfg.Port,
clusterName: cfg.ClusterName,
pulsarServiceURL: cfg.PulsarServiceURL,
killAfterIdleMs: cfg.KillAfterIdleMs,
killAfterIdle: cfg.KillAfterIdleMs,
funcDetails: pb.FunctionDetails{
Tenant: cfg.Tenant,
Namespace: cfg.NameSpace,
Expand Down
6 changes: 3 additions & 3 deletions pulsar-function-go/pf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"fmt"
)

func getProperties(fullyQualifiedName string, instanceId int) map[string]string {
func getProperties(fullyQualifiedName string, instanceID int) map[string]string {
propertiesMap := make(map[string]string)
propertiesMap["application"] = "pulsar-function"
propertiesMap["id"] = fullyQualifiedName
propertiesMap["instance_id"] = fmt.Sprintf("%d", instanceId)
propertiesMap["instance_id"] = fmt.Sprintf("%d", instanceID)

return propertiesMap
}
Expand All @@ -36,6 +36,6 @@ func getDefaultSubscriptionName(tenant, namespace, name string) string {
return fmt.Sprintf("%s/%s/%s", tenant, namespace, name)
}

func getFullyQualifiedInstanceId(tenant, namespace, name string, instanceID int) string {
func getFullyQualifiedInstanceID(tenant, namespace, name string, instanceID int) string {
return fmt.Sprintf("%s/%s/%s:%d", tenant, namespace, name, instanceID)
}
2 changes: 1 addition & 1 deletion pulsar-function-go/pf/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func TestUtils(t *testing.T) {
actualtMap := getProperties(fqfn, 100)
assert.Equal(t, propertiesMap, actualtMap)

expectedRes := getFullyQualifiedInstanceId(tenant, namespace, name, instanceID)
expectedRes := getFullyQualifiedInstanceID(tenant, namespace, name, instanceID)
assert.Equal(t, expectedRes, "pulsar/function/go:100")
}

0 comments on commit f28c32f

Please sign in to comment.