diff --git a/.github/workflows/linux.yaml b/.github/workflows/linux.yaml new file mode 100644 index 00000000..0482f443 --- /dev/null +++ b/.github/workflows/linux.yaml @@ -0,0 +1,29 @@ +name: Linux Unit tests +on: + pull_request: {} + push: {} + +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.17 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Sanity test + env: + GITHUB_ACTIONS: true + run: | + export PATH=$PATH:$HOME/.local/bin + make + echo "is running in github actions: $GITHUB_ACTIONS" + make sanity-test diff --git a/Makefile b/Makefile index 24b12289..cc611c82 100644 --- a/Makefile +++ b/Makefile @@ -17,3 +17,10 @@ all: build include release-tools/build.make +GOPATH ?= $(shell go env GOPATH) +GOBIN ?= $(GOPATH)/bin +export GOPATH GOBIN + +.PHONY: sanity-test +sanity-test: + ./test/sanity/run-test.sh diff --git a/pkg/iscsi/nodeserver.go b/pkg/iscsi/nodeserver.go index 15000dc8..288a02ef 100644 --- a/pkg/iscsi/nodeserver.go +++ b/pkg/iscsi/nodeserver.go @@ -28,6 +28,16 @@ type nodeServer struct { } func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) { + if req.GetVolumeCapability() == nil { + return nil, status.Error(codes.InvalidArgument, "Volume capability missing in request") + } + if len(req.GetVolumeId()) == 0 { + return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request") + } + if len(req.GetTargetPath()) == 0 { + return nil, status.Error(codes.InvalidArgument, "Target path not provided") + } + iscsiInfo, err := getISCSIInfo(req) if err != nil { return nil, status.Error(codes.Internal, err.Error()) @@ -35,24 +45,27 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis diskMounter := getISCSIDiskMounter(iscsiInfo, req) util := &ISCSIUtil{} - _, err = util.AttachDisk(*diskMounter) - if err != nil { + if _, err := util.AttachDisk(*diskMounter); err != nil { return nil, status.Error(codes.Internal, err.Error()) } - return &csi.NodePublishVolumeResponse{}, nil } func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) { - diskUnmounter := getISCSIDiskUnmounter(req) + if len(req.GetVolumeId()) == 0 { + return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request") + } targetPath := req.GetTargetPath() + if len(targetPath) == 0 { + return nil, status.Error(codes.InvalidArgument, "Target path not provided") + } + + diskUnmounter := getISCSIDiskUnmounter(req) iscsiutil := &ISCSIUtil{} - err := iscsiutil.DetachDisk(*diskUnmounter, targetPath) - if err != nil { + if err := iscsiutil.DetachDisk(*diskUnmounter, targetPath); err != nil { return nil, status.Error(codes.Internal, err.Error()) } - return &csi.NodeUnpublishVolumeResponse{}, nil } diff --git a/test/sanity/README.md b/test/sanity/README.md new file mode 100644 index 00000000..dc1cceac --- /dev/null +++ b/test/sanity/README.md @@ -0,0 +1,8 @@ +## Sanity Tests +Testing CSI driver using the [`sanity`](https://github.com/kubernetes-csi/csi-test/tree/master/pkg/sanity) package test suite. + +### Run sanity tests +``` +make sanity-test +``` + diff --git a/test/sanity/params.yaml b/test/sanity/params.yaml new file mode 100644 index 00000000..4abe6f96 --- /dev/null +++ b/test/sanity/params.yaml @@ -0,0 +1 @@ +source: "//127.0.0.1/share" \ No newline at end of file diff --git a/test/sanity/run-test.sh b/test/sanity/run-test.sh new file mode 100755 index 00000000..f149487c --- /dev/null +++ b/test/sanity/run-test.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# Copyright 2021 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. + +set -eo pipefail + +function cleanup { + echo 'pkill -f iscsiplugin' + if [ -z "$GITHUB_ACTIONS" ] + then + # if not running on github actions, do not use sudo + pkill -f iscsiplugin + else + # if running on github actions, use sudo + sudo pkill -f iscsiplugin + fi + echo 'Deleting CSI sanity test binary' + rm -rf csi-test +} + +trap cleanup EXIT + +function install_csi_sanity_bin { + echo 'Installing CSI sanity test binary...' + mkdir -p $GOPATH/src/github.com/kubernetes-csi + pushd $GOPATH/src/github.com/kubernetes-csi + export GO111MODULE=off + git clone https://github.com/kubernetes-csi/csi-test.git -b v4.3.0 + pushd csi-test/cmd/csi-sanity + make install + popd + popd +} + +if [[ -z "$(command -v csi-sanity)" ]]; then + install_csi_sanity_bin +fi + +readonly endpoint='unix:///tmp/csi.sock' +nodeid='CSINode' +if [[ "$#" -gt 0 ]] && [[ -n "$1" ]]; then + nodeid="$1" +fi + +ARCH=$(uname -p) +if [[ "${ARCH}" == "x86_64" || ${ARCH} == "unknown" ]]; then + ARCH="amd64" +fi + +if [ -z "$GITHUB_ACTIONS" ] +then + # if not running on github actions, do not use sudo + bin/iscsiplugin --endpoint "$endpoint" --nodeid "$nodeid" -v=5 & +else + # if running on github actions, use sudo + sudo bin/iscsiplugin --endpoint "$endpoint" --nodeid "$nodeid" -v=5 & +fi + +echo 'Begin to run sanity test...' +skipTests='Controller Server|should work|should be idempotent|should remove target path' +CSI_SANITY_BIN=$GOPATH/bin/csi-sanity +if [ -z "$GITHUB_ACTIONS" ] +then + # if not running on github actions, do not use sudo + "$CSI_SANITY_BIN" --ginkgo.v --csi.secrets="$(pwd)/test/sanity/secrets.yaml" --csi.testvolumeparameters="$(pwd)/test/sanity/params.yaml" --csi.endpoint="$endpoint" --ginkgo.skip="$skipTests" +else + # if running on github actions, use sudo + sudo "$CSI_SANITY_BIN" --ginkgo.v --csi.secrets="$(pwd)/test/sanity/secrets.yaml" --csi.testvolumeparameters="$(pwd)/test/sanity/params.yaml" --csi.endpoint="$endpoint" --ginkgo.skip="$skipTests" +fi diff --git a/test/sanity/secrets.yaml b/test/sanity/secrets.yaml new file mode 100644 index 00000000..bfd8b037 --- /dev/null +++ b/test/sanity/secrets.yaml @@ -0,0 +1,3 @@ +NodeStageVolumeSecret: + username: sanity + password: sanitytestpassword