Skip to content

Commit

Permalink
Code changes
Browse files Browse the repository at this point in the history
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
  • Loading branch information
sbezverk committed Aug 15, 2018
1 parent cb64561 commit bf9b842
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 33 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
language: go

services:
- docker

# Needed for e2e tests
sudo: true
go: 1.9.x

go: 1.10.x
go_import_path: github.com/kubernetes-csi/livenessprobe
install:
- go get -u github.com/golang/dep/cmd/dep
Expand All @@ -11,6 +16,7 @@ script:
- go fmt $(go list ./... | grep -v vendor) | wc -l | grep 0
- go vet $(go list ./... | grep -v vendor)
- go test $(go list ./... | grep -v vendor)
- ./hack/e2e-livenessprobe.sh
after_success:
- if [ "${TRAVIS_BRANCH}" == "master" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then
sudo make livenessprobe-container;
Expand Down
103 changes: 86 additions & 17 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@

[[constraint]]
name = "github.com/container-storage-interface/spec"
version = "0.2.0-rc1"
version = "~0.3.0"

[[constraint]]
branch = "master"
name = "github.com/golang/glog"

[[constraint]]
name = "github.com/golang/mock"
version = "1.0.0"

[[constraint]]
branch = "master"
name = "github.com/kubernetes-csi/csi-test"

[[constraint]]
name = "google.golang.org/grpc"
version = "1.10.0"

[[constraint]]
name = "github.com/golang/mock"
version = "1.1.1"
95 changes: 95 additions & 0 deletions cmd/livenessprobe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed 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.
*/

package main

import (
"context"
"testing"
"time"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/driver"
"github.com/kubernetes-csi/livenessprobe/pkg/connection"
)

const (
driverName = "foo/bar"
)

func createMockServer(t *testing.T) (
*gomock.Controller,
*driver.MockCSIDriver,
*driver.MockIdentityServer,
*driver.MockControllerServer,
*driver.MockNodeServer,
connection.CSIConnection,
error) {
// Start the mock server
mockController := gomock.NewController(t)
identityServer := driver.NewMockIdentityServer(mockController)
controllerServer := driver.NewMockControllerServer(mockController)
nodeServer := driver.NewMockNodeServer(mockController)
drv := driver.NewMockCSIDriver(&driver.MockCSIDriverServers{
Identity: identityServer,
Controller: controllerServer,
Node: nodeServer,
})
drv.Start()

// Create a client connection to it
addr := drv.Address()
csiConn, err := connection.NewConnection(addr, 10)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}

return mockController, drv, identityServer, controllerServer, nodeServer, csiConn, nil
}

func TestProbe(t *testing.T) {
mockController, driver, idServer, _, nodeServer, csiConn, err := createMockServer(t)
if err != nil {
t.Fatal(err)
}
defer mockController.Finish()
defer driver.Stop()
defer csiConn.Close()

// Setting up expected calls' responses
inPlugin := &csi.GetPluginInfoRequest{}
outPlugin := &csi.GetPluginInfoResponse{
Name: "foo/bar",
}
var injectedErr error
idServer.EXPECT().GetPluginInfo(gomock.Any(), inPlugin).Return(outPlugin, injectedErr).Times(1)

inNode := &csi.NodeGetIdRequest{}
outNode := &csi.NodeGetIdResponse{
NodeId: "test_node_id",
}
nodeServer.EXPECT().NodeGetId(gomock.Any(), inNode).Return(outNode, injectedErr).Times(1)
inProbe := &csi.ProbeRequest{}
outProbe := &csi.ProbeResponse{}
idServer.EXPECT().Probe(gomock.Any(), inProbe).Return(outProbe, injectedErr).Times(1)
// Calling Probing function
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := runProbe(ctx, csiConn); err != nil {
t.Fatalf("failed to run probe with error: %+v", err)
}
}
29 changes: 19 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ var (
healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests")
)

func runProbe(ctx context.Context) error {

// Connect to CSI.
glog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress)
csiConn, err := connection.NewConnection(*csiAddress, *connectionTimeout)
if err != nil {
return err
}
func runProbe(ctx context.Context, csiConn connection.CSIConnection) error {

// Get CSI driver name.
glog.Infof("Calling CSI driver to discover driver name.")
Expand All @@ -74,13 +67,29 @@ func runProbe(ctx context.Context) error {
return nil
}

func getCSIConnection() (connection.CSIConnection, error) {
// Connect to CSI.
glog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress)
csiConn, err := connection.NewConnection(*csiAddress, *connectionTimeout)
if err != nil {
return nil, err
}
return csiConn, nil
}

func chekcHealth(w http.ResponseWriter, req *http.Request) {

glog.Infof("Request: %s from: %s\n", req.URL.Path, req.RemoteAddr)
csiConn, err := getCSIConnection()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
glog.Infof("Failed to get connection to CSI with error: %v.", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), *connectionTimeout)
defer cancel()
err := runProbe(ctx)
if err != nil {
if err := runProbe(ctx, csiConn); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
glog.Infof("Health check failed with: %v.", err)
Expand Down
58 changes: 58 additions & 0 deletions hack/e2e-livenessprobe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
set -x

## This file is for livenessprove which runs in a pair with csi
## hostpath

## Must be run from the root of the repo
UDS="/tmp/e2e-csi-sanity.sock"
CSI_ENDPOINT="unix://${UDS}"
CSI_MOUNTPOINT="/mnt"
APP=hostpathplugin

SKIP="WithCapacity"
if [ x${TRAVIS} = x"true" ] ; then
SKIP="WithCapacity|NodeUnpublishVolume|NodePublishVolume"
fi

git clone https://github.com/kubernetes-csi/drivers $GOPATH/src/github.com/kubernetes-csi/drivers
pushd $GOPATH/src/github.com/kubernetes-csi/drivers
# Build
make hostpath; ret=$?
if [ $ret -ne 0 ]; then
echo "Failed to build hostpath plugin, file a bug against drivers repo"
exit 1
fi
popd

sudo rm -rf "$UDS" || true

# Start hostpathplugin in the background
sudo $GOPATH/src/github.com/kubernetes-csi/drivers/_output/$APP --endpoint=$CSI_ENDPOINT --nodeid=1 --v=5 &

# Start liveness probe in the background
sudo ./bin/livenessprobe --csi-address=$CSI_ENDPOINT &

# Give time to CSI hostpathplugin and livenessprobe to initialize
sleep 3

# Requesting health
health=$(curl -I http://localhost:9808/healthz | grep HTTP | awk '{print $2}')
if [[ "x$health" != "x200" ]]; then
echo "Health check failed, but it was not supposed to, exiting..."
exit 1
fi

# Killing hostpathplugin
sudo kill -9 $(pidof hostpathplugin)
sleep 3

# Requesting health, should fail since hostpathplugin is gone
health=$(curl -I http://localhost:9808/healthz| grep HTTP | awk '{print $2}')
if [[ "x$health" != "x500" ]]; then
echo "Health check did not detect driver failure, returned code: $health, exiting..."
exit 1
fi

sudo rm -f $UDS
exit 0

0 comments on commit bf9b842

Please sign in to comment.