Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasfrank committed Mar 29, 2023
1 parent 43f639d commit e958627
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 15 deletions.
51 changes: 51 additions & 0 deletions ori/testing/volume/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 OnMetal 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 volume

import (
"context"

ori "github.com/onmetal/onmetal-api/ori/apis/volume/v1alpha1"
"google.golang.org/grpc"
)

func NewFakeRuntimeClient() *FakeRuntimeClient {
return &FakeRuntimeClient{
FakeRuntime: FakeRuntime{
Volumes: make(map[string]*FakeVolume),
VolumeClasses: make(map[string]*FakeVolumeClass),
},
}
}

type FakeRuntimeClient struct {
FakeRuntime
}

func (f *FakeRuntimeClient) ListVolumes(ctx context.Context, in *ori.ListVolumesRequest, opts ...grpc.CallOption) (*ori.ListVolumesResponse, error) {
return f.listVolumes(ctx, in)
}

func (f *FakeRuntimeClient) CreateVolume(ctx context.Context, in *ori.CreateVolumeRequest, opts ...grpc.CallOption) (*ori.CreateVolumeResponse, error) {
return f.createVolume(ctx, in)
}

func (f *FakeRuntimeClient) DeleteVolume(ctx context.Context, in *ori.DeleteVolumeRequest, opts ...grpc.CallOption) (*ori.DeleteVolumeResponse, error) {
return f.deleteVolume(ctx, in)
}

func (f *FakeRuntimeClient) ListVolumeClasses(ctx context.Context, in *ori.ListVolumeClassesRequest, opts ...grpc.CallOption) (*ori.ListVolumeClassesResponse, error) {
return f.listVolumeClasses(ctx, in)
}
23 changes: 9 additions & 14 deletions ori/testing/volume/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

ori "github.com/onmetal/onmetal-api/ori/apis/volume/v1alpha1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -57,21 +58,14 @@ type FakeVolumeClass struct {
ori.VolumeClass
}

type FakeRuntimeService struct {
type FakeRuntime struct {
sync.Mutex

Volumes map[string]*FakeVolume
VolumeClasses map[string]*FakeVolumeClass
}

func NewFakeRuntimeService() *FakeRuntimeService {
return &FakeRuntimeService{
Volumes: make(map[string]*FakeVolume),
VolumeClasses: make(map[string]*FakeVolumeClass),
}
}

func (r *FakeRuntimeService) SetVolumes(volumes []*FakeVolume) {
func (r *FakeRuntime) SetVolumes(volumes []*FakeVolume) {
r.Lock()
defer r.Unlock()

Expand All @@ -81,7 +75,7 @@ func (r *FakeRuntimeService) SetVolumes(volumes []*FakeVolume) {
}
}

func (r *FakeRuntimeService) SetVolumeClasses(volumeClasses []*FakeVolumeClass) {
func (r *FakeRuntime) SetVolumeClasses(volumeClasses []*FakeVolumeClass) {
r.Lock()
defer r.Unlock()

Expand All @@ -91,7 +85,7 @@ func (r *FakeRuntimeService) SetVolumeClasses(volumeClasses []*FakeVolumeClass)
}
}

func (r *FakeRuntimeService) ListVolumes(ctx context.Context, req *ori.ListVolumesRequest) (*ori.ListVolumesResponse, error) {
func (r *FakeRuntime) listVolumes(ctx context.Context, req *ori.ListVolumesRequest, opts ...grpc.CallOption) (*ori.ListVolumesResponse, error) {
r.Lock()
defer r.Unlock()

Expand All @@ -114,13 +108,14 @@ func (r *FakeRuntimeService) ListVolumes(ctx context.Context, req *ori.ListVolum
return &ori.ListVolumesResponse{Volumes: res}, nil
}

func (r *FakeRuntimeService) CreateVolume(ctx context.Context, req *ori.CreateVolumeRequest) (*ori.CreateVolumeResponse, error) {
func (r *FakeRuntime) createVolume(ctx context.Context, req *ori.CreateVolumeRequest, opts ...grpc.CallOption) (*ori.CreateVolumeResponse, error) {
r.Lock()
defer r.Unlock()

volume := *req.Volume
volume.Metadata.Id = generateID(defaultIDLength)
volume.Metadata.CreatedAt = time.Now().UnixNano()
volume.Status = &ori.VolumeStatus{}

r.Volumes[volume.Metadata.Id] = &FakeVolume{
Volume: volume,
Expand All @@ -131,7 +126,7 @@ func (r *FakeRuntimeService) CreateVolume(ctx context.Context, req *ori.CreateVo
}, nil
}

func (r *FakeRuntimeService) DeleteVolume(ctx context.Context, req *ori.DeleteVolumeRequest) (*ori.DeleteVolumeResponse, error) {
func (r *FakeRuntime) deleteVolume(ctx context.Context, req *ori.DeleteVolumeRequest, opts ...grpc.CallOption) (*ori.DeleteVolumeResponse, error) {
r.Lock()
defer r.Unlock()

Expand All @@ -144,7 +139,7 @@ func (r *FakeRuntimeService) DeleteVolume(ctx context.Context, req *ori.DeleteVo
return &ori.DeleteVolumeResponse{}, nil
}

func (r *FakeRuntimeService) ListVolumeClasses(ctx context.Context, req *ori.ListVolumeClassesRequest) (*ori.ListVolumeClassesResponse, error) {
func (r *FakeRuntime) listVolumeClasses(ctx context.Context, req *ori.ListVolumeClassesRequest, opts ...grpc.CallOption) (*ori.ListVolumeClassesResponse, error) {
r.Lock()
defer r.Unlock()

Expand Down
50 changes: 50 additions & 0 deletions ori/testing/volume/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 OnMetal 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 volume

import (
"context"

ori "github.com/onmetal/onmetal-api/ori/apis/volume/v1alpha1"
)

func NewFakeRuntimeService() *FakeRuntimeService {
return &FakeRuntimeService{
FakeRuntime{
Volumes: make(map[string]*FakeVolume),
VolumeClasses: make(map[string]*FakeVolumeClass),
},
}
}

type FakeRuntimeService struct {
FakeRuntime
}

func (f *FakeRuntimeService) ListVolumes(ctx context.Context, in *ori.ListVolumesRequest) (*ori.ListVolumesResponse, error) {
return f.listVolumes(ctx, in)
}

func (f *FakeRuntimeService) CreateVolume(ctx context.Context, in *ori.CreateVolumeRequest) (*ori.CreateVolumeResponse, error) {
return f.createVolume(ctx, in)
}

func (f *FakeRuntimeService) DeleteVolume(ctx context.Context, in *ori.DeleteVolumeRequest) (*ori.DeleteVolumeResponse, error) {
return f.deleteVolume(ctx, in)
}

func (f *FakeRuntimeService) ListVolumeClasses(ctx context.Context, in *ori.ListVolumeClassesRequest) (*ori.ListVolumeClassesResponse, error) {
return f.listVolumeClasses(ctx, in)
}
Loading

0 comments on commit e958627

Please sign in to comment.