Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go CI. #125

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Go

on:
push:
branches: [ main ]
pull_request:
schedule:
- cron: "0 0 * * *"

jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.16', '1.17', '1.18', '1.x']
steps:

- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
id: go

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

- name: Get dependencies
run: |
go get -v -t -d ./...

- name: Build
run: go build -v ./...

- name: Test
run: go test -v -coverprofile=profile.cov ./...

static_analysis:
name: Static Analysis
runs-on: ubuntu-latest

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.x'
id: go

- name: Install required static analysis tools
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Check out code
uses: actions/checkout@v2

- name: Go vet
run: go vet -composites=false ./...

- name: Check gofmt
run: diff -u <(echo -n) <(gofmt -d -s .)

- name: Staticcheck
run: |
staticcheck ./...

coverage:
name: Submit coverage
runs-on: ubuntu-latest

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.x'
id: go

- name: Install required static analysis tools
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Check out code
uses: actions/checkout@v2

- name: Test
run: go test -v -coverprofile=profile.cov ./...

- name: Submit coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
51 changes: 38 additions & 13 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import (
// int64 (nanoseconds since epoch).
func T(n int64) time.Time { return time.Unix(0, n) }

// Now is a function that can be overridden in tests to alter the timestamps
// applied to deletes and metadata updates.
var Now = time.Now

// A Target hosts an indexed cache of state for a single target.
type Target struct {
name string // name of the target
Expand Down Expand Up @@ -502,28 +506,49 @@ func (t *Target) gnmiUpdate(n *pb.Notification) (*ctree.Leaf, error) {
return t.t.GetLeaf(path), nil
}

func toDeleteNotification(n *pb.Notification, timestamp int64) *pb.Notification {
d := &pb.Notification{
Timestamp: timestamp,
Prefix: &pb.Path{
Target: n.GetPrefix().GetTarget(),
Origin: n.GetPrefix().GetOrigin(),
},
}
prefix := n.GetPrefix()
path := n.Update[0].GetPath()
// Set origin if the origin is in the update
if origin := path.GetOrigin(); n.GetPrefix().GetOrigin() == "" && origin != "" {
d.Prefix.Origin = origin
}
switch {
case n.GetAtomic():
d.Delete = []*pb.Path{{Elem: prefix.GetElem(), Element: prefix.GetElement()}}
case len(prefix.GetElem()) > 0 || len(path.GetElem()) > 0:
d.Delete = []*pb.Path{{Elem: append(prefix.GetElem(), path.GetElem()...)}}
default:
d.Delete = []*pb.Path{{Element: append(prefix.GetElement(), path.GetElement()...)}}
}
return d
}

func (t *Target) gnmiRemove(n *pb.Notification) []*ctree.Leaf {
path := joinPrefixAndPath(n.Prefix, n.Delete[0])
if path[0] == metadata.Root {
t.meta.ResetEntry(path[1])
}
leaves := t.t.DeleteConditional(path, func(v interface{}) bool { return v.(*pb.Notification).GetTimestamp() < n.GetTimestamp() })
var leaves []*ctree.Leaf
f := func(v interface{}) {
d := v.(*pb.Notification)
leaves = append(leaves, ctree.DetachedLeaf(toDeleteNotification(d, n.GetTimestamp())))
}
t.t.WalkDeleted(path, func(v interface{}) bool { return v.(*pb.Notification).GetTimestamp() < n.GetTimestamp() }, f)
if len(leaves) == 0 {
return nil
}
deleted := int64(len(leaves))
t.meta.AddInt(metadata.LeafCount, -deleted)
t.meta.AddInt(metadata.DelCount, deleted)
var ls []*ctree.Leaf
for _, l := range leaves {
noti := &pb.Notification{
Timestamp: n.GetTimestamp(),
Prefix: &pb.Path{Target: n.GetPrefix().GetTarget()},
Delete: []*pb.Path{{Element: l}},
}
ls = append(ls, ctree.DetachedLeaf(noti))
}
return ls
return leaves
}

// updateCache calls fn for each Target.
Expand Down Expand Up @@ -645,7 +670,7 @@ func deleteNoti(t, o string, p []string) *pb.Notification {
pe = append(pe, &pb.PathElem{Name: e})
}
return &pb.Notification{
Timestamp: time.Now().UnixNano(),
Timestamp: Now().UnixNano(),
Prefix: &pb.Path{Target: t, Origin: o},
Delete: []*pb.Path{&pb.Path{Elem: pe}},
}
Expand All @@ -658,7 +683,7 @@ func metaNoti(t, m string, v *pb.TypedValue) *pb.Notification {
pe = append(pe, &pb.PathElem{Name: p})
}
return &pb.Notification{
Timestamp: time.Now().UnixNano(),
Timestamp: Now().UnixNano(),
Prefix: &pb.Path{Target: t},
Update: []*pb.Update{
&pb.Update{
Expand Down
Loading