Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Add CI workflows for testing and building COSI
Browse files Browse the repository at this point in the history
- Added Dockerfile, Makefile and CI workflows
- Added tests for DriverInfo in identity server
Issue: S3C-9222
  • Loading branch information
anurag4DSB committed Sep 13, 2024
1 parent f9b93bd commit a280c3c
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches:
- '**'

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22.6

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: make test

dev-container-build:
permissions:
contents: read
packages: write
uses: scality/workflows/.github/workflows/docker-build.yaml@v2
with:
name: cosi
namespace: ${{ github.repository_owner }}
tag: ${{ github.sha }}
31 changes: 31 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Release

on:
workflow_dispatch:
inputs:
tag:
description: "Tag to be released (e.g., v1.0.0)"
required: true
jobs:
prod-container-build:
permissions:
contents: read
packages: write
uses: scality/workflows/.github/workflows/docker-build.yaml@v2
with:
name: cosi
namespace: ${{ github.repository_owner }}
tag: ${{ github.event.inputs.tag }}

create-github-release:
runs-on: ubuntu-latest
needs: prod-container-build
steps:
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.event.inputs.tag }}
name: Release ${{ github.event.inputs.tag }}
generate_release_notes: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ release-tools
bin
.idea
vendor
*.txt
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.22.6 AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

# Copy the source code into the container
COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o scality-cosi-driver ./cmd/scality-cosi-driver

FROM gcr.io/distroless/static:latest
COPY --from=builder /app/scality-cosi-driver /scality-cosi-driver
ENTRYPOINT ["/scality-cosi-driver"]
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
APP_NAME = scality-cosi-driver

.PHONY: all
all: test build

.PHONY: build
build:
@echo "Building $(APP_NAME)..."
GOOS=linux GOARCH=amd64 go build -o ./bin/$(APP_NAME) ./cmd/$(APP_NAME)

.PHONY: test
test:
@echo "Running tests..."
go test ./... -coverprofile=coverage.txt -covermode=atomic

.PHONY: clean
clean:
@echo "Cleaning up..."
rm -rf ./bin
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/scality/cosi
go 1.22.6

require (
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.66.0
k8s.io/client-go v0.31.0
k8s.io/klog/v2 v2.130.1
Expand Down Expand Up @@ -32,6 +33,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand Down
97 changes: 97 additions & 0 deletions pkg/driver/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package driver

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
cosiapi "sigs.k8s.io/container-object-storage-interface-spec"
)

func TestDriverGetInfo(t *testing.T) {
// Define a long provisioner name to reuse in the test cases
longProvisioner := "scality-cosi-driver" + strings.Repeat("x", 1000)

// Define a table of test cases
tests := []struct {
name string
provisioner string
request *cosiapi.DriverGetInfoRequest
want *cosiapi.DriverGetInfoResponse
wantErr bool
errCode codes.Code
}{
{
name: "Valid provisioner name",
provisioner: "scality-cosi-driver",
request: &cosiapi.DriverGetInfoRequest{},
want: &cosiapi.DriverGetInfoResponse{Name: "scality-cosi-driver"},
wantErr: false,
},
{
name: "Empty provisioner name",
provisioner: "",
request: &cosiapi.DriverGetInfoRequest{},
want: nil,
wantErr: true,
errCode: codes.InvalidArgument,
},
{
name: "Empty request object",
provisioner: "scality-cosi-driver",
request: nil, // Test for nil request to ensure function handles nil input gracefully.
want: &cosiapi.DriverGetInfoResponse{Name: "scality-cosi-driver"},
wantErr: false,
},
{
name: "Long provisioner name",
provisioner: longProvisioner,
request: &cosiapi.DriverGetInfoRequest{},
want: &cosiapi.DriverGetInfoResponse{Name: longProvisioner},
wantErr: false,
},
{
name: "Provisioner name with special characters",
provisioner: "scality-cosi-driver-ß∂ƒ©",
request: &cosiapi.DriverGetInfoRequest{},
want: &cosiapi.DriverGetInfoResponse{Name: "scality-cosi-driver-ß∂ƒ©"},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create identityServer instance
server := newIdentityServer(tt.provisioner)

// Call DriverGetInfo with provided request
resp, err := server.DriverGetInfo(context.Background(), tt.request)

// Handle errors and assertions
if tt.wantErr {
assertErrorWithCode(t, err, tt.errCode)
} else {
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, tt.want, resp)
}
})
}
}

// Helper function to create identityServer
func newIdentityServer(provisioner string) *identityServer {
return &identityServer{
provisioner: provisioner,
}
}

// Helper function to assert error codes
func assertErrorWithCode(t *testing.T, err error, expectedCode codes.Code) {
assert.Error(t, err)
st, _ := status.FromError(err)
assert.Equal(t, expectedCode, st.Code())
}

0 comments on commit a280c3c

Please sign in to comment.