Skip to content

Commit

Permalink
Proper unit tests and code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Lucas <[email protected]>
  • Loading branch information
Xavier Lucas committed Aug 26, 2016
1 parent 14f860a commit 12f40fe
Show file tree
Hide file tree
Showing 45 changed files with 6,648 additions and 33 deletions.
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
language: go

go:
- 1.6.3
- 1.7
- tip

os:
- linux
- osx

script:
- go install
before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover

install: true

script:
- go install
- go test -v -covermode=count -coverprofile=profile.cov github.com/ovh/svfs/svfs
- $HOME/gopath/bin/goveralls -coverprofile=profile.cov -service=svfs
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Build Status](https://travis-ci.org/ovh/svfs.svg?branch=master)](https://travis-ci.org/ovh/svfs)
[![Go Report Card](https://goreportcard.com/badge/github.com/ovh/svfs)](https://goreportcard.com/report/github.com/ovh/svfs)
[![Coverage Status](https://coveralls.io/repos/github/ovh/svfs/badge.svg?branch=master)](https://coveralls.io/github/ovh/svfs?branch=master)
[![GoDoc](https://godoc.org/github.com/ovh/svfs/svfs?status.svg)](https://godoc.org/github.com/ovh/svfs/svfs)

**SVFS** is a Virtual File System over Openstack Swift built upon fuse. It is compatible with [hubiC](https://hubic.com),
Expand Down
66 changes: 66 additions & 0 deletions docs/Tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Running svfs tests

When contributing to svfs, you'll have to make sure you contribution doesn't break current features. Always make sure unit and integration tests pass.


## Unit tests

#### Authentication

In order to test features against a remote Swift storage, tests use your environment for authentication :

- Set `SVFS_TEST_AUTH` to either `HUBIC`, `OPENRC` or `TOKEN`.
- Set relevant variables as described below.

#### HubiC

```
SVFS_TEST_HUBIC_AUTH
SVFS_TEST_HUBIC_TOKEN
```

#### OpenRC

```
SVFS_TEST_AUTH_URL
SVFS_TEST_USERNAME
SVFS_TEST_PASSWORD
SVFS_TEST_TENANT_NAME
SVFS_TEST_REGION_NAME
```

#### Token and storage URL

```
SVFS_TEST_AUTH_TOKEN
SVFS_TEST_STORAGE_URL
```

#### Execution

Run `go test -v github.com/ovh/svfs/svfs`.


## Integration tests

#### Prerequisites

You must have [Rake](http://rake.rubyforge.org/) installed before running tests, you can install it using `gem install rake`.

Before running integration tests, you have to set 3 environment variables :

* `TEST_MOUNTPOINT` : the svfs mountpoint.
* `TEST_SEG_SIZE` : segmented file size value (in megabytes).
* `TEST_NSEG_SIZE` : standard file size value (in megabytes).

A test container will be created automatically.


#### Execution

Use `rake test` command once you have followed previous instructions.

#### Writing more tests

At first, you must follow [contribution guidelines](CONTRIBUTING.md) of SVFS and use the latest version of go.
All integration tests are located in the [test directory](test). If you want to add some, you need consider the [Rakefile](Rakefile) and comment your tests like existing ones.
29 changes: 0 additions & 29 deletions docs/Unit-tests.md

This file was deleted.

175 changes: 175 additions & 0 deletions svfs/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package svfs

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/xlucas/swift"
)

type ChangeCacheTestSuite struct {
suite.Suite
key string
item *Object
}

func (suite *ChangeCacheTestSuite) SetupTest() {
// Reset cache
changeCache = NewSimpleCache()

// Sample data
suite.item = &Object{
name: "item",
path: "dir/item",
c: &swift.Container{
Name: "container",
},
}
suite.key = changeCache.key(suite.item.c.Name, suite.item.path)
}

func (suite *ChangeCacheTestSuite) TestAdd() {
changeCache.Add(suite.item.c.Name, suite.item.path, suite.item)

require.NotNil(suite.T(), changeCache.changes[suite.key])
assert.Equal(suite.T(), changeCache.changes[suite.key], suite.item)
}

func (suite *ChangeCacheTestSuite) TestExist() {
suite.TestAdd()

assert.True(suite.T(), changeCache.Exist(suite.item.c.Name, suite.item.path))
}

func (suite *ChangeCacheTestSuite) TestGet() {
suite.TestAdd()

node := changeCache.Get(suite.item.c.Name, suite.item.path)

require.NotNil(suite.T(), node)
assert.Equal(suite.T(), node, suite.item)
}

func (suite *ChangeCacheTestSuite) TestRemove() {
suite.TestAdd()

changeCache.Remove(suite.item.c.Name, suite.item.path)

assert.Nil(suite.T(), changeCache.changes[suite.key])
}

func TestChangeCacheTestSuite(t *testing.T) {
suite.Run(t, new(ChangeCacheTestSuite))
}

type CacheTestSuite struct {
suite.Suite
nodes map[string]Node
key string
item1 *Object
item2 *Object
parent *Directory
}

func (suite *CacheTestSuite) SetupTest() {
// Cache settings
CacheTimeout = 5 * time.Minute
CacheMaxEntries = -1
CacheMaxAccess = -1

// Reset cache
directoryCache = NewCache()

// Sample data
suite.nodes = make(map[string]Node)
suite.parent = &Directory{
name: "dir",
path: "dir/",
c: &swift.Container{
Name: "container",
},
}
suite.item1 = &Object{name: "item1"}
suite.item2 = &Object{name: "item2"}
suite.key = directoryCache.key(suite.parent.c.Name, suite.parent.path)
suite.nodes[suite.item1.Name()] = suite.item1
}

func (suite *CacheTestSuite) TestAddAll() {
directoryCache.AddAll(suite.parent.c.Name, suite.parent.path, suite.parent, suite.nodes)

assert.Equal(suite.T(), directoryCache.nodeCount, uint64(1))
assert.Len(suite.T(), directoryCache.content[suite.key].nodes, 1)
}

func (suite *CacheTestSuite) TestGetAll() {
suite.TestAddAll()

cachedParent, cachedNodes := directoryCache.GetAll(suite.parent.c.Name, suite.parent.path)

assert.Len(suite.T(), cachedNodes, 1)
assert.IsType(suite.T(), &Object{}, cachedNodes[suite.item1.Name()])
assert.IsType(suite.T(), &Directory{}, cachedParent)
}

func (suite *CacheTestSuite) TestDeleteAll() {
suite.TestAddAll()

directoryCache.DeleteAll(suite.parent.c.Name, suite.parent.path)

assert.Nil(suite.T(), directoryCache.content[suite.key])
assert.Len(suite.T(), directoryCache.content, 0)
assert.Equal(suite.T(), directoryCache.nodeCount, uint64(0))
}

func (suite *CacheTestSuite) TestDelete() {
suite.TestSet()

var (
entries = directoryCache.content[suite.key].nodes
nodeCount = len(entries)
)

for _, node := range []Node{suite.item1, suite.item2} {
nodeCount--
directoryCache.Delete(suite.parent.c.Name, suite.parent.path, node.Name())
assert.Nil(suite.T(), entries[node.Name()])
assert.Len(suite.T(), entries, nodeCount)
}
}

func (suite *CacheTestSuite) TestGet() {
suite.TestSet()

for _, node := range []Node{suite.item1, suite.item2} {
cached := directoryCache.Get(suite.parent.c.Name, suite.parent.path, node.Name())
require.NotNil(suite.T(), cached)
assert.Equal(suite.T(), cached, node)
}
}

func (suite *CacheTestSuite) TestPeek() {
suite.TestAddAll()

parent, found := directoryCache.Peek(suite.parent.c.Name, suite.parent.path)

assert.True(suite.T(), found)
require.NotNil(suite.T(), parent)
assert.Equal(suite.T(), parent, suite.parent)
}

func (suite *CacheTestSuite) TestSet() {
suite.TestAddAll()

directoryCache.Set(suite.parent.c.Name, suite.parent.path, suite.item2.name, suite.item2)

require.NotNil(suite.T(), directoryCache.content[suite.key])
assert.NotNil(suite.T(), directoryCache.content[suite.key].nodes[suite.item2.name])
}

func TestCacheTestSuite(t *testing.T) {
suite.Run(t, new(CacheTestSuite))
}
Loading

0 comments on commit 12f40fe

Please sign in to comment.