Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from stoyanr/refactor-tests
Browse files Browse the repository at this point in the history
Update dependencies and refactor unit tests
  • Loading branch information
mfranczy authored Oct 15, 2020
2 parents 2fca650 + 6a77863 commit ef5d89c
Show file tree
Hide file tree
Showing 733 changed files with 92,195 additions and 111,795 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ revendor:
@env GO111MODULE=on go mod vendor -v
@env GO111MODULE=on go mod tidy -v

#########################################
# Rules for code generation
#########################################

.PHONY: generate
generate:
@env GO111MODULE=on go generate -mod=vendor ./pkg/...

#########################################
# Rules for testing
#########################################
Expand Down
31 changes: 14 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
module github.com/gardener/machine-controller-manager-provider-kubevirt

go 1.13
go 1.14

require (
github.com/Masterminds/semver v1.5.0
github.com/gardener/machine-controller-manager v0.29.0
github.com/onsi/ginkgo v1.10.1
github.com/onsi/gomega v1.7.0
github.com/pkg/errors v0.8.1
github.com/gardener/machine-controller-manager v0.33.0
github.com/golang/mock v1.4.4-0.20200731163441-8734ec565a4d
github.com/onsi/ginkgo v1.13.0
github.com/onsi/gomega v1.10.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.1 // indirect
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v12.0.0+incompatible
k8s.io/component-base v0.18.2
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
kubevirt.io/client-go v0.28.0
kubevirt.io/client-go v0.33.0
kubevirt.io/containerized-data-importer v1.10.6
sigs.k8s.io/controller-runtime v0.4.0
sigs.k8s.io/controller-runtime v0.5.5
)

replace (
github.com/gardener/machine-controller-manager => github.com/prashanth26/machine-controller-manager v0.0.0-20200601182012-6f9dee78a746
github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.2
k8s.io/api => k8s.io/api v0.16.8 // v0.16.8
k8s.io/apimachinery => k8s.io/apimachinery v0.16.8 // v0.16.8
k8s.io/apiserver => k8s.io/apiserver v0.16.8 // v0.16.8
k8s.io/client-go => k8s.io/client-go v0.16.8 // v0.16.8
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.16.8 // v0.16.8
k8s.io/code-generator => k8s.io/code-generator v0.16.8 // v0.16.8
k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20190816220812-743ec37842bf
k8s.io/api => k8s.io/api v0.17.9
k8s.io/apimachinery => k8s.io/apimachinery v0.17.9
k8s.io/apiserver => k8s.io/apiserver v0.17.9
k8s.io/client-go => k8s.io/client-go v0.17.9
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.17.9
k8s.io/code-generator => k8s.io/code-generator v0.17.9
)
206 changes: 160 additions & 46 deletions go.sum

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions pkg/kubevirt/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,41 @@ func (f ServerVersionFactoryFunc) GetServerVersion(secret *corev1.Secret) (strin
return f(secret)
}

// Timer returns the current local time.
type Timer interface {
// Now returns the current local time.
Now() time.Time
}

// TimerFunc is a function that implements Timer.
type TimerFunc func() time.Time

// Now returns the current local time.
func (f TimerFunc) Now() time.Time {
return f()
}

// PluginSPIImpl is the implementation of PluginSPI interface.
type PluginSPIImpl struct {
cf ClientFactory
svf ServerVersionFactory
cf ClientFactory
svf ServerVersionFactory
timer Timer
}

// NewPluginSPIImpl creates a new PluginSPIImpl with the given ClientFactory and ServerVersionFactory.
func NewPluginSPIImpl(cf ClientFactory, svf ServerVersionFactory) *PluginSPIImpl {
// NewPluginSPIImpl creates a new PluginSPIImpl with the given ClientFactory, ServerVersionFactory, and Timer.
func NewPluginSPIImpl(cf ClientFactory, svf ServerVersionFactory, timer Timer) *PluginSPIImpl {
return &PluginSPIImpl{
cf: cf,
svf: svf,
cf: cf,
svf: svf,
timer: timer,
}
}

// CreateMachine creates a machine with the given name, using the given provider spec and secret.
// Here it creates a kubevirt virtual machine and a secret containing the userdata (cloud-init).
func (p PluginSPIImpl) CreateMachine(ctx context.Context, machineName string, providerSpec *api.KubeVirtProviderSpec, secret *corev1.Secret) (providerID string, err error) {
// Generate a unique name for the userdata secret
userDataSecretName := fmt.Sprintf("userdata-%s-%s", machineName, strconv.Itoa(int(time.Now().Unix())))
userDataSecretName := fmt.Sprintf("userdata-%s-%s", machineName, strconv.Itoa(int(p.timer.Now().Unix())))

// Get client and namespace from secret
c, namespace, err := p.cf.GetClient(secret)
Expand Down Expand Up @@ -140,13 +156,13 @@ func (p PluginSPIImpl) CreateMachine(ctx context.Context, machineName string, pr
},
Spec: kubevirtv1.VirtualMachineInstanceSpec{
Domain: kubevirtv1.DomainSpec{
CPU: providerSpec.CPU,
Memory: providerSpec.Memory,
Resources: providerSpec.Resources,
CPU: providerSpec.CPU,
Memory: providerSpec.Memory,
Devices: kubevirtv1.Devices{
Disks: disks,
Interfaces: interfaces,
},
Resources: providerSpec.Resources,
},
Affinity: affinity,
TerminationGracePeriodSeconds: pointer.Int64Ptr(30),
Expand Down Expand Up @@ -201,7 +217,7 @@ func (p PluginSPIImpl) DeleteMachine(ctx context.Context, machineName, _ string,
virtualMachine, err := p.getVM(ctx, c, machineName, namespace)
if err != nil {
if IsMachineNotFoundError(err) {
klog.V(2).Infof("VirtualMachine %s not found", machineName)
klog.V(2).Infof("VirtualMachine %q not found", machineName)
return "", nil
}
return "", err
Expand Down
27 changes: 27 additions & 0 deletions pkg/kubevirt/core/core_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 core_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestCore(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Core Suite")
}
Loading

0 comments on commit ef5d89c

Please sign in to comment.