Skip to content

Commit

Permalink
Add unit tests for create resources use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Morales committed Feb 4, 2025
1 parent 54039b3 commit e4c33b8
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmd/kar/app/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func installFlags(flags *pflag.FlagSet, cmdOptions *Opts) {
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()

flags.StringVarP(&cmdOptions.VmTemplate, "kubevirt-vm-template", "t", "vm-template",
flags.StringVarP(&cmdOptions.VMTemplate, "kubevirt-vm-template", "t", "vm-template",
"The VirtualMachine resource to use as the template.")
flags.StringVarP(&cmdOptions.RunnerName, "runner-name", "r", "runner",
"The name of the runner.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/kar/app/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package app

// Opts stores all the options for configuring the root kar command.
type Opts struct {
VmTemplate string
VMTemplate string
RunnerName string
JitConfig string
}
7 changes: 5 additions & 2 deletions cmd/kar/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package app

import (
"context"
"errors"

runner "github.com/electrocucaracha/kubevirt-actions-runner/internal"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -42,7 +42,10 @@ func NewRootCommand(ctx context.Context, runner runner.Runner, opts Opts) *cobra
}

func run(ctx context.Context, runner runner.Runner, opts Opts) error {
runner.CreateResources(ctx, opts.VmTemplate, opts.RunnerName, opts.JitConfig)
err := runner.CreateResources(ctx, opts.VMTemplate, opts.RunnerName, opts.JitConfig)
if err != nil {
return errors.Wrap(err, "fail to create resources")
}
defer runner.DeleteResources(ctx)

runner.WaitForVirtualMachineInstance(ctx)
Expand Down
17 changes: 12 additions & 5 deletions cmd/kar/app/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package app_test
import (
"context"

"slices"

"github.com/electrocucaracha/kubevirt-actions-runner/cmd/kar/app"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"slices"
)

type mock struct {
Expand All @@ -40,20 +41,26 @@ func (m *mock) Failed() bool {
return m.failed
}

func (m *mock) CreateResources(ctx context.Context, vmTemplate, runnerName, jitConfig string,
) {
func (m *mock) GetVMIName() string {
return m.runnerName
}

func (m *mock) CreateResources(_ context.Context, vmTemplate, runnerName, jitConfig string,
) error {
m.vmTemplate = vmTemplate
m.runnerName = runnerName
m.jitConfig = jitConfig

m.createCalled = true

return nil
}

func (m *mock) WaitForVirtualMachineInstance(ctx context.Context) {
func (m *mock) WaitForVirtualMachineInstance(_ context.Context) {
m.waitCalled = true
}

func (m *mock) DeleteResources(ctx context.Context) {
func (m *mock) DeleteResources(_ context.Context) {
m.deleteCalled = true
}

Expand Down
44 changes: 32 additions & 12 deletions cmd/kar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/electrocucaracha/kubevirt-actions-runner/cmd/kar/app"
runner "github.com/electrocucaracha/kubevirt-actions-runner/internal"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"kubevirt.io/client-go/kubecli"
)

type buildInfo struct {
Expand All @@ -36,31 +38,49 @@ type buildInfo struct {
}

func getBuildInfo() buildInfo {
b := buildInfo{}
out := buildInfo{}

if info, ok := debug.ReadBuildInfo(); ok {
b.goVersion = info.GoVersion
for _, kv := range info.Settings {
switch kv.Key {
out.goVersion = info.GoVersion

for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
b.gitCommit = kv.Value
out.gitCommit = setting.Value
case "vcs.time":
b.buildDate = kv.Value
out.buildDate = setting.Value
case "vcs.modified":
b.gitTreeModified = kv.Value
out.gitTreeModified = setting.Value
}
}
}

return b
return out
}

func main() {
var opts app.Opts
var (
opts app.Opts
err error
)

buildInfo := getBuildInfo()
log.Printf("starting kubevirt action runner\ncommit: %v\tmodified:%v\n",
buildInfo.gitCommit, buildInfo.gitTreeModified)

clientConfig := kubecli.DefaultClientConfig(&pflag.FlagSet{})

b := getBuildInfo()
log.Printf("starting kubevirt action runner\ncommit: %v\tmodified:%v\n", b.gitCommit, b.gitTreeModified)
namespace, _, err := clientConfig.Namespace()
if err != nil {
log.Fatalf("error in namespace : %v\n", err)
}

virtClient, err := kubecli.GetKubevirtClientFromClientConfig(clientConfig)
if err != nil {
log.Fatalf("cannot obtain KubeVirt client: %v\n", err)
}

runner := runner.NewRunner()
runner := runner.NewRunner(namespace, virtClient)

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.4
replace k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f

require (
github.com/golang/mock v1.6.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -36,7 +37,6 @@ require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand Down Expand Up @@ -79,11 +79,13 @@ require (
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/apiserver v0.31.0 // indirect
k8s.io/client-go v0.31.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.31.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSPG+6V4=
gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inf.v0 v0.9.0/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
Expand Down Expand Up @@ -837,6 +839,8 @@ k8s.io/apimachinery v0.20.0/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRp
k8s.io/apimachinery v0.23.3/go.mod h1:BEuFMMBaIbcOqVIJqNZJXGFTP4W6AycEpb5+m/97hrM=
k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4=
k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/apiserver v0.31.0 h1:p+2dgJjy+bk+B1Csz+mc2wl5gHwvNkC9QJV+w55LVrY=
k8s.io/apiserver v0.31.0/go.mod h1:KI9ox5Yu902iBnnyMmy7ajonhKnkeZYJhTZ/YI+WEMk=
k8s.io/client-go v0.0.0-20181115111358-9bea17718df8/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/client-go v0.19.0/go.mod h1:H9E/VT95blcFQnlyShFgnFT9ZnJOAceiUHM3MlRC+mU=
k8s.io/client-go v0.20.0/go.mod h1:4KWh/g+Ocd8KkCwKF8vUNnmqgv+EVnQDK4MBF4oB5tY=
Expand Down
28 changes: 28 additions & 0 deletions internal/app_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright © 2025
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 runner_test

import (
"testing"

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

func TestApp(t *testing.T) {
t.Parallel()

RegisterFailHandler(Fail)
RunSpecs(t, "Runner App Suite")
}
28 changes: 28 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright © 2025
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 runner

import "errors"

// ErrEmptyVMTemplate indicates that virtual machine template provided is empty.
var ErrEmptyVMTemplate = errors.New("empty vm template")

// ErrEmptyRunnerName indicates that runner name provided is empty.
var ErrEmptyRunnerName = errors.New("empty runner name")

// ErrEmptyJitConfig indicates that Just-in-Time configuration provided is empty.
var ErrEmptyJitConfig = errors.New("empty jit config")
Loading

0 comments on commit e4c33b8

Please sign in to comment.